diff options
author | tragait <rahul.tyagi@est.tech> | 2021-08-16 15:12:36 +0100 |
---|---|---|
committer | tragait <rahul.tyagi@est.tech> | 2021-08-19 12:46:37 +0100 |
commit | c328551bdfd069343cc4c4e0249516d07938c78a (patch) | |
tree | 5beae0e58a123d67a4e89e904bc24d5239942437 /cps-ncmp-service/src/test/groovy/org | |
parent | 24112c0a500e94dc6068be71105874f8d81678b7 (diff) |
get resource data for operational passthrough
Issue-ID: CPS-487
Signed-off-by: tragait <rahul.tyagi@est.tech>
Change-Id: Id1b761f3f6a388556d0cc334fd6f196c78badc39
Diffstat (limited to 'cps-ncmp-service/src/test/groovy/org')
3 files changed, 204 insertions, 1 deletions
diff --git a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/NetworkCmProxyDataServiceImplSpec.groovy b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/NetworkCmProxyDataServiceImplSpec.groovy index 6d53e4067e..65d96a4296 100644 --- a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/NetworkCmProxyDataServiceImplSpec.groovy +++ b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/NetworkCmProxyDataServiceImplSpec.groovy @@ -18,21 +18,29 @@ * SPDX-License-Identifier: Apache-2.0 * ============LICENSE_END========================================================= */ + package org.onap.cps.ncmp.api.impl +import com.fasterxml.jackson.core.JsonProcessingException import com.fasterxml.jackson.databind.ObjectMapper import org.onap.cps.api.CpsDataService import org.onap.cps.api.CpsQueryService +import org.onap.cps.ncmp.api.impl.exception.NcmpException +import org.onap.cps.ncmp.api.impl.operation.DmiOperations import org.onap.cps.ncmp.api.models.CmHandle import org.onap.cps.ncmp.api.models.DmiPluginRegistration import org.onap.cps.spi.FetchDescendantsOption +import org.onap.cps.spi.model.DataNode +import org.springframework.http.HttpStatus +import org.springframework.http.ResponseEntity import spock.lang.Specification class NetworkCmProxyDataServiceImplSpec extends Specification { def mockCpsDataService = Mock(CpsDataService) def mockCpsQueryService = Mock(CpsQueryService) - def objectUnderTest = new NetworkCmProxyDataServiceImpl(mockCpsDataService, mockCpsQueryService, new ObjectMapper()) + def mockDmiOperations = Mock(DmiOperations) + def objectUnderTest = new NetworkCmProxyDataServiceImpl(mockDmiOperations, mockCpsDataService, mockCpsQueryService, new ObjectMapper()) def cmHandle = 'some handle' def expectedDataspaceName = 'NFP-Operational' @@ -108,4 +116,86 @@ class NetworkCmProxyDataServiceImplSpec extends Specification { then: 'the CPS service method is invoked once with the expected parameters' 1 * mockCpsDataService.saveListNodeData('NCMP-Admin', 'ncmp-dmi-registry', '/dmi-registry', expectedJsonData) } + def 'Get resource data for pass-through operational from dmi.'() { + given: 'xpath' + def xpath = "/dmi-registry/cm-handles[@id='testCmHandle']" + and: 'data node' + def dataNode = new DataNode() + dataNode.leaves = ['dmi-service-name':'testDmiService'] + def childDataNode = new DataNode() + childDataNode.leaves = ['name':'testName','value':'testValue'] + dataNode.childDataNodes = [childDataNode] + when: 'get resource data is called' + def response = objectUnderTest.getResourceDataOperationalFoCmHandle('testCmHandle', + 'testResourceId', + 'testAcceptParam', + 'testFieldQuery', + 5) + then: 'cps data service is being called once to get data node' + 1 * mockCpsDataService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry', + xpath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> dataNode + and: 'dmi operation is being calle to get resource data' + 1 * mockDmiOperations.getResouceDataFromDmi('testDmiService', + 'testCmHandle', + 'testResourceId', + 'testFieldQuery', + 5, + 'testAcceptParam', + '{"operation":"read","cmHandleProperties":{"testName":"testValue"}}') >> new ResponseEntity<>('result-json', HttpStatus.OK) + and: 'dmi returns ok response' + response == 'result-json' + } + def 'Get resource data for pass-through operational from dmi threw parsing exception.'() { + given: 'xpath' + def xpath = "/dmi-registry/cm-handles[@id='testCmHandle']" + and: 'data node' + def dataNode = new DataNode() + dataNode.leaves = ['dmi-service-name':'testDmiService'] + def childDataNode = new DataNode() + childDataNode.leaves = ['name':'testName','value':'testValue'] + dataNode.childDataNodes = [childDataNode] + mockCpsDataService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry', + xpath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> dataNode + and: 'objectMapper not able to parse object' + def mockObjectMapper = Mock(ObjectMapper) + objectUnderTest.objectMapper = mockObjectMapper + mockObjectMapper.writeValueAsString(_) >> { throw new JsonProcessingException("testException") } + when: 'get resource data is called' + def response = objectUnderTest.getResourceDataOperationalFoCmHandle('testCmHandle', + 'testResourceId', + 'testAcceptParam', + 'testFieldQuery', + 5) + then: 'exception is thrown' + thrown(NcmpException.class) + } + def 'Get resource data for pass-through operational from dmi return NOK response.'() { + given: 'xpath' + def xpath = "/dmi-registry/cm-handles[@id='testCmHandle']" + and: 'data node' + def dataNode = new DataNode() + dataNode.leaves = ['dmi-service-name':'testDmiService'] + def childDataNode = new DataNode() + childDataNode.leaves = ['name':'testName','value':'testValue'] + dataNode.childDataNodes = [childDataNode] + mockCpsDataService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry', + xpath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> dataNode + and: 'dmi returns NOK response' + mockDmiOperations.getResouceDataFromDmi('testDmiService', + 'testCmHandle', + 'testResourceId', + 'testFieldQuery', + 5, + 'testAcceptParam', + '{"operation":"read","cmHandleProperties":{"testName":"testValue"}}') + >> new ResponseEntity<>('NOK-json', HttpStatus.NOT_FOUND) + when: 'get resource data is called' + def response = objectUnderTest.getResourceDataOperationalFoCmHandle('testCmHandle', + 'testResourceId', + 'testAcceptParam', + 'testFieldQuery', + 5) + then: 'exception is thrown' + thrown(NcmpException.class) + } } diff --git a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/client/DmiRestClientSpec.groovy b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/client/DmiRestClientSpec.groovy new file mode 100644 index 0000000000..98bbe8748f --- /dev/null +++ b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/client/DmiRestClientSpec.groovy @@ -0,0 +1,55 @@ +/* + * ============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.api.impl.client + +import org.onap.cps.ncmp.api.impl.config.NcmpConfiguration +import org.springframework.http.HttpEntity +import org.springframework.http.HttpHeaders +import org.springframework.http.ResponseEntity +import org.springframework.web.client.RestTemplate +import spock.lang.Specification +import org.springframework.http.HttpMethod + +class DmiRestClientSpec extends Specification { + + def mockDmiProperties = Mock(NcmpConfiguration.DmiProperties) + def mockRestTemplate = Mock(RestTemplate) + def objectUnderTest = new DmiRestClient(mockRestTemplate, mockDmiProperties) + + def 'DMI PUT operation.'() { + given: 'a get url' + def getResourceDataUrl = 'http://some-uri/getResourceDataUrl' + and: 'dmi properties' + setupTestConfigurationData() + and: 'the rest template returns a valid response entity' + def mockResponseEntity = Mock(ResponseEntity) + mockRestTemplate.exchange(getResourceDataUrl, HttpMethod.PUT, _ as HttpEntity, Object.class) >> mockResponseEntity + when: 'PUT operation is invoked' + def result = objectUnderTest.putOperationWithJsonData(getResourceDataUrl, 'json-data', new HttpHeaders()) + then: 'the output of the method is equal to the output from the test template' + result == mockResponseEntity + } + + def setupTestConfigurationData() { + mockDmiProperties.authUsername >> 'some-username' + mockDmiProperties.authPassword >> 'some-password' + } +}
\ No newline at end of file diff --git a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/operation/DmiOperationsSpec.groovy b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/operation/DmiOperationsSpec.groovy new file mode 100644 index 0000000000..75b5383d8a --- /dev/null +++ b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/operation/DmiOperationsSpec.groovy @@ -0,0 +1,58 @@ +/* + * ============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.api.impl.operation + +import org.onap.cps.ncmp.api.impl.client.DmiRestClient +import org.onap.cps.ncmp.api.impl.config.NcmpConfiguration +import org.onap.cps.ncmp.api.impl.operation.DmiOperations +import org.spockframework.spring.SpringBean +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.boot.test.context.SpringBootTest +import org.springframework.http.HttpHeaders +import org.springframework.test.context.ContextConfiguration +import spock.lang.Specification + +@SpringBootTest +@ContextConfiguration(classes = [NcmpConfiguration.DmiProperties, DmiOperations]) +class DmiOperationsSpec extends Specification { + + @SpringBean + DmiRestClient mockDmiRestClient = Mock() + + @Autowired + DmiOperations objectUnderTest = new DmiOperations(mockDmiRestClient) + + def 'call get resource data for pass-through:operational datastore from dmi.'() { + given: 'expected url' + def expectedUrl = 'testDmiBasePath/v1/ch/testCmhandle/data/ds' + + '/ncmp-datastore:passthrough-operational/testResourceId?fields=testFieldsQuery&depth=10' + when: 'get resource data is called to dmi' + objectUnderTest.getResouceDataFromDmi('testDmiBasePath', + 'testCmhandle', + 'testResourceId', + 'testFieldsQuery', + 10, + 'testAcceptJson', + 'testJsonbody') + then: 'the put operation is executed with the correct URL' + 1 * mockDmiRestClient.putOperationWithJsonData(expectedUrl, 'testJsonbody', _ as HttpHeaders) + } +}
\ No newline at end of file |