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 com.fasterxml.jackson.databind.ObjectMapper
25 import org.onap.cps.events.EventsPublisher
26 import org.onap.cps.ncmp.api.impl.config.DmiWebClientConfiguration
27 import org.onap.cps.ncmp.api.impl.exception.HttpClientRequestException
28 import org.onap.cps.ncmp.api.impl.utils.DmiServiceUrlBuilder
29 import org.onap.cps.ncmp.api.impl.utils.context.CpsApplicationContext
30 import org.onap.cps.ncmp.api.models.DataOperationRequest
31 import org.onap.cps.ncmp.api.models.CmResourceAddress
32 import org.onap.cps.ncmp.events.async1_0_0.DataOperationEvent
33 import org.onap.cps.ncmp.utils.TestUtils
34 import org.onap.cps.utils.JsonObjectMapper
35 import org.spockframework.spring.SpringBean
36 import org.springframework.beans.factory.annotation.Autowired
37 import org.springframework.boot.test.context.SpringBootTest
38 import org.springframework.http.HttpStatus
39 import org.springframework.http.ResponseEntity
40 import org.springframework.test.context.ContextConfiguration
41 import spock.lang.Shared
43 import java.util.concurrent.TimeoutException
45 import static org.onap.cps.ncmp.api.NcmpResponseStatus.DMI_SERVICE_NOT_RESPONDING
46 import static org.onap.cps.ncmp.api.NcmpResponseStatus.UNABLE_TO_READ_RESOURCE_DATA
47 import static org.onap.cps.ncmp.api.impl.events.mapper.CloudEventMapper.toTargetEvent
48 import static org.onap.cps.ncmp.api.impl.operations.DatastoreType.PASSTHROUGH_OPERATIONAL
49 import static org.onap.cps.ncmp.api.impl.operations.DatastoreType.PASSTHROUGH_RUNNING
50 import static org.onap.cps.ncmp.api.impl.operations.OperationType.CREATE
51 import static org.onap.cps.ncmp.api.impl.operations.OperationType.READ
52 import static org.onap.cps.ncmp.api.impl.operations.OperationType.UPDATE
55 @ContextConfiguration(classes = [EventsPublisher, CpsApplicationContext, DmiWebClientConfiguration.DmiProperties, DmiDataOperations])
56 class DmiDataOperationsSpec extends DmiOperationsBaseSpec {
59 DmiServiceUrlBuilder dmiServiceUrlBuilder = Mock()
60 def dmiServiceBaseUrl = "${dmiServiceName}/dmi/v1/ch/${cmHandleId}/data/ds/ncmp-datastore:"
62 def NO_REQUEST_ID = null
63 def NO_AUTH_HEADER = null
65 def OPTIONS_PARAM = '(a=1,b=2)'
68 JsonObjectMapper spiedJsonObjectMapper = Spy(new JsonObjectMapper(new ObjectMapper()))
71 DmiDataOperations objectUnderTest
74 EventsPublisher eventsPublisher = Stub()
76 def 'call get resource data for #expectedDatastoreInUrl from DMI without topic #scenario.'() {
77 given: 'a cm handle for #cmHandleId'
78 mockYangModelCmHandleRetrieval(dmiProperties)
79 and: 'a positive response from DMI service when it is called with the expected parameters'
80 def responseFromDmi = new ResponseEntity<Object>(HttpStatus.OK)
81 def expectedUrl = dmiServiceBaseUrl + "${expectedDatastoreInUrl}?resourceIdentifier=${resourceIdentifier}${expectedOptionsInUrl}"
82 mockDmiRestClient.postOperationWithJsonData(expectedUrl, expectedJson, READ, NO_AUTH_HEADER) >> responseFromDmi
83 dmiServiceUrlBuilder.getDmiDatastoreUrl(_, _) >> expectedUrl
84 when: 'get resource data is invoked'
85 def cmResourceAddress = new CmResourceAddress(dataStore.datastoreName, cmHandleId, resourceIdentifier)
86 def result = objectUnderTest.getResourceDataFromDmi(cmResourceAddress, options, NO_TOPIC, NO_REQUEST_ID, NO_AUTH_HEADER)
87 then: 'the result is the response from the DMI service'
88 assert result == responseFromDmi
89 where: 'the following parameters are used'
90 scenario | dmiProperties | dataStore | options || expectedJson | expectedDatastoreInUrl | expectedOptionsInUrl
91 'without properties' | [] | PASSTHROUGH_OPERATIONAL | OPTIONS_PARAM || '{"operation":"read","cmHandleProperties":{}}' | 'passthrough-operational' | '&options=(a=1,b=2)'
92 'with properties' | [yangModelCmHandleProperty] | PASSTHROUGH_OPERATIONAL | OPTIONS_PARAM || '{"operation":"read","cmHandleProperties":{"prop1":"val1"}}' | 'passthrough-operational' | '&options=(a=1,b=2)'
93 'null options' | [yangModelCmHandleProperty] | PASSTHROUGH_OPERATIONAL | null || '{"operation":"read","cmHandleProperties":{"prop1":"val1"}}' | 'passthrough-operational' | ''
94 'empty options' | [yangModelCmHandleProperty] | PASSTHROUGH_OPERATIONAL | '' || '{"operation":"read","cmHandleProperties":{"prop1":"val1"}}' | 'passthrough-operational' | ''
95 'datastore running without properties' | [] | PASSTHROUGH_RUNNING | OPTIONS_PARAM || '{"operation":"read","cmHandleProperties":{}}' | 'passthrough-running' | '&options=(a=1,b=2)'
96 'datastore running with properties' | [yangModelCmHandleProperty] | PASSTHROUGH_RUNNING | OPTIONS_PARAM || '{"operation":"read","cmHandleProperties":{"prop1":"val1"}}' | 'passthrough-running' | '&options=(a=1,b=2)'
99 def 'Execute (async) data operation from DMI service.'() {
100 given: 'collection of yang model cm Handles and data operation request'
101 mockYangModelCmHandleCollectionRetrieval([yangModelCmHandleProperty])
102 def dataOperationBatchRequestJsonData = TestUtils.getResourceFileContent('dataOperationRequest.json')
103 def dataOperationRequest = spiedJsonObjectMapper.convertJsonString(dataOperationBatchRequestJsonData, DataOperationRequest.class)
104 dataOperationRequest.dataOperationDefinitions[0].cmHandleIds = [cmHandleId]
105 and: 'a positive response from DMI service when it is called with valid request parameters'
106 def responseFromDmi = new ResponseEntity<Object>(HttpStatus.ACCEPTED)
107 def expectedDmiBatchResourceDataUrl = "ncmp/v1/data/topic=my-topic-name"
108 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"}}]}]}'
109 mockDmiRestClient.postOperationWithJsonData(expectedDmiBatchResourceDataUrl, _, READ.operationName, NO_AUTH_HEADER) >> responseFromDmi
110 dmiServiceUrlBuilder.getDataOperationRequestUrl(_, _) >> expectedDmiBatchResourceDataUrl
111 when: 'get resource data for group of cm handles are invoked'
112 objectUnderTest.requestResourceDataFromDmi('my-topic-name', dataOperationRequest, 'requestId', NO_AUTH_HEADER)
113 then: 'the post operation was called and ncmp generated dmi request body json args'
114 1 * mockDmiRestClient.postOperationWithJsonData(expectedDmiBatchResourceDataUrl, expectedBatchRequestAsJson, READ, NO_AUTH_HEADER)
117 def 'Execute (async) data operation from DMI service for #scenario.'() {
118 given: 'data operation request body and dmi resource url'
119 def dmiDataOperation = DmiDataOperation.builder().operationId('some-operation-id').build()
120 dmiDataOperation.getCmHandles().add(CmHandle.builder().id('some-cm-handle-id').build())
121 def dmiDataOperationResourceDataUrl = "http://dmi-service-name:dmi-port/dmi/v1/data?topic=my-topic-name&requestId=some-request-id"
122 def actualDataOperationCloudEvent = null
123 when: 'exception occurs after sending request to dmi service'
124 objectUnderTest.handleTaskCompletionException(new Throwable(exception), dmiDataOperationResourceDataUrl, List.of(dmiDataOperation))
125 then: 'a cloud event is published'
126 eventsPublisher.publishCloudEvent('my-topic-name', 'some-request-id', _) >> { args -> actualDataOperationCloudEvent = args[2] }
127 and: 'the event contains the expected error details'
128 def eventDataValue = extractDataValue(actualDataOperationCloudEvent)
129 assert eventDataValue.operationId == dmiDataOperation.operationId
130 assert eventDataValue.ids == dmiDataOperation.cmHandles.id
131 assert eventDataValue.statusCode == responseCode.code
132 assert eventDataValue.statusMessage == responseCode.message
133 where: 'the following exceptions are occurred'
134 scenario | exception || responseCode
135 'http client request exception' | new HttpClientRequestException('error-message', 'error-details', HttpStatus.SERVICE_UNAVAILABLE.value()) || UNABLE_TO_READ_RESOURCE_DATA
136 'timeout exception' | new TimeoutException() || DMI_SERVICE_NOT_RESPONDING
139 def 'call get all resource data.'() {
140 given: 'the system returns a cm handle with a sample property'
141 mockYangModelCmHandleRetrieval([yangModelCmHandleProperty])
142 and: 'a positive response from DMI service when it is called with the expected parameters'
143 def responseFromDmi = new ResponseEntity<Object>(HttpStatus.OK)
144 def expectedUrl = dmiServiceBaseUrl + "passthrough-operational?resourceIdentifier=/"
145 mockDmiRestClient.postOperationWithJsonData(expectedUrl, '{"operation":"read","cmHandleProperties":{"prop1":"val1"}}', READ, null) >> responseFromDmi
146 dmiServiceUrlBuilder.getDmiDatastoreUrl(_, _) >> expectedUrl
147 when: 'get resource data is invoked'
148 def result = objectUnderTest.getResourceDataFromDmi( PASSTHROUGH_OPERATIONAL.datastoreName, cmHandleId, NO_REQUEST_ID)
149 then: 'the result is the response from the DMI service'
150 assert result == responseFromDmi
153 def 'Write data for pass-through:running datastore in DMI.'() {
154 given: 'a cm handle for #cmHandleId'
155 mockYangModelCmHandleRetrieval([yangModelCmHandleProperty])
156 and: 'a positive response from DMI service when it is called with the expected parameters'
157 def expectedUrl = dmiServiceBaseUrl + "passthrough-running?resourceIdentifier=${resourceIdentifier}"
158 def expectedJson = '{"operation":"' + expectedOperationInUrl + '","dataType":"some data type","data":"requestData","cmHandleProperties":{"prop1":"val1"}}'
159 def responseFromDmi = new ResponseEntity<Object>(HttpStatus.OK)
160 dmiServiceUrlBuilder.getDmiDatastoreUrl(_, _) >> expectedUrl
161 mockDmiRestClient.postOperationWithJsonData(expectedUrl, expectedJson, operation, NO_AUTH_HEADER) >> responseFromDmi
162 when: 'write resource method is invoked'
163 def result = objectUnderTest.writeResourceDataPassThroughRunningFromDmi(cmHandleId, 'parent/child', operation, 'requestData', 'some data type', NO_AUTH_HEADER)
164 then: 'the result is the response from the DMI service'
165 assert result == responseFromDmi
166 where: 'the following operation is performed'
167 operation || expectedOperationInUrl
172 def extractDataValue(actualDataOperationCloudEvent) {
173 return toTargetEvent(actualDataOperationCloudEvent, DataOperationEvent.class).data.responses[0]