2 * ============LICENSE_START=======================================================
3 * Copyright (c) 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.api.impl.events.cmsubscription
23 import ch.qos.logback.classic.Level
24 import ch.qos.logback.classic.Logger
25 import ch.qos.logback.classic.spi.ILoggingEvent
26 import ch.qos.logback.core.read.ListAppender
27 import com.fasterxml.jackson.databind.ObjectMapper
28 import io.cloudevents.CloudEvent
29 import io.cloudevents.core.builder.CloudEventBuilder
30 import org.apache.kafka.clients.consumer.ConsumerRecord
31 import org.onap.cps.ncmp.api.impl.events.cmsubscription.consumer.CmNotificationSubscriptionDmiOutEventConsumer
32 import org.onap.cps.ncmp.api.impl.events.cmsubscription.model.CmNotificationSubscriptionStatus
33 import org.onap.cps.ncmp.api.kafka.MessagingBaseSpec
34 import org.onap.cps.ncmp.events.cmnotificationsubscription_merge1_0_0.dmi_to_ncmp.CmNotificationSubscriptionDmiOutEvent
35 import org.onap.cps.ncmp.events.cmnotificationsubscription_merge1_0_0.dmi_to_ncmp.Data
36 import org.onap.cps.ncmp.utils.TestUtils
37 import org.onap.cps.utils.JsonObjectMapper
38 import org.slf4j.LoggerFactory
39 import org.spockframework.spring.SpringBean
40 import org.springframework.beans.factory.annotation.Autowired
41 import org.springframework.boot.test.context.SpringBootTest
43 @SpringBootTest(classes = [ObjectMapper, JsonObjectMapper])
44 class CmNotificationSubscriptionDmiOutEventConsumerSpec extends MessagingBaseSpec {
47 JsonObjectMapper jsonObjectMapper
50 ObjectMapper objectMapper
52 def mockDmiCmNotificationSubscriptionCacheHandler = Mock(DmiCmNotificationSubscriptionCacheHandler)
53 def mockCmNotificationSubscriptionEventsHandler = Mock(CmNotificationSubscriptionEventsHandler)
54 def mockCmNotificationSubscriptionMappersHandler = Mock(CmNotificationSubscriptionMappersHandler)
56 def objectUnderTest = new CmNotificationSubscriptionDmiOutEventConsumer(mockDmiCmNotificationSubscriptionCacheHandler, mockCmNotificationSubscriptionEventsHandler, mockCmNotificationSubscriptionMappersHandler)
57 def logger = Spy(ListAppender<ILoggingEvent>)
60 ((Logger) LoggerFactory.getLogger(CmNotificationSubscriptionDmiOutEventConsumer.class)).addAppender(logger)
65 ((Logger) LoggerFactory.getLogger(CmNotificationSubscriptionDmiOutEventConsumer.class)).detachAndStopAllAppenders()
69 def 'Consume valid CM Subscription response from DMI Plugin'() {
70 given: 'a cmsubscription event'
71 def jsonData = TestUtils.getResourceFileContent('cmSubscription/cmNotificationSubscriptionDmiOutEvent.json')
72 def testEventSent = jsonObjectMapper.convertJsonString(jsonData, CmNotificationSubscriptionDmiOutEvent.class)
73 def testCloudEventSent = CloudEventBuilder.v1()
74 .withData(objectMapper.writeValueAsBytes(testEventSent))
75 .withId('random-uuid')
76 .withType('subscriptionCreateResponse')
77 .withSource(URI.create('test-dmi-plugin-name'))
78 .withExtension('correlationid', 'sub-1#test-dmi-plugin-name').build()
79 def consumerRecord = new ConsumerRecord<String, CloudEvent>('topic-name', 0, 0, 'event-key', testCloudEventSent)
80 when: 'the valid event is consumed'
81 objectUnderTest.consumeCmNotificationSubscriptionDmiOutEvent(consumerRecord)
82 then: 'an event is logged with level INFO'
83 def loggingEvent = getLoggingEvent()
84 assert loggingEvent.level == Level.INFO
85 and: 'the log indicates the task completed successfully'
86 assert loggingEvent.formattedMessage == 'Cm Subscription with id : sub-1 handled by the dmi-plugin : test-dmi-plugin-name has the status : accepted'
89 def 'Consume a valid CM Notification Subscription Event and perform correct actions base on status'() {
90 given: 'a cmNotificationSubscription event'
91 def dmiOutEventData = new Data(statusCode: statusCode, statusMessage: subscriptionStatus.toString())
92 def cmNotificationSubscriptionDmiOutEvent = new CmNotificationSubscriptionDmiOutEvent().withData(dmiOutEventData)
93 def testCloudEventSent = CloudEventBuilder.v1()
94 .withData(objectMapper.writeValueAsBytes(cmNotificationSubscriptionDmiOutEvent))
95 .withId('random-uuid')
96 .withType('subscriptionCreateResponse')
97 .withSource(URI.create('test-dmi-plugin-name'))
98 .withExtension('correlationid', 'sub-1#test-dmi-plugin-name').build()
99 def consumerRecord = new ConsumerRecord<String, CloudEvent>('topic-name', 0, 0, 'event-key', testCloudEventSent)
100 when: 'the event is consumed'
101 objectUnderTest.consumeCmNotificationSubscriptionDmiOutEvent(consumerRecord)
102 then: 'correct number of calls to cache'
103 expectedCacheCalls * mockDmiCmNotificationSubscriptionCacheHandler.updateDmiCmNotificationSubscriptionStatusPerDmi('sub-1','test-dmi-plugin-name', subscriptionStatus)
104 and: 'correct number of calls to persist cache'
105 expectedPersistenceCalls * mockDmiCmNotificationSubscriptionCacheHandler.persistIntoDatabasePerDmi('sub-1','test-dmi-plugin-name')
106 and: 'correct number of calls to map the ncmp out event'
107 1 * mockCmNotificationSubscriptionMappersHandler.toCmNotificationSubscriptionNcmpOutEvent('sub-1', _)
108 and: 'correct number of calls to publish the ncmp out event to client'
109 1 * mockCmNotificationSubscriptionEventsHandler.publishCmNotificationSubscriptionNcmpOutEvent('sub-1', 'subscriptionCreateResponse', _, false)
110 where: 'the following parameters are used'
111 scenario | subscriptionStatus | statusCode || expectedCacheCalls | expectedPersistenceCalls
112 'Accepted Status' | CmNotificationSubscriptionStatus.ACCEPTED | '1' || 1 | 1
113 'Rejected Status' | CmNotificationSubscriptionStatus.REJECTED | '2' || 1 | 0
116 def getLoggingEvent() {
117 return logger.list[0]