From f5f5e6be4e7d222a7530b96d2d77ed611934822f Mon Sep 17 00:00:00 2001 From: emaclee Date: Fri, 15 Jul 2022 12:31:46 +0100 Subject: Groovy clean tests clean-up - test labels are modified to set standards for writing tests - labels are not expected to be technical - labels need to be in simple language that ANYONE can understand - labels should not have exact method names except for some mocking - variables are modified to give highlight to ONLY important parameter of test - wild cards are preferred when possible i.e. for exception cases Issue-ID: CPS-588 Signed-off-by: emaclee Change-Id: I3d42471e7aa4bc61f962ad19cc1f237f6985a9a2 --- .../rest/controller/ControllerSecuritySpec.groovy | 2 +- .../rest/controller/DmiRestControllerSpec.groovy | 112 +++++------ .../cps/ncmp/dmi/service/DmiServiceImplSpec.groovy | 204 ++++++++++----------- .../dmi/service/client/NcmpRestClientSpec.groovy | 16 +- .../service/client/SdncRestconfClientSpec.groovy | 38 ++-- .../service/operation/SdncOperationsSpec.groovy | 37 ++-- src/test/resources/GetModules.json | 17 -- src/test/resources/moduleResources.json | 17 ++ 8 files changed, 222 insertions(+), 221 deletions(-) delete mode 100644 src/test/resources/GetModules.json create mode 100644 src/test/resources/moduleResources.json diff --git a/src/test/groovy/org/onap/cps/ncmp/dmi/rest/controller/ControllerSecuritySpec.groovy b/src/test/groovy/org/onap/cps/ncmp/dmi/rest/controller/ControllerSecuritySpec.groovy index 8b13fc79..67de1b93 100644 --- a/src/test/groovy/org/onap/cps/ncmp/dmi/rest/controller/ControllerSecuritySpec.groovy +++ b/src/test/groovy/org/onap/cps/ncmp/dmi/rest/controller/ControllerSecuritySpec.groovy @@ -38,7 +38,7 @@ class ControllerSecuritySpec extends Specification { def testEndpoint = '/test' - def 'Get request with authentication'() { + def 'Get request with valid authentication'() { when: 'request is sent with authentication' def response = mvc.perform( get(testEndpoint).header("Authorization", 'Basic Y3BzdXNlcjpjcHNyMGNrcyE=') 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 5bfbc400..2be0b59c 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 @@ -77,20 +77,20 @@ class DmiRestControllerSpec extends Specification { def basePathV1 def 'Get all modules.'() { - given: 'REST endpoint for getting all modules' + given: 'URL for getting all modules and some request data' def getModuleUrl = "$basePathV1/ch/node1/modules" - and: 'get modules for cm-handle returns a json' - def json = '{"cmHandleProperties" : {}}' + def someValidJson = '{}' + and: 'DMI service returns some module' def moduleSetSchema = new ModuleSetSchemas(namespace:'some-namespace', - moduleName:'some-moduleName', - revision:'some-revision') + moduleName:'some-moduleName', + revision:'some-revision') def moduleSetSchemasList = [moduleSetSchema] as List def moduleSet = new ModuleSet() moduleSet.schemas(moduleSetSchemasList) mockDmiService.getModulesForCmHandle('node1') >> moduleSet - when: 'post is being called' + when: 'the request is posted' def response = mvc.perform(post(getModuleUrl) - .contentType(MediaType.APPLICATION_JSON).content(json)) + .contentType(MediaType.APPLICATION_JSON).content(someValidJson)) .andReturn().response then: 'status is OK' response.status == OK.value() @@ -99,14 +99,14 @@ class DmiRestControllerSpec extends Specification { } def 'Get all modules with exception handling of #scenario.'() { - given: 'REST endpoint for getting all modules' + given: 'URL for getting all modules and some request data' def getModuleUrl = "$basePathV1/ch/node1/modules" - and: 'given request body and get modules for cm-handle throws #exceptionClass' - def json = '{"cmHandleProperties" : {}}' + def someValidJson = '{}' + and: 'a #exception is thrown during the process' mockDmiService.getModulesForCmHandle('node1') >> { throw exception } - when: 'post is invoked' + when: 'the request is posted' def response = mvc.perform( post(getModuleUrl) - .contentType(MediaType.APPLICATION_JSON).content(json)) + .contentType(MediaType.APPLICATION_JSON).content(someValidJson)) .andReturn().response then: 'response status is #expectedResponse' response.status == expectedResponse.value() @@ -119,10 +119,10 @@ class DmiRestControllerSpec extends Specification { } def 'Register given list.'() { - given: 'register cm handle url and cm handles json' + given: 'register cm handle url and cmHandles' def registerCmhandlesPost = "${basePathV1}/inventory/cmHandles" def cmHandleJson = '{"cmHandles":["node1", "node2"]}' - when: 'register cm handles api is invoked with POST' + when: 'the request is posted' def response = mvc.perform( post(registerCmhandlesPost) .contentType(MediaType.APPLICATION_JSON) @@ -138,7 +138,7 @@ class DmiRestControllerSpec extends Specification { 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' + when: 'the request is posted' def response = mvc.perform( post(registerCmhandlesPost).contentType(MediaType.APPLICATION_JSON) .content(emptyJson) @@ -150,9 +150,10 @@ class DmiRestControllerSpec extends Specification { } def 'Retrieve module resources.'() { - given: 'an endpoint and json data' + given: 'URL to get module resources' def getModulesEndpoint = "$basePathV1/ch/some-cm-handle/moduleResources" - String jsonData = TestUtils.getResourceFileContent('GetModules.json') + and: 'request data to get some modules' + String jsonData = TestUtils.getResourceFileContent('moduleResources.json') and: 'the DMI service returns the yang resources' ModuleReference moduleReference1 = new ModuleReference(name: 'ietf-yang-library', revision: '2016-06-21') ModuleReference moduleReference2 = new ModuleReference(name: 'nc-notifications', revision: '2008-07-14') @@ -161,23 +162,24 @@ class DmiRestControllerSpec extends Specification { def yangResource = new YangResource(yangSource: '"some-data"', moduleName: 'NAME', revision: 'REVISION') yangResources.add(yangResource) mockDmiService.getModuleResources('some-cm-handle', moduleReferences) >> yangResources - when: 'get module resource api is invoked' + when: 'the request is posted' def response = mvc.perform(post(getModulesEndpoint) .contentType(MediaType.APPLICATION_JSON) .content(jsonData)).andReturn().response then: 'a OK status is returned' response.status == OK.value() - and: 'the expected response is returned' + and: 'the response content matches the result from the DMI service' response.getContentAsString() == '[{"yangSource":"\\"some-data\\"","moduleName":"NAME","revision":"REVISION"}]' } def 'Retrieve module resources with exception handling.'() { - given: 'an endpoint and json data' + given: 'URL to get module resources' def getModulesEndpoint = "$basePathV1/ch/some-cm-handle/moduleResources" - String jsonData = TestUtils.getResourceFileContent('GetModules.json') - and: 'the service method is invoked to get module resources and throws an exception' + and: 'request data to get some modules' + String jsonData = TestUtils.getResourceFileContent('moduleResources.json') + and: 'the system throws a not-found exception (during the processing)' mockDmiService.getModuleResources('some-cm-handle', _) >> { throw Mock(ModuleResourceNotFoundException.class) } - when: 'get module resource api is invoked' + when: 'the request is posted' def response = mvc.perform(post(getModulesEndpoint) .contentType(MediaType.APPLICATION_JSON) .content(jsonData)).andReturn().response @@ -186,31 +188,32 @@ class DmiRestControllerSpec extends Specification { } def 'Get resource data for pass-through operational.'() { - given: 'Get resource data url' + given: 'Get resource data url and some request data' def getResourceDataForCmHandleUrl = "${basePathV1}/ch/some-cmHandle/data/ds/ncmp-datastore:passthrough-operational" + "?resourceIdentifier=parent/child&options=(fields=myfields,depth=5)" - def json = '{"cmHandleProperties" : { "prop1" : "value1", "prop2" : "value2"}}' - when: 'get resource data POST api is invoked' + def someValidJson = '{}' + when: 'the request is posted' def response = mvc.perform( - post(getResourceDataForCmHandleUrl).contentType(MediaType.APPLICATION_JSON).content(json) + post(getResourceDataForCmHandleUrl).contentType(MediaType.APPLICATION_JSON).content(someValidJson) ).andReturn().response then: 'response status is ok' response.status == OK.value() - and: 'dmi service called with get resource data' + and: 'dmi service method to get resource data is invoked once' 1 * mockDmiService.getResourceData('some-cmHandle', 'parent/child', '(fields=myfields,depth=5)', 'content=all') } - def 'Get resource data for pass-through operational with bad request.'() { + def 'Get resource data for pass-through operational with write request (invalid).'() { given: 'Get resource data url' def getResourceDataForCmHandleUrl = "${basePathV1}/ch/some-cmHandle/data/ds/ncmp-datastore:passthrough-operational" + - "?resourceIdentifier=parent/child&options=(fields=myfields,depth=5)" - def jsonData = TestUtils.getResourceFileContent('createDataWithNormalChar.json') - when: 'get resource data POST api is invoked' + "?resourceIdentifier=parent/child&options=(fields=myfields,depth=5)" + and: 'an invalid write request data for "create" operation' + def jsonData = '{"operation":"create"}' + when: 'the request is posted' def response = mvc.perform( - post(getResourceDataForCmHandleUrl).contentType(MediaType.APPLICATION_JSON).content(jsonData) + post(getResourceDataForCmHandleUrl).contentType(MediaType.APPLICATION_JSON).content(jsonData) ).andReturn().response then: 'response status is bad request' response.status == BAD_REQUEST.value() @@ -219,22 +222,23 @@ class DmiRestControllerSpec extends Specification { } def 'data with #scenario operation using passthrough running.'() { - given: 'write data for passthrough running url and jsonData' + given: 'write data for passthrough running url' def writeDataForPassthroughRunning = "${basePathV1}/ch/some-cmHandle/data/ds/ncmp-datastore:passthrough-running" + "?resourceIdentifier=some-resourceIdentifier" + and: 'request data for #scenario' def jsonData = TestUtils.getResourceFileContent(requestBodyFile) and: 'dmi service is called' mockDmiService.writeData(operationEnum, 'some-cmHandle', 'some-resourceIdentifier', dataType, 'normal request body' ) >> '{some-json}' - when: 'write data for passthrough running post api is invoked with json data' + when: 'the request is posted' def response = mvc.perform( post(writeDataForPassthroughRunning).contentType(MediaType.APPLICATION_JSON) .content(jsonData) ).andReturn().response - then: 'response status is #expectedResponseStatus' + then: 'response status is #expectedResponseStatus' response.status == expectedResponseStatus - and: 'the data in the request body is as expected' + and: 'the response content matches the result from the DMI service' response.getContentAsString() == expectedJsonResponse where: 'given request body and data' scenario | requestBodyFile | operationEnum | dataType || expectedResponseStatus | expectedJsonResponse @@ -246,51 +250,53 @@ class DmiRestControllerSpec extends Specification { } def 'Create data using passthrough for special characters.'(){ - given: 'create data for cmHandle url and JsonData' + given: 'create data for cmHandle url' def writeDataForCmHandlePassthroughRunning = "${basePathV1}/ch/some-cmHandle/data/ds/ncmp-datastore:passthrough-running" + - "?resourceIdentifier=some-resourceIdentifier" + "?resourceIdentifier=some-resourceIdentifier" + and: 'request data with special characters' def jsonData = TestUtils.getResourceFileContent('createDataWithSpecialChar.json') - and: 'dmi service is called' + and: 'dmi service returns data' mockDmiService.writeData(CREATE, 'some-cmHandle', 'some-resourceIdentifier', 'application/json', - 'data with quote \" and new line \n') >> '{some-json}' - when: 'create cmHandle passthrough running post api is invoked with json data with special chars' + 'data with quote \" and new line \n') >> '{some-json}' + when: 'the request is posted' def response = mvc.perform( - post(writeDataForCmHandlePassthroughRunning).contentType(MediaType.APPLICATION_JSON).content(jsonData) + post(writeDataForCmHandlePassthroughRunning).contentType(MediaType.APPLICATION_JSON).content(jsonData) ).andReturn().response - then: 'response status is CREATED' + then: 'response status is CREATED' response.status == CREATED.value() - and: 'the data in the request body is as expected' + and: 'the response content matches the result from the DMI service' response.getContentAsString() == '{some-json}' } def 'PassThrough Returns OK when topic is used for async'(){ - given: 'an endpoint' + given: 'Passthrough read URL and request data with a topic (parameter)' def readPassThroughUrl ="${basePathV1}/ch/some-cmHandle/data/ds/ncmp-datastore:" + - resourceIdentifier + - '?resourceIdentifier=some-resourceIdentifier&topic=test-topic' - when: 'endpoint is invoked' + resourceIdentifier + + '?resourceIdentifier=some-resourceIdentifier&topic=test-topic' def jsonData = TestUtils.getResourceFileContent('readData.json') + when: 'the request is posted' def response = mvc.perform( - post(readPassThroughUrl).contentType(MediaType.APPLICATION_JSON).content(jsonData) + post(readPassThroughUrl).contentType(MediaType.APPLICATION_JSON).content(jsonData) ).andReturn().response then: 'response status is OK' assert response.status == HttpStatus.NO_CONTENT.value() where: 'the following values are used' - resourceIdentifier << ['passthrough-operational', 'passthrough-running'] + resourceIdentifier << ['passthrough-operational', 'passthrough-running'] } def 'Get resource data for pass-through running with #scenario value in resource identifier param.'() { given: 'Get resource data url' def getResourceDataForCmHandleUrl = "${basePathV1}/ch/some-cmHandle/data/ds/ncmp-datastore:passthrough-running" + "?resourceIdentifier="+resourceIdentifier+"&options=(fields=myfields,depth=5)" + and: 'some valid json data' def json = '{"cmHandleProperties" : { "prop1" : "value1", "prop2" : "value2"}}' - when: 'get resource data POST api is invoked' + when: 'the request is posted' def response = mvc.perform( post(getResourceDataForCmHandleUrl).contentType(MediaType.APPLICATION_JSON).content(json) ).andReturn().response then: 'response status is ok' response.status == OK.value() - and: 'dmi service called with get resource data for a cm handle' + and: 'dmi service method to get resource data is invoked once with correct parameters' 1 * mockDmiService.getResourceData('some-cmHandle', resourceIdentifier, '(fields=myfields,depth=5)', 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 1d87b775..8531d35f 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 @@ -1,6 +1,6 @@ /* * ============LICENSE_START======================================================= - * Copyright (C) 2021 Nordix Foundation + * Copyright (C) 2021-2022 Nordix Foundation * Modifications Copyright (C) 2021-2022 Bell Canada * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -53,58 +53,27 @@ class DmiServiceImplSpec extends Specification { def mockSdncOperations = Mock(SdncOperations) def objectUnderTest = new DmiServiceImpl(mockDmiPluginProperties, mockNcmpRestClient, mockSdncOperations, spyObjectMapper) - def ' Get modules for a cm-handle.'() { - given: 'a cm handle' - def cmHandle = 'node1' - and: 'sdnc operations returns one module schema for the cmhandle' - def moduleSchema = new ModuleSchema( - identifier: "example-identifier", - namespace: "example:namespace", - version: "example-version") - mockSdncOperations.getModuleSchemasFromNode(cmHandle) >> List.of(moduleSchema) - when: 'get modules for cm-handle is called' - def result = objectUnderTest.getModulesForCmHandle(cmHandle) - then: 'one module is returned' - result.schemas.size() == 1 - and: 'module has expected values' - with(result.schemas[0]) { - it.getRevision() == moduleSchema.getVersion() - it.getModuleName() == moduleSchema.getIdentifier() - it.getNamespace() == moduleSchema.getNamespace(); - } - } - - def 'no modules found for the cmhandle.'() { - given: 'cm handle id' - def cmHandle = 'node1' - and: 'sdnc operations returns no modules' - mockSdncOperations.getModuleSchemasFromNode(cmHandle) >> Collections.emptyList(); - when: 'get modules for cm-handle is called' - objectUnderTest.getModulesForCmHandle(cmHandle) - then: 'module not found exception is thrown' - thrown(ModulesNotFoundException) - } - def 'Register cm handles with ncmp.'() { - given: 'cm-handle list and json payload' + given: 'some cm-handle ids' def givenCmHandlesList = ['node1', 'node2'] + and: 'json payload' def expectedJson = '{"dmiPlugin":"test-dmi-service","createdCmHandles":[{"cmHandle":"node1"},{"cmHandle":"node2"}]}' - and: 'mockDmiPluginProperties returns test-dmi-service' + and: 'process returns "test-dmi-service" for service name' mockDmiPluginProperties.getDmiServiceUrl() >> 'test-dmi-service' - when: 'register cm handles service method with the given cm handles' + when: 'the cm handles are registered' objectUnderTest.registerCmHandles(givenCmHandlesList) - then: 'register cm handle with ncmp called once and return "created" status' + then: 'register cm handle with ncmp is called with the expected json 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' + given: 'some cm-handle ids' def cmHandlesList = ['node1', 'node2'] - and: 'dmi plugin service name is "test-dmi-service"' + and: 'process returns "test-dmi-service" for service name' mockDmiPluginProperties.getDmiServiceUrl() >> 'test-dmi-service' - and: 'ncmp rest client returns #responseEntity' + and: 'returns #responseEntity' mockNcmpRestClient.registerCmHandlesWithNcmp(_ as String) >> responseEntity - when: 'register cm handles service method called' + when: 'the cm handles are registered' objectUnderTest.registerCmHandles(cmHandlesList) then: 'a registration exception is thrown' thrown(CmHandleRegistrationException.class) @@ -115,29 +84,62 @@ class DmiServiceImplSpec extends Specification { } def 'Register cm handles with ncmp with wrong data.'() { - given: 'objectMapper mock and cm-handle list' + given: 'some cm-handle ids' def cmHandlesList = ['node1', 'node2'] - and: 'objectMapper returns "JsonProcessingException" during parse' + and: ' "JsonProcessingException" occurs during parsing' objectUnderTest.objectMapper = mockObjectMapper mockObjectMapper.writeValueAsString(_) >> { throw new JsonProcessingException('some error.') } - when: 'register cm handles service method called' + when: 'the cmHandles are registered' objectUnderTest.registerCmHandles(cmHandlesList) then: 'a dmi exception is thrown' thrown(DmiException.class) } + def ' Get modules for a cm-handle.'() { + given: 'a cm handle' + def cmHandle = 'node1' + and: 'process returns one module schema for the cmhandle' + def moduleSchema = new ModuleSchema( + identifier: "example-identifier", + namespace: "example:namespace", + version: "example-version") + mockSdncOperations.getModuleSchemasFromNode(cmHandle) >> List.of(moduleSchema) + when: 'modules for cmHandle is requested' + def result = objectUnderTest.getModulesForCmHandle(cmHandle) + then: 'one module is returned' + result.schemas.size() == 1 + and: 'module has expected values' + with(result.schemas[0]) { + it.getRevision() == moduleSchema.getVersion() + it.getModuleName() == moduleSchema.getIdentifier() + it.getNamespace() == moduleSchema.getNamespace(); + } + } + + def 'no modules found for the cmhandle.'() { + given: 'cm handle id' + def cmHandle = 'node1' + and: 'process returns no modules' + mockSdncOperations.getModuleSchemasFromNode(cmHandle) >> Collections.emptyList(); + when: 'modules for cm-handle is requested' + objectUnderTest.getModulesForCmHandle(cmHandle) + then: 'module not found exception is thrown' + thrown(ModulesNotFoundException) + } + def 'Get multiple module resources.'() { - given: 'a cmHandle and module reference list' + given: 'a cmHandle' def cmHandle = 'some-cmHandle' + and: 'multiple module references' def moduleReference1 = new ModuleReference(name: 'name-1', revision: 'revision-1') def moduleReference2 = new ModuleReference(name: 'name-2', revision: 'revision-2') def moduleList = [moduleReference1, moduleReference2] - when: 'get module resources is invoked with the given cm handle and a module list' + when: 'module resources is requested' def result = objectUnderTest.getModuleResources(cmHandle, moduleList) - then: 'get modules resources is called twice' + then: 'SDNC operation service is called same number of module references given' 2 * mockSdncOperations.getModuleResource(cmHandle, _) >>> [new ResponseEntity('{"ietf-netconf-monitoring:output": {"data": "some-data1"}}', HttpStatus.OK), new ResponseEntity('{"ietf-netconf-monitoring:output": {"data": "some-data2"}}', HttpStatus.OK)] - and: 'the result is a yang resources object with the expected names, revisions and yang-sources' + and: 'the result contains the expected properties' def yangResources = new YangResources() def yangResource1 = new YangResource(yangSource: 'some-data1', moduleName: 'name-1', revision: 'revision-1') def yangResource2 = new YangResource(yangSource: 'some-data2', moduleName: 'name-2', revision: 'revision-2') @@ -151,11 +153,11 @@ class DmiServiceImplSpec extends Specification { def cmHandle = 'some-cmHandle' def moduleReference = new ModuleReference(name: 'NAME', revision: 'REVISION') def moduleList = [moduleReference] - when: 'get module resources is invoked with the given cm handle and a module list' + when: 'module resources is requested' objectUnderTest.getModuleResources(cmHandle, moduleList) - then: 'get modules resources is called once with a response body that contains no data' + then: 'SDNC operation service is called once with a response body that contains no data' 1 * mockSdncOperations.getModuleResource(cmHandle, _) >> new ResponseEntity(responseBody, HttpStatus.OK) - and: 'a module resource not found exception is thrown' + and: 'an exception is thrown' thrown(ModuleResourceNotFoundException) where: 'the following values are returned' scenario | responseBody @@ -164,28 +166,28 @@ class DmiServiceImplSpec extends Specification { } def 'Get module resources when sdnc returns #scenario response.'() { - given: 'get module schema is invoked and returns a response from sdnc' + given: 'sdnc returns a #scenario response' mockSdncOperations.getModuleResource(_ as String, _ as String) >> new ResponseEntity('some-response-body', httpStatus) - when: 'get module resources is invoked with the given cm handle and a module list' + when: 'module resources is requested' objectUnderTest.getModuleResources('some-cmHandle', [new ModuleReference()] as LinkedList) then: '#expectedException is thrown' thrown(expectedException) where: 'the following values are returned' - scenario | httpStatus || expectedException - 'not found' | HttpStatus.NOT_FOUND || ModuleResourceNotFoundException - 'a internal server' | HttpStatus.INTERNAL_SERVER_ERROR || DmiException + scenario | httpStatus || expectedException + 'not found' | HttpStatus.NOT_FOUND || ModuleResourceNotFoundException + 'internal server error' | HttpStatus.INTERNAL_SERVER_ERROR || DmiException } def 'Get module resources with JSON processing exception.'() { - given: 'a json processing exception during conversion' + given: 'a json processing exception during process' def mockObjectWriter = Mock(ObjectWriter) spyObjectMapper.writer() >> mockObjectWriter mockObjectWriter.withRootName(_) >> mockObjectWriter def jsonProcessingException = new JsonProcessingException('') mockObjectWriter.writeValueAsString(_) >> { throw jsonProcessingException } - when: 'get module resources is invoked with the given cm handle and a module list' + when: 'module resources is requested' objectUnderTest.getModuleResources('some-cmHandle', [new ModuleReference()] as LinkedList) - then: 'a DMI exception is thrown' + then: 'an exception is thrown' def thrownException = thrown(DmiException.class) and: 'the exception has the expected message and details' thrownException.message == 'Unable to process JSON.' @@ -195,47 +197,41 @@ class DmiServiceImplSpec extends Specification { } def 'Get resource data for passthrough operational.'() { - given: 'cm-handle, passthrough parameter, resourceId, accept header, fields, depth' - def cmHandle = 'testCmHandle' - def resourceId = 'testResourceId' - def optionsParam = '(fields=x/y/z,depth=10,test=abc)' - def contentQuery = 'content=all' - and: 'sdnc operation returns OK response' - mockSdncOperations.getResouceDataForOperationalAndRunning(cmHandle, resourceId, optionsParam, contentQuery) >> new ResponseEntity<>('response json', HttpStatus.OK) - when: 'get resource data from cm handles service method invoked' - def response = objectUnderTest.getResourceData(cmHandle, - resourceId, optionsParam, contentQuery) - then: 'response have expected json' + given: 'sdnc operation returns OK response' + mockSdncOperations.getResouceDataForOperationalAndRunning( + 'someCmHandle', + 'someResourceId', + '(fields=x/y/z,depth=10,test=abc)', + 'content=all') >> new ResponseEntity<>('response json', HttpStatus.OK) + when: 'resource data is requested' + def response = objectUnderTest.getResourceData( + 'someCmHandle', + 'someResourceId', + '(fields=x/y/z,depth=10,test=abc)', + 'content=all') + then: 'response matches the response returned from the SDNC service' response == 'response json' } def 'Get resource data with not found exception.'() { - given: 'cm-handle, passthrough parameter, resourceId, accept header, fields, depth, query param' - def cmHandle = 'testCmHandle' - def resourceId = 'testResourceId' - def optionsParam = '(fields=x/y/z,depth=10,test=abc)' - def restConfQueryParam = 'content=config' - and: 'sdnc operation returns "NOT_FOUND" response' - mockSdncOperations.getResouceDataForOperationalAndRunning(cmHandle, resourceId, optionsParam, _ as String) >> new ResponseEntity<>(HttpStatus.NOT_FOUND) - when: 'get resource data from cm handles service method invoked' - objectUnderTest.getResourceData(cmHandle, - resourceId, optionsParam, restConfQueryParam) - then: 'resource data not found' + given: 'sdnc operation returns "NOT_FOUND" response' + mockSdncOperations.getResouceDataForOperationalAndRunning(*_) >> new ResponseEntity<>(HttpStatus.NOT_FOUND) + when: 'resource data is requested' + objectUnderTest.getResourceData('someCmHandle', 'someResourceId', + '(fields=x/y/z,depth=10,test=abc)', 'content=config') + then: 'http client request exception' thrown(HttpClientRequestException.class) } def 'Get resource data for passthrough running.'() { - given: 'cm-handle, passthrough parameter, resourceId, accept header, fields, depth' - def cmHandle = 'testCmHandle' - def resourceId = 'testResourceId' - def optionsParam = '(fields=x/y/z,depth=10,test=abc)' - def contentQuery = 'content=config' - and: 'sdnc operation returns OK response' - mockSdncOperations.getResouceDataForOperationalAndRunning(cmHandle, resourceId, optionsParam, - contentQuery) >> new ResponseEntity<>('response json', HttpStatus.OK) - when: 'get resource data from cm handles service method invoked' - def response = objectUnderTest.getResourceData(cmHandle, - resourceId, optionsParam, contentQuery) + given: 'sdnc operation returns OK response' + mockSdncOperations.getResouceDataForOperationalAndRunning(*_) >> new ResponseEntity<>('response json', HttpStatus.OK) + when: 'resource data is requested' + def response = objectUnderTest.getResourceData( + 'someCmHandle', + 'someResourceId', + '(fields=x/y/z,depth=10,test=abc)', + 'content=config') then: 'response have expected json' response == 'response json' } @@ -243,10 +239,10 @@ class DmiServiceImplSpec extends Specification { def 'Write resource data for passthrough running with a #scenario from sdnc.'() { given: 'sdnc returns a response with #scenario' mockSdncOperations.writeData(operationEnum, _, _, _, _) >> new ResponseEntity('response json', httpResponse) - when: 'write resource data for cm handle method invoked' - def response = objectUnderTest.writeData(operationEnum,'some-cmHandle', - 'some-resourceIdentifier', 'some-dataType', '{some-data}') - then: 'the response contains the expected json data from sdnc' + when: 'resource data is written to sdnc' + def response = objectUnderTest.writeData(operationEnum, 'some-cmHandle', + 'some-resourceIdentifier', 'some-dataType', '{some-data}') + then: 'the response matches the expected data' response == 'response json' where: 'the following values are used' scenario | httpResponse | operationEnum @@ -257,21 +253,21 @@ class DmiServiceImplSpec extends Specification { def 'Write resource data with special characters.'() { given: 'sdnc returns a created response' mockSdncOperations.writeData(CREATE, 'some-cmHandle', - 'some-resourceIdentifier', 'some-dataType', 'data with quote " and \n new line') >> new ResponseEntity('response json', HttpStatus.CREATED) - when: 'write resource data from cm handles service method invoked' + 'some-resourceIdentifier', 'some-dataType', 'data with quote " and \n new line') >> new ResponseEntity('response json', HttpStatus.CREATED) + when: 'resource data is written to sdnc' def response = objectUnderTest.writeData(CREATE, 'some-cmHandle', - 'some-resourceIdentifier', 'some-dataType', 'data with quote " and \n new line') - then: 'response have expected json' + 'some-resourceIdentifier', 'some-dataType', 'data with quote " and \n new line') + then: 'the response matches the expected data' response == 'response json' } def 'Write resource data for passthrough running with a 500 response from sdnc.'() { - given: 'sdnc returns a 500 response for the write operation' + given: 'sdnc returns internal server error response' mockSdncOperations.writeData(CREATE, _, _, _, _) >> new ResponseEntity('response json', HttpStatus.INTERNAL_SERVER_ERROR) - when: 'write resource data for passthrough method is invoked' + when: 'resource data is written to sdnc' objectUnderTest.writeData(CREATE, 'some-cmHandle', - 'some-resourceIdentifier', 'some-dataType', _ as String) + 'some-resourceIdentifier', 'some-dataType', _ as String) then: 'a dmi exception is thrown' thrown(DmiException.class) } -} +} \ No newline at end of file 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 32df97b6..4d7e27e2 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 @@ -1,6 +1,6 @@ /* * ============LICENSE_START======================================================= - * Copyright (C) 2021 Nordix Foundation + * Copyright (C) 2021-2022 Nordix Foundation * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -37,8 +37,8 @@ class NcmpRestClientSpec extends Specification { } def 'Register a cm handle.'() { - given: 'json data' - def jsonData = 'some json' + given: 'some request data' + def someRequestData = 'some request data' and: 'configuration data' mockCpsProperties.baseUrl >> 'http://some-uri' mockCpsProperties.dmiRegistrationUrl >> 'some-url' @@ -46,12 +46,12 @@ class NcmpRestClientSpec extends Specification { mockCpsProperties.authPassword >> 'some-password' and: 'the rest template returns a valid response entity' def mockResponseEntity = Mock(ResponseEntity) - 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' + when: 'registering a cm handle' + def result = objectUnderTest.registerCmHandlesWithNcmp(someRequestData) + then: 'the rest template is called with the correct uri and original request data in the body' 1 * mockRestTemplate.exchange({ it.toString() == 'http://some-uri/some-url' }, - HttpMethod.POST, { it.body.contains(jsonData) }, String.class) >> mockResponseEntity - and: 'the output of the method is equal to the output from the test template' + HttpMethod.POST, { it.body.contains(someRequestData) }, String.class) >> mockResponseEntity + and: 'the output of the method is equal to the output from the rest template service' 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 index 8a3170b4..f334f780 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 @@ -1,6 +1,6 @@ /* * ============LICENSE_START======================================================= - * Copyright (C) 2021 Nordix Foundation + * Copyright (C) 2021-2022 Nordix Foundation * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -40,35 +40,35 @@ class SdncRestconfClientSpec extends Specification { def objectUnderTest = new SdncRestconfClient(mockSdncProperties, mockRestTemplate) def 'SDNC GET operation.'() { - given: 'a get url' + given: 'a get resource url' def getResourceUrl = '/getResourceUrl' - and: 'sdnc properties' + and: 'test configuration data' setupTestConfigurationData() - and: 'the rest template returns a valid response entity' + and: 'the process returns a valid response entity' def mockResponseEntity = Mock(ResponseEntity) mockRestTemplate.exchange({ it.toString() == 'http://some-uri/getResourceUrl' }, HttpMethod.GET, _ as HttpEntity, String.class) >> mockResponseEntity - when: 'GET operation is invoked' + when: 'the resource is fetched' def result = objectUnderTest.getOperation(getResourceUrl) - then: 'the output of the method is equal to the output from the test template' + then: 'the output of the method is equal to the output from the rest template service' result == mockResponseEntity } def 'SDNC #scenario operation called.'() { - given: 'json data' - def jsonData = 'some-json' + given: 'some request data' + def someRequestData = 'some request data' and: 'a url for get module resources' def getModuleResourceUrl = '/getModuleResourceUrl' - and: 'configuration data' + and: 'test configuration data' setupTestConfigurationData() - and: 'the rest template returns a valid response entity' + and: 'the process returns a valid response entity' def mockResponseEntity = Mock(ResponseEntity) - when: 'get module resources is invoked' - def result = objectUnderTest.httpOperationWithJsonData(expectedHttpMethod, getModuleResourceUrl, jsonData, new HttpHeaders()) + when: 'the resource is fetched' + def result = objectUnderTest.httpOperationWithJsonData(expectedHttpMethod, getModuleResourceUrl, someRequestData, new HttpHeaders()) then: 'the rest template is called with the correct uri and json in the body' 1 * mockRestTemplate.exchange({ it.toString() == 'http://some-uri/getModuleResourceUrl' }, - expectedHttpMethod, { it.body.contains(jsonData) }, String.class) >> mockResponseEntity - and: 'the output of the method is the same as the output from the test template' + expectedHttpMethod, { it.body.contains(someRequestData) }, String.class) >> mockResponseEntity + and: 'the output of the method is the same as the output from the rest template service' result == mockResponseEntity where: 'the following values are used' scenario || expectedHttpMethod @@ -78,18 +78,18 @@ class SdncRestconfClientSpec extends Specification { 'DELETE' || DELETE } - def 'SDNC GET operation with header.'() { + def 'SDNC GET operation with headers.'() { given: 'a get url' def getResourceUrl = '/getResourceUrl' - and: 'sdnc properties' + and: 'test configuration data' setupTestConfigurationData() - and: 'the rest template returns a valid response entity' + and: 'the process returns a valid response entity' def mockResponseEntity = Mock(ResponseEntity) mockRestTemplate.exchange({ it.toString() == 'http://some-uri/getResourceUrl' }, HttpMethod.GET, _ as HttpEntity, String.class) >> mockResponseEntity - when: 'GET operation is invoked' + when: 'the resource is fetched with headers' def result = objectUnderTest.getOperation(getResourceUrl, new HttpHeaders()) - then: 'the output of the method is equal to the output from the test template' + then: 'the output of the method is equal to the output from the rest template service' result == mockResponseEntity } 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 724d2d4e..9dcb72e6 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 @@ -52,13 +52,13 @@ class SdncOperationsSpec extends Specification { SdncOperations objectUnderTest def 'get modules from node.'() { - given: 'node id and url' + given: 'a 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' - and: 'sdnc returns one module in response' + and: 'sdnc returns one module during process' mockSdncRestClient.getOperation(expectedUrl) >> ResponseEntity.ok(TestUtils.getResourceFileContent('ModuleSchema.json')) - when: 'get modules from node is called' + when: 'module schemas from node are fetched' def moduleSchemas = objectUnderTest.getModuleSchemasFromNode(nodeId) then: 'one module is found' moduleSchemas.size() == 1 @@ -78,9 +78,9 @@ class SdncOperationsSpec extends Specification { def expectedUrl = '/rests/data/network-topology:network-topology/topology=test-topology/node=node1/yang-ext:mount/ietf-netconf-monitoring:netconf-state/schemas' and: 'sdnc operation returns #scenario' mockSdncRestClient.getOperation(expectedUrl) >> ResponseEntity.ok(responseBody) - when: 'modules from node is called' + when: 'the module schemas are requested' def moduleSchemas = objectUnderTest.getModuleSchemasFromNode(nodeId) - then: 'no modules are returned' + then: 'no module schemas are returned' moduleSchemas.size() == 0 where: scenario | responseBody @@ -93,9 +93,9 @@ class SdncOperationsSpec extends Specification { 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' - and: 'sdnc operation returns configured response' + and: '#scenario is returned during process' mockSdncRestClient.getOperation(expectedUrl) >> new ResponseEntity<>(sdncResponseBody, sdncHttpStatus) - when: 'modules for node are fetched' + when: 'module schemas from node are fetched' objectUnderTest.getModuleSchemasFromNode(nodeId) then: 'SDNCException is thrown' def thrownException = thrown(SdncException) @@ -111,28 +111,28 @@ class SdncOperationsSpec extends Specification { 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' + when: 'module resource is fetched with the expected parameters' objectUnderTest.getModuleResource(nodeId, 'some-json-data') - then: 'the SDNC Rest client is invoked with the correct URL and json data' + then: 'the SDNC Rest client is invoked with the correct parameters' 1 * mockSdncRestClient.httpOperationWithJsonData(HttpMethod.POST, expectedUrl, 'some-json-data', _ as HttpHeaders) } def 'Get resource data from node to SDNC.'() { - given: 'expected url, topology-id, sdncOperation object' + given: 'expected url' def expectedUrl = '/rests/data/network-topology:network-topology/topology=test-topology/node=node1/yang-ext:mount/testResourceId?a=1&b=2&content=testContent' - when: 'called get modules from node' + when: 'resource data is fetched for given node ID' objectUnderTest.getResouceDataForOperationalAndRunning('node1', 'testResourceId', '(a=1,b=2)', 'content=testContent') - then: 'the get operation is executed with the correct URL' + then: 'the SDNC get operation is executed with the correct URL' 1 * mockSdncRestClient.getOperation(expectedUrl) } def 'Write resource data with #scenario operation to SDNC.'() { - given: 'expected url, topology-id, sdncOperation object' + given: 'expected url' def expectedUrl = '/rests/data/network-topology:network-topology/topology=test-topology/node=node1/yang-ext:mount/testResourceId' when: 'write resource data for passthrough running is called' objectUnderTest.writeData(operationEnum, 'node1', 'testResourceId', 'application/json', 'requestData') - then: 'the #expectedHttpMethod operation is executed with the correct URL and data' + then: 'the #expectedHttpMethod operation is executed with the correct parameters' 1 * mockSdncRestClient.httpOperationWithJsonData(expectedHttpMethod, expectedUrl, 'requestData', _ as HttpHeaders) where: 'the following values are used' scenario | operationEnum || expectedHttpMethod @@ -144,10 +144,10 @@ class SdncOperationsSpec extends Specification { } def 'build query param list for SDNC where options #scenario'() { - when: 'build query param list is called with #scenario' + when: 'query param list is built' def result = objectUnderTest.buildQueryParamMap(optionsParamInQuery, 'd=4') .toSingleValueMap().toString() - then: 'result equals to expected result' + then: 'result matches the expected result' result == expectedResult where: 'following parameters are used' scenario | optionsParamInQuery || expectedResult @@ -162,10 +162,9 @@ class SdncOperationsSpec extends Specification { } def 'options parameters contains a comma #scenario'() { - // https://jira.onap.org/browse/CPS-719 - when: 'build query param list is called with #scenario' + when: 'query param list is built with #scenario' def result = objectUnderTest.buildQueryParamMap(optionsParamInQuery, 'd=4').toSingleValueMap() - then: 'expect 2 elements from options where we are ignoring empty query param value +1 from content query param (2+1) = 3 elements' + then: 'expect 2 elements from options where we are ignoring empty query param value, +1 from content query param (2+1) = 3 elements' def expectedNoOfElements = 3 and: 'results contains equal elements as expected' result.size() == expectedNoOfElements diff --git a/src/test/resources/GetModules.json b/src/test/resources/GetModules.json deleted file mode 100644 index 57f5aefd..00000000 --- a/src/test/resources/GetModules.json +++ /dev/null @@ -1,17 +0,0 @@ -{ - "data": { - "modules": [ - { - "name": "ietf-yang-library", - "revision": "2016-06-21" - }, - { - "name": "nc-notifications", - "revision": "2008-07-14" - } - ] - }, - "cmHandleProperties": { - "subsystemId": "system-001" - } -} diff --git a/src/test/resources/moduleResources.json b/src/test/resources/moduleResources.json new file mode 100644 index 00000000..57f5aefd --- /dev/null +++ b/src/test/resources/moduleResources.json @@ -0,0 +1,17 @@ +{ + "data": { + "modules": [ + { + "name": "ietf-yang-library", + "revision": "2016-06-21" + }, + { + "name": "nc-notifications", + "revision": "2008-07-14" + } + ] + }, + "cmHandleProperties": { + "subsystemId": "system-001" + } +} -- cgit 1.2.3-korg