diff options
author | emaclee <lee.anjella.macabuhay@est.tech> | 2022-07-15 12:31:46 +0100 |
---|---|---|
committer | emaclee <lee.anjella.macabuhay@est.tech> | 2022-07-26 10:18:46 +0100 |
commit | f5f5e6be4e7d222a7530b96d2d77ed611934822f (patch) | |
tree | e329f206b51163aa70d853bed82010bc820f77bf /src/test/groovy/org/onap/cps/ncmp/dmi/service/DmiServiceImplSpec.groovy | |
parent | af8d434068e6f0d2b88c152b069afdcaf162f6c2 (diff) |
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 <lee.anjella.macabuhay@est.tech>
Change-Id: I3d42471e7aa4bc61f962ad19cc1f237f6985a9a2
Diffstat (limited to 'src/test/groovy/org/onap/cps/ncmp/dmi/service/DmiServiceImplSpec.groovy')
-rw-r--r-- | src/test/groovy/org/onap/cps/ncmp/dmi/service/DmiServiceImplSpec.groovy | 204 |
1 files changed, 100 insertions, 104 deletions
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<String>('{"ietf-netconf-monitoring:output": {"data": "some-data1"}}', HttpStatus.OK), new ResponseEntity<String>('{"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<String>(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<String>('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<ModuleReference>) 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<ModuleReference>) - 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<String>('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<String>('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<String>('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<String>('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 |