2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021-2023 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.ncmp.api.impl.config.NcmpConfiguration
26 import org.onap.cps.ncmp.api.impl.events.EventsPublisher
27 import org.onap.cps.ncmp.api.impl.utils.DmiServiceUrlBuilder
28 import org.onap.cps.ncmp.api.impl.utils.context.CpsApplicationContext
29 import org.onap.cps.ncmp.api.models.DataOperationRequest
30 import org.onap.cps.ncmp.event.model.NcmpAsyncRequestResponseEvent
31 import org.onap.cps.ncmp.utils.TestUtils
32 import org.onap.cps.utils.JsonObjectMapper
33 import org.spockframework.spring.SpringBean
34 import org.springframework.beans.factory.annotation.Autowired
35 import org.springframework.boot.test.context.SpringBootTest
36 import org.springframework.http.ResponseEntity
37 import org.springframework.test.context.ContextConfiguration
38 import org.springframework.http.HttpStatus
39 import spock.lang.Shared
41 import static org.onap.cps.ncmp.api.impl.operations.DatastoreType.PASSTHROUGH_OPERATIONAL
42 import static org.onap.cps.ncmp.api.impl.operations.DatastoreType.PASSTHROUGH_RUNNING
43 import static org.onap.cps.ncmp.api.impl.operations.OperationType.CREATE
44 import static org.onap.cps.ncmp.api.impl.operations.OperationType.READ
45 import static org.onap.cps.ncmp.api.impl.operations.OperationType.UPDATE
48 @ContextConfiguration(classes = [EventsPublisher, CpsApplicationContext, NcmpConfiguration.DmiProperties, DmiDataOperations])
49 class DmiDataOperationsSpec extends DmiOperationsBaseSpec {
52 DmiServiceUrlBuilder dmiServiceUrlBuilder = Mock()
53 def dmiServiceBaseUrl = "${dmiServiceName}/dmi/v1/ch/${cmHandleId}/data/ds/ncmp-datastore:"
55 def NO_REQUEST_ID = null
57 def OPTIONS_PARAM = '(a=1,b=2)'
60 JsonObjectMapper spiedJsonObjectMapper = Spy(new JsonObjectMapper(new ObjectMapper()))
63 DmiDataOperations objectUnderTest
66 EventsPublisher eventsPublisher = Stub()
68 def 'call get resource data for #expectedDatastoreInUrl from DMI without topic #scenario.'() {
69 given: 'a cm handle for #cmHandleId'
70 mockYangModelCmHandleRetrieval(dmiProperties)
71 and: 'a positive response from DMI service when it is called with the expected parameters'
72 def responseFromDmi = new ResponseEntity<Object>(HttpStatus.OK)
73 def expectedUrl = dmiServiceBaseUrl + "${expectedDatastoreInUrl}?resourceIdentifier=${resourceIdentifier}${expectedOptionsInUrl}"
74 mockDmiRestClient.postOperationWithJsonData(expectedUrl, expectedJson, READ) >> responseFromDmi
75 dmiServiceUrlBuilder.getDmiDatastoreUrl(_, _) >> expectedUrl
76 when: 'get resource data is invoked'
77 def result = objectUnderTest.getResourceDataFromDmi(dataStore.datastoreName, cmHandleId, resourceIdentifier,
78 options, NO_TOPIC, NO_REQUEST_ID)
79 then: 'the result is the response from the DMI service'
80 assert result == responseFromDmi
81 where: 'the following parameters are used'
82 scenario | dmiProperties | dataStore | options || expectedJson | expectedDatastoreInUrl | expectedOptionsInUrl
83 'without properties' | [] | PASSTHROUGH_OPERATIONAL | OPTIONS_PARAM || '{"operation":"read","cmHandleProperties":{}}' | 'passthrough-operational' | '&options=(a=1,b=2)'
84 'with properties' | [yangModelCmHandleProperty] | PASSTHROUGH_OPERATIONAL | OPTIONS_PARAM || '{"operation":"read","cmHandleProperties":{"prop1":"val1"}}' | 'passthrough-operational' | '&options=(a=1,b=2)'
85 'null options' | [yangModelCmHandleProperty] | PASSTHROUGH_OPERATIONAL | null || '{"operation":"read","cmHandleProperties":{"prop1":"val1"}}' | 'passthrough-operational' | ''
86 'empty options' | [yangModelCmHandleProperty] | PASSTHROUGH_OPERATIONAL | '' || '{"operation":"read","cmHandleProperties":{"prop1":"val1"}}' | 'passthrough-operational' | ''
87 'datastore running without properties' | [] | PASSTHROUGH_RUNNING | OPTIONS_PARAM || '{"operation":"read","cmHandleProperties":{}}' | 'passthrough-running' | '&options=(a=1,b=2)'
88 'datastore running with properties' | [yangModelCmHandleProperty] | PASSTHROUGH_RUNNING | OPTIONS_PARAM || '{"operation":"read","cmHandleProperties":{"prop1":"val1"}}' | 'passthrough-running' | '&options=(a=1,b=2)'
91 def 'Execute (async) data operation from DMI service.'() {
92 given: 'collection of yang model cm Handles and data operation request'
93 mockYangModelCmHandleCollectionRetrieval([yangModelCmHandleProperty])
94 def dataOperationBatchRequestJsonData = TestUtils.getResourceFileContent('dataOperationRequest.json')
95 def dataOperationRequest = spiedJsonObjectMapper.convertJsonString(dataOperationBatchRequestJsonData, DataOperationRequest.class)
96 dataOperationRequest.dataOperationDefinitions[0].cmHandleIds = [cmHandleId]
97 def requestBodyAsJsonStringArg = null
98 and: 'a positive response from DMI service when it is called with valid request parameters'
99 def responseFromDmi = new ResponseEntity<Object>(HttpStatus.ACCEPTED)
100 def expectedDmiBatchResourceDataUrl = "ncmp/v1/data/topic=my-topic-name"
101 def expectedBatchRequestAsJson = '[{"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"}}]}]'
102 mockDmiRestClient.postOperationWithJsonData(expectedDmiBatchResourceDataUrl, _, READ.operationName) >> responseFromDmi
103 dmiServiceUrlBuilder.getDataOperationRequestUrl(_, _) >> expectedDmiBatchResourceDataUrl
104 when: 'get resource data for group of cm handles are invoked'
105 objectUnderTest.requestResourceDataFromDmi('my-topic-name', dataOperationRequest, 'requestId')
106 then: 'wait a little to allow execution of service method by task executor (on separate thread)'
108 then: 'validate ncmp generated dmi request body json args'
109 1 * mockDmiRestClient.postOperationWithJsonData(expectedDmiBatchResourceDataUrl, _, READ) >> { args -> requestBodyAsJsonStringArg = args[1] }
110 assert requestBodyAsJsonStringArg == expectedBatchRequestAsJson
113 def 'call get all resource data.'() {
114 given: 'the system returns a cm handle with a sample property'
115 mockYangModelCmHandleRetrieval([yangModelCmHandleProperty])
116 and: 'a positive response from DMI service when it is called with the expected parameters'
117 def responseFromDmi = new ResponseEntity<Object>(HttpStatus.OK)
118 def expectedUrl = dmiServiceBaseUrl + "passthrough-operational?resourceIdentifier=/"
119 mockDmiRestClient.postOperationWithJsonData(expectedUrl, '{"operation":"read","cmHandleProperties":{"prop1":"val1"}}', READ) >> responseFromDmi
120 dmiServiceUrlBuilder.getDmiDatastoreUrl(_, _) >> expectedUrl
121 when: 'get resource data is invoked'
122 def result = objectUnderTest.getResourceDataFromDmi( PASSTHROUGH_OPERATIONAL.datastoreName, cmHandleId, NO_REQUEST_ID)
123 then: 'the result is the response from the DMI service'
124 assert result == responseFromDmi
127 def 'Write data for pass-through:running datastore in DMI.'() {
128 given: 'a cm handle for #cmHandleId'
129 mockYangModelCmHandleRetrieval([yangModelCmHandleProperty])
130 and: 'a positive response from DMI service when it is called with the expected parameters'
131 def expectedUrl = dmiServiceBaseUrl + "passthrough-running?resourceIdentifier=${resourceIdentifier}"
132 def expectedJson = '{"operation":"' + expectedOperationInUrl + '","dataType":"some data type","data":"requestData","cmHandleProperties":{"prop1":"val1"}}'
133 def responseFromDmi = new ResponseEntity<Object>(HttpStatus.OK)
134 dmiServiceUrlBuilder.getDmiDatastoreUrl(_, _) >> expectedUrl
135 mockDmiRestClient.postOperationWithJsonData(expectedUrl, expectedJson, operation) >> responseFromDmi
136 when: 'write resource method is invoked'
137 def result = objectUnderTest.writeResourceDataPassThroughRunningFromDmi(cmHandleId, 'parent/child', operation, 'requestData', 'some data type')
138 then: 'the result is the response from the DMI service'
139 assert result == responseFromDmi
140 where: 'the following operation is performed'
141 operation || expectedOperationInUrl