Update ONAP DMI version in NCMP
[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.springframework.http.HttpStatus
32 import reactor.core.publisher.Mono
33 import spock.lang.Specification
34 import spock.util.concurrent.PollingConditions
35
36 class NcmpDatastoreRequestHandlerSpec extends Specification {
37
38     def mockNetworkCmProxyDataService = Mock(NetworkCmProxyDataService)
39
40     def objectUnderTest = new NcmpPassthroughResourceRequestHandler(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 CM resource address'
52             def cmResourceAddress = new CmResourceAddress('ds', 'ch1', 'resource1')
53         and: 'the (mocked) service is called with the correct parameters returns OK'
54             1 * mockNetworkCmProxyDataService.getResourceDataForCmHandle(cmResourceAddress, 'options', _, _, NO_AUTH_HEADER) >> Mono.just(HttpStatus.OK)
55         when: 'get request is executed with topic = #topic'
56             def response= objectUnderTest.executeRequest(cmResourceAddress, 'options', topic, false, NO_AUTH_HEADER)
57         then: 'a successful response with/without request id is returned'
58             assert response.statusCode.value == 200
59             assert response.body instanceof Map == expectedResponseBodyIsMap
60         where: 'the following parameters are used'
61             scenario                   | notificationFeatureEnabled | topic   || expectedCalls | expectedResponseBodyIsMap
62             'feature on, valid topic'  | true                       | 'valid' || 1             | true
63             'feature on, no topic'     | true                       | null    || 0             | false
64             'feature off, valid topic' | false                      | 'valid' || 0             | false
65             'feature off, no topic'    | false                      | null    || 0             | false
66     }
67
68     def 'Attempt to execute async data operation request with feature #scenario.'() {
69         given: 'a extended request handler that supports bulk requests'
70            def objectUnderTest = new NcmpPassthroughResourceRequestHandler(mockNetworkCmProxyDataService)
71         and: 'notification feature is turned on/off'
72             objectUnderTest.notificationFeatureEnabled = notificationFeatureEnabled
73         when: 'data operation request is executed'
74             objectUnderTest.executeRequest('someTopic', new DataOperationRequest(), NO_AUTH_HEADER)
75         then: 'the task is executed in an async fashion or not'
76             expectedCalls * mockNetworkCmProxyDataService.executeDataOperationForCmHandles('someTopic', _, _, null)
77         where: 'the following parameters are used'
78             scenario | notificationFeatureEnabled || expectedCalls
79             'on'     | true                       || 1
80             'off'    | false                      || 0
81     }
82
83     def 'Execute async data operation request with datastore #datastore.'() {
84         given: 'notification feature is turned on'
85             objectUnderTest.notificationFeatureEnabled = true
86         and: 'a data operation request with datastore: #datastore'
87             def dataOperationDefinition = new DataOperationDefinition(operation: 'read', datastore: datastore)
88             def dataOperationRequest = new DataOperationRequest(dataOperationDefinitions: [dataOperationDefinition])
89         and: ' a flag to track the network service call'
90             def networkServiceMethodCalled = false
91         and: 'the (mocked) service will use the flag to indicate it is called'
92             mockNetworkCmProxyDataService.executeDataOperationForCmHandles('myTopic', dataOperationRequest, _, NO_AUTH_HEADER) >> {
93                 networkServiceMethodCalled = true
94             }
95         when: 'data operation request is executed'
96             def response = objectUnderTest.executeRequest('myTopic', dataOperationRequest, NO_AUTH_HEADER)
97         and: 'a successful response with request id is returned'
98             assert response.statusCode.value == 200
99             assert response.body.requestId != null
100         then: '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: 'a data operation definition with datastore: #datastore'
110             def dataOperationDefinition = new DataOperationDefinition(operation: 'read', datastore: datastore)
111         when: 'data operation request is executed'
112             def dataOperationRequest = new DataOperationRequest(dataOperationDefinitions: [dataOperationDefinition])
113             objectUnderTest.executeRequest('myTopic', dataOperationRequest, NO_AUTH_HEADER)
114         then: 'the correct error is thrown'
115             def thrown = thrown(InvalidDatastoreException)
116             assert thrown.message.contains(expectedErrorMessage)
117         where: 'the following datastore names are used'
118             scenario                | datastore                    || expectedErrorMessage
119             'unsupported datastore' | 'ncmp-datastore:operational' || 'not supported'
120             'invalid datastore'     | 'invalid'                    || 'invalid datastore name'
121     }
122
123     def 'Attempt to execute async data operation request with #scenario operation: #operation.'() {
124         given: 'a data operation definition with operation: #operation'
125             def dataOperationDefinition = new DataOperationDefinition(operation: operation, datastore: 'ncmp-datastore:passthrough-running')
126         when: 'data operation request is executed'
127             objectUnderTest.executeRequest('someTopic', new DataOperationRequest(dataOperationDefinitions:[dataOperationDefinition]), NO_AUTH_HEADER)
128         then: 'the expected type of exception is thrown'
129             thrown(expectedException)
130         where: 'the following operations are used'
131             scenario      | operation || expectedException
132             'invalid'     | 'invalid' || InvalidOperationException
133             'unsupported' | 'create'  || OperationNotSupportedException
134             'unsupported' | 'update'  || OperationNotSupportedException
135             'unsupported' | 'patch'   || OperationNotSupportedException
136             'unsupported' | 'delete'  || OperationNotSupportedException
137     }
138
139     def 'Attempt to execute async data operation request with too many cm handles.'() {
140         given: 'a data operation definition with too many cm handles'
141             def tooMany = objectUnderTest.MAXIMUM_CM_HANDLES_PER_OPERATION+1
142             def cmHandleIds = new String[tooMany]
143             def dataOperationDefinition = new DataOperationDefinition(operationId: 'abc', operation: 'read', datastore: 'ncmp-datastore:passthrough-running', cmHandleIds: cmHandleIds)
144         when: 'data operation request is executed'
145             objectUnderTest.executeRequest('someTopic', new DataOperationRequest(dataOperationDefinitions:[dataOperationDefinition]), NO_AUTH_HEADER)
146         then: 'a payload too large exception is thrown'
147             def exceptionThrown = thrown(PayloadTooLargeException)
148         and: 'the error message contains the offending number of cm handles'
149             assert exceptionThrown.message == "Operation 'abc' affects too many (${tooMany}) cm handles"
150     }
151
152 }