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 org.apache.commons.lang3.SerializationUtils
25 import org.apache.kafka.clients.consumer.ConsumerRecord
26 import org.apache.kafka.clients.consumer.KafkaConsumer
27 import org.apache.kafka.common.header.internals.RecordHeader
28 import org.apache.kafka.common.serialization.StringDeserializer
29 import org.onap.cps.ncmp.api.impl.events.EventsPublisher
30 import org.onap.cps.ncmp.api.kafka.MessagingBaseSpec
31 import org.onap.cps.ncmp.events.async.BatchDataResponseEventV1
32 import org.onap.cps.ncmp.utils.TestUtils
33 import org.onap.cps.utils.JsonObjectMapper
34 import org.spockframework.spring.SpringBean
35 import org.springframework.beans.factory.annotation.Autowired
36 import org.springframework.boot.test.context.SpringBootTest
37 import org.springframework.kafka.listener.adapter.RecordFilterStrategy
38 import org.springframework.test.annotation.DirtiesContext
39 import org.testcontainers.spock.Testcontainers
41 import java.time.Duration
43 @SpringBootTest(classes = [EventsPublisher, NcmpAsyncBatchEventConsumer, BatchRecordFilterStrategy,JsonObjectMapper,
47 class NcmpAsyncBatchEventConsumerSpec extends MessagingBaseSpec {
50 EventsPublisher asyncBatchEventPublisher = new EventsPublisher<BatchDataResponseEventV1>(legacyEventKafkaTemplate, cloudEventKafkaTemplate)
53 NcmpAsyncBatchEventConsumer asyncBatchEventConsumer = new NcmpAsyncBatchEventConsumer(asyncBatchEventPublisher)
56 JsonObjectMapper jsonObjectMapper
59 RecordFilterStrategy<String, BatchDataResponseEventV1> recordFilterStrategy
61 def legacyEventKafkaConsumer = new KafkaConsumer<>(eventConsumerConfigProperties('test', StringDeserializer))
62 def static clientTopic = 'client-topic'
63 def static batchEventType = 'org.onap.cps.ncmp.events.async.BatchDataResponseEventV1'
65 def 'Consume and publish event to client specified topic'() {
66 given: 'consumer subscribing to client topic'
67 legacyEventKafkaConsumer.subscribe([clientTopic])
68 and: 'consumer record for batch event'
69 def consumerRecordIn = createConsumerRecord(batchEventType)
70 when: 'the batch event is consumed and published to client specified topic'
71 asyncBatchEventConsumer.consumeAndPublish(consumerRecordIn)
72 and: 'the client specified topic is polled'
73 def consumerRecordOut = legacyEventKafkaConsumer.poll(Duration.ofMillis(1500))[0]
74 then: 'verifying consumed event operationID is same as published event operationID'
75 def operationIdIn = consumerRecordIn.value.event.batchResponses[0].operationId
76 def operationIdOut = jsonObjectMapper.convertJsonString((String)consumerRecordOut.value(), BatchDataResponseEventV1.class).event.batchResponses[0].operationId
77 assert operationIdIn == operationIdOut
80 def 'Filter an event with type #eventType'() {
81 given: 'consumer record for event with type #eventType'
82 def consumerRecord = createConsumerRecord(eventType)
83 when: 'while consuming the topic ncmp-async-m2m it executes the filter strategy'
84 def result = recordFilterStrategy.filter(consumerRecord)
85 then: 'the event is #description'
86 assert result == expectedResult
87 where: 'filter the event based on the eventType #eventType'
88 description | eventType || expectedResult
89 'not filtered(the consumer will see the event)' | batchEventType || false
90 'filtered(the consumer will not see the event)' | 'wrongType' || true
93 def createConsumerRecord(eventTypeAsString) {
94 def jsonData = TestUtils.getResourceFileContent('batchDataEvent.json')
95 def testEventSent = jsonObjectMapper.convertJsonString(jsonData, BatchDataResponseEventV1.class)
96 def eventTarget = SerializationUtils.serialize(clientTopic)
97 def eventType = SerializationUtils.serialize(eventTypeAsString)
98 def eventId = SerializationUtils.serialize('12345')
99 def consumerRecord = new ConsumerRecord<String, Object>(clientTopic, 1, 1L, '123', testEventSent)
100 consumerRecord.headers().add(new RecordHeader('eventId', eventId))
101 consumerRecord.headers().add(new RecordHeader('eventTarget', eventTarget))
102 consumerRecord.headers().add(new RecordHeader('eventType', eventType))
103 return consumerRecord