+
+ def 'Get resource data for pass-through operational from dmi.'() {
+ given: 'data node representing cmHandle and its properties'
+ def cmHandleDataNode = getCmHandleDataNodeForTest(true)
+ and: 'data node is got from data service'
+ mockCpsDataService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry',
+ cmHandleXPath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> cmHandleDataNode
+ and: 'resource data is got from DMI'
+ mockDmiOperations.getResourceDataOperationalFromDmi('testDmiService',
+ 'testCmHandle',
+ 'testResourceId',
+ '(a=1,b=2)',
+ 'testAcceptParam',
+ '{"operation":"read","cmHandleProperties":{"testName":"testValue"}}') >> new ResponseEntity<>('result-json', HttpStatus.OK)
+ when: 'get resource data is called'
+ def response = objectUnderTest.getResourceDataOperationalForCmHandle('testCmHandle',
+ 'testResourceId',
+ 'testAcceptParam',
+ '(a=1,b=2)')
+ then: 'dmi returns ok response'
+ response == 'result-json'
+ }
+
+ def 'Get resource data for pass-through operational from dmi threw parsing exception.'() {
+ given: 'data node representing cmHandle and its properties'
+ def cmHandleDataNode = getCmHandleDataNodeForTest(true)
+ and: 'cps data service returns valid cmHandle data node'
+ mockCpsDataService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry',
+ cmHandleXPath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> cmHandleDataNode
+ and: 'objectMapper not able to parse object'
+ def mockObjectMapper = Mock(ObjectMapper)
+ objectUnderTest.objectMapper = mockObjectMapper
+ mockObjectMapper.writeValueAsString(_) >> { throw new JsonProcessingException('testException') }
+ when: 'get resource data is called'
+ def response = objectUnderTest.getResourceDataOperationalForCmHandle('testCmHandle',
+ 'testResourceId',
+ 'testAcceptParam',
+ '(a=1,b=2)')
+ then: 'exception is thrown with the expected details'
+ def exceptionThrown = thrown(NcmpException.class)
+ exceptionThrown.details == 'testException'
+ }
+
+ def 'Get resource data for pass-through operational from dmi return NOK response.'() {
+ given: 'data node representing cmHandle and its properties'
+ def cmHandleDataNode = getCmHandleDataNodeForTest(true)
+ and: 'cps data service returns valid cmHandle data node'
+ mockCpsDataService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry',
+ cmHandleXPath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> cmHandleDataNode
+ and: 'dmi returns NOK response'
+ mockDmiOperations.getResourceDataOperationalFromDmi('testDmiService',
+ 'testCmHandle',
+ 'testResourceId',
+ '(a=1,b=2)',
+ 'testAcceptParam',
+ '{"operation":"read","cmHandleProperties":{"testName":"testValue"}}')
+ >> new ResponseEntity<>('NOK-json', HttpStatus.NOT_FOUND)
+ when: 'get resource data is called'
+ def response = objectUnderTest.getResourceDataOperationalForCmHandle('testCmHandle',
+ 'testResourceId',
+ 'testAcceptParam',
+ '(a=1,b=2)')
+ then: 'exception is thrown'
+ def exceptionThrown = thrown(NcmpException.class)
+ and: 'details contains the original response'
+ exceptionThrown.details.contains('NOK-json')
+ }
+
+ def 'Get resource data for pass-through running from dmi.'() {
+ given: 'data node representing cmHandle and its properties'
+ def cmHandleDataNode = getCmHandleDataNodeForTest(true)
+ and: 'cpsDataService returns valid dataNode'
+ mockCpsDataService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry',
+ cmHandleXPath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> cmHandleDataNode
+ and: 'dmi returns valid response and data'
+ mockDmiOperations.getResourceDataPassThroughRunningFromDmi('testDmiService',
+ 'testCmHandle',
+ 'testResourceId',
+ '(a=1,b=2)',
+ 'testAcceptParam',
+ '{"operation":"read","cmHandleProperties":{"testName":"testValue"}}') >> new ResponseEntity<>('{result-json}', HttpStatus.OK)
+ when: 'get resource data is called'
+ def response = objectUnderTest.getResourceDataPassThroughRunningForCmHandle('testCmHandle',
+ 'testResourceId',
+ 'testAcceptParam',
+ '(a=1,b=2)')
+ then: 'get resource data returns expected response'
+ response == '{result-json}'
+ }
+
+ def 'Get resource data for pass-through running from dmi threw parsing exception.'() {
+ given: 'data node representing cmHandle and its properties'
+ def cmHandleDataNode = getCmHandleDataNodeForTest(true)
+ and: 'cpsDataService returns valid dataNode'
+ mockCpsDataService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry',
+ cmHandleXPath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> cmHandleDataNode
+ and: 'objectMapper not able to parse object'
+ def mockObjectMapper = Mock(ObjectMapper)
+ objectUnderTest.objectMapper = mockObjectMapper
+ mockObjectMapper.writeValueAsString(_) >> { throw new JsonProcessingException('testException') }
+ when: 'get resource data is called'
+ def response = objectUnderTest.getResourceDataPassThroughRunningForCmHandle('testCmHandle',
+ 'testResourceId',
+ 'testAcceptParam',
+ '(a=1,b=2)')
+ then: 'exception is thrown with the expected details'
+ def exceptionThrown = thrown(NcmpException.class)
+ exceptionThrown.details == 'testException'
+ }
+
+ def 'Get resource data for pass-through running from dmi return NOK response.'() {
+ given: 'data node representing cmHandle and its properties'
+ def cmHandleDataNode = getCmHandleDataNodeForTest(true)
+ and: 'cpsDataService returns valid dataNode'
+ mockCpsDataService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry',
+ cmHandleXPath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> cmHandleDataNode
+ and: 'dmi returns NOK response'
+ mockDmiOperations.getResourceDataPassThroughRunningFromDmi('testDmiService',
+ 'testCmHandle',
+ 'testResourceId',
+ '(a=1,b=2)',
+ 'testAcceptParam',
+ '{"operation":"read","cmHandleProperties":{"testName":"testValue"}}')
+ >> new ResponseEntity<>('NOK-json', HttpStatus.NOT_FOUND)
+ when: 'get resource data is called'
+ def response = objectUnderTest.getResourceDataPassThroughRunningForCmHandle('testCmHandle',
+ 'testResourceId',
+ 'testAcceptParam',
+ '(a=1,b=2)')
+ then: 'exception is thrown'
+ def exceptionThrown = thrown(NcmpException.class)
+ and: 'details contains the original response'
+ exceptionThrown.details.contains('NOK-json')
+ }
+
+ def 'Write resource data for pass-through running from dmi using POST #scenario cm handle properties.'() {
+ given: 'data node representing cmHandle #scenario cm handle properties'
+ def cmHandleDataNode = getCmHandleDataNodeForTest(includeCmHandleProperties)
+ and: 'cpsDataService returns valid cm-handle datanode'
+ mockCpsDataService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry',
+ cmHandleXPath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> cmHandleDataNode
+ when: 'get resource data is called'
+ objectUnderTest.createResourceDataPassThroughRunningForCmHandle('testCmHandle',
+ 'testResourceId',
+ '{some-json}', 'application/json')
+ then: 'dmi called with correct data'
+ 1 * mockDmiOperations.createResourceDataPassThroughRunningFromDmi('testDmiService',
+ 'testCmHandle',
+ 'testResourceId',
+ '{"operation":"create","dataType":"application/json","data":"{some-json}","cmHandleProperties":'
+ + expectedJsonForCmhandleProperties+ '}')
+ >> { new ResponseEntity<>(HttpStatus.CREATED) }
+ where:
+ scenario | includeCmHandleProperties || expectedJsonForCmhandleProperties
+ 'with' | true || '{"testName":"testValue"}'
+ 'without' | false || '{}'
+ }
+
+ def 'Write resource data for pass-through running from dmi using POST "not found" response (from DMI).'() {
+ given: 'data node representing cmHandle and its properties'
+ def cmHandleDataNode = getCmHandleDataNodeForTest(true)
+ and: 'cpsDataService returns valid dataNode'
+ mockCpsDataService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry',
+ cmHandleXPath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> cmHandleDataNode
+ and: 'dmi throws exception'
+ mockDmiOperations.createResourceDataPassThroughRunningFromDmi(_ as String, _ as String, _ as String, _ as String)
+ >> { new ResponseEntity<>(HttpStatus.NOT_FOUND) }
+ when: 'get resource data is called'
+ objectUnderTest.createResourceDataPassThroughRunningForCmHandle('testCmHandle',
+ 'testResourceId',
+ '{some-json}', 'application/json')
+ then: 'exception is thrown'
+ def exceptionThrown = thrown(NcmpException.class)
+ and: 'details contains (not found) error code: 404'
+ exceptionThrown.details.contains('404')
+ }
+
+ def 'Sync model for a (new) cm handle with #scenario'() {
+ given: 'persistence cm handle is given'
+ def cmHandleForModelSync = new PersistenceCmHandle(id:'some cm handle', dmiServiceName: 'some service name')
+ and: 'additional properties are set as required'
+ if (additionalProperties!=null) {
+ cmHandleForModelSync.setAdditionalProperties(additionalProperties)
+ }
+ and: 'dmi operations returns some module references'
+ def jsonData = TestUtils.getResourceFileContent('cmHandleModules.json')
+ def expectedJsonBody = '{"cmHandleProperties":' + expectedJsonForAdditionalProperties + '}'
+ mockDmiProperties.getAuthUsername() >> 'someUser'
+ mockDmiProperties.getAuthPassword() >> 'somePassword'
+ def moduleReferencesFromCmHandleAsJson = new ResponseEntity<String>(jsonData, HttpStatus.OK)
+ mockDmiOperations.getResourceFromDmiWithJsonData('some service name', expectedJsonBody, 'some cm handle', 'modules') >> moduleReferencesFromCmHandleAsJson
+ and: 'CPS-Core returns list of known modules'
+ mockCpsModuleService.getYangResourceModuleReferences(_) >> existingModuleResourcesInCps
+ and: 'DMI-Plugin returns resource(s) for "new" module(s)'
+ def moduleResources = new ResponseEntity<String>(sdncReponseBody, HttpStatus.OK)
+ def jsonDataToFetchYangResource = '{"data":{"modules":[{"name":"module1","revision":"1"}]},"cmHandleProperties":' + expectedJsonForAdditionalProperties + '}'
+ mockDmiOperations.getResourceFromDmiWithJsonData('some service name', jsonDataToFetchYangResource, 'some cm handle', 'moduleResources') >> moduleResources
+ when: 'module Sync is triggered'
+ objectUnderTest.syncModulesAndCreateAnchor(cmHandleForModelSync)
+ then: 'the CPS module service is called once with the correct parameters'
+ 1 * mockCpsModuleService.createSchemaSetFromModules(expectedDataspaceName, cmHandleForModelSync.getId(), expectedYangResourceToContentMap, expectedKnownModules)
+ and: 'admin service create anchor method has been called with correct parameters'
+ 1 * mockCpsAdminService.createAnchor(expectedDataspaceName, cmHandleForModelSync.getId(), cmHandleForModelSync.getId())
+ where: 'the following responses are received from SDNC'
+ scenario | additionalProperties | existingModuleResourcesInCps | sdncReponseBody || expectedYangResourceToContentMap | expectedKnownModules | expectedJsonForAdditionalProperties
+ 'one unknown module' | ['name1':'value1'] | [new ModuleReference('module2', '2'), new ModuleReference('module3', '3')] | '[{"moduleName" : "module1", "revision" : "1","yangSource": "[some yang source]"}]' || [module1: 'some yang source'] | [new ModuleReference('module2', '2')] |'{"name1":"value1"}'
+ 'no add. properties' | [:] | [new ModuleReference('module2', '2'), new ModuleReference('module3', '3')] | '[{"moduleName" : "module1", "revision" : "1","yangSource": "[some yang source]"}]' || [module1: 'some yang source'] | [new ModuleReference('module2', '2')] |'{}'
+ 'additional properties is null' | null | [new ModuleReference('module2', '2'), new ModuleReference('module3', '3')] | '[{"moduleName" : "module1", "revision" : "1","yangSource": "[some yang source]"}]' || [module1: 'some yang source'] | [new ModuleReference('module2', '2')] |'{}'
+ 'no unknown module' | [:] | [new ModuleReference('module1', '1'), new ModuleReference('module2', '2')] | '[]' || [:] | [new ModuleReference('module1', '1'), new ModuleReference('module2', '2')] |'{}'
+ }
+
+ def 'Getting Yang Resources.'() {
+ when: 'yang resources is called'
+ objectUnderTest.getYangResourcesModuleReferences('some cm handle')
+ then: 'CPS module services is invoked for the correct dataspace and cm handle'
+ 1 * mockCpsModuleService.getYangResourcesModuleReferences('NFP-Operational','some cm handle')
+ }
+
+ def 'Create the request body to get yang resources from DMI.'() {
+ given: 'the expected json request'
+ def expectedRequestBody = '{"data":{"modules":[{"name":"module1","revision":"1"},{"name":"module2","revision":"2"}]},"cmHandleProperties":{"name1":"value1"}}'
+ and: 'module references and cm handle properties'
+ def moduleReferences = [new ModuleReference('module1', '1'),new ModuleReference('module2', '2')]
+ def cmHandleProperties = ['name1':'value1']
+ when: 'get request body to fetch yang resources from DMI is called'
+ def result = objectUnderTest.getRequestBodyToFetchYangResourceFromDmi(moduleReferences, cmHandleProperties)
+ then: 'the result is the same as the expected request body'
+ result == expectedRequestBody
+ }
+
+ def 'Get cm handle identifiers for the given module names.'() {
+ when: 'execute a cm handle search for the given module names'
+ objectUnderTest.executeCmHandleHasAllModulesSearch(['some-module-name'])
+ then: 'get anchor identifiers is invoked with the expected parameters'
+ 1 * mockCpsAdminService.queryAnchorNames('NFP-Operational', ['some-module-name'])
+ }
+
+ def 'Update resource data for pass-through running from dmi using POST #scenario cm handle properties.'() {
+ given: 'data node representing cmHandle #scenario cm handle properties'
+ def cmHandleDataNode = getCmHandleDataNodeForTest(includeCmHandleProperties)
+ and: 'cpsDataService returns valid cm-handle datanode'
+ mockCpsDataService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry',
+ cmHandleXPath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> cmHandleDataNode
+ when: 'update resource data is called'
+ objectUnderTest.updateResourceDataPassThroughRunningForCmHandle('testCmHandle',
+ 'testResourceId',
+ '{some-json}', 'application/json')
+ then: 'dmi called with correct data'
+ 1 * mockDmiOperations.updateResourceDataPassThroughRunningFromDmi('testDmiService',
+ 'testCmHandle',
+ 'testResourceId',
+ '{"operation":"update","dataType":"application/json","data":"{some-json}","cmHandleProperties":'
+ + expectedJsonForCmhandleProperties + '}')
+ >> new ResponseEntity<>(HttpStatus.OK)
+ where:
+ scenario | includeCmHandleProperties || expectedJsonForCmhandleProperties
+ 'with' | true || '{"testName":"testValue"}'
+ 'without' | false || '{}'
+ }
+
+ def getObjectUnderTestWithModelSyncDisabled() {
+ def objectUnderTest = Spy(new NetworkCmProxyDataServiceImpl(mockDmiOperations, mockCpsModuleService,
+ mockCpsDataService, mockCpsQueryService, mockCpsAdminService, spyObjectMapper))
+ objectUnderTest.syncModulesAndCreateAnchor(_) >> null
+ return objectUnderTest
+ }
+
+ def getCmHandleDataNodeForTest(boolean includeCmHandleProperties) {
+ def cmHandleDataNode = new DataNode()
+ cmHandleDataNode.leaves = ['dmi-service-name': 'testDmiService']
+ if (includeCmHandleProperties) {
+ def cmHandlePropertyDataNode = new DataNode()
+ cmHandlePropertyDataNode.leaves = ['name': 'testName', 'value': 'testValue']
+ cmHandleDataNode.childDataNodes = [cmHandlePropertyDataNode]
+ }
+ return cmHandleDataNode
+ }
+