[cps] Fix getResourceDataForPassthroughOperational endpoint
[cps.git] / cps-ncmp-service / src / test / groovy / org / onap / cps / ncmp / api / impl / operations / DmiDataOperationsSpec.groovy
1 /*
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
9  *
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  *  SPDX-License-Identifier: Apache-2.0
19  *  ============LICENSE_END=========================================================
20  */
21
22 package org.onap.cps.ncmp.api.impl.operations
23
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 org.springframework.util.MultiValueMap
34 import spock.lang.Shared
35
36 import static org.onap.cps.ncmp.api.impl.operations.DmiOperations.DataStoreEnum.PASSTHROUGH_OPERATIONAL
37 import static org.onap.cps.ncmp.api.impl.operations.DmiOperations.DataStoreEnum.PASSTHROUGH_RUNNING
38 import static org.onap.cps.ncmp.api.impl.operations.DmiRequestBody.OperationEnum.CREATE
39 import static org.onap.cps.ncmp.api.impl.operations.DmiRequestBody.OperationEnum.UPDATE
40 import org.springframework.http.HttpStatus
41
42 @SpringBootTest
43 @ContextConfiguration(classes = [NcmpConfiguration.DmiProperties, DmiDataOperations])
44 class DmiDataOperationsSpec extends DmiOperationsBaseSpec {
45
46     @SpringBean
47     DmiServiceUrlBuilder dmiServiceUrlBuilder = Mock()
48     def dmiServiceBaseUrl = "${dmiServiceName}/dmi/v1/ch/${cmHandleId}/data/ds/ncmp-datastore:"
49     def NO_TOPIC = null
50     def NO_REQUEST_ID = null
51     @Shared
52     def OPTIONS_PARAM = '(a=1,b=2)'
53
54     @SpringBean
55     JsonObjectMapper spiedJsonObjectMapper = Spy(new JsonObjectMapper(new ObjectMapper()))
56
57     @Autowired
58     DmiDataOperations objectUnderTest
59
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) >> 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)'
81     }
82
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
98             CREATE    || 'create'
99             UPDATE    || 'update'
100     }
101 }