2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021-2022 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.utils.JsonObjectMapper
28 import org.spockframework.spring.SpringBean
29 import org.springframework.beans.factory.annotation.Autowired
30 import org.springframework.boot.test.context.SpringBootTest
31 import org.springframework.http.ResponseEntity
32 import org.springframework.test.context.ContextConfiguration
33 import spock.lang.Shared
35 import static org.onap.cps.ncmp.api.impl.operations.DmiOperations.DataStoreEnum.PASSTHROUGH_OPERATIONAL
36 import static org.onap.cps.ncmp.api.impl.operations.DmiOperations.DataStoreEnum.PASSTHROUGH_RUNNING
37 import static org.onap.cps.ncmp.api.impl.operations.DmiRequestBody.OperationEnum.CREATE
38 import static org.onap.cps.ncmp.api.impl.operations.DmiRequestBody.OperationEnum.READ
39 import static org.onap.cps.ncmp.api.impl.operations.DmiRequestBody.OperationEnum.UPDATE
40 import org.springframework.http.HttpStatus
43 @ContextConfiguration(classes = [NcmpConfiguration.DmiProperties, DmiDataOperations])
44 class DmiDataOperationsSpec extends DmiOperationsBaseSpec {
47 DmiServiceUrlBuilder dmiServiceUrlBuilder = Mock()
48 def dmiServiceBaseUrl = "${dmiServiceName}/dmi/v1/ch/${cmHandleId}/data/ds/ncmp-datastore:"
50 def NO_REQUEST_ID = null
52 def OPTIONS_PARAM = '(a=1,b=2)'
55 JsonObjectMapper spiedJsonObjectMapper = Spy(new JsonObjectMapper(new ObjectMapper()))
58 DmiDataOperations objectUnderTest
60 def 'call get resource data for #expectedDatastoreInUrl from DMI without topic #scenario.'() {
61 given: 'a cm handle for #cmHandleId'
62 mockYangModelCmHandleRetrieval(dmiProperties)
63 and: 'a positive response from DMI service when it is called with the expected parameters'
64 def responseFromDmi = new ResponseEntity<Object>(HttpStatus.OK)
65 def expectedUrl = dmiServiceBaseUrl + "${expectedDatastoreInUrl}?resourceIdentifier=${resourceIdentifier}${expectedOptionsInUrl}"
66 mockDmiRestClient.postOperationWithJsonData(expectedUrl, expectedJson, READ) >> responseFromDmi
67 dmiServiceUrlBuilder.getDmiDatastoreUrl(_, _) >> expectedUrl
68 when: 'get resource data is invoked'
69 def result = objectUnderTest.getResourceDataFromDmi(cmHandleId, resourceIdentifier,
70 options, dataStore, NO_REQUEST_ID, NO_TOPIC)
71 then: 'the result is the response from the DMI service'
72 assert result == responseFromDmi
73 where: 'the following parameters are used'
74 scenario | dmiProperties | dataStore | options || expectedJson | expectedDatastoreInUrl | expectedOptionsInUrl
75 'without properties' | [] | PASSTHROUGH_OPERATIONAL | OPTIONS_PARAM || '{"operation":"read","cmHandleProperties":{}}' | 'passthrough-operational' | '&options=(a=1,b=2)'
76 'with properties' | [yangModelCmHandleProperty] | PASSTHROUGH_OPERATIONAL | OPTIONS_PARAM || '{"operation":"read","cmHandleProperties":{"prop1":"val1"}}' | 'passthrough-operational' | '&options=(a=1,b=2)'
77 'null options' | [yangModelCmHandleProperty] | PASSTHROUGH_OPERATIONAL | null || '{"operation":"read","cmHandleProperties":{"prop1":"val1"}}' | 'passthrough-operational' | ''
78 'empty options' | [yangModelCmHandleProperty] | PASSTHROUGH_OPERATIONAL | '' || '{"operation":"read","cmHandleProperties":{"prop1":"val1"}}' | 'passthrough-operational' | ''
79 'datastore running without properties' | [] | PASSTHROUGH_RUNNING | OPTIONS_PARAM || '{"operation":"read","cmHandleProperties":{}}' | 'passthrough-running' | '&options=(a=1,b=2)'
80 'datastore running with properties' | [yangModelCmHandleProperty] | PASSTHROUGH_RUNNING | OPTIONS_PARAM || '{"operation":"read","cmHandleProperties":{"prop1":"val1"}}' | 'passthrough-running' | '&options=(a=1,b=2)'
83 def 'call get all resource data.'() {
84 given: 'the system returns a cm handle with a sample property'
85 mockYangModelCmHandleRetrieval([yangModelCmHandleProperty])
86 and: 'a positive response from DMI service when it is called with the expected parameters'
87 def responseFromDmi = new ResponseEntity<Object>(HttpStatus.OK)
88 def expectedUrl = dmiServiceBaseUrl + "passthrough-operational?resourceIdentifier=/"
89 mockDmiRestClient.postOperationWithJsonData(expectedUrl, '{"operation":"read","cmHandleProperties":{"prop1":"val1"}}', READ) >> responseFromDmi
90 dmiServiceUrlBuilder.getDmiDatastoreUrl(_, _) >> expectedUrl
91 when: 'get resource data is invoked'
92 def result = objectUnderTest.getResourceDataFromDmi(cmHandleId, PASSTHROUGH_OPERATIONAL, NO_REQUEST_ID)
93 then: 'the result is the response from the DMI service'
94 assert result == responseFromDmi
97 def 'Write data for pass-through:running datastore in DMI.'() {
98 given: 'a cm handle for #cmHandleId'
99 mockYangModelCmHandleRetrieval([yangModelCmHandleProperty])
100 and: 'a positive response from DMI service when it is called with the expected parameters'
101 def expectedUrl = dmiServiceBaseUrl + "passthrough-running?resourceIdentifier=${resourceIdentifier}"
102 def expectedJson = '{"operation":"' + expectedOperationInUrl + '","dataType":"some data type","data":"requestData","cmHandleProperties":{"prop1":"val1"}}'
103 def responseFromDmi = new ResponseEntity<Object>(HttpStatus.OK)
104 dmiServiceUrlBuilder.getDmiDatastoreUrl(_, _) >> expectedUrl
105 mockDmiRestClient.postOperationWithJsonData(expectedUrl, expectedJson, operation) >> responseFromDmi
106 when: 'write resource method is invoked'
107 def result = objectUnderTest.writeResourceDataPassThroughRunningFromDmi(cmHandleId, 'parent/child', operation, 'requestData', 'some data type')
108 then: 'the result is the response from the DMI service'
109 assert result == responseFromDmi
110 where: 'the following operation is performed'
111 operation || expectedOperationInUrl