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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.cps.ncmp.api.impl.async
23 import com.fasterxml.jackson.databind.ObjectMapper
24 import io.cloudevents.CloudEvent
25 import io.cloudevents.kafka.CloudEventDeserializer
26 import io.cloudevents.kafka.CloudEventSerializer
27 import io.cloudevents.kafka.impl.KafkaHeaders
28 import io.cloudevents.core.builder.CloudEventBuilder
29 import org.apache.kafka.clients.consumer.ConsumerRecord
30 import org.apache.kafka.clients.consumer.KafkaConsumer
31 import org.apache.kafka.common.header.internals.RecordHeaders
32 import org.onap.cps.ncmp.api.impl.events.EventsPublisher
33 import org.onap.cps.ncmp.api.kafka.MessagingBaseSpec
34 import org.onap.cps.ncmp.events.async1_0_0.DataOperationEvent
35 import org.onap.cps.ncmp.utils.TestUtils
36 import org.onap.cps.utils.JsonObjectMapper
37 import org.spockframework.spring.SpringBean
38 import org.springframework.beans.factory.annotation.Autowired
39 import org.springframework.boot.test.context.SpringBootTest
40 import org.springframework.kafka.listener.adapter.RecordFilterStrategy
41 import org.springframework.test.annotation.DirtiesContext
42 import org.testcontainers.spock.Testcontainers
43 import java.time.Duration
45 import static org.onap.cps.ncmp.api.impl.events.mapper.CloudEventMapper.toTargetEvent
47 @SpringBootTest(classes = [EventsPublisher, DataOperationEventConsumer, RecordFilterStrategies, JsonObjectMapper, ObjectMapper])
50 class DataOperationEventConsumerSpec extends MessagingBaseSpec {
53 EventsPublisher asyncDataOperationEventPublisher = new EventsPublisher<CloudEvent>(legacyEventKafkaTemplate, cloudEventKafkaTemplate)
56 DataOperationEventConsumer objectUnderTest = new DataOperationEventConsumer(asyncDataOperationEventPublisher)
59 JsonObjectMapper jsonObjectMapper
62 RecordFilterStrategy<String, CloudEvent> dataOperationRecordFilterStrategy
64 def cloudEventKafkaConsumer = new KafkaConsumer<>(eventConsumerConfigProperties('test', CloudEventDeserializer))
65 def static clientTopic = 'client-topic'
66 def static dataOperationType = 'org.onap.cps.ncmp.events.async1_0_0.DataOperationEvent'
68 def 'Consume and publish event to client specified topic'() {
69 given: 'consumer subscribing to client topic'
70 cloudEventKafkaConsumer.subscribe([clientTopic])
71 and: 'consumer record for data operation event'
72 def consumerRecordIn = createConsumerRecord(dataOperationType)
73 when: 'the data operation event is consumed and published to client specified topic'
74 objectUnderTest.consumeAndPublish(consumerRecordIn)
75 and: 'the client specified topic is polled'
76 def consumerRecordOut = cloudEventKafkaConsumer.poll(Duration.ofMillis(1500))[0]
77 then: 'verify cloud compliant headers'
78 def consumerRecordOutHeaders = consumerRecordOut.headers()
79 assert KafkaHeaders.getParsedKafkaHeader(consumerRecordOutHeaders, 'ce_correlationid') == 'request-id'
80 assert KafkaHeaders.getParsedKafkaHeader(consumerRecordOutHeaders, 'ce_id') == 'some-uuid'
81 assert KafkaHeaders.getParsedKafkaHeader(consumerRecordOutHeaders, 'ce_type') == dataOperationType
82 and: 'verify that extension is included into header'
83 assert KafkaHeaders.getParsedKafkaHeader(consumerRecordOutHeaders, 'ce_destination') == clientTopic
84 and: 'map consumer record to expected event type'
85 def dataOperationResponseEvent = toTargetEvent(consumerRecordOut.value(), DataOperationEvent.class)
86 and: 'verify published response data properties'
87 def response = dataOperationResponseEvent.data.responses[0]
88 response.operationId == 'some-operation-id'
89 response.statusCode == 'any-success-status-code'
90 response.statusMessage == 'Successfully applied changes'
91 response.result as String == '[some-key:some-value]'
94 def 'Filter an event with type #eventType'() {
95 given: 'consumer record for event with type #eventType'
96 def consumerRecord = createConsumerRecord(eventType)
97 when: 'while consuming the topic ncmp-async-m2m it executes the filter strategy'
98 def result = dataOperationRecordFilterStrategy.filter(consumerRecord)
99 then: 'the event is #description'
100 assert result == expectedResult
101 where: 'filter the event based on the eventType #eventType'
102 description | eventType || expectedResult
103 'not filtered(the consumer will see the event)' | dataOperationType || false
104 'filtered(the consumer will not see the event)' | 'wrongType' || true
107 def createConsumerRecord(eventTypeAsString) {
108 def jsonData = TestUtils.getResourceFileContent('dataOperationEvent.json')
109 def testEventSentAsBytes = jsonObjectMapper.asJsonBytes(jsonObjectMapper.convertJsonString(jsonData, DataOperationEvent.class))
111 CloudEvent cloudEvent = getCloudEvent(eventTypeAsString, testEventSentAsBytes)
113 def headers = new RecordHeaders()
114 def cloudEventSerializer = new CloudEventSerializer()
115 cloudEventSerializer.serialize(clientTopic, headers, cloudEvent)
117 def consumerRecord = new ConsumerRecord<String, CloudEvent>(clientTopic, 0, 0L, 'sample-message-key', cloudEvent)
118 headers.forEach(header -> consumerRecord.headers().add(header))
119 return consumerRecord
122 def getCloudEvent(eventTypeAsString, byte[] testEventSentAsBytes) {
123 return CloudEventBuilder.v1()
125 .withType(eventTypeAsString)
126 .withSource(URI.create("sample-test-source"))
127 .withData(testEventSentAsBytes)
128 .withExtension("correlationid", "request-id")
129 .withExtension("destination", clientTopic)