Update ONAP DMI version in NCMP
[cps.git] / cps-ncmp-rest / src / test / groovy / org / onap / cps / ncmp / rest / controller / handlers / NcmpDatastoreRequestHandlerSpec.groovy
index ddeac51..00b0cb0 100644 (file)
@@ -25,17 +25,19 @@ import org.onap.cps.ncmp.api.impl.exception.InvalidDatastoreException
 import org.onap.cps.ncmp.api.impl.exception.InvalidOperationException
 import org.onap.cps.ncmp.api.models.DataOperationDefinition
 import org.onap.cps.ncmp.api.models.DataOperationRequest
 import org.onap.cps.ncmp.api.impl.exception.InvalidOperationException
 import org.onap.cps.ncmp.api.models.DataOperationDefinition
 import org.onap.cps.ncmp.api.models.DataOperationRequest
+import org.onap.cps.ncmp.api.models.CmResourceAddress
 import org.onap.cps.ncmp.rest.exceptions.OperationNotSupportedException
 import org.onap.cps.ncmp.rest.exceptions.OperationNotSupportedException
-import org.onap.cps.ncmp.rest.executor.CpsNcmpTaskExecutor
+import org.onap.cps.ncmp.rest.exceptions.PayloadTooLargeException
+import org.springframework.http.HttpStatus
+import reactor.core.publisher.Mono
 import spock.lang.Specification
 import spock.util.concurrent.PollingConditions
 
 class NcmpDatastoreRequestHandlerSpec extends Specification {
 
 import spock.lang.Specification
 import spock.util.concurrent.PollingConditions
 
 class NcmpDatastoreRequestHandlerSpec extends Specification {
 
-    def spiedCpsNcmpTaskExecutor = Spy(CpsNcmpTaskExecutor)
     def mockNetworkCmProxyDataService = Mock(NetworkCmProxyDataService)
 
     def mockNetworkCmProxyDataService = Mock(NetworkCmProxyDataService)
 
-    def objectUnderTest = new NcmpPassthroughResourceRequestHandler(spiedCpsNcmpTaskExecutor, mockNetworkCmProxyDataService)
+    def objectUnderTest = new NcmpPassthroughResourceRequestHandler(mockNetworkCmProxyDataService)
 
     def NO_AUTH_HEADER = null
 
 
     def NO_AUTH_HEADER = null
 
@@ -46,37 +48,32 @@ class NcmpDatastoreRequestHandlerSpec extends Specification {
     def 'Attempt to execute async get request with #scenario.'() {
         given: 'notification feature is turned on/off'
             objectUnderTest.notificationFeatureEnabled = notificationFeatureEnabled
     def 'Attempt to execute async get request with #scenario.'() {
         given: 'notification feature is turned on/off'
             objectUnderTest.notificationFeatureEnabled = notificationFeatureEnabled
-        and: 'a flag to track the network service call'
-            def networkServiceMethodCalled = false
-        and: 'the (mocked) service will use the flag to indicate if it is called'
-            mockNetworkCmProxyDataService.getResourceDataForCmHandle('ds', 'ch1', 'resource1', 'options', _, _, NO_AUTH_HEADER) >> {
-                networkServiceMethodCalled = true
-            }
+        and: 'a CM resource address'
+            def cmResourceAddress = new CmResourceAddress('ds', 'ch1', 'resource1')
+        and: 'the (mocked) service is called with the correct parameters returns OK'
+            1 * mockNetworkCmProxyDataService.getResourceDataForCmHandle(cmResourceAddress, 'options', _, _, NO_AUTH_HEADER) >> Mono.just(HttpStatus.OK)
         when: 'get request is executed with topic = #topic'
         when: 'get request is executed with topic = #topic'
-            objectUnderTest.executeRequest('ds', 'ch1', 'resource1', 'options', topic, false, NO_AUTH_HEADER)
-        then: 'the task is executed in an async fashion or not'
-            expectedCalls * spiedCpsNcmpTaskExecutor.executeTask(*_)
-        and: 'the service request is invoked'
-            new PollingConditions().within(1) {
-                assert networkServiceMethodCalled == true
-            }
+            def response= objectUnderTest.executeRequest(cmResourceAddress, 'options', topic, false, NO_AUTH_HEADER)
+        then: 'a successful response with/without request id is returned'
+            assert response.statusCode.value == 200
+            assert response.body instanceof Map == expectedResponseBodyIsMap
         where: 'the following parameters are used'
         where: 'the following parameters are used'
-            scenario                   | notificationFeatureEnabled | topic   || expectedCalls
-            'feature on, valid topic'  | true                       | 'valid' || 1
-            'feature on, no topic'     | true                       | null    || 0
-            'feature off, valid topic' | false                      | 'valid' || 0
-            'feature off, no topic'    | false                      | null    || 0
+            scenario                   | notificationFeatureEnabled | topic   || expectedCalls | expectedResponseBodyIsMap
+            'feature on, valid topic'  | true                       | 'valid' || 1             | true
+            'feature on, no topic'     | true                       | null    || 0             | false
+            'feature off, valid topic' | false                      | 'valid' || 0             | false
+            'feature off, no topic'    | false                      | null    || 0             | false
     }
 
     def 'Attempt to execute async data operation request with feature #scenario.'() {
         given: 'a extended request handler that supports bulk requests'
     }
 
     def 'Attempt to execute async data operation request with feature #scenario.'() {
         given: 'a extended request handler that supports bulk requests'
-           def objectUnderTest = new NcmpPassthroughResourceRequestHandler(spiedCpsNcmpTaskExecutor, mockNetworkCmProxyDataService)
+           def objectUnderTest = new NcmpPassthroughResourceRequestHandler(mockNetworkCmProxyDataService)
         and: 'notification feature is turned on/off'
             objectUnderTest.notificationFeatureEnabled = notificationFeatureEnabled
         when: 'data operation request is executed'
             objectUnderTest.executeRequest('someTopic', new DataOperationRequest(), NO_AUTH_HEADER)
         then: 'the task is executed in an async fashion or not'
         and: 'notification feature is turned on/off'
             objectUnderTest.notificationFeatureEnabled = notificationFeatureEnabled
         when: 'data operation request is executed'
             objectUnderTest.executeRequest('someTopic', new DataOperationRequest(), NO_AUTH_HEADER)
         then: 'the task is executed in an async fashion or not'
-            expectedCalls * spiedCpsNcmpTaskExecutor.executeTask(*_)
+            expectedCalls * mockNetworkCmProxyDataService.executeDataOperationForCmHandles('someTopic', _, _, null)
         where: 'the following parameters are used'
             scenario | notificationFeatureEnabled || expectedCalls
             'on'     | true                       || 1
         where: 'the following parameters are used'
             scenario | notificationFeatureEnabled || expectedCalls
             'on'     | true                       || 1
@@ -96,10 +93,11 @@ class NcmpDatastoreRequestHandlerSpec extends Specification {
                 networkServiceMethodCalled = true
             }
         when: 'data operation request is executed'
                 networkServiceMethodCalled = true
             }
         when: 'data operation request is executed'
-            objectUnderTest.executeRequest('myTopic', dataOperationRequest, NO_AUTH_HEADER)
-        then: 'the task is executed in an async fashion'
-            1 * spiedCpsNcmpTaskExecutor.executeTask(*_)
-        and: 'the network service is invoked'
+            def response = objectUnderTest.executeRequest('myTopic', dataOperationRequest, NO_AUTH_HEADER)
+        and: 'a successful response with request id is returned'
+            assert response.statusCode.value == 200
+            assert response.body.requestId != null
+        then: 'the network service is invoked'
             new PollingConditions().within(1) {
                 assert networkServiceMethodCalled == true
             }
             new PollingConditions().within(1) {
                 assert networkServiceMethodCalled == true
             }
@@ -108,9 +106,7 @@ class NcmpDatastoreRequestHandlerSpec extends Specification {
     }
 
     def 'Attempt to execute async data operation request with error #scenario'() {
     }
 
     def 'Attempt to execute async data operation request with error #scenario'() {
-        given: 'notification feature is turned on'
-            objectUnderTest.notificationFeatureEnabled = true
-        and: 'a data operation definition with datastore: #datastore'
+        given: 'a data operation definition with datastore: #datastore'
             def dataOperationDefinition = new DataOperationDefinition(operation: 'read', datastore: datastore)
         when: 'data operation request is executed'
             def dataOperationRequest = new DataOperationRequest(dataOperationDefinitions: [dataOperationDefinition])
             def dataOperationDefinition = new DataOperationDefinition(operation: 'read', datastore: datastore)
         when: 'data operation request is executed'
             def dataOperationRequest = new DataOperationRequest(dataOperationDefinitions: [dataOperationDefinition])
@@ -125,11 +121,9 @@ class NcmpDatastoreRequestHandlerSpec extends Specification {
     }
 
     def 'Attempt to execute async data operation request with #scenario operation: #operation.'() {
     }
 
     def 'Attempt to execute async data operation request with #scenario operation: #operation.'() {
-        given: 'notification feature is turned on'
-            objectUnderTest.notificationFeatureEnabled = true
-        and: 'a data operation definition with operation: #operation'
+        given: 'a data operation definition with operation: #operation'
             def dataOperationDefinition = new DataOperationDefinition(operation: operation, datastore: 'ncmp-datastore:passthrough-running')
             def dataOperationDefinition = new DataOperationDefinition(operation: operation, datastore: 'ncmp-datastore:passthrough-running')
-        when: 'bulk request is executed'
+        when: 'data operation request is executed'
             objectUnderTest.executeRequest('someTopic', new DataOperationRequest(dataOperationDefinitions:[dataOperationDefinition]), NO_AUTH_HEADER)
         then: 'the expected type of exception is thrown'
             thrown(expectedException)
             objectUnderTest.executeRequest('someTopic', new DataOperationRequest(dataOperationDefinitions:[dataOperationDefinition]), NO_AUTH_HEADER)
         then: 'the expected type of exception is thrown'
             thrown(expectedException)
@@ -142,4 +136,17 @@ class NcmpDatastoreRequestHandlerSpec extends Specification {
             'unsupported' | 'delete'  || OperationNotSupportedException
     }
 
             'unsupported' | 'delete'  || OperationNotSupportedException
     }
 
+    def 'Attempt to execute async data operation request with too many cm handles.'() {
+        given: 'a data operation definition with too many cm handles'
+            def tooMany = objectUnderTest.MAXIMUM_CM_HANDLES_PER_OPERATION+1
+            def cmHandleIds = new String[tooMany]
+            def dataOperationDefinition = new DataOperationDefinition(operationId: 'abc', operation: 'read', datastore: 'ncmp-datastore:passthrough-running', cmHandleIds: cmHandleIds)
+        when: 'data operation request is executed'
+            objectUnderTest.executeRequest('someTopic', new DataOperationRequest(dataOperationDefinitions:[dataOperationDefinition]), NO_AUTH_HEADER)
+        then: 'a payload too large exception is thrown'
+            def exceptionThrown = thrown(PayloadTooLargeException)
+        and: 'the error message contains the offending number of cm handles'
+            assert exceptionThrown.message == "Operation 'abc' affects too many (${tooMany}) cm handles"
+    }
+
 }
 }