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.async1_0_0.DataOperationEvent
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
40 import java.time.Duration
42 @SpringBootTest(classes = [EventsPublisher, NcmpAsyncDataOperationEventConsumer, DataOperationRecordFilterStrategy,JsonObjectMapper, ObjectMapper])
45 class NcmpAsyncDataOperationEventConsumerSpec extends MessagingBaseSpec {
48 EventsPublisher asyncDataOperationEventPublisher = new EventsPublisher<DataOperationEvent>(legacyEventKafkaTemplate, cloudEventKafkaTemplate)
51 NcmpAsyncDataOperationEventConsumer asyncDataOperationEventConsumer = new NcmpAsyncDataOperationEventConsumer(asyncDataOperationEventPublisher)
54 JsonObjectMapper jsonObjectMapper
57 RecordFilterStrategy<String, DataOperationEvent> recordFilterStrategy
59 def legacyEventKafkaConsumer = new KafkaConsumer<>(eventConsumerConfigProperties('test', StringDeserializer))
60 def static clientTopic = 'client-topic'
61 def static dataOperationType = 'org.onap.cps.ncmp.events.async1_0_0.DataOperationEvent'
63 def 'Consume and publish event to client specified topic'() {
64 given: 'consumer subscribing to client topic'
65 legacyEventKafkaConsumer.subscribe([clientTopic])
66 and: 'consumer record for data operation event'
67 def consumerRecordIn = createConsumerRecord(dataOperationType)
68 when: 'the data operation event is consumed and published to client specified topic'
69 asyncDataOperationEventConsumer.consumeAndPublish(consumerRecordIn)
70 and: 'the client specified topic is polled'
71 def consumerRecordOut = legacyEventKafkaConsumer.poll(Duration.ofMillis(1500))[0]
72 then: 'verifying consumed event operationID is same as published event operationID'
73 def operationIdIn = consumerRecordIn.value.data.responses[0].operationId
74 def operationIdOut = jsonObjectMapper.convertJsonString((String)consumerRecordOut.value(), DataOperationEvent.class).data.responses[0].operationId
75 assert operationIdIn == operationIdOut
78 def 'Filter an event with type #eventType'() {
79 given: 'consumer record for event with type #eventType'
80 def consumerRecord = createConsumerRecord(eventType)
81 when: 'while consuming the topic ncmp-async-m2m it executes the filter strategy'
82 def result = recordFilterStrategy.filter(consumerRecord)
83 then: 'the event is #description'
84 assert result == expectedResult
85 where: 'filter the event based on the eventType #eventType'
86 description | eventType || expectedResult
87 'not filtered(the consumer will see the event)' | dataOperationType || false
88 'filtered(the consumer will not see the event)' | 'wrongType' || true
91 def createConsumerRecord(eventTypeAsString) {
92 def jsonData = TestUtils.getResourceFileContent('dataOperationEvent.json')
93 def testEventSent = jsonObjectMapper.convertJsonString(jsonData, DataOperationEvent.class)
94 def eventTarget = SerializationUtils.serialize(clientTopic)
95 def eventType = SerializationUtils.serialize(eventTypeAsString)
96 def eventId = SerializationUtils.serialize('12345')
97 def consumerRecord = new ConsumerRecord<String, Object>(clientTopic, 1, 1L, '123', testEventSent)
98 consumerRecord.headers().add(new RecordHeader('eventId', eventId))
99 consumerRecord.headers().add(new RecordHeader('eventTarget', eventTarget))
100 consumerRecord.headers().add(new RecordHeader('eventType', eventType))
101 return consumerRecord