Revert "Migrate CPS to Spring-boot 3.0"
[cps.git] / cps-ncmp-rest / src / test / groovy / org / onap / cps / ncmp / rest / controller / handlers / NcmpDatastoreRequestHandlerSpec.groovy
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2023 Nordix Foundation
4  *  ================================================================================
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  *  SPDX-License-Identifier: Apache-2.0
18  *  ============LICENSE_END=========================================================
19  */
20
21 package org.onap.cps.ncmp.rest.controller.handlers
22
23 import org.onap.cps.ncmp.api.NetworkCmProxyDataService
24 import org.onap.cps.ncmp.api.impl.exception.InvalidDatastoreException
25 import org.onap.cps.ncmp.api.impl.exception.InvalidOperationException
26 import org.onap.cps.ncmp.api.models.DataOperationDefinition
27 import org.onap.cps.ncmp.api.models.DataOperationRequest
28 import org.onap.cps.ncmp.rest.exceptions.OperationNotSupportedException
29 import org.onap.cps.ncmp.rest.executor.CpsNcmpTaskExecutor
30 import spock.lang.Specification
31 import spock.util.concurrent.PollingConditions
32
33 class NcmpDatastoreRequestHandlerSpec extends Specification {
34
35     def spiedCpsNcmpTaskExecutor = Spy(CpsNcmpTaskExecutor)
36     def mockNetworkCmProxyDataService = Mock(NetworkCmProxyDataService)
37
38     def objectUnderTest = new NcmpPassthroughResourceRequestHandler(spiedCpsNcmpTaskExecutor, mockNetworkCmProxyDataService)
39
40     def 'Attempt to execute async get request with #scenario.'() {
41         given: 'notification feature is turned on/off'
42             objectUnderTest.notificationFeatureEnabled = notificationFeatureEnabled
43         when: 'get request is executed with topic = #topic'
44             objectUnderTest.executeRequest('ds', 'ch1', 'resource1', 'options', topic, false)
45         and: 'wait a little for async execution (only if expected)'
46             if (expectedCalls > 0) {
47                 Thread.sleep(100)
48             }
49         then: 'the task is executed in an async fashion or not'
50             expectedCalls * spiedCpsNcmpTaskExecutor.executeTask(*_)
51         and: 'the service request is always invoked'
52             1 * mockNetworkCmProxyDataService.getResourceDataForCmHandle('ds', 'ch1', 'resource1', 'options', _, _)
53         where: 'the following parameters are used'
54             scenario                   | notificationFeatureEnabled | topic   || expectedCalls
55             'feature on, valid topic'  | true                       | 'valid' || 1
56             'feature on, no topic'     | true                       | null    || 0
57             'feature off, valid topic' | false                      | 'valid' || 0
58             'feature off, no topic'    | false                      | null    || 0
59     }
60
61     def 'Attempt to execute async data operation request with feature #scenario.'() {
62         given: 'a extended request handler that supports bulk requests'
63            def objectUnderTest = new NcmpPassthroughResourceRequestHandler(spiedCpsNcmpTaskExecutor, mockNetworkCmProxyDataService)
64         and: 'notification feature is turned on/off'
65             objectUnderTest.notificationFeatureEnabled = notificationFeatureEnabled
66         when: 'data operation request is executed'
67             objectUnderTest.executeRequest('someTopic', new DataOperationRequest())
68         then: 'the task is executed in an async fashion or not'
69             expectedCalls * spiedCpsNcmpTaskExecutor.executeTask(*_)
70         where: 'the following parameters are used'
71             scenario | notificationFeatureEnabled || expectedCalls
72             'on'     | true                       || 1
73             'off'    | false                      || 0
74     }
75
76     def 'Execute async data operation request with datastore #datastore.'() {
77         given: 'notification feature is turned on'
78             objectUnderTest.notificationFeatureEnabled = true
79         and: 'a data operation request with datastore: #datastore'
80             def dataOperationDefinition = new DataOperationDefinition(operation: 'read', datastore: datastore)
81             def dataOperationRequest = new DataOperationRequest(dataOperationDefinitions: [dataOperationDefinition])
82         and: ' a flag to track the network service call'
83             def networkServiceMethodCalled = false
84         and: 'the (mocked) service will use the flag to indicate it is called'
85             mockNetworkCmProxyDataService.executeDataOperationForCmHandles('myTopic', dataOperationRequest, _) >> {
86                 networkServiceMethodCalled = true
87             }
88         when: 'data operation request is executed'
89             objectUnderTest.executeRequest('myTopic', dataOperationRequest)
90         then: 'the task is executed in an async fashion'
91             1 * spiedCpsNcmpTaskExecutor.executeTask(*_)
92         and: 'the network service is invoked (wait max. 5 seconds)'
93             new PollingConditions(timeout: 30).eventually {
94                 //TODO Fix test assertion
95             }
96         where: 'the following datastores are used'
97             datastore << ['ncmp-datastore:passthrough-running', 'ncmp-datastore:passthrough-operational']
98     }
99
100     def 'Attempt to execute async data operation request with error #scenario'() {
101         given: 'notification feature is turned on'
102             objectUnderTest.notificationFeatureEnabled = true
103         and: 'a data operation definition with datastore: #datastore'
104             def dataOperationDefinition = new DataOperationDefinition(operation: 'read', datastore: datastore)
105         when: 'data operation request is executed'
106             def dataOperationRequest = new DataOperationRequest(dataOperationDefinitions: [dataOperationDefinition])
107             objectUnderTest.executeRequest('myTopic', dataOperationRequest)
108         then: 'the correct error is thrown'
109             def thrown = thrown(InvalidDatastoreException)
110             assert thrown.message.contains(expectedErrorMessage)
111         where: 'the following datastore names are used'
112             scenario                | datastore                    || expectedErrorMessage
113             'unsupported datastore' | 'ncmp-datastore:operational' || 'not supported'
114             'invalid datastore'     | 'invalid'                    || 'invalid datastore name'
115     }
116
117     def 'Attempt to execute async data operation request with #scenario operation: #operation.'() {
118         given: 'notification feature is turned on'
119             objectUnderTest.notificationFeatureEnabled = true
120         and: 'a data operation definition with operation: #operation'
121             def dataOperationDefinition = new DataOperationDefinition(operation: operation, datastore: 'ncmp-datastore:passthrough-running')
122         when: 'bulk request is executed'
123             objectUnderTest.executeRequest('someTopic', new DataOperationRequest(dataOperationDefinitions:[dataOperationDefinition]))
124         then: 'the expected type of exception is thrown'
125             thrown(expectedException)
126         where: 'the following operations are used'
127             scenario      | operation || expectedException
128             'invalid'     | 'invalid' || InvalidOperationException
129             'unsupported' | 'create'  || OperationNotSupportedException
130             'unsupported' | 'update'  || OperationNotSupportedException
131             'unsupported' | 'patch'   || OperationNotSupportedException
132             'unsupported' | 'delete'  || OperationNotSupportedException
133     }
134
135 }