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