NCMP : Handle non-existing and non-ready cm handles
[cps.git] / cps-ncmp-service / src / test / groovy / org / onap / cps / ncmp / api / impl / utils / data / operation / ResourceDataOperationRequestUtilsSpec.groovy
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2023 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.utils.data.operation
22
23 import com.fasterxml.jackson.databind.ObjectMapper
24 import io.cloudevents.CloudEvent
25 import io.cloudevents.core.CloudEventUtils
26 import io.cloudevents.jackson.PojoCloudEventDataMapper
27 import io.cloudevents.kafka.CloudEventDeserializer
28 import io.cloudevents.kafka.impl.KafkaHeaders
29 import org.apache.kafka.clients.consumer.KafkaConsumer
30 import org.onap.cps.ncmp.api.impl.events.EventsPublisher
31 import org.onap.cps.ncmp.api.impl.utils.context.CpsApplicationContext
32 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle
33 import org.onap.cps.ncmp.api.inventory.CmHandleState
34 import org.onap.cps.ncmp.api.inventory.CompositeStateBuilder
35 import org.onap.cps.ncmp.api.kafka.MessagingBaseSpec
36 import org.onap.cps.ncmp.api.models.DataOperationRequest
37 import org.onap.cps.ncmp.events.async1_0_0.DataOperationEvent
38 import org.onap.cps.ncmp.utils.TestUtils
39 import org.onap.cps.utils.JsonObjectMapper
40 import org.spockframework.spring.SpringBean
41 import org.springframework.beans.factory.annotation.Autowired
42 import org.springframework.test.context.ContextConfiguration
43 import java.time.Duration
44
45 @ContextConfiguration(classes = [EventsPublisher, CpsApplicationContext, ObjectMapper])
46 class ResourceDataOperationRequestUtilsSpec extends MessagingBaseSpec {
47
48     def cloudEventKafkaConsumer = new KafkaConsumer<>(eventConsumerConfigProperties('test', CloudEventDeserializer))
49     def static clientTopic = 'my-topic-name'
50     def static dataOperationType = 'org.onap.cps.ncmp.events.async1_0_0.DataOperationEvent'
51
52     @SpringBean
53     JsonObjectMapper jsonObjectMapper = new JsonObjectMapper(new ObjectMapper())
54
55     @SpringBean
56     EventsPublisher eventPublisher = new EventsPublisher<CloudEvent>(legacyEventKafkaTemplate, cloudEventKafkaTemplate)
57
58     @Autowired
59     ObjectMapper objectMapper
60
61     def 'Process per data operation request with #serviceName.'() {
62         given: 'data operation request with 3 operations'
63             def dataOperationRequestJsonData = TestUtils.getResourceFileContent('dataOperationRequest.json')
64             def dataOperationRequest = jsonObjectMapper.convertJsonString(dataOperationRequestJsonData, DataOperationRequest.class)
65         and: '4 known cm handles: ch1-dmi1, ch2-dmi1, ch3-dmi2, ch4-dmi2'
66             def yangModelCmHandles = getYangModelCmHandles()
67         when: 'data operation request is processed'
68             def operationsOutPerDmiServiceName = ResourceDataOperationRequestUtils.processPerDefinitionInDataOperationsRequest(clientTopic,'request-id', dataOperationRequest, yangModelCmHandles)
69         and: 'converted to a json node'
70             def dmiDataOperationRequestBody = jsonObjectMapper.asJsonString(operationsOutPerDmiServiceName.get(serviceName))
71             def dmiDataOperationRequestBodyAsJsonNode = jsonObjectMapper.convertToJsonNode(dmiDataOperationRequestBody).get(operationIndex)
72         then: 'it contains the correct operation details'
73             assert dmiDataOperationRequestBodyAsJsonNode.get('operation').asText() == 'read'
74             assert dmiDataOperationRequestBodyAsJsonNode.get('operationId').asText() == expectedOperationId
75             assert dmiDataOperationRequestBodyAsJsonNode.get('datastore').asText() == expectedDatastore
76         and: 'the correct cm handles (just for #serviceName)'
77             assert dmiDataOperationRequestBodyAsJsonNode.get('cmHandles').size() == expectedCmHandleIds.size()
78             expectedCmHandleIds.each {
79                 dmiDataOperationRequestBodyAsJsonNode.get('cmHandles').toString().contains(it)
80             }
81         where: 'the following dmi service and operations are checked'
82             serviceName | operationIndex || expectedOperationId | expectedDatastore                        | expectedCmHandleIds
83             'dmi1'      | 0              || 'operational-14'    | 'ncmp-datastore:passthrough-operational' | ['ch6-dmi1']
84             'dmi1'      | 1              || 'running-12'        | 'ncmp-datastore:passthrough-running'     | ['ch1-dmi1', 'ch2-dmi1']
85             'dmi1'      | 2              || 'operational-15'    | 'ncmp-datastore:passthrough-operational' | ['ch6-dmi1']
86             'dmi2'      | 0              || 'operational-14'    | 'ncmp-datastore:passthrough-operational' | ['ch3-dmi2']
87             'dmi2'      | 1              || 'running-12'        | 'ncmp-datastore:passthrough-running'     | ['ch7-dmi2']
88             'dmi2'      | 2              || 'operational-15'    | 'ncmp-datastore:passthrough-operational' | ['ch4-dmi2']
89     }
90
91     def 'Process per data operation request with non-ready, non-existing cm handle and publish event to client specified topic'() {
92         given: 'consumer subscribing to client topic'
93             cloudEventKafkaConsumer.subscribe([clientTopic])
94         and: 'data operation request having non-ready and non-existing cm handle ids'
95             def dataOperationRequestJsonData = TestUtils.getResourceFileContent('dataOperationRequest.json')
96             def dataOperationRequest = jsonObjectMapper.convertJsonString(dataOperationRequestJsonData, DataOperationRequest.class)
97         when: 'data operation request is processed'
98             ResourceDataOperationRequestUtils.processPerDefinitionInDataOperationsRequest(clientTopic, 'request-id', dataOperationRequest, yangModelCmHandles)
99         and: 'subscribed client specified topic is polled and first record is selected'
100             def consumerRecordOut = cloudEventKafkaConsumer.poll(Duration.ofMillis(1500))[0]
101         then: 'verify cloud compliant headers'
102             def consumerRecordOutHeaders = consumerRecordOut.headers()
103             assert KafkaHeaders.getParsedKafkaHeader(consumerRecordOutHeaders, 'ce_id') != null
104             assert KafkaHeaders.getParsedKafkaHeader(consumerRecordOutHeaders, 'ce_type') == dataOperationType
105         and: 'verify that extension is included into header'
106             assert KafkaHeaders.getParsedKafkaHeader(consumerRecordOutHeaders, 'ce_correlationid') == 'request-id'
107             assert KafkaHeaders.getParsedKafkaHeader(consumerRecordOutHeaders, 'ce_destination') == clientTopic
108         and: 'map consumer record to expected event type'
109             def dataOperationResponseEvent = CloudEventUtils.mapData(consumerRecordOut.value(),
110                     PojoCloudEventDataMapper.from(objectMapper, DataOperationEvent.class)).getValue()
111         and: 'data operation response event response size is 3'
112             dataOperationResponseEvent.data.responses.size() == 3
113         and: 'verify published response data as json string'
114             jsonObjectMapper.asJsonString(dataOperationResponseEvent.data.responses)
115                     == '[{"operationId":"operational-14","ids":["unknown-cm-handle"],"statusCode":"100","statusMessage":"cm handle id(s) not found"},{"operationId":"operational-14","ids":["non-ready-cm handle"],"statusCode":"101","statusMessage":"cm handle(s) not ready"},{"operationId":"running-12","ids":["non-ready-cm handle"],"statusCode":"101","statusMessage":"cm handle(s) not ready"}]'
116     }
117
118     static def getYangModelCmHandles() {
119         def dmiProperties = [new YangModelCmHandle.Property('prop', 'some DMI property')]
120         def readyState = new CompositeStateBuilder().withCmHandleState(CmHandleState.READY).withLastUpdatedTimeNow().build()
121         def advisedState = new CompositeStateBuilder().withCmHandleState(CmHandleState.ADVISED).withLastUpdatedTimeNow().build()
122         return [new YangModelCmHandle(id: 'ch1-dmi1', dmiServiceName: 'dmi1', dmiProperties: dmiProperties, compositeState: readyState),
123                 new YangModelCmHandle(id: 'ch2-dmi1', dmiServiceName: 'dmi1', dmiProperties: dmiProperties, compositeState: readyState),
124                 new YangModelCmHandle(id: 'ch6-dmi1', dmiServiceName: 'dmi1', dmiProperties: dmiProperties, compositeState: readyState),
125                 new YangModelCmHandle(id: 'ch8-dmi1', dmiServiceName: 'dmi1', dmiProperties: dmiProperties, compositeState: readyState),
126                 new YangModelCmHandle(id: 'ch3-dmi2', dmiServiceName: 'dmi2', dmiProperties: dmiProperties, compositeState: readyState),
127                 new YangModelCmHandle(id: 'ch4-dmi2', dmiServiceName: 'dmi2', dmiProperties: dmiProperties, compositeState: readyState),
128                 new YangModelCmHandle(id: 'ch7-dmi2', dmiServiceName: 'dmi2', dmiProperties: dmiProperties, compositeState: readyState),
129                 new YangModelCmHandle(id: 'non-ready-cm handle', dmiServiceName: 'dmi2', dmiProperties: dmiProperties, compositeState: advisedState)
130         ]
131     }
132 }