637e9ebfec0afdf1d7786c1cb91760c603f93cf7
[cps.git] /
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.impl.cmnotificationsubscription.ncmp
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.kafka.MessagingBaseSpec
32 import org.onap.cps.ncmp.impl.cmnotificationsubscription_1_0_0.client_to_ncmp.NcmpInEvent
33 import org.onap.cps.ncmp.utils.TestUtils
34 import org.onap.cps.utils.JsonObjectMapper
35 import org.slf4j.LoggerFactory
36 import org.springframework.beans.factory.annotation.Autowired
37 import org.springframework.boot.test.context.SpringBootTest
38
39 @SpringBootTest(classes = [ObjectMapper, JsonObjectMapper])
40 class NcmpInEventConsumerSpec extends MessagingBaseSpec {
41
42     def mockCmSubscriptionHandler = Mock(CmSubscriptionHandler)
43     def objectUnderTest = new NcmpInEventConsumer(mockCmSubscriptionHandler)
44     def logger = Spy(ListAppender<ILoggingEvent>)
45
46     @Autowired
47     JsonObjectMapper jsonObjectMapper
48
49     @Autowired
50     ObjectMapper objectMapper
51
52     void setup() {
53         ((Logger) LoggerFactory.getLogger(NcmpInEventConsumer.class)).addAppender(logger)
54         logger.start()
55     }
56
57     void cleanup() {
58         ((Logger) LoggerFactory.getLogger(NcmpInEventConsumer.class)).detachAndStopAllAppenders()
59     }
60
61
62     def 'Consume valid CmNotificationSubscriptionNcmpInEvent create message'() {
63         given: 'a cmNotificationSubscription event'
64             def jsonData = TestUtils.getResourceFileContent('cmSubscription/cmNotificationSubscriptionNcmpInEvent.json')
65             def testEventSent = jsonObjectMapper.convertJsonString(jsonData, NcmpInEvent.class)
66             def testCloudEventSent = CloudEventBuilder.v1()
67                 .withData(objectMapper.writeValueAsBytes(testEventSent))
68                 .withId('subscriptionCreated')
69                 .withType('subscriptionCreateRequest')
70                 .withSource(URI.create('some-resource'))
71                 .withExtension('correlationid', 'test-cmhandle1').build()
72             def consumerRecord = new ConsumerRecord<String, CloudEvent>('topic-name', 0, 0, 'event-key', testCloudEventSent)
73         when: 'the valid event is consumed'
74             objectUnderTest.consumeSubscriptionEvent(consumerRecord)
75         then: 'an event is logged with level INFO'
76             def loggingEvent = getLoggingEvent()
77             assert loggingEvent.level == Level.INFO
78         and: 'the log indicates the task completed successfully'
79             assert loggingEvent.formattedMessage == 'Subscription create request for source some-resource with subscription id test-id ...'
80         and: 'the subscription handler service is called once'
81             1 * mockCmSubscriptionHandler.processSubscriptionCreateRequest('test-id',_)
82     }
83
84     def 'Consume valid CmNotificationSubscriptionNcmpInEvent delete message'() {
85         given: 'a cmNotificationSubscription event'
86             def jsonData = TestUtils.getResourceFileContent('cmSubscription/cmNotificationSubscriptionNcmpInEvent.json')
87             def testEventSent = jsonObjectMapper.convertJsonString(jsonData, NcmpInEvent.class)
88             def testCloudEventSent = CloudEventBuilder.v1()
89                 .withData(objectMapper.writeValueAsBytes(testEventSent))
90                 .withId('sub-id')
91                 .withType('subscriptionDeleteRequest')
92                 .withSource(URI.create('some-resource'))
93                 .withExtension('correlationid', 'test-cmhandle1').build()
94             def consumerRecord = new ConsumerRecord<String, CloudEvent>('topic-name', 0, 0, 'event-key', testCloudEventSent)
95         when: 'the valid event is consumed'
96             objectUnderTest.consumeSubscriptionEvent(consumerRecord)
97         then: 'an event is logged with level INFO'
98             def loggingEvent = getLoggingEvent()
99             assert loggingEvent.level == Level.INFO
100         and: 'the log indicates the task completed successfully'
101             assert loggingEvent.formattedMessage == 'Subscription delete request for source some-resource with subscription id test-id ...'
102         and: 'the subscription handler service is called once'
103             1 * mockCmSubscriptionHandler.processSubscriptionDeleteRequest('test-id',_)
104     }
105
106
107     def getLoggingEvent() {
108         return logger.list[1]
109     }
110
111 }