Post impl for passthrough running (Ncmp impl.)
[cps.git] / cps-ncmp-service / src / test / groovy / org / onap / cps / ncmp / api / impl / NetworkCmProxyDataServiceImplSpec.groovy
index 45fa0af..8a32ad5 100644 (file)
@@ -51,6 +51,7 @@ class NetworkCmProxyDataServiceImplSpec extends Specification {
 
     def cmHandle = 'some handle'
     def noTimestamp = null
+    def cmHandleXPath = "/dmi-registry/cm-handles[@id='testCmHandle']"
 
     def expectedDataspaceName = 'NFP-Operational'
     def 'Query data nodes by cps path with #fetchDescendantsOption.'() {
@@ -159,14 +160,8 @@ class NetworkCmProxyDataServiceImplSpec extends Specification {
     }
 
     def 'Get resource data for pass-through operational from dmi.'() {
-        given: 'xpath'
-            def xpath = "/dmi-registry/cm-handles[@id='testCmHandle']"
-        and: 'data node'
-            def dataNode = new DataNode()
-            dataNode.leaves = ['dmi-service-name':'testDmiService']
-            def childDataNode = new DataNode()
-            childDataNode.leaves = ['name':'testName','value':'testValue']
-            dataNode.childDataNodes = [childDataNode]
+        given: 'data node representing cmHandle and its properties'
+            def cmHandleDataNode = prepareCmHandleDataNode()
         when: 'get resource data is called'
             def response = objectUnderTest.getResourceDataOperationalForCmHandle('testCmHandle',
             'testResourceId',
@@ -175,9 +170,9 @@ class NetworkCmProxyDataServiceImplSpec extends Specification {
             5)
         then: 'cps data service is being called once to get data node'
             1 * mockCpsDataService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry',
-                    xpath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> dataNode
+                    cmHandleXPath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> cmHandleDataNode
         and: 'dmi operation is being called to get resource data'
-            1 * mockDmiOperations.getResouceDataOperationalFromDmi('testDmiService',
+            1 * mockDmiOperations.getResourceDataOperationalFromDmi('testDmiService',
                     'testCmHandle',
                     'testResourceId',
                     'testFieldQuery',
@@ -188,17 +183,13 @@ class NetworkCmProxyDataServiceImplSpec extends Specification {
         and: 'dmi returns ok response'
             response == 'result-json'
     }
+
     def 'Get resource data for pass-through operational from dmi threw parsing exception.'() {
-        given: 'xpath'
-            def xpath = "/dmi-registry/cm-handles[@id='testCmHandle']"
-        and: 'data node'
-            def dataNode = new DataNode()
-            dataNode.leaves = ['dmi-service-name':'testDmiService']
-            def childDataNode = new DataNode()
-            childDataNode.leaves = ['name':'testName','value':'testValue']
-            dataNode.childDataNodes = [childDataNode]
+        given: 'data node representing cmHandle and its properties'
+            def cmHandleDataNode = prepareCmHandleDataNode()
+        and: 'cps data service returns valid cmHandle data node'
             mockCpsDataService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry',
-                    xpath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> dataNode
+                    cmHandleXPath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> cmHandleDataNode
         and: 'objectMapper not able to parse object'
             def mockObjectMapper = Mock(ObjectMapper)
             objectUnderTest.objectMapper = mockObjectMapper
@@ -212,19 +203,15 @@ class NetworkCmProxyDataServiceImplSpec extends Specification {
         then: 'exception is thrown'
             thrown(NcmpException.class)
     }
+
     def 'Get resource data for pass-through operational from dmi return NOK response.'() {
-        given: 'xpath'
-            def xpath = "/dmi-registry/cm-handles[@id='testCmHandle']"
-        and: 'data node'
-            def dataNode = new DataNode()
-            dataNode.leaves = ['dmi-service-name':'testDmiService']
-            def childDataNode = new DataNode()
-            childDataNode.leaves = ['name':'testName','value':'testValue']
-            dataNode.childDataNodes = [childDataNode]
+        given: 'data node representing cmHandle and its properties'
+            def cmHandleDataNode = prepareCmHandleDataNode()
+        and: 'cps data service returns valid cmHandle data node'
             mockCpsDataService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry',
-                    xpath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> dataNode
+                    cmHandleXPath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> cmHandleDataNode
         and: 'dmi returns NOK response'
-            mockDmiOperations.getResouceDataOperationalFromDmi('testDmiService',
+            mockDmiOperations.getResourceDataOperationalFromDmi('testDmiService',
                     'testCmHandle',
                     'testResourceId',
                     'testFieldQuery',
@@ -241,20 +228,15 @@ class NetworkCmProxyDataServiceImplSpec extends Specification {
         then: 'exception is thrown'
             thrown(NcmpException.class)
     }
+
     def 'Get resource data for pass-through running from dmi.'() {
-        given: 'xpath'
-            def xpath = "/dmi-registry/cm-handles[@id='testCmHandle']"
-        and: 'data node representing cmhandle and its properties'
-            def dataNode = new DataNode()
-            dataNode.leaves = ['dmi-service-name':'testDmiService']
-            def childDataNode = new DataNode()
-            childDataNode.leaves = ['name':'testName','value':'testValue']
-            dataNode.childDataNodes = [childDataNode]
+        given: 'data node representing cmHandle and its properties'
+            def cmHandleDataNode = prepareCmHandleDataNode()
         and: 'cpsDataService returns valid dataNode'
             mockCpsDataService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry',
-                    xpath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> dataNode
+                    cmHandleXPath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> cmHandleDataNode
         and: 'dmi returns valid response and data'
-            mockDmiOperations.getResouceDataPassThroughRunningFromDmi('testDmiService',
+            mockDmiOperations.getResourceDataPassThroughRunningFromDmi('testDmiService',
                     'testCmHandle',
                     'testResourceId',
                     'testFieldQuery',
@@ -270,18 +252,13 @@ class NetworkCmProxyDataServiceImplSpec extends Specification {
         then: 'get resource data returns expected response'
             response == '{result-json}'
     }
+
     def 'Get resource data for pass-through running from dmi threw parsing exception.'() {
-        given: 'xpath'
-            def xpath = "/dmi-registry/cm-handles[@id='testCmHandle']"
-        and: 'data node representing cmhandle and its properties'
-            def dataNode = new DataNode()
-            dataNode.leaves = ['dmi-service-name':'testDmiService']
-            def childDataNode = new DataNode()
-            childDataNode.leaves = ['name':'testName','value':'testValue']
-            dataNode.childDataNodes = [childDataNode]
+        given: 'data node representing cmHandle and its properties'
+            def cmHandleDataNode = prepareCmHandleDataNode()
         and: 'cpsDataService returns valid dataNode'
             mockCpsDataService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry',
-                    xpath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> dataNode
+                    cmHandleXPath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> cmHandleDataNode
         and: 'objectMapper not able to parse object'
             def mockObjectMapper = Mock(ObjectMapper)
             objectUnderTest.objectMapper = mockObjectMapper
@@ -295,20 +272,15 @@ class NetworkCmProxyDataServiceImplSpec extends Specification {
         then: 'exception is thrown'
             thrown(NcmpException.class)
     }
+
     def 'Get resource data for pass-through running from dmi return NOK response.'() {
-        given: 'xpath'
-            def xpath = "/dmi-registry/cm-handles[@id='testCmHandle']"
-        and: 'data node representing cmhandle and its properties'
-            def dataNode = new DataNode()
-            dataNode.leaves = ['dmi-service-name':'testDmiService']
-            def childDataNode = new DataNode()
-            childDataNode.leaves = ['name':'testName','value':'testValue']
-            dataNode.childDataNodes = [childDataNode]
+        given: 'data node representing cmHandle and its properties'
+            def cmHandleDataNode = prepareCmHandleDataNode()
         and: 'cpsDataService returns valid dataNode'
             mockCpsDataService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry',
-                    xpath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> dataNode
+                    cmHandleXPath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> cmHandleDataNode
         and: 'dmi returns NOK response'
-            mockDmiOperations.getResouceDataPassThroughRunningFromDmi('testDmiService',
+            mockDmiOperations.getResourceDataPassThroughRunningFromDmi('testDmiService',
                     'testCmHandle',
                     'testResourceId',
                     'testFieldQuery',
@@ -325,4 +297,48 @@ class NetworkCmProxyDataServiceImplSpec extends Specification {
         then: 'exception is thrown'
             thrown(NcmpException.class)
     }
+
+    def 'Write resource data for pass-through running from dmi using POST.'() {
+        given: 'data node representing cmHandle and its properties'
+            def cmHandleDataNode = prepareCmHandleDataNode()
+        and: 'cpsDataService returns valid 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":{"testName":"testValue"}}')
+                >> { new ResponseEntity<>(HttpStatus.CREATED) }
+    }
+
+    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 = prepareCmHandleDataNode()
+        and: 'cpsDataService returns valid dataNode'
+            mockCpsDataService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry',
+                    cmHandleXPath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> cmHandleDataNode
+        and: 'dmi throws exception'
+            1 * 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'
+            thrown(NcmpException.class)
+    }
+
+    private DataNode prepareCmHandleDataNode() {
+        def cmHandleDataNode = new DataNode()
+        cmHandleDataNode.leaves = ['dmi-service-name': 'testDmiService']
+        def cmHandlePropertyDataNode = new DataNode()
+        cmHandlePropertyDataNode.leaves = ['name': 'testName', 'value': 'testValue']
+        cmHandleDataNode.childDataNodes = [cmHandlePropertyDataNode]
+        cmHandleDataNode
+    }
 }