2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021-2024 Nordix Foundation
4 * Modifications Copyright (C) 2022 Bell Canada
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.cps.ncmp.api.impl.operations
24 import static org.onap.cps.ncmp.api.impl.events.mapper.CloudEventMapper.toTargetEvent
25 import static org.onap.cps.ncmp.api.impl.operations.DatastoreType.PASSTHROUGH_OPERATIONAL
26 import static org.onap.cps.ncmp.api.impl.operations.DatastoreType.PASSTHROUGH_RUNNING
27 import static org.onap.cps.ncmp.api.impl.operations.OperationType.CREATE
28 import static org.onap.cps.ncmp.api.impl.operations.OperationType.READ
29 import static org.onap.cps.ncmp.api.impl.operations.OperationType.UPDATE
30 import static org.onap.cps.ncmp.api.NcmpResponseStatus.UNABLE_TO_READ_RESOURCE_DATA
31 import static org.onap.cps.ncmp.api.NcmpResponseStatus.DMI_SERVICE_NOT_RESPONDING
33 import com.fasterxml.jackson.databind.ObjectMapper
34 import org.onap.cps.events.EventsPublisher
35 import org.onap.cps.ncmp.api.impl.config.NcmpConfiguration
36 import org.onap.cps.ncmp.api.impl.exception.HttpClientRequestException
37 import org.onap.cps.ncmp.api.impl.utils.DmiServiceUrlBuilder
38 import org.onap.cps.ncmp.api.impl.utils.context.CpsApplicationContext
39 import org.onap.cps.ncmp.api.models.DataOperationRequest
40 import org.onap.cps.ncmp.events.async1_0_0.DataOperationEvent
41 import org.onap.cps.ncmp.utils.TestUtils
42 import org.onap.cps.utils.JsonObjectMapper
43 import org.spockframework.spring.SpringBean
44 import org.springframework.beans.factory.annotation.Autowired
45 import org.springframework.boot.test.context.SpringBootTest
46 import org.springframework.http.ResponseEntity
47 import org.springframework.test.context.ContextConfiguration
48 import org.springframework.http.HttpStatus
49 import spock.lang.Shared
50 import spock.util.concurrent.PollingConditions
51 import java.util.concurrent.TimeoutException
54 @ContextConfiguration(classes = [EventsPublisher, CpsApplicationContext, NcmpConfiguration.DmiProperties, DmiDataOperations])
55 class DmiDataOperationsSpec extends DmiOperationsBaseSpec {
58 DmiServiceUrlBuilder dmiServiceUrlBuilder = Mock()
59 def dmiServiceBaseUrl = "${dmiServiceName}/dmi/v1/ch/${cmHandleId}/data/ds/ncmp-datastore:"
61 def NO_REQUEST_ID = null
63 def OPTIONS_PARAM = '(a=1,b=2)'
66 JsonObjectMapper spiedJsonObjectMapper = Spy(new JsonObjectMapper(new ObjectMapper()))
69 DmiDataOperations objectUnderTest
72 EventsPublisher eventsPublisher = Stub()
74 def 'call get resource data for #expectedDatastoreInUrl from DMI without topic #scenario.'() {
75 given: 'a cm handle for #cmHandleId'
76 mockYangModelCmHandleRetrieval(dmiProperties)
77 and: 'a positive response from DMI service when it is called with the expected parameters'
78 def responseFromDmi = new ResponseEntity<Object>(HttpStatus.OK)
79 def expectedUrl = dmiServiceBaseUrl + "${expectedDatastoreInUrl}?resourceIdentifier=${resourceIdentifier}${expectedOptionsInUrl}"
80 mockDmiRestClient.postOperationWithJsonData(expectedUrl, expectedJson, READ) >> responseFromDmi
81 dmiServiceUrlBuilder.getDmiDatastoreUrl(_, _) >> expectedUrl
82 when: 'get resource data is invoked'
83 def result = objectUnderTest.getResourceDataFromDmi(dataStore.datastoreName, cmHandleId, resourceIdentifier,
84 options, NO_TOPIC, NO_REQUEST_ID)
85 then: 'the result is the response from the DMI service'
86 assert result == responseFromDmi
87 where: 'the following parameters are used'
88 scenario | dmiProperties | dataStore | options || expectedJson | expectedDatastoreInUrl | expectedOptionsInUrl
89 'without properties' | [] | PASSTHROUGH_OPERATIONAL | OPTIONS_PARAM || '{"operation":"read","cmHandleProperties":{}}' | 'passthrough-operational' | '&options=(a=1,b=2)'
90 'with properties' | [yangModelCmHandleProperty] | PASSTHROUGH_OPERATIONAL | OPTIONS_PARAM || '{"operation":"read","cmHandleProperties":{"prop1":"val1"}}' | 'passthrough-operational' | '&options=(a=1,b=2)'
91 'null options' | [yangModelCmHandleProperty] | PASSTHROUGH_OPERATIONAL | null || '{"operation":"read","cmHandleProperties":{"prop1":"val1"}}' | 'passthrough-operational' | ''
92 'empty options' | [yangModelCmHandleProperty] | PASSTHROUGH_OPERATIONAL | '' || '{"operation":"read","cmHandleProperties":{"prop1":"val1"}}' | 'passthrough-operational' | ''
93 'datastore running without properties' | [] | PASSTHROUGH_RUNNING | OPTIONS_PARAM || '{"operation":"read","cmHandleProperties":{}}' | 'passthrough-running' | '&options=(a=1,b=2)'
94 'datastore running with properties' | [yangModelCmHandleProperty] | PASSTHROUGH_RUNNING | OPTIONS_PARAM || '{"operation":"read","cmHandleProperties":{"prop1":"val1"}}' | 'passthrough-running' | '&options=(a=1,b=2)'
97 def 'Execute (async) data operation from DMI service.'() {
98 given: 'collection of yang model cm Handles and data operation request'
99 mockYangModelCmHandleCollectionRetrieval([yangModelCmHandleProperty])
100 def dataOperationBatchRequestJsonData = TestUtils.getResourceFileContent('dataOperationRequest.json')
101 def dataOperationRequest = spiedJsonObjectMapper.convertJsonString(dataOperationBatchRequestJsonData, DataOperationRequest.class)
102 dataOperationRequest.dataOperationDefinitions[0].cmHandleIds = [cmHandleId]
103 and: 'a positive response from DMI service when it is called with valid request parameters'
104 def responseFromDmi = new ResponseEntity<Object>(HttpStatus.ACCEPTED)
105 def expectedDmiBatchResourceDataUrl = "ncmp/v1/data/topic=my-topic-name"
106 def expectedBatchRequestAsJson = '{"operations":[{"operation":"read","operationId":"operational-14","datastore":"ncmp-datastore:passthrough-operational","options":"some option","resourceIdentifier":"some resource identifier","cmHandles":[{"id":"some-cm-handle","cmHandleProperties":{"prop1":"val1"}}]}]}'
107 mockDmiRestClient.postOperationWithJsonData(expectedDmiBatchResourceDataUrl, _, READ.operationName) >> responseFromDmi
108 dmiServiceUrlBuilder.getDataOperationRequestUrl(_, _) >> expectedDmiBatchResourceDataUrl
109 and: ' a flag to track the post operation call'
110 def postOperationWithJsonDataMethodCalled = false
111 and: 'the (mocked) dmi rest client will use the flag to indicate it is called and capture the request body'
112 mockDmiRestClient.postOperationWithJsonData(expectedDmiBatchResourceDataUrl, expectedBatchRequestAsJson, READ) >> {
113 postOperationWithJsonDataMethodCalled = true
115 when: 'get resource data for group of cm handles are invoked'
116 objectUnderTest.requestResourceDataFromDmi('my-topic-name', dataOperationRequest, 'requestId')
117 then: 'validate the post operation was called and ncmp generated dmi request body json args'
118 new PollingConditions().within(1) {
119 assert postOperationWithJsonDataMethodCalled == true
123 def 'Execute (async) data operation from DMI service for #scenario.'() {
124 given: 'data operation request body and dmi resource url'
125 def dmiDataOperation = DmiDataOperation.builder().operationId('some-operation-id').build()
126 dmiDataOperation.getCmHandles().add(CmHandle.builder().id('some-cm-handle-id').build())
127 def dmiDataOperationResourceDataUrl = "http://dmi-service-name:dmi-port/dmi/v1/data?topic=my-topic-name&requestId=some-request-id"
128 def actualDataOperationCloudEvent = null
129 when: 'exception occurs after sending request to dmi service'
130 objectUnderTest.handleTaskCompletionException(new Throwable(exception), dmiDataOperationResourceDataUrl, List.of(dmiDataOperation))
131 then: 'a cloud event is published'
132 eventsPublisher.publishCloudEvent('my-topic-name', 'some-request-id', _) >> { args -> actualDataOperationCloudEvent = args[2] }
133 and: 'the event contains the expected error details'
134 def eventDataValue = extractDataValue(actualDataOperationCloudEvent)
135 assert eventDataValue.operationId == dmiDataOperation.operationId
136 assert eventDataValue.ids == dmiDataOperation.cmHandles.id
137 assert eventDataValue.statusCode == responseCode.code
138 assert eventDataValue.statusMessage == responseCode.message
139 where: 'the following exceptions are occurred'
140 scenario | exception || responseCode
141 'http client request exception' | new HttpClientRequestException('error-message', 'error-details', HttpStatus.SERVICE_UNAVAILABLE.value()) || UNABLE_TO_READ_RESOURCE_DATA
142 'timeout exception' | new TimeoutException() || DMI_SERVICE_NOT_RESPONDING
145 def 'call get all resource data.'() {
146 given: 'the system returns a cm handle with a sample property'
147 mockYangModelCmHandleRetrieval([yangModelCmHandleProperty])
148 and: 'a positive response from DMI service when it is called with the expected parameters'
149 def responseFromDmi = new ResponseEntity<Object>(HttpStatus.OK)
150 def expectedUrl = dmiServiceBaseUrl + "passthrough-operational?resourceIdentifier=/"
151 mockDmiRestClient.postOperationWithJsonData(expectedUrl, '{"operation":"read","cmHandleProperties":{"prop1":"val1"}}', READ) >> responseFromDmi
152 dmiServiceUrlBuilder.getDmiDatastoreUrl(_, _) >> expectedUrl
153 when: 'get resource data is invoked'
154 def result = objectUnderTest.getResourceDataFromDmi( PASSTHROUGH_OPERATIONAL.datastoreName, cmHandleId, NO_REQUEST_ID)
155 then: 'the result is the response from the DMI service'
156 assert result == responseFromDmi
159 def 'Write data for pass-through:running datastore in DMI.'() {
160 given: 'a cm handle for #cmHandleId'
161 mockYangModelCmHandleRetrieval([yangModelCmHandleProperty])
162 and: 'a positive response from DMI service when it is called with the expected parameters'
163 def expectedUrl = dmiServiceBaseUrl + "passthrough-running?resourceIdentifier=${resourceIdentifier}"
164 def expectedJson = '{"operation":"' + expectedOperationInUrl + '","dataType":"some data type","data":"requestData","cmHandleProperties":{"prop1":"val1"}}'
165 def responseFromDmi = new ResponseEntity<Object>(HttpStatus.OK)
166 dmiServiceUrlBuilder.getDmiDatastoreUrl(_, _) >> expectedUrl
167 mockDmiRestClient.postOperationWithJsonData(expectedUrl, expectedJson, operation) >> responseFromDmi
168 when: 'write resource method is invoked'
169 def result = objectUnderTest.writeResourceDataPassThroughRunningFromDmi(cmHandleId, 'parent/child', operation, 'requestData', 'some data type')
170 then: 'the result is the response from the DMI service'
171 assert result == responseFromDmi
172 where: 'the following operation is performed'
173 operation || expectedOperationInUrl
178 def extractDataValue(actualDataOperationCloudEvent) {
179 return toTargetEvent(actualDataOperationCloudEvent, DataOperationEvent.class).data.responses[0]