Add CSIT test: Delete CM Handle
[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-2024 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 org.onap.cps.events.EventsPublisher
25
26 import static org.onap.cps.ncmp.api.impl.events.mapper.CloudEventMapper.toTargetEvent
27 import static org.onap.cps.ncmp.api.impl.operations.DatastoreType.PASSTHROUGH_OPERATIONAL
28 import static org.onap.cps.ncmp.api.impl.operations.DatastoreType.PASSTHROUGH_RUNNING
29 import static org.onap.cps.ncmp.api.impl.operations.OperationType.CREATE
30 import static org.onap.cps.ncmp.api.impl.operations.OperationType.READ
31 import static org.onap.cps.ncmp.api.impl.operations.OperationType.UPDATE
32 import static org.onap.cps.ncmp.api.NcmpResponseStatus.UNABLE_TO_READ_RESOURCE_DATA
33 import static org.onap.cps.ncmp.api.NcmpResponseStatus.DMI_SERVICE_NOT_RESPONDING
34
35 import com.fasterxml.jackson.databind.ObjectMapper
36 import org.onap.cps.ncmp.api.impl.config.NcmpConfiguration
37
38 import org.onap.cps.ncmp.api.impl.exception.HttpClientRequestException
39 import org.onap.cps.ncmp.api.impl.utils.DmiServiceUrlBuilder
40 import org.onap.cps.ncmp.api.impl.utils.context.CpsApplicationContext
41 import org.onap.cps.ncmp.api.models.DataOperationRequest
42 import org.onap.cps.ncmp.events.async1_0_0.DataOperationEvent
43 import org.onap.cps.ncmp.utils.TestUtils
44 import org.onap.cps.utils.JsonObjectMapper
45 import org.spockframework.spring.SpringBean
46 import org.springframework.beans.factory.annotation.Autowired
47 import org.springframework.boot.test.context.SpringBootTest
48 import org.springframework.http.ResponseEntity
49 import org.springframework.test.context.ContextConfiguration
50 import org.springframework.http.HttpStatus
51 import spock.lang.Shared
52 import java.util.concurrent.TimeoutException
53
54 @SpringBootTest
55 @ContextConfiguration(classes = [EventsPublisher, CpsApplicationContext, NcmpConfiguration.DmiProperties, DmiDataOperations])
56 class DmiDataOperationsSpec extends DmiOperationsBaseSpec {
57
58     @SpringBean
59     DmiServiceUrlBuilder dmiServiceUrlBuilder = Mock()
60     def dmiServiceBaseUrl = "${dmiServiceName}/dmi/v1/ch/${cmHandleId}/data/ds/ncmp-datastore:"
61     def NO_TOPIC = null
62     def NO_REQUEST_ID = null
63     @Shared
64     def OPTIONS_PARAM = '(a=1,b=2)'
65
66     @SpringBean
67     JsonObjectMapper spiedJsonObjectMapper = Spy(new JsonObjectMapper(new ObjectMapper()))
68
69     @Autowired
70     DmiDataOperations objectUnderTest
71
72     @SpringBean
73     EventsPublisher eventsPublisher = Stub()
74
75     def 'call get resource data for #expectedDatastoreInUrl from DMI without topic #scenario.'() {
76         given: 'a cm handle for #cmHandleId'
77             mockYangModelCmHandleRetrieval(dmiProperties)
78         and: 'a positive response from DMI service when it is called with the expected parameters'
79             def responseFromDmi = new ResponseEntity<Object>(HttpStatus.OK)
80             def expectedUrl = dmiServiceBaseUrl + "${expectedDatastoreInUrl}?resourceIdentifier=${resourceIdentifier}${expectedOptionsInUrl}"
81             mockDmiRestClient.postOperationWithJsonData(expectedUrl, expectedJson, READ) >> responseFromDmi
82             dmiServiceUrlBuilder.getDmiDatastoreUrl(_, _) >> expectedUrl
83         when: 'get resource data is invoked'
84             def result = objectUnderTest.getResourceDataFromDmi(dataStore.datastoreName, cmHandleId, resourceIdentifier,
85                     options, NO_TOPIC, NO_REQUEST_ID)
86         then: 'the result is the response from the DMI service'
87             assert result == responseFromDmi
88         where: 'the following parameters are used'
89             scenario                               | dmiProperties               | dataStore               | options       || expectedJson                                                 | expectedDatastoreInUrl    | expectedOptionsInUrl
90             'without properties'                   | []                          | PASSTHROUGH_OPERATIONAL | OPTIONS_PARAM || '{"operation":"read","cmHandleProperties":{}}'               | 'passthrough-operational' | '&options=(a=1,b=2)'
91             'with properties'                      | [yangModelCmHandleProperty] | PASSTHROUGH_OPERATIONAL | OPTIONS_PARAM || '{"operation":"read","cmHandleProperties":{"prop1":"val1"}}' | 'passthrough-operational' | '&options=(a=1,b=2)'
92             'null options'                         | [yangModelCmHandleProperty] | PASSTHROUGH_OPERATIONAL | null          || '{"operation":"read","cmHandleProperties":{"prop1":"val1"}}' | 'passthrough-operational' | ''
93             'empty options'                        | [yangModelCmHandleProperty] | PASSTHROUGH_OPERATIONAL | ''            || '{"operation":"read","cmHandleProperties":{"prop1":"val1"}}' | 'passthrough-operational' | ''
94             'datastore running without properties' | []                          | PASSTHROUGH_RUNNING     | OPTIONS_PARAM || '{"operation":"read","cmHandleProperties":{}}'               | 'passthrough-running'     | '&options=(a=1,b=2)'
95             'datastore running with properties'    | [yangModelCmHandleProperty] | PASSTHROUGH_RUNNING     | OPTIONS_PARAM || '{"operation":"read","cmHandleProperties":{"prop1":"val1"}}' | 'passthrough-running'     | '&options=(a=1,b=2)'
96     }
97
98     def 'Execute (async) data operation from DMI service.'() {
99         given: 'collection of yang model cm Handles and data operation request'
100             mockYangModelCmHandleCollectionRetrieval([yangModelCmHandleProperty])
101             def dataOperationBatchRequestJsonData = TestUtils.getResourceFileContent('dataOperationRequest.json')
102             def dataOperationRequest = spiedJsonObjectMapper.convertJsonString(dataOperationBatchRequestJsonData, DataOperationRequest.class)
103             dataOperationRequest.dataOperationDefinitions[0].cmHandleIds = [cmHandleId]
104             def requestBodyAsJsonStringArg = null
105         and: 'a positive response from DMI service when it is called with valid request parameters'
106             def responseFromDmi = new ResponseEntity<Object>(HttpStatus.ACCEPTED)
107             def expectedDmiBatchResourceDataUrl = "ncmp/v1/data/topic=my-topic-name"
108             def expectedBatchRequestAsJson = '{"operations":[{"operation":"read","operationId":"operational-14","datastore":"ncmp-datastore:passthrough-operational","options":"some option","resourceIdentifier":"some resource identifier","cmHandles":[{"id":"some-cm-handle","cmHandleProperties":{"prop1":"val1"}}]}]}'
109             mockDmiRestClient.postOperationWithJsonData(expectedDmiBatchResourceDataUrl, _, READ.operationName) >> responseFromDmi
110             dmiServiceUrlBuilder.getDataOperationRequestUrl(_, _) >> expectedDmiBatchResourceDataUrl
111         when: 'get resource data for group of cm handles are invoked'
112             objectUnderTest.requestResourceDataFromDmi('my-topic-name', dataOperationRequest, 'requestId')
113         then: 'wait a little to allow execution of service method by task executor (on separate thread)'
114             Thread.sleep(100)
115         then: 'validate ncmp generated dmi request body json args'
116             1 * mockDmiRestClient.postOperationWithJsonData(expectedDmiBatchResourceDataUrl, _, READ) >> { args -> requestBodyAsJsonStringArg = args[1] }
117             assert requestBodyAsJsonStringArg == expectedBatchRequestAsJson
118     }
119
120     def 'Execute (async) data operation from DMI service for #scenario.'() {
121         given: 'data operation request body and dmi resource url'
122             def dmiDataOperation = DmiDataOperation.builder().operationId('some-operation-id').build()
123             dmiDataOperation.getCmHandles().add(CmHandle.builder().id('some-cm-handle-id').build())
124             def dmiDataOperationResourceDataUrl = "http://dmi-service-name:dmi-port/dmi/v1/data?topic=my-topic-name&requestId=some-request-id"
125             def actualDataOperationCloudEvent = null
126         when: 'exception occurs after sending request to dmi service'
127             objectUnderTest.handleTaskCompletionException(new Throwable(exception), dmiDataOperationResourceDataUrl, List.of(dmiDataOperation))
128         then: 'a cloud event is published'
129             eventsPublisher.publishCloudEvent('my-topic-name', 'some-request-id', _) >> { args -> actualDataOperationCloudEvent = args[2] }
130         and: 'the event contains the expected error details'
131             def eventDataValue = extractDataValue(actualDataOperationCloudEvent)
132             assert eventDataValue.operationId == dmiDataOperation.operationId
133             assert eventDataValue.ids == dmiDataOperation.cmHandles.id
134             assert eventDataValue.statusCode == responseCode.code
135             assert eventDataValue.statusMessage == responseCode.message
136         where: 'the following exceptions are occurred'
137             scenario                        | exception                                                                                                || responseCode
138             'http client request exception' | new HttpClientRequestException('error-message', 'error-details', HttpStatus.SERVICE_UNAVAILABLE.value()) || UNABLE_TO_READ_RESOURCE_DATA
139             'timeout exception'             | new TimeoutException()                                                                                   || DMI_SERVICE_NOT_RESPONDING
140     }
141
142     def 'call get all resource data.'() {
143         given: 'the system returns a cm handle with a sample property'
144             mockYangModelCmHandleRetrieval([yangModelCmHandleProperty])
145         and: 'a positive response from DMI service when it is called with the expected parameters'
146             def responseFromDmi = new ResponseEntity<Object>(HttpStatus.OK)
147             def expectedUrl = dmiServiceBaseUrl + "passthrough-operational?resourceIdentifier=/"
148             mockDmiRestClient.postOperationWithJsonData(expectedUrl, '{"operation":"read","cmHandleProperties":{"prop1":"val1"}}', READ) >> responseFromDmi
149             dmiServiceUrlBuilder.getDmiDatastoreUrl(_, _) >> expectedUrl
150         when: 'get resource data is invoked'
151             def result = objectUnderTest.getResourceDataFromDmi( PASSTHROUGH_OPERATIONAL.datastoreName, cmHandleId, NO_REQUEST_ID)
152         then: 'the result is the response from the DMI service'
153             assert result == responseFromDmi
154     }
155
156     def 'Write data for pass-through:running datastore in DMI.'() {
157         given: 'a cm handle for #cmHandleId'
158             mockYangModelCmHandleRetrieval([yangModelCmHandleProperty])
159         and: 'a positive response from DMI service when it is called with the expected parameters'
160             def expectedUrl = dmiServiceBaseUrl + "passthrough-running?resourceIdentifier=${resourceIdentifier}"
161             def expectedJson = '{"operation":"' + expectedOperationInUrl + '","dataType":"some data type","data":"requestData","cmHandleProperties":{"prop1":"val1"}}'
162             def responseFromDmi = new ResponseEntity<Object>(HttpStatus.OK)
163             dmiServiceUrlBuilder.getDmiDatastoreUrl(_, _) >> expectedUrl
164             mockDmiRestClient.postOperationWithJsonData(expectedUrl, expectedJson, operation) >> responseFromDmi
165         when: 'write resource method is invoked'
166             def result = objectUnderTest.writeResourceDataPassThroughRunningFromDmi(cmHandleId, 'parent/child', operation, 'requestData', 'some data type')
167         then: 'the result is the response from the DMI service'
168             assert result == responseFromDmi
169         where: 'the following operation is performed'
170             operation || expectedOperationInUrl
171             CREATE    || 'create'
172             UPDATE    || 'update'
173     }
174
175     def extractDataValue(actualDataOperationCloudEvent) {
176         return toTargetEvent(actualDataOperationCloudEvent, DataOperationEvent.class).data.responses[0]
177     }
178 }