3df862ac5c21fe223886b89ae59780805e8fa121
[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  *  ================================================================================
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
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  *  SPDX-License-Identifier: Apache-2.0
18  *  ============LICENSE_END=========================================================
19  */
20
21 package org.onap.cps.ncmp.api.impl.operations
22
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
34
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
40
41 @SpringBootTest
42 @ContextConfiguration(classes = [NcmpConfiguration.DmiProperties, DmiDataOperations])
43 class DmiDataOperationsSpec extends DmiOperationsBaseSpec {
44
45     @SpringBean
46     DmiServiceUrlBuilder dmiServiceUrlBuilder = Mock()
47     def dmiServiceBaseUrl = "${dmiServiceName}/dmi/v1/ch/${cmHandleId}/data/ds/ncmp-datastore:"
48     def NO_TOPIC = null
49     def NO_REQUEST_ID = null
50     @Shared
51     def OPTIONS_PARAM = '(a=1,b=2)'
52
53     @SpringBean
54     JsonObjectMapper spiedJsonObjectMapper = Spy(new JsonObjectMapper(new ObjectMapper()))
55
56     @Autowired
57     DmiDataOperations objectUnderTest
58
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)'
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 }