2  * ============LICENSE_START=======================================================
 
   3  * Copyright (C) 2022-2024 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.impl.inventory.sync.lcm
 
  23 import com.fasterxml.jackson.databind.ObjectMapper
 
  24 import org.apache.kafka.clients.consumer.KafkaConsumer
 
  25 import org.apache.kafka.common.serialization.StringDeserializer
 
  26 import org.onap.cps.events.EventsPublisher
 
  27 import org.onap.cps.ncmp.api.kafka.MessagingBaseSpec
 
  28 import org.onap.cps.ncmp.events.lcm.v1.Event
 
  29 import org.onap.cps.ncmp.events.lcm.v1.LcmEvent
 
  30 import org.onap.cps.ncmp.utils.TestUtils
 
  31 import org.onap.cps.utils.JsonObjectMapper
 
  32 import org.spockframework.spring.SpringBean
 
  33 import org.springframework.beans.factory.annotation.Autowired
 
  34 import org.springframework.boot.test.context.SpringBootTest
 
  35 import org.springframework.test.annotation.DirtiesContext
 
  36 import org.springframework.util.SerializationUtils
 
  37 import org.testcontainers.spock.Testcontainers
 
  39 import java.time.Duration
 
  41 @SpringBootTest(classes = [ObjectMapper, JsonObjectMapper])
 
  44 class LcmEventsPublisherSpec extends MessagingBaseSpec {
 
  46     def legacyEventKafkaConsumer = new KafkaConsumer<>(eventConsumerConfigProperties('ncmp-group', StringDeserializer))
 
  48     def testTopic = 'ncmp-events-test'
 
  51     EventsPublisher<LcmEvent> lcmEventsPublisher = new EventsPublisher(legacyEventKafkaTemplate, cloudEventKafkaTemplate)
 
  54     JsonObjectMapper jsonObjectMapper
 
  57     def 'Produce and Consume Lcm Event'() {
 
  58         given: 'event key and event data'
 
  60             def eventId = 'test-uuid'
 
  61             def eventCorrelationId = 'cmhandle-test'
 
  62             def eventSource = 'org.onap.ncmp'
 
  63             def eventTime = '2022-12-31T20:30:40.000+0000'
 
  64             def eventType = 'org.onap.ncmp.cmhandle.lcm.event'
 
  65             def eventSchema = 'org.onap.ncmp.cmhandle.lcm.event'
 
  66             def eventSchemaVersion = 'v1'
 
  67             def eventData = new LcmEvent(
 
  69                 eventCorrelationId: eventCorrelationId,
 
  70                 eventSource: eventSource,
 
  73                 eventSchema: eventSchema,
 
  74                 eventSchemaVersion: eventSchemaVersion,
 
  75                 event: new Event(cmHandleId: 'cmhandle-test'))
 
  76         and: 'we have a event header'
 
  79                 eventCorrelationId: eventCorrelationId,
 
  80                 eventSource       : eventSource,
 
  81                 eventTime         : eventTime,
 
  82                 eventType         : eventType,
 
  83                 eventSchema       : eventSchema,
 
  84                 eventSchemaVersion: eventSchemaVersion]
 
  85         and: 'consumer has a subscription'
 
  86             legacyEventKafkaConsumer.subscribe([testTopic] as List<String>)
 
  87         when: 'an event is published'
 
  88             lcmEventsPublisher.publishEvent(testTopic, eventKey, eventHeader, eventData)
 
  89         and: 'topic is polled'
 
  90             def records = legacyEventKafkaConsumer.poll(Duration.ofMillis(1500))
 
  91         then: 'poll returns one record'
 
  92             assert records.size() == 1
 
  93         and: 'record key matches the expected event key'
 
  94             def record = records.iterator().next()
 
  95             assert eventKey == record.key
 
  96         and: 'record matches the expected event'
 
  97             def expectedJsonString = TestUtils.getResourceFileContent('expectedLcmEvent.json')
 
  98             def expectedLcmEvent = jsonObjectMapper.convertJsonString(expectedJsonString, LcmEvent.class)
 
  99             assert expectedLcmEvent == jsonObjectMapper.convertJsonString(record.value, LcmEvent.class)
 
 100         and: 'record header matches the expected parameters'
 
 101             assert SerializationUtils.deserialize(record.headers().lastHeader('eventId').value()) == eventId
 
 102             assert SerializationUtils.deserialize(record.headers().lastHeader('eventCorrelationId').value()) == eventCorrelationId