Handle Subscription Response from DMI
[cps.git] / cps-ncmp-service / src / test / groovy / org / onap / cps / ncmp / api / impl / events / cmsubscription / CmNotificationSubscriptionDmiOutEventConsumerSpec.groovy
1 /*
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
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.events.cmsubscription
22
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.model.CmNotificationSubscriptionStatus
32 import org.onap.cps.ncmp.api.kafka.MessagingBaseSpec
33 import org.onap.cps.ncmp.events.cmnotificationsubscription_merge1_0_0.dmi_to_ncmp.CmNotificationSubscriptionDmiOutEvent
34 import org.onap.cps.ncmp.events.cmnotificationsubscription_merge1_0_0.dmi_to_ncmp.Data
35 import org.onap.cps.ncmp.utils.TestUtils
36 import org.onap.cps.utils.JsonObjectMapper
37 import org.slf4j.LoggerFactory
38 import org.spockframework.spring.SpringBean
39 import org.springframework.beans.factory.annotation.Autowired
40 import org.springframework.boot.test.context.SpringBootTest
41
42 @SpringBootTest(classes = [ObjectMapper, JsonObjectMapper])
43 class CmNotificationSubscriptionDmiOutEventConsumerSpec extends MessagingBaseSpec {
44
45     @Autowired
46     JsonObjectMapper jsonObjectMapper
47
48     @Autowired
49     ObjectMapper objectMapper
50
51     @SpringBean
52     DmiCmNotificationSubscriptionCacheHandler mockDmiCmNotificationSubscriptionCacheHandler = Mock(DmiCmNotificationSubscriptionCacheHandler)
53
54     def objectUnderTest = new CmNotificationSubscriptionDmiOutEventConsumer(mockDmiCmNotificationSubscriptionCacheHandler)
55     def logger = Spy(ListAppender<ILoggingEvent>)
56
57     void setup() {
58         ((Logger) LoggerFactory.getLogger(CmNotificationSubscriptionDmiOutEventConsumer.class)).addAppender(logger)
59         logger.start()
60     }
61
62     void cleanup() {
63         ((Logger) LoggerFactory.getLogger(CmNotificationSubscriptionDmiOutEventConsumer.class)).detachAndStopAllAppenders()
64     }
65
66
67     def 'Consume valid CM Subscription response from DMI Plugin'() {
68         given: 'a cmsubscription event'
69             def jsonData = TestUtils.getResourceFileContent('cmSubscription/cmNotificationSubscriptionDmiOutEvent.json')
70             def testEventSent = jsonObjectMapper.convertJsonString(jsonData, CmNotificationSubscriptionDmiOutEvent.class)
71             def testCloudEventSent = CloudEventBuilder.v1()
72                 .withData(objectMapper.writeValueAsBytes(testEventSent))
73                 .withId('random-uuid')
74                 .withType('subscriptionCreateResponse')
75                 .withSource(URI.create('test-dmi-plugin-name'))
76                 .withExtension('correlationid', 'sub-1#test-dmi-plugin-name').build()
77             def consumerRecord = new ConsumerRecord<String, CloudEvent>('topic-name', 0, 0, 'event-key', testCloudEventSent)
78         when: 'the valid event is consumed'
79             objectUnderTest.consumeCmNotificationSubscriptionDmiOutEvent(consumerRecord)
80         then: 'an event is logged with level INFO'
81             def loggingEvent = getLoggingEvent()
82             assert loggingEvent.level == Level.INFO
83         and: 'the log indicates the task completed successfully'
84             assert loggingEvent.formattedMessage == 'Cm Subscription with id : sub-1 handled by the dmi-plugin : test-dmi-plugin-name has the status : accepted'
85     }
86
87     def 'Consume a valid CM Notification Subscription Event and perform correct actions base on status'() {
88         given: 'a cmNotificationSubscription event'
89             def dmiOutEventData = new Data(statusCode: statusCode, statusMessage: subscriptionStatus.toString())
90             def cmNotificationSubscriptionDmiOutEvent = new CmNotificationSubscriptionDmiOutEvent().withData(dmiOutEventData)
91             def testCloudEventSent = CloudEventBuilder.v1()
92                 .withData(objectMapper.writeValueAsBytes(cmNotificationSubscriptionDmiOutEvent))
93                 .withId('random-uuid')
94                 .withType('subscriptionCreateResponse')
95                 .withSource(URI.create('test-dmi-plugin-name'))
96                 .withExtension('correlationid', 'sub-1#test-dmi-plugin-name').build()
97             def consumerRecord = new ConsumerRecord<String, CloudEvent>('topic-name', 0, 0, 'event-key', testCloudEventSent)
98         when: 'the event is consumed'
99             objectUnderTest.consumeCmNotificationSubscriptionDmiOutEvent(consumerRecord)
100         then: 'correct number of calls to cache'
101             expectedCacheCalls * mockDmiCmNotificationSubscriptionCacheHandler.updateDmiCmNotificationSubscriptionStatusPerDmi('sub-1','test-dmi-plugin-name', subscriptionStatus)
102         and: 'correct number of calls to persist cache'
103             expectedPersistenceCalls * mockDmiCmNotificationSubscriptionCacheHandler.persistIntoDatabasePerDmi('sub-1','test-dmi-plugin-name')
104         where: 'the following parameters are used'
105             scenario            | subscriptionStatus                            | statusCode || expectedCacheCalls | expectedPersistenceCalls
106             'Accepted Status'   | CmNotificationSubscriptionStatus.ACCEPTED     | '1'        || 1                  | 1
107             'Rejected Status'   | CmNotificationSubscriptionStatus.REJECTED     | '2'        || 1                  | 0
108     }
109
110     def getLoggingEvent() {
111         return logger.list[0]
112     }
113
114 }