2 * ============LICENSE_START=======================================================
3 * Copyright (c) 2024-2025 OpenInfra Foundation Europe. All rights reserved.
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.datajobs.subscription.dmi
23 import static org.onap.cps.ncmp.impl.datajobs.subscription.models.CmSubscriptionStatus.ACCEPTED
25 import ch.qos.logback.classic.Level
26 import ch.qos.logback.classic.Logger
27 import ch.qos.logback.classic.spi.ILoggingEvent
28 import ch.qos.logback.core.read.ListAppender
29 import com.fasterxml.jackson.databind.ObjectMapper
30 import io.cloudevents.CloudEvent
31 import io.cloudevents.core.builder.CloudEventBuilder
32 import org.apache.kafka.clients.consumer.ConsumerRecord
33 import org.onap.cps.ncmp.impl.datajobs.subscription.dmi_to_ncmp.DataJobSubscriptionDmiOutEvent
34 import org.onap.cps.ncmp.impl.datajobs.subscription.ncmp.CmSubscriptionHandler
35 import org.onap.cps.ncmp.utils.TestUtils
36 import org.onap.cps.utils.JsonObjectMapper
37 import org.slf4j.LoggerFactory
38 import spock.lang.Specification
40 class EventConsumerSpec extends Specification {
42 def logger = new ListAppender<ILoggingEvent>()
43 def objectMapper = new ObjectMapper()
44 def jsonObjectMapper = new JsonObjectMapper(objectMapper)
46 def mockCmSubscriptionHandler = Mock(CmSubscriptionHandler)
47 def objectUnderTest = new EventConsumer(mockCmSubscriptionHandler)
50 ((Logger) LoggerFactory.getLogger(EventConsumer.class)).addAppender(logger)
55 ((Logger) LoggerFactory.getLogger(EventConsumer.class)).detachAndStopAllAppenders()
58 def 'Consume subscription CREATE response with status ACCEPTED from DMI Plugin'() {
59 given: 'a response event from DMI'
60 def jsonData = TestUtils.getResourceFileContent(
61 'datajobs/subscription/cmNotificationSubscriptionDmiOutEvent.json')
62 def testEventSent = jsonObjectMapper.convertJsonString(jsonData, DataJobSubscriptionDmiOutEvent.class)
63 def testCloudEventSent = CloudEventBuilder.v1()
64 .withData(objectMapper.writeValueAsBytes(testEventSent))
65 .withId('random-uuid')
66 .withType('subscriptionCreateResponse')
67 .withSource(URI.create('myDmi'))
68 .withExtension('correlationid', 'sub-1#myDmi').build()
69 def consumerRecord = new ConsumerRecord<String, CloudEvent>('topic-name', 0, 0, 'event-key', testCloudEventSent)
70 when: 'the event is consumed'
71 objectUnderTest.consumeDmiOutEvent(consumerRecord)
72 then: 'an event is logged with level INFO'
73 def loggingEvent = getLoggingEvent()
74 assert loggingEvent.level == Level.INFO
75 and: 'the log indicates the task completed successfully'
76 assert loggingEvent.formattedMessage == 'Consumed DMI subscription response event with details: | correlationId=sub-1#myDmi | eventType=subscriptionCreateResponse'
77 and: 'the subscription handler is called to update status of subscription with correct details'
78 1 * mockCmSubscriptionHandler.updateCmSubscriptionStatus('sub-1', 'myDmi', ACCEPTED)
81 def getLoggingEvent() {