54ae0aa6fec5a6298345cc33b42fe2c045763970
[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(additionalProperties)
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             | additionalProperties       | 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             'null properties'    | null                       | PASSTHROUGH_OPERATIONAL | '(a=1,b=2)' || '{"operation":"read","cmHandleProperties":{}}'               | 'passthrough-operational' | '&options=(a=1,b=2)'
64             'with properties'    | [sampleAdditionalProperty] | PASSTHROUGH_OPERATIONAL | '(a=1,b=2)' || '{"operation":"read","cmHandleProperties":{"prop1":"val1"}}' | 'passthrough-operational' | '&options=(a=1,b=2)'
65             'null options'       | [sampleAdditionalProperty] | PASSTHROUGH_OPERATIONAL | null        || '{"operation":"read","cmHandleProperties":{"prop1":"val1"}}' | 'passthrough-operational' | ''
66             'empty options'      | [sampleAdditionalProperty] | PASSTHROUGH_OPERATIONAL | ''          || '{"operation":"read","cmHandleProperties":{"prop1":"val1"}}' | 'passthrough-operational' | ''
67             'datastore running'  | []                         | PASSTHROUGH_RUNNING     | '(a=1,b=2)' || '{"operation":"read","cmHandleProperties":{}}'               | 'passthrough-running'     | '&options=(a=1,b=2)'
68     }
69
70     def 'Write data for pass-through:running datastore in DMI.'() {
71         given: 'a persistence cm handle for #cmHandleId'
72             mockPersistenceCmHandleRetrieval([sampleAdditionalProperty])
73         and: 'a positive response from dmi service when it is called with the expected parameters'
74             def expectedUrl = "${dmiServiceName}/dmi/v1/ch/${cmHandleId}/data/ds" +
75                 "/ncmp-datastore:passthrough-running?resourceIdentifier=${resourceIdentifier}"
76             def expectedJson = '{"operation":"' + expectedOperationInUrl + '","dataType":"some data type","data":"requestData","cmHandleProperties":{"prop1":"val1"}}'
77             def responseFromDmi = new ResponseEntity<Object>(HttpStatus.OK)
78             mockDmiRestClient.postOperationWithJsonData(expectedUrl, expectedJson, [:]) >> responseFromDmi
79         when: 'write resource method is invoked'
80             def result = objectUnderTest.writeResourceDataPassThroughRunningFromDmi(cmHandleId,'parent/child', operation, 'requestData', 'some data type')
81         then: 'the result is the response from the dmi service'
82             assert result == responseFromDmi
83         where: 'the following operation is performed'
84             operation || expectedOperationInUrl
85             CREATE    || 'create'
86             UPDATE    || 'update'
87     }
88
89 }