X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=cps-ncmp-service%2Fsrc%2Ftest%2Fgroovy%2Forg%2Fonap%2Fcps%2Fncmp%2Fapi%2Fimpl%2Foperations%2FDmiModelOperationsSpec.groovy;h=574f609e9e48ef48b6052a680952c7fc639c6c62;hb=d69742c1f02585ae5d82f49542581698367e9cde;hp=335bc062ccc9e118eedd78ddf8b2f70960708c30;hpb=b7485757b6fd56c2b32cd90c5ad1d0b5bd7b3a6a;p=cps.git diff --git a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/operations/DmiModelOperationsSpec.groovy b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/operations/DmiModelOperationsSpec.groovy index 335bc062c..574f609e9 100644 --- a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/operations/DmiModelOperationsSpec.groovy +++ b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/operations/DmiModelOperationsSpec.groovy @@ -1,6 +1,7 @@ /* * ============LICENSE_START======================================================= - * Copyright (C) 2021 Nordix Foundation + * Copyright (C) 2021-2022 Nordix Foundation + * Modifications Copyright (C) 2022 Bell Canada * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,14 +22,18 @@ package org.onap.cps.ncmp.api.impl.operations import com.fasterxml.jackson.core.JsonProcessingException +import com.fasterxml.jackson.databind.ObjectMapper import org.onap.cps.ncmp.api.impl.config.NcmpConfiguration -import org.onap.cps.ncmp.api.impl.exception.NcmpException +import org.onap.cps.ncmp.api.impl.utils.DmiServiceUrlBuilder import org.onap.cps.spi.model.ModuleReference +import org.onap.cps.utils.JsonObjectMapper +import org.spockframework.spring.SpringBean import org.springframework.beans.factory.annotation.Autowired import org.springframework.boot.test.context.SpringBootTest import org.springframework.http.HttpStatus import org.springframework.http.ResponseEntity import org.springframework.test.context.ContextConfiguration +import org.springframework.web.util.UriComponentsBuilder import spock.lang.Shared @SpringBootTest @@ -41,32 +46,36 @@ class DmiModelOperationsSpec extends DmiOperationsBaseSpec { @Autowired DmiModelOperations objectUnderTest + @SpringBean + JsonObjectMapper spiedJsonObjectMapper = Spy(new JsonObjectMapper(new ObjectMapper())) + def 'Retrieving module references.'() { - given: 'a persistence cm handle' - mockPersistenceCmHandleRetrieval([]) - and: 'a positive response from dmi service when it is called with the expected parameters' - def moduleReferencesAsLisOfMaps = [[moduleName:'mod1',revision:'A'],[moduleName:'mod2',revision:'X']] - def responseFromDmi = new ResponseEntity([schemas:moduleReferencesAsLisOfMaps], HttpStatus.OK) - mockDmiRestClient.postOperationWithJsonData("${dmiServiceName}/dmi/v1/ch/${cmHandleId}/modules", - '{"cmHandleProperties":{}}', [:]) >> responseFromDmi + given: 'a cm handle' + mockYangModelCmHandleRetrieval([]) + and: 'a positive response from DMI service when it is called with the expected parameters' + def moduleReferencesAsLisOfMaps = [[moduleName: 'mod1', revision: 'A'], [moduleName: 'mod2', revision: 'X']] + def expectedUrl = "${dmiServiceName}/dmi/v1/ch/${cmHandleId}/modules" + def responseFromDmi = new ResponseEntity([schemas: moduleReferencesAsLisOfMaps], HttpStatus.OK) + mockDmiRestClient.postOperationWithJsonData(expectedUrl, '{"cmHandleProperties":{}}') + >> responseFromDmi when: 'get module references is called' - def result = objectUnderTest.getModuleReferences(persistenceCmHandle) + def result = objectUnderTest.getModuleReferences(yangModelCmHandle) then: 'the result consists of expected module references' - assert result == [new ModuleReference(moduleName:'mod1',revision:'A'), new ModuleReference(moduleName:'mod2',revision:'X')] + assert result == [new ModuleReference(moduleName: 'mod1', revision: 'A'), new ModuleReference(moduleName: 'mod2', revision: 'X')] } def 'Retrieving module references edge case: #scenario.'() { - given: 'a persistence cm handle' - mockPersistenceCmHandleRetrieval([]) - and: 'any response from dmi service when it is called with the expected parameters' + given: 'a cm handle' + mockYangModelCmHandleRetrieval([]) + and: 'any response from DMI service when it is called with the expected parameters' // TODO (toine): production code ignores any error code from DMI, this should be improved in future def responseFromDmi = new ResponseEntity(bodyAsMap, HttpStatus.NO_CONTENT) mockDmiRestClient.postOperationWithJsonData(*_) >> responseFromDmi when: 'get module references is called' - def result = objectUnderTest.getModuleReferences(persistenceCmHandle) + def result = objectUnderTest.getModuleReferences(yangModelCmHandle) then: 'the result is empty' assert result == [] - where: 'the dmi response body has the following content' + where: 'the DMI response body has the following content' scenario | bodyAsMap 'no modules' | [schemas:[]] 'modules null' | [schemas:null] @@ -74,35 +83,34 @@ class DmiModelOperationsSpec extends DmiOperationsBaseSpec { 'no body' | null } - def 'Retrieving module references, additional property handling: #scenario.'() { - given: 'a persistence cm handle' - mockPersistenceCmHandleRetrieval(additionalPropertiesObject) - and: 'a positive response from dmi service when it is called with tha expected parameters' + def 'Retrieving module references, DMI property handling: #scenario.'() { + given: 'a cm handle' + mockYangModelCmHandleRetrieval(dmiProperties) + and: 'a positive response from DMI service when it is called with tha expected parameters' def responseFromDmi = new ResponseEntity(HttpStatus.OK) mockDmiRestClient.postOperationWithJsonData("${dmiServiceName}/dmi/v1/ch/${cmHandleId}/modules", - '{"cmHandleProperties":' + expectedAdditionalPropertiesInRequest + '}', [:]) >> responseFromDmi + '{"cmHandleProperties":' + expectedAdditionalPropertiesInRequest + '}') >> responseFromDmi when: 'a get module references is called' - def result = objectUnderTest.getModuleReferences(persistenceCmHandle) - then: 'the result is the response from dmi service' + def result = objectUnderTest.getModuleReferences(yangModelCmHandle) + then: 'the result is the response from DMI service' assert result == [] - where: 'the following additional properties are used' - scenario | additionalPropertiesObject || expectedAdditionalPropertiesInRequest - 'with properties' | [sampleAdditionalProperty] || '{"prop1":"val1"}' - 'with null properties' | null || '{}' - 'without properties' | [] || '{}' + where: 'the following DMI properties are used' + scenario | dmiProperties || expectedAdditionalPropertiesInRequest + 'with properties' | [yangModelCmHandleProperty] || '{"prop1":"val1"}' + 'without properties' | [] || '{}' } def 'Retrieving yang resources.'() { - given: 'a persistence cm handle' - mockPersistenceCmHandleRetrieval([]) - and: 'a positive response from dmi service when it is called with tha expected parameters' + given: 'a cm handle' + mockYangModelCmHandleRetrieval([]) + and: 'a positive response from DMI service when it is called with the expected parameters' def responseFromDmi = new ResponseEntity([[moduleName: 'mod1', revision: 'A', yangSource: 'some yang source'], [moduleName: 'mod2', revision: 'C', yangSource: 'other yang source']], HttpStatus.OK) def expectedModuleReferencesInRequest = '{"name":"mod1","revision":"A"},{"name":"mod2","revision":"X"}' mockDmiRestClient.postOperationWithJsonData("${dmiServiceName}/dmi/v1/ch/${cmHandleId}/moduleResources", - '{"data":{"modules":[' + expectedModuleReferencesInRequest + ']},"cmHandleProperties":{}}', [:]) >> responseFromDmi - when: 'get new yang resources from dmi service' - def result = objectUnderTest.getNewYangResourcesFromDmi(persistenceCmHandle, newModuleReferences) + '{"data":{"modules":[' + expectedModuleReferencesInRequest + ']},"cmHandleProperties":{}}') >> responseFromDmi + when: 'get new yang resources from DMI service' + def result = objectUnderTest.getNewYangResourcesFromDmi(yangModelCmHandle, newModuleReferences) then: 'the result has the 2 expected yang (re)sources (order is not guaranteed)' assert result.size() == 2 assert result.get('mod1') == 'some yang source' @@ -110,59 +118,58 @@ class DmiModelOperationsSpec extends DmiOperationsBaseSpec { } def 'Retrieving yang resources, edge case: scenario.'() { - given: 'a persistence cm handle' - mockPersistenceCmHandleRetrieval([]) - and: 'a positive response from dmi service when it is called with tha expected parameters' + given: 'a cm handle' + mockYangModelCmHandleRetrieval([]) + and: 'a positive response from DMI service when it is called with tha expected parameters' // TODO (toine): production code ignores any error code from DMI, this should be improved in future def responseFromDmi = new ResponseEntity(responseFromDmiBody, HttpStatus.NO_CONTENT) mockDmiRestClient.postOperationWithJsonData(*_) >> responseFromDmi - when: 'get new yang resources from dmi service' - def result = objectUnderTest.getNewYangResourcesFromDmi(persistenceCmHandle, newModuleReferences) + when: 'get new yang resources from DMI service' + def result = objectUnderTest.getNewYangResourcesFromDmi(yangModelCmHandle, newModuleReferences) then: 'the result is empty' assert result == [:] - where: 'the dmi response body has the following content' + where: 'the DMI response body has the following content' scenario | responseFromDmiBody 'empty array' | [] 'null array' | null } - def 'Retrieving yang resources, additional property handling #scenario.'() { - given: 'a persistence cm handle' - mockPersistenceCmHandleRetrieval(additionalPropertiesObject) - and: 'a positive response from dmi service when it is called with the expected parameters' + def 'Retrieving yang resources, DMI property handling #scenario.'() { + given: 'a cm handle' + mockYangModelCmHandleRetrieval(dmiProperties) + and: 'a positive response from DMI service when it is called with the expected parameters' def responseFromDmi = new ResponseEntity<>([[moduleName: 'mod1', revision: 'A', yangSource: 'some yang source']], HttpStatus.OK) mockDmiRestClient.postOperationWithJsonData("${dmiServiceName}/dmi/v1/ch/${cmHandleId}/moduleResources", - '{"data":{"modules":[' + expectedModuleReferencesInRequest + ']},"cmHandleProperties":'+expectedAdditionalPropertiesInRequest+'}', - [:]) >> responseFromDmi - when: 'get new yang resources from dmi service' - def result = objectUnderTest.getNewYangResourcesFromDmi(persistenceCmHandle, unknownModuleReferences) - then: 'the result is the response from dmi service' + '{"data":{"modules":[' + expectedModuleReferencesInRequest + ']},"cmHandleProperties":'+expectedAdditionalPropertiesInRequest+'}') >> responseFromDmi + when: 'get new yang resources from DMI service' + def result = objectUnderTest.getNewYangResourcesFromDmi(yangModelCmHandle, unknownModuleReferences) + then: 'the result is the response from DMI service' assert result == [mod1:'some yang source'] - where: 'the following additional properties are used' - scenario | additionalPropertiesObject | unknownModuleReferences || expectedAdditionalPropertiesInRequest | expectedModuleReferencesInRequest - 'with module references and properties' | [sampleAdditionalProperty] | newModuleReferences || '{"prop1":"val1"}' | '{"name":"mod1","revision":"A"},{"name":"mod2","revision":"X"}' - 'without module references' | [sampleAdditionalProperty] | [] || '{"prop1":"val1"}' | '' - 'without properties' | [] | newModuleReferences || '{}' | '{"name":"mod1","revision":"A"},{"name":"mod2","revision":"X"}' + where: 'the following DMI properties are used' + scenario | dmiProperties | unknownModuleReferences || expectedAdditionalPropertiesInRequest | expectedModuleReferencesInRequest + 'with module references and properties' | [yangModelCmHandleProperty] | newModuleReferences || '{"prop1":"val1"}' | '{"name":"mod1","revision":"A"},{"name":"mod2","revision":"X"}' + 'without module references' | [yangModelCmHandleProperty] | [] || '{"prop1":"val1"}' | '' + 'without properties' | [] | newModuleReferences || '{}' | '{"name":"mod1","revision":"A"},{"name":"mod2","revision":"X"}' } - def 'Retrieving yang resources from dmi with additional properties null.'() { - given: 'a persistence cm handle' - mockPersistenceCmHandleRetrieval(null) - when: 'a get new yang resources from dmi is called' - objectUnderTest.getNewYangResourcesFromDmi(persistenceCmHandle, []) + def 'Retrieving yang resources from DMI with null DMI properties.'() { + given: 'a cm handle' + mockYangModelCmHandleRetrieval(null) + when: 'a get new yang resources from DMI is called' + objectUnderTest.getNewYangResourcesFromDmi(yangModelCmHandle, []) then: 'a null pointer is thrown (we might need to address this later)' thrown(NullPointerException) } def 'Retrieving module references with Json processing exception.'() { - given: 'a persistence cm handle' - mockPersistenceCmHandleRetrieval([]) + given: 'a cm handle' + mockYangModelCmHandleRetrieval([]) and: 'a Json processing exception occurs' - spyObjectMapper.writeValueAsString(_) >> {throw (new JsonProcessingException(''))} - when: 'a dmi operation is executed' - objectUnderTest.getModuleReferences(persistenceCmHandle) + spiedJsonObjectMapper.asJsonString(_) >> {throw (new JsonProcessingException('parsing error'))} + when: 'a DMI operation is executed' + objectUnderTest.getModuleReferences(yangModelCmHandle) then: 'an ncmp exception is thrown' - def exceptionThrown = thrown(NcmpException) + def exceptionThrown = thrown(JsonProcessingException) and: 'the message indicates a parsing error' exceptionThrown.message.toLowerCase().contains('parsing error') }