From 7c4a9aa88269dbdb21c5c54bc47508463548bc1e Mon Sep 17 00:00:00 2001 From: tragait Date: Mon, 19 Jul 2021 13:46:37 +0100 Subject: implement dmi get modules using sdnc client Issue-ID: CPS-483 Change-Id: Ib9b730cabeba308f11db31ef1b45bbd92e3a6ed5 Signed-off-by: tragait --- .../rest/controller/DmiRestControllerSpec.groovy | 58 +++++++++++++++------- .../cps/ncmp/dmi/service/DmiServiceImplSpec.groovy | 47 ++++++++++++++++-- .../dmi/service/client/NcmpRestClientSpec.groovy | 8 +-- .../service/client/SdncRestconfClientSpec.groovy | 52 +++++++++++++++++++ .../service/operation/SdncOperationsSpec.groovy | 44 ++++++++++++++++ 5 files changed, 182 insertions(+), 27 deletions(-) create mode 100644 src/test/groovy/org/onap/cps/ncmp/dmi/service/client/SdncRestconfClientSpec.groovy create mode 100644 src/test/groovy/org/onap/cps/ncmp/dmi/service/operation/SdncOperationsSpec.groovy (limited to 'src/test/groovy/org') 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 f8b4c015..f249de92 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,19 +20,21 @@ package org.onap.cps.ncmp.dmi.rest.controller +import org.onap.cps.ncmp.dmi.exception.DmiException +import org.onap.cps.ncmp.dmi.exception.ModulesNotFoundException import org.onap.cps.ncmp.dmi.service.DmiService - -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get - import org.spockframework.spring.SpringBean -import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc -import org.springframework.http.HttpStatus import org.springframework.beans.factory.annotation.Autowired import org.springframework.beans.factory.annotation.Value +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest +import org.springframework.http.HttpStatus +import org.springframework.http.MediaType import org.springframework.test.web.servlet.MockMvc import spock.lang.Specification +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post + @WebMvcTest @AutoConfigureMockMvc(addFilters = false) class DmiRestControllerSpec extends Specification { @@ -43,22 +45,40 @@ class DmiRestControllerSpec extends Specification { @Autowired private MockMvc mvc - @Value('${rest.api.dmi-base-path}') - def basePath - - def 'Get Hello World'() { - given: 'hello world endpoint' - def helloWorldEndpoint = "$basePath/v1/helloworld" + @Value('${rest.api.dmi-base-path}/v1') + def basePathV1 - when: 'get hello world api is invoked' - def response = mvc.perform( - get(helloWorldEndpoint) - ).andReturn().response - - then: 'Response Status is OK and contains expected text' + def 'Get all modules for given cm handle.'() { + given: 'REST endpoint for getting all modules' + def getModuleUrl = "$basePathV1/ch/node1/modules" + and: 'get modules for cm-handle returns a json' + def someJson = 'some-json' + mockDmiService.getModulesForCmHandle('node1') >> someJson + when: 'post is being called' + def response = mvc.perform( post(getModuleUrl) + .contentType(MediaType.APPLICATION_JSON)) + .andReturn().response + then: 'status is OK' response.status == HttpStatus.OK.value() - then: 'the java API was called with the correct parameters' - 1 * mockDmiService.getHelloWorld() + and: 'the response content matches the result from the DMI service' + response.getContentAsString() == someJson } + def 'Get all modules for given cm handle with exception handling of #scenario.'() { + given: 'REST endpoint for getting all modules' + def getModuleUrl = "$basePathV1/ch/node1/modules" + and: 'get modules for cm-handle throws #exceptionClass' + mockDmiService.getModulesForCmHandle('node1') >> { throw Mock(exceptionClass) } + when: 'post is invoked' + 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() + } } 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 2124c9b4..66612960 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 @@ -20,15 +20,54 @@ package org.onap.cps.ncmp.dmi.service - +import org.onap.cps.ncmp.dmi.exception.DmiException +import org.onap.cps.ncmp.dmi.exception.ModulesNotFoundException +import org.onap.cps.ncmp.dmi.service.operation.SdncOperations +import org.springframework.http.HttpStatus +import org.springframework.http.ResponseEntity import spock.lang.Specification class DmiServiceImplSpec extends Specification { + def objectUnderTest = new DmiServiceImpl() - def 'Retrieve Hello World'() { - expect: 'Hello World is Returned' - objectUnderTest.getHelloWorld() == 'Hello World' + def mockSdncOperations = Mock(SdncOperations) + + def setup() { + objectUnderTest.sdncOperations = mockSdncOperations + } + + def 'Call get modules for cm-handle on dmi Service.'() { + given: 'cm handle id' + def cmHandle = 'node1' + and: 'request operation returns OK' + def body = 'body' + mockSdncOperations.getModulesFromNode(cmHandle) >> new ResponseEntity(body, HttpStatus.OK) + when: 'get modules for cm-handle is called' + def result = objectUnderTest.getModulesForCmHandle(cmHandle) + then: 'result is equal to the response from the SDNC service' + result == body + } + + def 'Call get modules for cm-handle and SDNC returns "bad request" status.'() { + given: 'cm handle id' + def cmHandle = 'node1' + and: 'get modules from node returns "bad request" status' + mockSdncOperations.getModulesFromNode(cmHandle) >> new ResponseEntity('body', HttpStatus.BAD_REQUEST) + when: 'get modules for cm-handle is called' + objectUnderTest.getModulesForCmHandle(cmHandle) + then: 'dmi exception is thrown' + thrown( DmiException ) } + def 'Call get modules for cm-handle and SDNC returns OK with empty body.'() { + given: 'cm handle id' + def cmHandle = 'node1' + and: 'get modules for cm-handle returns OK with empty body' + mockSdncOperations.getModulesFromNode(cmHandle) >> new ResponseEntity('', HttpStatus.OK) + when: 'get modules for cm-handle is called' + objectUnderTest.getModulesForCmHandle(cmHandle) + then: 'ModulesNotFoundException is thrown' + thrown( ModulesNotFoundException ) + } } diff --git a/src/test/groovy/org/onap/cps/ncmp/dmi/service/client/NcmpRestClientSpec.groovy b/src/test/groovy/org/onap/cps/ncmp/dmi/service/client/NcmpRestClientSpec.groovy index 4f929865..f5c059c7 100644 --- a/src/test/groovy/org/onap/cps/ncmp/dmi/service/client/NcmpRestClientSpec.groovy +++ b/src/test/groovy/org/onap/cps/ncmp/dmi/service/client/NcmpRestClientSpec.groovy @@ -20,14 +20,14 @@ package org.onap.cps.ncmp.dmi.service.client -import org.onap.cps.ncmp.dmi.config.CpsConfiguration +import org.onap.cps.ncmp.dmi.config.DmiConfiguration import org.springframework.http.ResponseEntity import org.springframework.web.client.RestTemplate import spock.lang.Specification class NcmpRestClientSpec extends Specification { def objectUnderTest = new NcmpRestClient(mockCpsProperties, mockRestTemplate) - def mockCpsProperties = Mock(CpsConfiguration.CpsProperties) + def mockCpsProperties = Mock(DmiConfiguration.CpsProperties) def mockRestTemplate = Mock(RestTemplate) def setup() { @@ -45,12 +45,12 @@ class NcmpRestClientSpec extends Specification { mockCpsProperties.authPassword >> 'some-password' and: 'the rest template returns a valid response entity' def mockResponseEntity = Mock(ResponseEntity) - when: 'registerCmHandle is invoked' + when: 'register cm-handle with ncmp is invoked' def result = objectUnderTest.registerCmHandlesWithNcmp(jsonData) then: 'the rest template is called with the correct uri and json in the body' 1 * mockRestTemplate.postForEntity({ it.toString() == 'http://some-uri/some-url' }, { it.body.contains(jsonData) }, String.class) >> mockResponseEntity - and: 'the output of the method is the same as the output from the test template' + and: 'the output of the method is equal to the output from the test template' result == mockResponseEntity } } \ No newline at end of file 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 new file mode 100644 index 00000000..0b192f05 --- /dev/null +++ b/src/test/groovy/org/onap/cps/ncmp/dmi/service/client/SdncRestconfClientSpec.groovy @@ -0,0 +1,52 @@ +/* + * ============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.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 + +class SdncRestconfClientSpec extends Specification { + + def mockSdncProperties = Mock(DmiConfiguration.SdncProperties) + def mockRestTemplate = Mock(RestTemplate) + def objectUnderTest = new SdncRestconfClient(mockSdncProperties, mockRestTemplate) + + def 'SDNC GET operation.'() { + 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' + 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 + 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 + } +} \ 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 new file mode 100644 index 00000000..956834ad --- /dev/null +++ b/src/test/groovy/org/onap/cps/ncmp/dmi/service/operation/SdncOperationsSpec.groovy @@ -0,0 +1,44 @@ +/* + * ============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.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 spock.lang.Specification + +class SdncOperationsSpec extends Specification { + def mockSdncProperties = Mock(DmiConfiguration.SdncProperties) + def mockSdncRestClient = Mock(SdncRestconfClient) + + def 'call get modules from node to SDNC.'() { + given: 'nodeid, topology-id, responseentity' + 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) + } +} -- cgit 1.2.3-korg