2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021-2022 Nordix Foundation
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.cps.ncmp.api.impl.operations
23 import com.fasterxml.jackson.databind.ObjectMapper
24 import org.onap.cps.ncmp.api.impl.config.NcmpConfiguration
25 import org.onap.cps.ncmp.api.impl.utils.DmiServiceUrlBuilder
26 import org.onap.cps.utils.JsonObjectMapper
27 import org.spockframework.spring.SpringBean
28 import org.springframework.beans.factory.annotation.Autowired
29 import org.springframework.boot.test.context.SpringBootTest
30 import org.springframework.http.ResponseEntity
31 import org.springframework.test.context.ContextConfiguration
32 import org.springframework.util.MultiValueMap
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.UPDATE
39 import org.springframework.http.HttpStatus
42 @ContextConfiguration(classes = [NcmpConfiguration.DmiProperties, DmiDataOperations])
43 class DmiDataOperationsSpec extends DmiOperationsBaseSpec {
46 DmiServiceUrlBuilder dmiServiceUrlBuilder = Mock()
47 def dmiServiceBaseUrl = "${dmiServiceName}/dmi/v1/ch/${cmHandleId}/data/ds/ncmp-datastore:"
49 def NO_REQUEST_ID = null
51 def OPTIONS_PARAM = '(a=1,b=2)'
54 JsonObjectMapper spiedJsonObjectMapper = Spy(new JsonObjectMapper(new ObjectMapper()))
57 DmiDataOperations objectUnderTest
59 def 'call get resource data for #expectedDatastoreInUrl from DMI without topic #scenario.'() {
60 given: 'a cm handle for #cmHandleId'
61 mockYangModelCmHandleRetrieval(dmiProperties)
62 and: 'a positive response from DMI service when it is called with the expected parameters'
63 def responseFromDmi = new ResponseEntity<Object>(HttpStatus.OK)
64 def expectedUrl = dmiServiceBaseUrl + "${expectedDatastoreInUrl}?resourceIdentifier=${resourceIdentifier}${expectedOptionsInUrl}"
65 mockDmiRestClient.postOperationWithJsonData(expectedUrl,
66 expectedJson, [Accept: ['sample accept header']]) >> responseFromDmi
67 dmiServiceUrlBuilder.getDmiDatastoreUrl(_, _) >> expectedUrl
68 when: 'get resource data is invoked'
69 def result = objectUnderTest.getResourceDataFromDmi(cmHandleId, resourceIdentifier,
70 options, 'sample accept header', 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 'Write data for pass-through:running datastore in DMI.'() {
84 given: 'a cm handle for #cmHandleId'
85 mockYangModelCmHandleRetrieval([yangModelCmHandleProperty])
86 and: 'a positive response from DMI service when it is called with the expected parameters'
87 def expectedUrl = dmiServiceBaseUrl + "passthrough-running?resourceIdentifier=${resourceIdentifier}"
88 def expectedJson = '{"operation":"' + expectedOperationInUrl + '","dataType":"some data type","data":"requestData","cmHandleProperties":{"prop1":"val1"}}'
89 def responseFromDmi = new ResponseEntity<Object>(HttpStatus.OK)
90 dmiServiceUrlBuilder.getDmiDatastoreUrl(_, _) >> expectedUrl
91 mockDmiRestClient.postOperationWithJsonData(expectedUrl, expectedJson, [:]) >> responseFromDmi
92 when: 'write resource method is invoked'
93 def result = objectUnderTest.writeResourceDataPassThroughRunningFromDmi(cmHandleId, 'parent/child', operation, 'requestData', 'some data type')
94 then: 'the result is the response from the DMI service'
95 assert result == responseFromDmi
96 where: 'the following operation is performed'
97 operation || expectedOperationInUrl