Replace sleep with PollingConditions
[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-2024 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 setup() {
41         objectUnderTest.timeOutInMilliSeconds = 100
42     }
43
44     def 'Attempt to execute async get request with #scenario.'() {
45         given: 'notification feature is turned on/off'
46             objectUnderTest.notificationFeatureEnabled = notificationFeatureEnabled
47         and: 'a flag to track the network service call'
48             def networkServiceMethodCalled = false
49         and: 'the (mocked) service will use the flag to indicate if it is called'
50             mockNetworkCmProxyDataService.getResourceDataForCmHandle('ds', 'ch1', 'resource1', 'options', _, _) >> {
51                 networkServiceMethodCalled = true
52             }
53         when: 'get request is executed with topic = #topic'
54             objectUnderTest.executeRequest('ds', 'ch1', 'resource1', 'options', topic, false)
55         then: 'the task is executed in an async fashion or not'
56             expectedCalls * spiedCpsNcmpTaskExecutor.executeTask(*_)
57         and: 'the service request is invoked'
58             new PollingConditions().within(1) {
59                 assert networkServiceMethodCalled == true
60             }
61         where: 'the following parameters are used'
62             scenario                   | notificationFeatureEnabled | topic   || expectedCalls
63             'feature on, valid topic'  | true                       | 'valid' || 1
64             'feature on, no topic'     | true                       | null    || 0
65             'feature off, valid topic' | false                      | 'valid' || 0
66             'feature off, no topic'    | false                      | null    || 0
67     }
68
69     def 'Attempt to execute async data operation request with feature #scenario.'() {
70         given: 'a extended request handler that supports bulk requests'
71            def objectUnderTest = new NcmpPassthroughResourceRequestHandler(spiedCpsNcmpTaskExecutor, mockNetworkCmProxyDataService)
72         and: 'notification feature is turned on/off'
73             objectUnderTest.notificationFeatureEnabled = notificationFeatureEnabled
74         when: 'data operation request is executed'
75             objectUnderTest.executeRequest('someTopic', new DataOperationRequest())
76         then: 'the task is executed in an async fashion or not'
77             expectedCalls * spiedCpsNcmpTaskExecutor.executeTask(*_)
78         where: 'the following parameters are used'
79             scenario | notificationFeatureEnabled || expectedCalls
80             'on'     | true                       || 1
81             'off'    | false                      || 0
82     }
83
84     def 'Execute async data operation request with datastore #datastore.'() {
85         given: 'notification feature is turned on'
86             objectUnderTest.notificationFeatureEnabled = true
87         and: 'a data operation request with datastore: #datastore'
88             def dataOperationDefinition = new DataOperationDefinition(operation: 'read', datastore: datastore)
89             def dataOperationRequest = new DataOperationRequest(dataOperationDefinitions: [dataOperationDefinition])
90         and: ' a flag to track the network service call'
91             def networkServiceMethodCalled = false
92         and: 'the (mocked) service will use the flag to indicate it is called'
93             mockNetworkCmProxyDataService.executeDataOperationForCmHandles('myTopic', dataOperationRequest, _) >> {
94                 networkServiceMethodCalled = true
95             }
96         when: 'data operation request is executed'
97             objectUnderTest.executeRequest('myTopic', dataOperationRequest)
98         then: 'the task is executed in an async fashion'
99             1 * spiedCpsNcmpTaskExecutor.executeTask(*_)
100         and: 'the network service is invoked'
101             new PollingConditions().within(1) {
102                 assert networkServiceMethodCalled == true
103             }
104         where: 'the following datastores are used'
105             datastore << ['ncmp-datastore:passthrough-running', 'ncmp-datastore:passthrough-operational']
106     }
107
108     def 'Attempt to execute async data operation request with error #scenario'() {
109         given: 'notification feature is turned on'
110             objectUnderTest.notificationFeatureEnabled = true
111         and: 'a data operation definition with datastore: #datastore'
112             def dataOperationDefinition = new DataOperationDefinition(operation: 'read', datastore: datastore)
113         when: 'data operation request is executed'
114             def dataOperationRequest = new DataOperationRequest(dataOperationDefinitions: [dataOperationDefinition])
115             objectUnderTest.executeRequest('myTopic', dataOperationRequest)
116         then: 'the correct error is thrown'
117             def thrown = thrown(InvalidDatastoreException)
118             assert thrown.message.contains(expectedErrorMessage)
119         where: 'the following datastore names are used'
120             scenario                | datastore                    || expectedErrorMessage
121             'unsupported datastore' | 'ncmp-datastore:operational' || 'not supported'
122             'invalid datastore'     | 'invalid'                    || 'invalid datastore name'
123     }
124
125     def 'Attempt to execute async data operation request with #scenario operation: #operation.'() {
126         given: 'notification feature is turned on'
127             objectUnderTest.notificationFeatureEnabled = true
128         and: 'a data operation definition with operation: #operation'
129             def dataOperationDefinition = new DataOperationDefinition(operation: operation, datastore: 'ncmp-datastore:passthrough-running')
130         when: 'bulk request is executed'
131             objectUnderTest.executeRequest('someTopic', new DataOperationRequest(dataOperationDefinitions:[dataOperationDefinition]))
132         then: 'the expected type of exception is thrown'
133             thrown(expectedException)
134         where: 'the following operations are used'
135             scenario      | operation || expectedException
136             'invalid'     | 'invalid' || InvalidOperationException
137             'unsupported' | 'create'  || OperationNotSupportedException
138             'unsupported' | 'update'  || OperationNotSupportedException
139             'unsupported' | 'patch'   || OperationNotSupportedException
140             'unsupported' | 'delete'  || OperationNotSupportedException
141     }
142
143 }