From 577efb09a39282c608b53a3099b7edc95954f9c2 Mon Sep 17 00:00:00 2001 From: niamhcore Date: Wed, 11 Aug 2021 16:13:53 +0100 Subject: Transform module information from NetConf node to Generic format for NCMP Issue-ID: CPS-531 Signed-off-by: niamhcore Change-Id: I918be4db5066d92b23e25fc7fbc22d4535fafc8c --- .../rest/controller/DmiRestControllerSpec.groovy | 47 ++++++++++++++++++---- .../cps/ncmp/dmi/service/DmiServiceImplSpec.groovy | 20 ++++++++- 2 files changed, 57 insertions(+), 10 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 0c8ae3fc..f24f65a8 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,11 +20,19 @@ package org.onap.cps.ncmp.dmi.rest.controller + +import com.fasterxml.jackson.databind.ObjectMapper 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.model.ModuleSchemaList +import org.onap.cps.ncmp.dmi.model.ModuleSchemaProperties +import org.onap.cps.ncmp.dmi.model.ModuleSchemas +import org.onap.cps.ncmp.dmi.model.ModuleSet +import org.onap.cps.ncmp.dmi.model.ModuleSetSchemas import org.onap.cps.ncmp.dmi.service.DmiService import org.spockframework.spring.SpringBean import org.springframework.beans.factory.annotation.Autowired @@ -47,6 +55,9 @@ class DmiRestControllerSpec extends Specification { @SpringBean DmiService mockDmiService = Mock() + @SpringBean + ObjectMapper mockObjectMapper = Spy() + @Autowired private MockMvc mvc @@ -57,8 +68,14 @@ class DmiRestControllerSpec extends Specification { 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 + def moduleSetSchema = new ModuleSetSchemas() + moduleSetSchema.namespace('some-namespace') + moduleSetSchema.moduleName('some-moduleName') + moduleSetSchema.revision('some-revision') + def moduleSetSchemasList = [moduleSetSchema] as List + def moduleSet = new ModuleSet() + moduleSet.schemas(moduleSetSchemasList) + mockDmiService.getModulesForCmHandle('node1') >> moduleSet when: 'post is being called' def response = mvc.perform(post(getModuleUrl) .contentType(MediaType.APPLICATION_JSON)) @@ -66,7 +83,21 @@ class DmiRestControllerSpec extends Specification { then: 'status is OK' response.status == HttpStatus.OK.value() and: 'the response content matches the result from the DMI service' - response.getContentAsString() == someJson + response.getContentAsString() == '{"schemas":[{"moduleName":"some-moduleName","revision":"some-revision","namespace":"some-namespace"}]}' + } + + def 'Get all modules for given cm handle with invalid json.'() { + given: 'REST endpoint for getting all modules' + def getModuleUrl = "$basePathV1/ch/node1/modules" + and: 'get modules for cmHandle throws an exception' + mockObjectMapper.readValue(_ as String, _ as ModuleSchemaList) >> { throw Mock(DmiException.class) } + mockDmiService.getModulesForCmHandle('node1') >> 'some-value' + when: 'post is being called' + def response = mvc.perform(post(getModuleUrl) + .contentType(MediaType.APPLICATION_JSON)) + .andReturn().response + then: 'the status is as expected' + response.status == HttpStatus.INTERNAL_SERVER_ERROR.value() } def 'Get all modules for given cm handle with exception handling of #scenario.'() { @@ -75,16 +106,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.'() { 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 da8ff6b8..e456d411 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 @@ -22,6 +22,7 @@ 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.TestUtils import org.onap.cps.ncmp.dmi.config.DmiPluginConfig import org.onap.cps.ncmp.dmi.exception.CmHandleRegistrationException import org.onap.cps.ncmp.dmi.exception.DmiException @@ -29,6 +30,8 @@ import org.onap.cps.ncmp.dmi.exception.ModuleResourceNotFoundException import org.onap.cps.ncmp.dmi.exception.ModulesNotFoundException import org.onap.cps.ncmp.dmi.exception.ResourceDataNotFound import org.onap.cps.ncmp.dmi.model.ModuleReference +import org.onap.cps.ncmp.dmi.model.ModuleSchemaList +import org.onap.cps.ncmp.dmi.model.ModuleSchemas import org.onap.cps.ncmp.dmi.service.client.NcmpRestClient import org.onap.cps.ncmp.dmi.service.operation.SdncOperations import org.springframework.http.HttpStatus @@ -49,12 +52,25 @@ class DmiServiceImplSpec extends Specification { given: 'cm handle id' def cmHandle = 'node1' and: 'request operation returns OK' - def body = 'body' + def body = TestUtils.getResourceFileContent('ModuleSchema.json') 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 + result.toString().contains('moduleName: example-identifier') + result.toString().contains('revision: example-version') + } + + def 'Call get modules for cm-handle with invalid json.'() { + given: 'cm handle id' + def cmHandle = 'node1' + and: 'request operation returns invalid json' + def body = TestUtils.getResourceFileContent('ModuleSchema.json') + mockSdncOperations.getModulesFromNode(cmHandle) >> new ResponseEntity('invalid json', HttpStatus.OK) + when: 'get modules for cm-handle is called' + def result = objectUnderTest.getModulesForCmHandle(cmHandle) + then: 'a dmi exception is thrown' + thrown(DmiException) } def 'Call get modules for cm-handle and SDNC returns "bad request" status.'() { -- cgit 1.2.3-korg