Merge "NCMP : forward bulk response messages to client topic"
[cps.git] / cps-ncmp-service / src / test / groovy / org / onap / cps / ncmp / api / impl / async / NcmpAsyncBatchEventConsumerSpec.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.async
22
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.onap.cps.ncmp.api.impl.events.EventsPublisher
29 import org.onap.cps.ncmp.api.kafka.MessagingBaseSpec
30 import org.onap.cps.ncmp.events.async.BatchDataResponseEventV1
31 import org.onap.cps.ncmp.utils.TestUtils
32 import org.onap.cps.utils.JsonObjectMapper
33 import org.spockframework.spring.SpringBean
34 import org.springframework.beans.factory.annotation.Autowired
35 import org.springframework.boot.test.context.SpringBootTest
36 import org.springframework.kafka.listener.adapter.RecordFilterStrategy
37 import org.springframework.test.annotation.DirtiesContext
38 import org.testcontainers.spock.Testcontainers
39
40 import java.time.Duration
41
42 @SpringBootTest(classes = [EventsPublisher, NcmpAsyncBatchEventConsumer, BatchRecordFilterStrategy,JsonObjectMapper,
43                 ObjectMapper])
44 @Testcontainers
45 @DirtiesContext
46 class NcmpAsyncBatchEventConsumerSpec extends MessagingBaseSpec {
47
48     @SpringBean
49     EventsPublisher asyncBatchEventPublisher = new EventsPublisher<BatchDataResponseEventV1>(kafkaTemplate)
50
51     @SpringBean
52     NcmpAsyncBatchEventConsumer asyncBatchEventConsumer = new NcmpAsyncBatchEventConsumer(asyncBatchEventPublisher)
53
54     @Autowired
55     JsonObjectMapper jsonObjectMapper
56
57     @Autowired
58     RecordFilterStrategy<String, BatchDataResponseEventV1> recordFilterStrategy
59
60     def kafkaConsumer = new KafkaConsumer<>(consumerConfigProperties('test'))
61     def static clientTopic = 'client-topic'
62     def static batchEventType = 'org.onap.cps.ncmp.events.async.BatchDataResponseEventV1'
63
64     def 'Consume and publish event to client specified topic'() {
65         given: 'consumer subscribing to client topic'
66             kafkaConsumer.subscribe([clientTopic])
67         and: 'consumer record for batch event'
68             def consumerRecordIn = createConsumerRecord(batchEventType)
69         when: 'the batch event is consumed and published to client specified topic'
70             asyncBatchEventConsumer.consumeAndPublish(consumerRecordIn)
71         and: 'the client specified topic is polled'
72             def consumerRecordOut = kafkaConsumer.poll(Duration.ofMillis(1500))[0]
73         then: 'verifying consumed event operationID is same as published event operationID'
74             def operationIdIn = consumerRecordIn.value.event.batchResponses[0].operationId
75             def operationIdOut = jsonObjectMapper.convertJsonString((String)consumerRecordOut.value(), BatchDataResponseEventV1.class).event.batchResponses[0].operationId
76             assert operationIdIn == operationIdOut
77     }
78
79     def 'Filter an event with type #eventType'() {
80         given: 'consumer record for event with type #eventType'
81             def consumerRecord = createConsumerRecord(eventType)
82         when: 'while consuming the topic ncmp-async-m2m it executes the filter strategy'
83             def result = recordFilterStrategy.filter(consumerRecord)
84         then: 'the event is #description'
85             assert result == expectedResult
86         where: 'filter the event based on the eventType #eventType'
87             description                                     | eventType       || expectedResult
88             'not filtered(the consumer will see the event)' | batchEventType  || false
89             'filtered(the consumer will not see the event)' | 'wrongType'     || true
90     }
91
92     def createConsumerRecord(eventTypeAsString) {
93         def jsonData = TestUtils.getResourceFileContent('batchDataEvent.json')
94         def testEventSent = jsonObjectMapper.convertJsonString(jsonData, BatchDataResponseEventV1.class)
95         def eventTarget = SerializationUtils.serialize(clientTopic)
96         def eventType = SerializationUtils.serialize(eventTypeAsString)
97         def eventId = SerializationUtils.serialize('12345')
98         def consumerRecord = new ConsumerRecord<String, Object>(clientTopic, 1, 1L, '123', testEventSent)
99         consumerRecord.headers().add(new RecordHeader('eventId', eventId))
100         consumerRecord.headers().add(new RecordHeader('eventTarget', eventTarget))
101         consumerRecord.headers().add(new RecordHeader('eventType', eventType))
102         return consumerRecord
103     }
104 }