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