diff options
Diffstat (limited to 'src/test')
3 files changed, 137 insertions, 7 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 f249de92..993b80c6 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 @@ -56,8 +56,8 @@ class DmiRestControllerSpec extends Specification { mockDmiService.getModulesForCmHandle('node1') >> someJson when: 'post is being called' def response = mvc.perform( post(getModuleUrl) - .contentType(MediaType.APPLICATION_JSON)) - .andReturn().response + .contentType(MediaType.APPLICATION_JSON)) + .andReturn().response then: 'status is OK' response.status == HttpStatus.OK.value() and: 'the response content matches the result from the DMI service' @@ -81,4 +81,35 @@ class DmiRestControllerSpec extends Specification { '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.'() { + given: 'register cm handle url and cm handles json' + def registerCmhandlesPost = "${basePathV1}/inventory/cmHandles" + def cmHandleJson = '{"cmHandles":["node1", "node2"]}' + when: 'post register cm handles api is invoked' + def response = mvc.perform( + post(registerCmhandlesPost) + .contentType(MediaType.APPLICATION_JSON) + .content(cmHandleJson) + ).andReturn().response + then: 'register cm handles in dmi service is called once' + 1 * mockDmiService.registerCmHandles(_ as List<String>) + and: 'response status is created' + response.status == HttpStatus.CREATED.value() + } + + def 'register cm handles called with empty content.'() { + given: 'register cm handle url and empty json' + def registerCmhandlesPost = "${basePathV1}/inventory/cmHandles" + def emptyJson = '{"cmHandles":[]}' + when: 'register cm handles post api is invoked with no content' + def response = mvc.perform( + post(registerCmhandlesPost).contentType(MediaType.APPLICATION_JSON) + .content(emptyJson) + ).andReturn().response + then: 'response status is "bad request"' + response.status == HttpStatus.BAD_REQUEST.value() + and: 'dmi service is not called' + 0 * mockDmiService.registerCmHandles(_) + } } 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 66612960..9d6bc358 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,8 +20,13 @@ package org.onap.cps.ncmp.dmi.service +import com.fasterxml.jackson.core.JsonProcessingException +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.ModulesNotFoundException +import org.onap.cps.ncmp.dmi.service.client.NcmpRestClient import org.onap.cps.ncmp.dmi.service.operation.SdncOperations import org.springframework.http.HttpStatus import org.springframework.http.ResponseEntity @@ -29,13 +34,13 @@ import spock.lang.Specification class DmiServiceImplSpec extends Specification { - def objectUnderTest = new DmiServiceImpl() + def mockNcmpRestClient = Mock(NcmpRestClient) + def mockDmiPluginProperties = Mock(DmiPluginConfig.DmiPluginProperties) + def objectMapper = new ObjectMapper() + def mockObjectMapper = Mock(ObjectMapper) def mockSdncOperations = Mock(SdncOperations) - - def setup() { - objectUnderTest.sdncOperations = mockSdncOperations - } + def objectUnderTest = new DmiServiceImpl(mockDmiPluginProperties, mockNcmpRestClient, objectMapper, mockSdncOperations) def 'Call get modules for cm-handle on dmi Service.'() { given: 'cm handle id' @@ -70,4 +75,45 @@ class DmiServiceImplSpec extends Specification { then: 'ModulesNotFoundException is thrown' thrown( ModulesNotFoundException ) } + + def 'Register cm handles with ncmp.'() { + given: 'cm-handle list and json payload' + def givenCmHandlesList = ['node1', 'node2'] + def expectedJson = '{"dmiPlugin":"test-dmi-service","createdCmHandles":[{"cmHandle":"node1"},{"cmHandle":"node2"}]}' + and: 'mockDmiPluginProperties returns test-dmi-service' + mockDmiPluginProperties.getDmiServiceName() >> 'test-dmi-service' + when: 'register cm handles service method with the given cm handles' + objectUnderTest.registerCmHandles(givenCmHandlesList) + then: 'register cm handle with ncmp called once and return "created" status' + 1 * mockNcmpRestClient.registerCmHandlesWithNcmp(expectedJson) >> new ResponseEntity<>(HttpStatus.CREATED) + } + + def 'Register cm handles with ncmp called with exception #scenario.'() { + given: 'cm-handle list' + def cmHandlesList = ['node1', 'node2'] + and: 'dmi plugin service name is "test-dmi-service"' + mockDmiPluginProperties.getDmiServiceName() >> 'test-dmi-service' + and: 'ncmp rest client returns #responseEntity' + mockNcmpRestClient.registerCmHandlesWithNcmp(_ as String) >> responseEntity + when: 'register cm handles service method called' + objectUnderTest.registerCmHandles(cmHandlesList) + 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) + } + + def 'Register cm handles with ncmp with wrong data.'() { + given: 'objectMapper mock and cm-handle list' + def cmHandlesList = ['node1', 'node2'] + and: 'objectMapper returns "JsonProcessingException" during parse' + objectUnderTest.objectMapper = mockObjectMapper + mockObjectMapper.writeValueAsString(_) >> { throw new JsonProcessingException('some error.') } + when: 'register cm handles service method called' + objectUnderTest.registerCmHandles(cmHandlesList) + then: 'a dmi exception is thrown' + thrown(DmiException.class) + } } diff --git a/src/test/java/org/onap/cps/ncmp/dmi/TestUtils.java b/src/test/java/org/onap/cps/ncmp/dmi/TestUtils.java new file mode 100644 index 00000000..b82a6f5c --- /dev/null +++ b/src/test/java/org/onap/cps/ncmp/dmi/TestUtils.java @@ -0,0 +1,53 @@ +/* + * ============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; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; + +/** + * Common convenience methods for testing. + */ +public class TestUtils { + + /** + * Convert a file in the test resource folder to file. + * + * @param filename to name of the file in test/resources + * @return the file + * @throws IOException when there is an IO issue + */ + public static File readFile(final String filename) { + return new File(ClassLoader.getSystemClassLoader().getResource(filename).getFile()); + } + + /** + * Convert a file in the test resource folder to a string. + * + * @param filename to name of the file in test/resources + * @return the content of the file as a String + * @throws IOException when there is an IO issue + */ + public static String getResourceFileContent(final String filename) throws IOException { + final File file = readFile(filename); + return new String(Files.readAllBytes(file.toPath())); + } +} |