89b3a2ff26724603d6e1ee2d164b83201802c3a9
[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-2023 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.http.HttpStatus
34 import spock.lang.Shared
35
36 import static org.onap.cps.ncmp.api.impl.operations.DataStoreEnum.PASSTHROUGH_OPERATIONAL
37 import static org.onap.cps.ncmp.api.impl.operations.DataStoreEnum.PASSTHROUGH_RUNNING
38 import static org.onap.cps.ncmp.api.impl.operations.OperationEnum.CREATE
39 import static org.onap.cps.ncmp.api.impl.operations.OperationEnum.READ
40 import static org.onap.cps.ncmp.api.impl.operations.OperationEnum.UPDATE
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     @Shared
54     def expectedBulkRequestAsJson = '{"operation": "read","data": {"fe1c1f1a070c4ce5bbfda7198592a0d3": {"neType": "RadioNode"},"b8e42eed0d9541ed8d8839e8eb86b3e0": {"neType": "RadioNode"}},"requestId": "bbb67474-f705-410a-93d1-b2844e7f45fd"}'
55
56     @SpringBean
57     JsonObjectMapper spiedJsonObjectMapper = Spy(new JsonObjectMapper(new ObjectMapper()))
58
59     @Autowired
60     DmiDataOperations objectUnderTest
61
62     def 'call get resource data for #expectedDatastoreInUrl from DMI without topic #scenario.'() {
63         given: 'a cm handle for #cmHandleId'
64             mockYangModelCmHandleRetrieval(dmiProperties)
65         and: 'a positive response from DMI service when it is called with the expected parameters'
66             def responseFromDmi = new ResponseEntity<Object>(HttpStatus.OK)
67             def expectedUrl = dmiServiceBaseUrl + "${expectedDatastoreInUrl}?resourceIdentifier=${resourceIdentifier}${expectedOptionsInUrl}"
68             mockDmiRestClient.postOperationWithJsonData(expectedUrl, expectedJson, READ) >> responseFromDmi
69             dmiServiceUrlBuilder.getDmiDatastoreUrl(_, _) >> expectedUrl
70         when: 'get resource data is invoked'
71             def result = objectUnderTest.getResourceDataFromDmi(dataStore.value, cmHandleId, resourceIdentifier,
72                     options, NO_TOPIC, NO_REQUEST_ID)
73         then: 'the result is the response from the DMI service'
74             assert result == responseFromDmi
75         where: 'the following parameters are used'
76             scenario                               | dmiProperties               | dataStore               | options       || expectedJson                                                 | expectedDatastoreInUrl    | expectedOptionsInUrl
77             'without properties'                   | []                          | PASSTHROUGH_OPERATIONAL | OPTIONS_PARAM || '{"operation":"read","cmHandleProperties":{}}'               | 'passthrough-operational' | '&options=(a=1,b=2)'
78             'with properties'                      | [yangModelCmHandleProperty] | PASSTHROUGH_OPERATIONAL | OPTIONS_PARAM || '{"operation":"read","cmHandleProperties":{"prop1":"val1"}}' | 'passthrough-operational' | '&options=(a=1,b=2)'
79             'null options'                         | [yangModelCmHandleProperty] | PASSTHROUGH_OPERATIONAL | null          || '{"operation":"read","cmHandleProperties":{"prop1":"val1"}}' | 'passthrough-operational' | ''
80             'empty options'                        | [yangModelCmHandleProperty] | PASSTHROUGH_OPERATIONAL | ''            || '{"operation":"read","cmHandleProperties":{"prop1":"val1"}}' | 'passthrough-operational' | ''
81             'datastore running without properties' | []                          | PASSTHROUGH_RUNNING     | OPTIONS_PARAM || '{"operation":"read","cmHandleProperties":{}}'               | 'passthrough-running'     | '&options=(a=1,b=2)'
82             'datastore running with properties'    | [yangModelCmHandleProperty] | PASSTHROUGH_RUNNING     | OPTIONS_PARAM || '{"operation":"read","cmHandleProperties":{"prop1":"val1"}}' | 'passthrough-running'     | '&options=(a=1,b=2)'
83     }
84
85     def 'call get bulk resource data for #dataStore from DMI service with topic #scenario.'() {
86         given: 'collection of yang model cm Handles'
87             mockYangModelCmHandleCollectionRetrieval([yangModelCmHandleProperty])
88         and: 'a positive response from DMI service when it is called with the expected parameters'
89             def responseFromDmi = new ResponseEntity<Object>(HttpStatus.ACCEPTED)
90             def expectedDmiBulkResourceDataUrl = "ncmp/v1/batch/data/ds/${dataStore}?resourceIdentifier=parent/child%26options=(a=1,b=2)&topic=my-topic-name&options=(fields=schemas/schema)"
91             mockDmiRestClient.postOperationWithJsonData(expectedDmiBulkResourceDataUrl, expectedBulkRequestAsJson, READ) >> responseFromDmi
92             dmiServiceUrlBuilder.getBulkRequestUrl(_, _) >> expectedDmiBulkResourceDataUrl
93         when: 'get resource data for bulk cm handle is invoked'
94             def result = objectUnderTest.getResourceDataFromDmi( dataStore.value, [cmHandleId], resourceIdentifier,
95                     OPTIONS_PARAM,  'some-topic','requestId')
96         then: 'the result is the response from the DMI service'
97             assert result == responseFromDmi
98         where: 'the following parameters are used'
99             scenario                | dataStore
100             'datastore operational' | PASSTHROUGH_OPERATIONAL
101             'datastore running'     | PASSTHROUGH_RUNNING
102     }
103
104     def 'call get all resource data.'() {
105         given: 'the system returns a cm handle with a sample property'
106             mockYangModelCmHandleRetrieval([yangModelCmHandleProperty])
107         and: 'a positive response from DMI service when it is called with the expected parameters'
108             def responseFromDmi = new ResponseEntity<Object>(HttpStatus.OK)
109             def expectedUrl = dmiServiceBaseUrl + "passthrough-operational?resourceIdentifier=/"
110             mockDmiRestClient.postOperationWithJsonData(expectedUrl, '{"operation":"read","cmHandleProperties":{"prop1":"val1"}}', READ) >> responseFromDmi
111             dmiServiceUrlBuilder.getDmiDatastoreUrl(_, _) >> expectedUrl
112         when: 'get resource data is invoked'
113             def result = objectUnderTest.getResourceDataFromDmi( PASSTHROUGH_OPERATIONAL.value, cmHandleId, NO_REQUEST_ID)
114         then: 'the result is the response from the DMI service'
115             assert result == responseFromDmi
116     }
117
118     def 'Write data for pass-through:running datastore in DMI.'() {
119         given: 'a cm handle for #cmHandleId'
120             mockYangModelCmHandleRetrieval([yangModelCmHandleProperty])
121         and: 'a positive response from DMI service when it is called with the expected parameters'
122             def expectedUrl = dmiServiceBaseUrl + "passthrough-running?resourceIdentifier=${resourceIdentifier}"
123             def expectedJson = '{"operation":"' + expectedOperationInUrl + '","dataType":"some data type","data":"requestData","cmHandleProperties":{"prop1":"val1"}}'
124             def responseFromDmi = new ResponseEntity<Object>(HttpStatus.OK)
125             dmiServiceUrlBuilder.getDmiDatastoreUrl(_, _) >> expectedUrl
126             mockDmiRestClient.postOperationWithJsonData(expectedUrl, expectedJson, operation) >> responseFromDmi
127         when: 'write resource method is invoked'
128             def result = objectUnderTest.writeResourceDataPassThroughRunningFromDmi(cmHandleId, 'parent/child', operation, 'requestData', 'some data type')
129         then: 'the result is the response from the DMI service'
130             assert result == responseFromDmi
131         where: 'the following operation is performed'
132             operation || expectedOperationInUrl
133             CREATE    || 'create'
134             UPDATE    || 'update'
135     }
136 }