From 2270d76e4f33ad231cdae317e88ea1769297cfec Mon Sep 17 00:00:00 2001 From: tragait Date: Thu, 8 Jul 2021 15:42:19 +0100 Subject: create post request for cmhandles registration Issue-ID: CPS-406 Change-Id: I64e88643221403e117146443287d03788fb71c3e Signed-off-by: tragait --- .../rest/controller/DmiRestControllerSpec.groovy | 35 +++++++++++++- .../cps/ncmp/dmi/service/DmiServiceImplSpec.groovy | 56 ++++++++++++++++++++-- 2 files changed, 84 insertions(+), 7 deletions(-) (limited to 'src/test/groovy') 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) + 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) + } } -- cgit 1.2.3-korg