diff options
author | ToineSiebelink <toine.siebelink@est.tech> | 2021-12-02 13:53:24 +0000 |
---|---|---|
committer | ToineSiebelink <toine.siebelink@est.tech> | 2021-12-03 10:14:54 +0000 |
commit | c60d8590ffe123dce9f6b455fa615573ee75b767 (patch) | |
tree | 3c72c3a4a83fa88d2e2acf32cddf71c15245581a /src/test | |
parent | c24a6781eea0729f97cb7b593114b43c0ecef96e (diff) |
Fix conversion to string for result from YangResource request on SDN-C
-Extracted out relevant method to separate class
-Added comprehensive tests around relevant methods
Issue-ID: CPS-788
Signed-off-by: ToineSiebelink <toine.siebelink@est.tech>
Change-Id: Icb2104ca606b28f1904ead368bcde27f578e7e53
Diffstat (limited to 'src/test')
-rw-r--r-- | src/test/groovy/org/onap/cps/ncmp/dmi/service/DmiServiceImplSpec.groovy | 4 | ||||
-rw-r--r-- | src/test/groovy/org/onap/cps/ncmp/dmi/service/YangResourceExtractorSpec.groovy | 98 |
2 files changed, 100 insertions, 2 deletions
diff --git a/src/test/groovy/org/onap/cps/ncmp/dmi/service/DmiServiceImplSpec.groovy b/src/test/groovy/org/onap/cps/ncmp/dmi/service/DmiServiceImplSpec.groovy index a463a344..edf1a80a 100644 --- a/src/test/groovy/org/onap/cps/ncmp/dmi/service/DmiServiceImplSpec.groovy +++ b/src/test/groovy/org/onap/cps/ncmp/dmi/service/DmiServiceImplSpec.groovy @@ -139,8 +139,8 @@ class DmiServiceImplSpec extends Specification { new ResponseEntity<String>('{"ietf-netconf-monitoring:output": {"data": "some-data2"}}', HttpStatus.OK)] and: 'the result is a yang resources object with the expected names, revisions and yang-sources' def yangResources = new YangResources() - def yangResource1 = new YangResource(yangSource: '"some-data1"', moduleName: 'name-1', revision: 'revision-1') - def yangResource2 = new YangResource(yangSource: '"some-data2"', moduleName: 'name-2', revision: 'revision-2') + def yangResource1 = new YangResource(yangSource: 'some-data1', moduleName: 'name-1', revision: 'revision-1') + def yangResource2 = new YangResource(yangSource: 'some-data2', moduleName: 'name-2', revision: 'revision-2') yangResources.add(yangResource1) yangResources.add(yangResource2) assert result == yangResources diff --git a/src/test/groovy/org/onap/cps/ncmp/dmi/service/YangResourceExtractorSpec.groovy b/src/test/groovy/org/onap/cps/ncmp/dmi/service/YangResourceExtractorSpec.groovy new file mode 100644 index 00000000..656cfcb5 --- /dev/null +++ b/src/test/groovy/org/onap/cps/ncmp/dmi/service/YangResourceExtractorSpec.groovy @@ -0,0 +1,98 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2021 Nordix Foundation + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.cps.ncmp.dmi.service + +import com.google.gson.JsonSyntaxException +import org.onap.cps.ncmp.dmi.exception.ModuleResourceNotFoundException +import org.onap.cps.ncmp.dmi.service.model.ModuleReference +import org.springframework.http.ResponseEntity +import spock.lang.Specification + +class YangResourceExtractorSpec extends Specification { + + static def BACK_SLASH = '\\'; + static def NEW_LINE = '\n'; + static def QUOTE = '"'; + static def TAB = '\t'; + + static def YANG_ESCAPED_NEW_LINE = '\\n' + static def YANG_ESCAPED_BACK_SLASH = '\\\\' + static def YANG_ESCAPED_QUOTE = '\\"' + static def YANG_ESCAPED_TAB = '\\t' + + static def SDNC_OUTPUT_JSON_NAME = '"ietf-netconf-monitoring:output"' + + def moduleReference = new ModuleReference(name: 'test', revision: 'rev') + def responseEntity = Mock(ResponseEntity) + + def 'Extract yang resource with escaped characters in the source.'() { + given: 'a response entity with a data field of value #jsonValue' + responseEntity.getBody() >> '{' + SDNC_OUTPUT_JSON_NAME + ': { "data": "' + jsonValue + '" }}' + when: 'the yang resource is extracted' + def result = YangResourceExtractor.toYangResource(moduleReference, responseEntity) + then: 'the yang source string is as expected' + result.getYangSource() == expectedString + where: 'the following data is used' + jsonValue || expectedString + 'line1' + YANG_ESCAPED_NEW_LINE + 'line2' || 'line1' + NEW_LINE + 'line2' + 'a' + YANG_ESCAPED_BACK_SLASH+'b' || 'a'+BACK_SLASH +'b' + 'a' + YANG_ESCAPED_QUOTE + 'b' || 'a'+QUOTE+'b' + 'a' + YANG_ESCAPED_TAB + 'b' || 'a'+TAB+'b' + } + + def 'Extract yang resource with escaped characters in the source inside escaped double quotes.'() { + given: 'a response entity with a data field of value #jsonValue wrapped in escaped double quotes' + responseEntity.getBody() >> '{' + SDNC_OUTPUT_JSON_NAME + ': { "data": "' + YANG_ESCAPED_QUOTE + jsonValue + YANG_ESCAPED_QUOTE + '" }}' + when: 'the yang resource is extracted' + def result = YangResourceExtractor.toYangResource(moduleReference, responseEntity) + then: 'the yang source string is as expected' + result.getYangSource() == expectedString + where: 'the following data is used' + jsonValue || expectedString + 'line1' + YANG_ESCAPED_NEW_LINE + 'line2' || '"line1' + NEW_LINE + 'line2"' + 'a' + YANG_ESCAPED_BACK_SLASH+'b' || '"a'+BACK_SLASH +'b"' + 'a' + YANG_ESCAPED_QUOTE + 'b' || '"a'+QUOTE+'b"' + 'a' + YANG_ESCAPED_TAB + 'b' || '"a'+TAB+'b"' + } + + def 'Attempt to extract yang resource with un-escaped double quotes in the source.'() { + given: 'a response entity with a data field with unescaped double quotes' + responseEntity.getBody() >> '{' + SDNC_OUTPUT_JSON_NAME + ': { "data": "' + QUOTE + 'some data' + QUOTE + '" }}' + when: 'Json is converted to String' + YangResourceExtractor.toYangResource(moduleReference, responseEntity) + then: 'the output of the method is equal to the output from the test template' + thrown(JsonSyntaxException) + } + + def 'Attempt to extract yang resource without #without.'() { + given: 'a response entity with a body without #without' + responseEntity.getBody() >> jsonBody + when: 'Json is converted to String' + YangResourceExtractor.toYangResource(moduleReference, responseEntity) + then: 'the output of the method is equal to the output from the test template' + thrown(ModuleResourceNotFoundException) + where: + without | jsonBody + 'data' | '{' + SDNC_OUTPUT_JSON_NAME + ': { "something": "else" }}' + SDNC_OUTPUT_JSON_NAME | '{"something:else": { "data": "data" }}' + } + +} |