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