Add get cm handles by modules names - service layer
[cps.git] / cps-ncmp-service / src / test / groovy / org / onap / cps / ncmp / api / impl / NetworkCmProxyDataServiceImplSpec.groovy
index 2b376e9..88277d3 100644 (file)
@@ -119,7 +119,7 @@ class NetworkCmProxyDataServiceImplSpec extends Specification {
         when: 'addListNodeElements is invoked'
             objectUnderTest.addListNodeElements(cmHandle, xpath, jsonData)
         then: 'the CPS service method is invoked once with the expected parameters'
-            1 * mockCpsDataService.saveListNodeData(expectedDataspaceName, cmHandle, xpath, jsonData, noTimestamp)
+            1 * mockCpsDataService.saveListElements(expectedDataspaceName, cmHandle, xpath, jsonData, noTimestamp)
     }
 
     def 'Update data node leaves.'() {
@@ -155,18 +155,18 @@ class NetworkCmProxyDataServiceImplSpec extends Specification {
             def expectedJsonData = '{"cm-handles":[{"id":"123","dmi-service-name":"my-server","additional-properties":[{"name":"name1","value":"value1"},{"name":"name2","value":"value2"}]}]}'
         when: 'registration is updated'
             objectUnderTest.updateDmiRegistrationAndSyncModule(dmiPluginRegistration)
-        then: 'the CPS save list node data is invoked with the expected parameters'
-            expectedCallsToSaveNode * mockCpsDataService.saveListNodeData('NCMP-Admin', 'ncmp-dmi-registry',
+        then: 'cps save list elements is invoked with the expected parameters'
+            expectedCallsToSaveNode * mockCpsDataService.saveListElements('NCMP-Admin', 'ncmp-dmi-registry',
                 '/dmi-registry', expectedJsonData, noTimestamp)
-        and: 'update Node and Child Data Nodes is invoked with correct parameters'
+        and: 'update node and child data nodes is invoked with correct parameters'
             expectedCallsToUpdateNode * mockCpsDataService.updateNodeLeavesAndExistingDescendantLeaves('NCMP-Admin',
                 'ncmp-dmi-registry', '/dmi-registry', expectedJsonData, noTimestamp)
-        and : 'delete list data node is invoked with the correct parameters'
-            expectedCallsToDeleteListDataNode * mockCpsDataService.deleteListNodeData('NCMP-Admin',
+        and : 'delete list or list element is invoked with the correct parameters'
+            expectedCallsToDeleteListElement * mockCpsDataService.deleteListOrListElement('NCMP-Admin',
                 'ncmp-dmi-registry', "/dmi-registry/cm-handles[@id='cmHandle001']", noTimestamp)
 
         where:
-            scenario                        | createdCmHandles      | updatedCmHandles      | removedCmHandles || expectedCallsToSaveNode   | expectedCallsToUpdateNode | expectedCallsToDeleteListDataNode
+            scenario                        | createdCmHandles      | updatedCmHandles      | removedCmHandles || expectedCallsToSaveNode   | expectedCallsToUpdateNode | expectedCallsToDeleteListElement
             'create'                        | [persistenceCmHandle] | []                    | []               || 1                         | 0                         | 0
             'update'                        | []                    | [persistenceCmHandle] | []               || 0                         | 1                         | 0
             'delete'                        | []                    | []                    | cmHandlesArray   || 0                         | 0                         | 1
@@ -185,8 +185,8 @@ class NetworkCmProxyDataServiceImplSpec extends Specification {
             def expectedJsonData = '{"cm-handles":[{"id":"123","dmi-service-name":"my-server","additional-properties":[]}]}'
         when: 'registration is updated'
             objectUnderTest.updateDmiRegistrationAndSyncModule(dmiPluginRegistration)
-        then: 'the CPS save list node data is invoked with the expected parameters'
-            1 * mockCpsDataService.saveListNodeData('NCMP-Admin', 'ncmp-dmi-registry',
+        then: 'the cps save list element is invoked with the expected parameters'
+            1 * mockCpsDataService.saveListElements('NCMP-Admin', 'ncmp-dmi-registry',
                 '/dmi-registry', expectedJsonData, noTimestamp)
     }
 
@@ -214,7 +214,7 @@ class NetworkCmProxyDataServiceImplSpec extends Specification {
             def dmiPluginRegistration = new DmiPluginRegistration()
             dmiPluginRegistration.removedCmHandles = ['some cm handle']
         and: 'an JSON processing exception occurs'
-            mockCpsDataService.deleteListNodeData(*_) >>  { throw (new DataNodeNotFoundException('','')) }
+            mockCpsDataService.deleteListOrListElement(*_) >>  { throw (new DataNodeNotFoundException('','')) }
         when: 'registration is updated'
             objectUnderTest.updateDmiRegistrationAndSyncModule(dmiPluginRegistration)
         then: 'no exception is thrown'
@@ -231,16 +231,14 @@ class NetworkCmProxyDataServiceImplSpec extends Specification {
             mockDmiOperations.getResourceDataOperationalFromDmi('testDmiService',
                 'testCmHandle',
                 'testResourceId',
-                'testFieldQuery',
-                5,
+                '(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',
-            'testFieldQuery',
-            5)
+            '(a=1,b=2)')
         then: 'dmi returns ok response'
             response == 'result-json'
     }
@@ -259,8 +257,7 @@ class NetworkCmProxyDataServiceImplSpec extends Specification {
             def response = objectUnderTest.getResourceDataOperationalForCmHandle('testCmHandle',
                     'testResourceId',
                     'testAcceptParam',
-                    'testFieldQuery',
-                    5)
+                    '(a=1,b=2)')
         then: 'exception is thrown with the expected details'
             def exceptionThrown = thrown(NcmpException.class)
             exceptionThrown.details == 'testException'
@@ -276,8 +273,7 @@ class NetworkCmProxyDataServiceImplSpec extends Specification {
             mockDmiOperations.getResourceDataOperationalFromDmi('testDmiService',
                     'testCmHandle',
                     'testResourceId',
-                    'testFieldQuery',
-                    5,
+                    '(a=1,b=2)',
                     'testAcceptParam',
                     '{"operation":"read","cmHandleProperties":{"testName":"testValue"}}')
                     >> new ResponseEntity<>('NOK-json', HttpStatus.NOT_FOUND)
@@ -285,8 +281,7 @@ class NetworkCmProxyDataServiceImplSpec extends Specification {
             def response = objectUnderTest.getResourceDataOperationalForCmHandle('testCmHandle',
                     'testResourceId',
                     'testAcceptParam',
-                    'testFieldQuery',
-                    5)
+                    '(a=1,b=2)')
         then: 'exception is thrown'
             def exceptionThrown = thrown(NcmpException.class)
         and: 'details contains the original response'
@@ -303,16 +298,14 @@ class NetworkCmProxyDataServiceImplSpec extends Specification {
             mockDmiOperations.getResourceDataPassThroughRunningFromDmi('testDmiService',
                     'testCmHandle',
                     'testResourceId',
-                    'testFieldQuery',
-                    5,
+                    '(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',
-                    'testFieldQuery',
-                    5)
+                    '(a=1,b=2)')
         then: 'get resource data returns expected response'
             response == '{result-json}'
     }
@@ -331,8 +324,7 @@ class NetworkCmProxyDataServiceImplSpec extends Specification {
             def response = objectUnderTest.getResourceDataPassThroughRunningForCmHandle('testCmHandle',
                     'testResourceId',
                     'testAcceptParam',
-                    'testFieldQuery',
-                    5)
+                    '(a=1,b=2)')
         then: 'exception is thrown with the expected details'
             def exceptionThrown = thrown(NcmpException.class)
             exceptionThrown.details == 'testException'
@@ -348,8 +340,7 @@ class NetworkCmProxyDataServiceImplSpec extends Specification {
             mockDmiOperations.getResourceDataPassThroughRunningFromDmi('testDmiService',
                     'testCmHandle',
                     'testResourceId',
-                    'testFieldQuery',
-                    5,
+                    '(a=1,b=2)',
                     'testAcceptParam',
                     '{"operation":"read","cmHandleProperties":{"testName":"testValue"}}')
                     >> new ResponseEntity<>('NOK-json', HttpStatus.NOT_FOUND)
@@ -357,8 +348,7 @@ class NetworkCmProxyDataServiceImplSpec extends Specification {
             def response = objectUnderTest.getResourceDataPassThroughRunningForCmHandle('testCmHandle',
                     'testResourceId',
                     'testAcceptParam',
-                    'testFieldQuery',
-                    5)
+                    '(a=1,b=2)')
         then: 'exception is thrown'
             def exceptionThrown = thrown(NcmpException.class)
         and: 'details contains the original response'
@@ -381,7 +371,7 @@ class NetworkCmProxyDataServiceImplSpec extends Specification {
                 'testResourceId',
                 '{"operation":"create","dataType":"application/json","data":"{some-json}","cmHandleProperties":'
                 + expectedJsonForCmhandleProperties+ '}')
-                >> { new ResponseEntity<>(HttpStatus.OK) }
+                >> { new ResponseEntity<>(HttpStatus.CREATED) }
         where:
             scenario  | includeCmHandleProperties || expectedJsonForCmhandleProperties
             'with'    | true                      || '{"testName":"testValue"}'
@@ -408,10 +398,7 @@ class NetworkCmProxyDataServiceImplSpec extends Specification {
     }
 
     def 'Sync model for a (new) cm handle with #scenario'() {
-        given: 'DMI Plug-in returns a list of module references'
-            def knownModule1 = new ModuleReference('module1', '1')
-            def knownOtherModule = new ModuleReference('some other module', 'some revision')
-        and: 'persistence cm handle is given'
+        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) {
@@ -419,29 +406,29 @@ class NetworkCmProxyDataServiceImplSpec extends Specification {
             }
         and: 'dmi operations returns some module references'
             def jsonData = TestUtils.getResourceFileContent('cmHandleModules.json')
-            def expectedJsonBody = '{"operation":"read","cmHandleProperties":' + expectedJsonForAdditionalProperties + '}'
+            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(_) >> [knownModule1, knownOtherModule]
+            mockCpsModuleService.getYangResourceModuleReferences(_) >> existingModuleResourcesInCps
         and: 'DMI-Plugin returns resource(s) for "new" module(s)'
             def moduleResources = new ResponseEntity<String>(sdncReponseBody, HttpStatus.OK)
-            def jsonDataToFetchYangResource = '{"operation":"read","dataType":"application/json","data":"{\\"modules\\":[{\\"name\\":\\"module2\\",\\"revision\\":\\"1\\"}]}","cmHandleProperties":' + expectedJsonForAdditionalProperties + '}'
+            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.createAnchorAndSyncModel(cmHandleForModelSync)
+            objectUnderTest.syncModulesAndCreateAnchor(cmHandleForModelSync)
         then: 'the CPS module service is called once with the correct parameters'
-            1 * mockCpsModuleService.createSchemaSetFromModules(expectedDataspaceName, cmHandleForModelSync.getId(), expectedYangResourceToContentMap, [knownModule1])
+            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 | sdncReponseBody                                                                        || expectedYangResourceToContentMap | expectedJsonForAdditionalProperties
-            'one unknown module'             | ['name1':'value1']   | '[{"moduleName" : "someModule", "revision" : "1","yangSource": "[some yang source]"}]' || [someModule: 'some yang source'] | '{"name1":"value1"}'
-            'no add. properties'             | [:]                  | '[{"moduleName" : "someModule", "revision" : "1","yangSource": "[some yang source]"}]' || [someModule: 'some yang source'] | '{}'
-            'additional properties is null'  | null                 | '[{"moduleName" : "someModule", "revision" : "1","yangSource": "[some yang source]"}]' || [someModule: 'some yang source'] | '{}'
-            'no unknown module'              | [:]                  | '[]'                                                                                   || [:]                              | '{}'
+            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.'() {
@@ -451,10 +438,29 @@ class NetworkCmProxyDataServiceImplSpec extends Specification {
             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 getObjectUnderTestWithModelSyncDisabled() {
         def objectUnderTest = Spy(new NetworkCmProxyDataServiceImpl(mockDmiOperations, mockCpsModuleService,
                 mockCpsDataService, mockCpsQueryService, mockCpsAdminService, spyObjectMapper))
-        objectUnderTest.createAnchorAndSyncModel(_) >> null
+        objectUnderTest.syncModulesAndCreateAnchor(_) >> null
         return objectUnderTest
     }