diff options
author | niamhcore <niamh.core@est.tech> | 2021-07-30 16:25:16 +0100 |
---|---|---|
committer | niamhcore <niamh.core@est.tech> | 2021-08-10 13:33:52 +0100 |
commit | 3139ece993c68ce7e40d166acdd5d9572a3a8a1e (patch) | |
tree | 4a9ee12234815d6f5bc14557d5228c53b66f3b81 /src/test/groovy/org/onap | |
parent | 2270d76e4f33ad231cdae317e88ea1769297cfec (diff) |
Retrieve yang-resources for one or more modules
Updating openapi to add a new rest endpoint
Updating restconf client to support post with json
Adding a ModuleResourceNotFound exception
Adding a test util class
Fixing merge conflict
Refactoring SDNC operations
Issue-ID: CPS-484
Signed-off-by: niamhcore <niamh.core@est.tech>
Change-Id: Id76dfe4cb12053771883e0271153d7bf7cd98548
Diffstat (limited to 'src/test/groovy/org/onap')
4 files changed, 134 insertions, 26 deletions
diff --git a/src/test/groovy/org/onap/cps/ncmp/dmi/rest/controller/DmiRestControllerSpec.groovy b/src/test/groovy/org/onap/cps/ncmp/dmi/rest/controller/DmiRestControllerSpec.groovy index 993b80c6..03bffe4b 100644 --- a/src/test/groovy/org/onap/cps/ncmp/dmi/rest/controller/DmiRestControllerSpec.groovy +++ b/src/test/groovy/org/onap/cps/ncmp/dmi/rest/controller/DmiRestControllerSpec.groovy @@ -20,8 +20,11 @@ package org.onap.cps.ncmp.dmi.rest.controller +import org.onap.cps.ncmp.dmi.TestUtils import org.onap.cps.ncmp.dmi.exception.DmiException +import org.onap.cps.ncmp.dmi.exception.ModuleResourceNotFoundException import org.onap.cps.ncmp.dmi.exception.ModulesNotFoundException +import org.onap.cps.ncmp.dmi.model.ModuleReference import org.onap.cps.ncmp.dmi.service.DmiService import org.spockframework.spring.SpringBean import org.springframework.beans.factory.annotation.Autowired @@ -55,7 +58,7 @@ class DmiRestControllerSpec extends Specification { def someJson = 'some-json' mockDmiService.getModulesForCmHandle('node1') >> someJson when: 'post is being called' - def response = mvc.perform( post(getModuleUrl) + def response = mvc.perform(post(getModuleUrl) .contentType(MediaType.APPLICATION_JSON)) .andReturn().response then: 'status is OK' @@ -70,16 +73,16 @@ class DmiRestControllerSpec extends Specification { and: 'get modules for cm-handle throws #exceptionClass' mockDmiService.getModulesForCmHandle('node1') >> { throw Mock(exceptionClass) } when: 'post is invoked' - def response = mvc.perform( post(getModuleUrl) + def response = mvc.perform(post(getModuleUrl) .contentType(MediaType.APPLICATION_JSON)) .andReturn().response then: 'response status is #expectedResponse' response.status == expectedResponse where: 'the scenario is #scenario' - scenario | exceptionClass || expectedResponse - 'dmi service exception' | DmiException.class || HttpStatus.INTERNAL_SERVER_ERROR.value() - 'no modules found' | ModulesNotFoundException.class || HttpStatus.NOT_FOUND.value() - 'any other runtime exception' | RuntimeException.class || HttpStatus.INTERNAL_SERVER_ERROR.value() + scenario | exceptionClass || expectedResponse + 'dmi service exception' | DmiException.class || HttpStatus.INTERNAL_SERVER_ERROR.value() + 'no modules found' | ModulesNotFoundException.class || HttpStatus.NOT_FOUND.value() + 'any other runtime exception' | RuntimeException.class || HttpStatus.INTERNAL_SERVER_ERROR.value() } def 'Register given list of cm handles.'() { @@ -112,4 +115,41 @@ class DmiRestControllerSpec extends Specification { and: 'dmi service is not called' 0 * mockDmiService.registerCmHandles(_) } + + def 'Retrieve module resources.'() { + given: 'an endpoint and json data' + def getModulesEndpoint = "$basePathV1/ch/some-cm-handle/moduleResources" + def jsonData = TestUtils.getResourceFileContent('GetModules.json') + and: 'the DMI service returns some json data' + ModuleReference moduleReference1 = new ModuleReference() + moduleReference1.name = 'ietf-yang-library' + moduleReference1.revision = '2016-06-21' + ModuleReference moduleReference2 = new ModuleReference() + moduleReference2.name = 'nc-notifications' + moduleReference2.revision = '2008-07-14' + def moduleReferences = [moduleReference1, moduleReference2] + mockDmiService.getModuleResources('some-cm-handle', moduleReferences ) >> '{some-json}' + when: 'get module resource api is invoked' + def response = mvc.perform(post(getModulesEndpoint) + .contentType(MediaType.APPLICATION_JSON) + .content(jsonData)).andReturn().response + then:'a OK status is returned' + response.status == HttpStatus.OK.value() + and: 'the expected response is returned' + response.getContentAsString() == '{some-json}' + } + + def 'Retrieve module resources with exception handling.'() { + given: 'an endpoint and json data' + def getModulesEndpoint = "$basePathV1/ch/some-cm-handle/moduleResources" + def jsonData = TestUtils.getResourceFileContent('GetModules.json') + and: 'the service method is invoked to get module resources and throws an exception' + mockDmiService.getModuleResources('some-cm-handle', _) >> { throw Mock(ModuleResourceNotFoundException.class) } + when: 'get module resource api is invoked' + def response = mvc.perform(post(getModulesEndpoint) + .contentType(MediaType.APPLICATION_JSON) + .content(jsonData)).andReturn().response + then: 'a not found status is returned' + response.status == HttpStatus.NOT_FOUND.value() + } } 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 9d6bc358..1854b24d 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 @@ -25,7 +25,9 @@ import com.fasterxml.jackson.databind.ObjectMapper import org.onap.cps.ncmp.dmi.config.DmiPluginConfig import org.onap.cps.ncmp.dmi.exception.CmHandleRegistrationException import org.onap.cps.ncmp.dmi.exception.DmiException +import org.onap.cps.ncmp.dmi.exception.ModuleResourceNotFoundException import org.onap.cps.ncmp.dmi.exception.ModulesNotFoundException +import org.onap.cps.ncmp.dmi.model.ModuleReference import org.onap.cps.ncmp.dmi.service.client.NcmpRestClient import org.onap.cps.ncmp.dmi.service.operation.SdncOperations import org.springframework.http.HttpStatus @@ -40,7 +42,7 @@ class DmiServiceImplSpec extends Specification { def objectMapper = new ObjectMapper() def mockObjectMapper = Mock(ObjectMapper) def mockSdncOperations = Mock(SdncOperations) - def objectUnderTest = new DmiServiceImpl(mockDmiPluginProperties, mockNcmpRestClient, objectMapper, mockSdncOperations) + def objectUnderTest = new DmiServiceImpl(mockDmiPluginProperties, mockNcmpRestClient, mockSdncOperations, objectMapper) def 'Call get modules for cm-handle on dmi Service.'() { given: 'cm handle id' @@ -62,7 +64,7 @@ class DmiServiceImplSpec extends Specification { when: 'get modules for cm-handle is called' objectUnderTest.getModulesForCmHandle(cmHandle) then: 'dmi exception is thrown' - thrown( DmiException ) + thrown(DmiException) } def 'Call get modules for cm-handle and SDNC returns OK with empty body.'() { @@ -73,7 +75,7 @@ class DmiServiceImplSpec extends Specification { when: 'get modules for cm-handle is called' objectUnderTest.getModulesForCmHandle(cmHandle) then: 'ModulesNotFoundException is thrown' - thrown( ModulesNotFoundException ) + thrown(ModulesNotFoundException) } def 'Register cm handles with ncmp.'() { @@ -100,9 +102,9 @@ class DmiServiceImplSpec extends Specification { then: 'a registration exception is thrown' thrown(CmHandleRegistrationException.class) where: 'given #scenario' - scenario | responseEntity - 'ncmp rest client returns bad request' | new ResponseEntity<>(HttpStatus.BAD_REQUEST) - 'ncmp rest client returns internal server error'| new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR) + scenario | responseEntity + 'ncmp rest client returns bad request' | new ResponseEntity<>(HttpStatus.BAD_REQUEST) + 'ncmp rest client returns internal server error' | new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR) } def 'Register cm handles with ncmp with wrong data.'() { @@ -116,4 +118,34 @@ class DmiServiceImplSpec extends Specification { then: 'a dmi exception is thrown' thrown(DmiException.class) } + + def 'Get module resources.'() { + given: 'cmHandle, expected jsons and module reference list' + def cmHandle = 'some-cmHandle' + def excpectedModulesJson1 = '{"ietf-netconf-monitoring:input":{"ietf-netconf-monitoring:identifier":"mRef1","ietf-netconf-monitoring:version":"mRefV1"}}' + def excpectedModulesJson2 = '{"ietf-netconf-monitoring:input":{"ietf-netconf-monitoring:identifier":"mRef2","ietf-netconf-monitoring:version":"mRefV2"}}' + def mRef1 = Mock(ModuleReference.class) + def mRef2 = Mock(ModuleReference.class) + mRef1.getName() >> 'mRef1' + mRef1.getRevision() >> 'mRefV1' + mRef2.getName() >> 'mRef2' + mRef2.getRevision() >> 'mRefV2' + def moduleList = [mRef1, mRef2] as LinkedList<ModuleReference> + when: 'get module resources is invoked with the given cm handle and a module list' + def response = objectUnderTest.getModuleResources(cmHandle, moduleList) + then: 'then get modules resources called correctly' + 1 * mockSdncOperations.getModuleResource(cmHandle,excpectedModulesJson1) >> new ResponseEntity<String>('response-body1', HttpStatus.OK) + 1 * mockSdncOperations.getModuleResource(cmHandle,excpectedModulesJson2) >> new ResponseEntity<String>('response-body2', HttpStatus.OK) + then: 'the response contains the expected response body' + response.contains('["response-body1","response-body2"]') + } + + def 'Get module resources for a failed get module schema request.'() { + given: 'get module schema is invoked and returns not found' + mockSdncOperations.getModuleResource(_ as String, _ as String) >> new ResponseEntity<String>('some-response-body', HttpStatus.BAD_REQUEST) + when: 'get module resources is invoked with the given cm handle and a module list' + objectUnderTest.getModuleResources('some-cmHandle', [new ModuleReference()] as LinkedList<ModuleReference> ) + then: 'ModuleResourceNotFoundException is thrown' + thrown(ModuleResourceNotFoundException) + } } diff --git a/src/test/groovy/org/onap/cps/ncmp/dmi/service/client/SdncRestconfClientSpec.groovy b/src/test/groovy/org/onap/cps/ncmp/dmi/service/client/SdncRestconfClientSpec.groovy index 0b192f05..6d9445c1 100644 --- a/src/test/groovy/org/onap/cps/ncmp/dmi/service/client/SdncRestconfClientSpec.groovy +++ b/src/test/groovy/org/onap/cps/ncmp/dmi/service/client/SdncRestconfClientSpec.groovy @@ -22,7 +22,6 @@ package org.onap.cps.ncmp.dmi.service.client import org.onap.cps.ncmp.dmi.config.DmiConfiguration import org.springframework.http.HttpEntity -import org.springframework.http.HttpMethod import org.springframework.http.ResponseEntity import org.springframework.web.client.RestTemplate import spock.lang.Specification @@ -37,16 +36,38 @@ class SdncRestconfClientSpec extends Specification { given: 'a get url' def getResourceUrl = '/getResourceUrl' and: 'sdnc properties' - mockSdncProperties.baseUrl >> 'http://test-sdnc-uri' - mockSdncProperties.authUsername >> 'test-username' - mockSdncProperties.authPassword >> 'test-password' - mockSdncProperties.topologyId >> 'testTopologyId' + setupTestConfigurationData() and: 'the rest template returns a valid response entity' def mockResponseEntity = Mock(ResponseEntity) - mockRestTemplate.getForEntity({ it.toString() == 'http://test-sdnc-uri/getResourceUrl' }, String.class, _ as HttpEntity) >> mockResponseEntity + mockRestTemplate.getForEntity({ it.toString() == 'http://some-uri/getResourceUrl' }, String.class, _ as HttpEntity) >> mockResponseEntity when: 'GET operation is invoked' def result = objectUnderTest.getOperation(getResourceUrl) then: 'the output of the method is equal to the output from the test template' result == mockResponseEntity } + + def 'SDNC POST operation called.'() { + given: 'json data' + def jsonData = 'some-json' + and: 'a url for get module resources' + def getModuleResourceUrl = '/getModuleResourceUrl' + and: 'configuration data' + setupTestConfigurationData() + and: 'the rest template returns a valid response entity' + def mockResponseEntity = Mock(ResponseEntity) + when: 'get module resources is invoked' + def result = objectUnderTest.postOperationWithJsonData(getModuleResourceUrl, jsonData) + then: 'the rest template is called with the correct uri and json in the body' + 1 * mockRestTemplate.postForEntity({ it.toString() == 'http://some-uri/getModuleResourceUrl' }, + { it.body.contains(jsonData) }, String.class) >> mockResponseEntity + and: 'the output of the method is the same as the output from the test template' + result == mockResponseEntity + } + + def setupTestConfigurationData() { + mockSdncProperties.baseUrl >> 'http://some-uri' + mockSdncProperties.authUsername >> 'some-username' + mockSdncProperties.authPassword >> 'some-password' + mockSdncProperties.topologyId >> 'some-topology-id' + } }
\ No newline at end of file diff --git a/src/test/groovy/org/onap/cps/ncmp/dmi/service/operation/SdncOperationsSpec.groovy b/src/test/groovy/org/onap/cps/ncmp/dmi/service/operation/SdncOperationsSpec.groovy index 956834ad..9b07d68e 100644 --- a/src/test/groovy/org/onap/cps/ncmp/dmi/service/operation/SdncOperationsSpec.groovy +++ b/src/test/groovy/org/onap/cps/ncmp/dmi/service/operation/SdncOperationsSpec.groovy @@ -22,23 +22,38 @@ package org.onap.cps.ncmp.dmi.service.operation import org.onap.cps.ncmp.dmi.config.DmiConfiguration import org.onap.cps.ncmp.dmi.service.client.SdncRestconfClient -import org.springframework.http.HttpStatus -import org.springframework.http.ResponseEntity +import org.spockframework.spring.SpringBean +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.boot.test.context.SpringBootTest +import org.springframework.test.context.ContextConfiguration import spock.lang.Specification -class SdncOperationsSpec extends Specification { - def mockSdncProperties = Mock(DmiConfiguration.SdncProperties) - def mockSdncRestClient = Mock(SdncRestconfClient) +@SpringBootTest +@ContextConfiguration(classes = [DmiConfiguration.SdncProperties, SdncOperations]) +class SdncOperationsSpec extends Specification { + + @SpringBean + SdncRestconfClient mockSdncRestClient = Mock() + @Autowired + SdncOperations objectUnderTest def 'call get modules from node to SDNC.'() { - given: 'nodeid, topology-id, responseentity' + given: 'node id and url' def nodeId = 'node1' def expectedUrl = '/rests/data/network-topology:network-topology/topology=test-topology/node=node1/yang-ext:mount/ietf-netconf-monitoring:netconf-state/schemas' - mockSdncProperties.getTopologyId() >> 'test-topology' - def objectUnderTest = new SdncOperations(mockSdncProperties, mockSdncRestClient) when: 'called get modules from node' objectUnderTest.getModulesFromNode(nodeId) then: 'the get operation is executed with the correct URL' 1 * mockSdncRestClient.getOperation(expectedUrl) } + + def 'Get module resources from SDNC.'() { + given: 'node id and url' + def nodeId = 'some-node' + def expectedUrl = '/rests/operations/network-topology:network-topology/topology=test-topology/node=some-node/yang-ext:mount/ietf-netconf-monitoring:get-schema' + when: 'get module resources is called with the expected parameters' + objectUnderTest.getModuleResource(nodeId, 'some-json-data') + then: 'the SDNC Rest client is invoked with the correct URL and json data' + 1 * mockSdncRestClient.postOperationWithJsonData(expectedUrl, 'some-json-data') + } } |