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