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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.cps.ncmp.rest.controller.handlers
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
35 class NcmpDatastoreRequestHandlerSpec extends Specification {
37 def spiedCpsNcmpTaskExecutor = Spy(CpsNcmpTaskExecutor)
38 def mockNetworkCmProxyDataService = Mock(NetworkCmProxyDataService)
40 def objectUnderTest = new NcmpPassthroughResourceRequestHandler(spiedCpsNcmpTaskExecutor, mockNetworkCmProxyDataService)
42 def NO_AUTH_HEADER = null
45 objectUnderTest.timeOutInMilliSeconds = 100
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
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
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 * mockNetworkCmProxyDataService.executeDataOperationForCmHandles('someTopic', _, _, null)
83 where: 'the following parameters are used'
84 scenario | notificationFeatureEnabled || expectedCalls
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
101 when: 'data operation request is executed'
102 def response = objectUnderTest.executeRequest('myTopic', dataOperationRequest, NO_AUTH_HEADER)
103 and: 'a successful response with request id is returned'
104 assert response.statusCode.value == 200
105 assert response.body.requestId != null
106 then: 'the network service is invoked'
107 new PollingConditions().within(1) {
108 assert networkServiceMethodCalled == true
110 where: 'the following datastores are used'
111 datastore << ['ncmp-datastore:passthrough-running', 'ncmp-datastore:passthrough-operational']
114 def 'Attempt to execute async data operation request with error #scenario'() {
115 given: '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'
129 def 'Attempt to execute async data operation request with #scenario operation: #operation.'() {
130 given: 'a data operation definition with operation: #operation'
131 def dataOperationDefinition = new DataOperationDefinition(operation: operation, datastore: 'ncmp-datastore:passthrough-running')
132 when: 'data operation request is executed'
133 objectUnderTest.executeRequest('someTopic', new DataOperationRequest(dataOperationDefinitions:[dataOperationDefinition]), NO_AUTH_HEADER)
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
145 def 'Attempt to execute async data operation request with too many cm handles.'() {
146 given: 'a data operation definition with too many cm handles'
147 def tooMany = objectUnderTest.MAXIMUM_CM_HANDLES_PER_OPERATION+1
148 def cmHandleIds = new String[tooMany]
149 def dataOperationDefinition = new DataOperationDefinition(operationId: 'abc', operation: 'read', datastore: 'ncmp-datastore:passthrough-running', cmHandleIds: cmHandleIds)
150 when: 'data operation request is executed'
151 objectUnderTest.executeRequest('someTopic', new DataOperationRequest(dataOperationDefinitions:[dataOperationDefinition]), NO_AUTH_HEADER)
152 then: 'a payload too large exception is thrown'
153 def exceptionThrown = thrown(PayloadTooLargeException)
154 and: 'the error message contains the offending number of cm handles'
155 assert exceptionThrown.message == "Operation 'abc' affects too many (${tooMany}) cm handles"