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