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