7873f39bbdee035486249c716280f2ef6db71d2e
[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.utils.JsonObjectMapper
26 import org.spockframework.spring.SpringBean
27 import org.springframework.beans.factory.annotation.Autowired
28 import org.springframework.boot.test.context.SpringBootTest
29 import org.springframework.http.ResponseEntity
30 import org.springframework.test.context.ContextConfiguration
31
32 import static org.onap.cps.ncmp.api.impl.operations.DmiOperations.DataStoreEnum.PASSTHROUGH_OPERATIONAL
33 import static org.onap.cps.ncmp.api.impl.operations.DmiOperations.DataStoreEnum.PASSTHROUGH_RUNNING
34 import static org.onap.cps.ncmp.api.impl.operations.DmiRequestBody.OperationEnum.CREATE
35 import static org.onap.cps.ncmp.api.impl.operations.DmiRequestBody.OperationEnum.UPDATE
36 import org.springframework.http.HttpStatus
37
38 @SpringBootTest
39 @ContextConfiguration(classes = [NcmpConfiguration.DmiProperties, DmiDataOperations])
40 class DmiDataOperationsSpec extends DmiOperationsBaseSpec {
41
42     @SpringBean
43     JsonObjectMapper jsonObjectMapper = new JsonObjectMapper(new ObjectMapper())
44
45     @Autowired
46     DmiDataOperations objectUnderTest
47
48     def 'call get resource data for #expectedDatastoreInUrl from DMI #scenario.'() {
49         given: 'a persistence cm handle for #cmHandleId'
50             mockPersistenceCmHandleRetrieval(dmiProperties)
51         and: 'a positive response from DMI service when it is called with the expected parameters'
52             def responseFromDmi = new ResponseEntity<Object>(HttpStatus.OK)
53             mockDmiRestClient.postOperationWithJsonData(
54                 "${dmiServiceName}/dmi/v1/ch/${cmHandleId}/data/ds/ncmp-datastore:${expectedDatastoreInUrl}?resourceIdentifier=${resourceIdentifier}${expectedOptionsInUrl}",
55                 expectedJson, [Accept:['sample accept header']]) >> responseFromDmi
56         when: 'get resource data is invoked'
57             def result = objectUnderTest.getResourceDataFromDmi(cmHandleId,resourceIdentifier, options,'sample accept header', dataStore)
58         then: 'the result is the response from the DMI service'
59             assert result == responseFromDmi
60         where: 'the following parameters are used'
61             scenario             | dmiProperties        | dataStore               | options     || expectedJson                                                 | expectedDatastoreInUrl    | expectedOptionsInUrl
62             'without properties' | []                   | PASSTHROUGH_OPERATIONAL | '(a=1,b=2)' || '{"operation":"read","cmHandleProperties":{}}'               | 'passthrough-operational' | '&options=(a=1,b=2)'
63             'with properties'    | [dmiSampleProperty]  | PASSTHROUGH_OPERATIONAL | '(a=1,b=2)' || '{"operation":"read","cmHandleProperties":{"prop1":"val1"}}' | 'passthrough-operational' | '&options=(a=1,b=2)'
64             'null options'       | [dmiSampleProperty]  | PASSTHROUGH_OPERATIONAL | null        || '{"operation":"read","cmHandleProperties":{"prop1":"val1"}}' | 'passthrough-operational' | ''
65             'empty options'      | [dmiSampleProperty]  | PASSTHROUGH_OPERATIONAL | ''          || '{"operation":"read","cmHandleProperties":{"prop1":"val1"}}' | 'passthrough-operational' | ''
66             'datastore running'  | []                   | PASSTHROUGH_RUNNING     | '(a=1,b=2)' || '{"operation":"read","cmHandleProperties":{}}'               | 'passthrough-running'     | '&options=(a=1,b=2)'
67     }
68
69     def 'Write data for pass-through:running datastore in DMI.'() {
70         given: 'a persistence cm handle for #cmHandleId'
71             mockPersistenceCmHandleRetrieval([dmiSampleProperty])
72         and: 'a positive response from DMI service when it is called with the expected parameters'
73             def expectedUrl = "${dmiServiceName}/dmi/v1/ch/${cmHandleId}/data/ds" +
74                 "/ncmp-datastore:passthrough-running?resourceIdentifier=${resourceIdentifier}"
75             def expectedJson = '{"operation":"' + expectedOperationInUrl + '","dataType":"some data type","data":"requestData","cmHandleProperties":{"prop1":"val1"}}'
76             def responseFromDmi = new ResponseEntity<Object>(HttpStatus.OK)
77             mockDmiRestClient.postOperationWithJsonData(expectedUrl, expectedJson, [:]) >> responseFromDmi
78         when: 'write resource method is invoked'
79             def result = objectUnderTest.writeResourceDataPassThroughRunningFromDmi(cmHandleId,'parent/child', operation, 'requestData', 'some data type')
80         then: 'the result is the response from the DMI service'
81             assert result == responseFromDmi
82         where: 'the following operation is performed'
83             operation || expectedOperationInUrl
84             CREATE    || 'create'
85             UPDATE    || 'update'
86     }
87
88 }