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.CmNotificationSubscriptionNcmpInEventConsumer
32 import org.onap.cps.ncmp.api.impl.events.cmsubscription.service.CmNotificationSubscriptionHandlerService
33 import org.onap.cps.ncmp.api.kafka.MessagingBaseSpec
34 import org.onap.cps.ncmp.events.cmnotificationsubscription_merge1_0_0.client_to_ncmp.CmNotificationSubscriptionNcmpInEvent
35 import org.onap.cps.ncmp.utils.TestUtils
36 import org.onap.cps.utils.JsonObjectMapper
37 import org.slf4j.LoggerFactory
38 import org.springframework.beans.factory.annotation.Autowired
39 import org.springframework.boot.test.context.SpringBootTest
41 @SpringBootTest(classes = [ObjectMapper, JsonObjectMapper])
42 class CmNotificationSubscriptionNcmpInEventConsumerSpec extends MessagingBaseSpec {
44 def mockCmNotificationSubscriptionHandlerService = Mock(CmNotificationSubscriptionHandlerService)
45 def objectUnderTest = new CmNotificationSubscriptionNcmpInEventConsumer(mockCmNotificationSubscriptionHandlerService)
46 def logger = Spy(ListAppender<ILoggingEvent>)
49 JsonObjectMapper jsonObjectMapper
52 ObjectMapper objectMapper
55 ((Logger) LoggerFactory.getLogger(CmNotificationSubscriptionNcmpInEventConsumer.class)).addAppender(logger)
60 ((Logger) LoggerFactory.getLogger(CmNotificationSubscriptionNcmpInEventConsumer.class)).detachAndStopAllAppenders()
64 def 'Consume valid CmNotificationSubscriptionNcmpInEvent create message'() {
65 given: 'a cmNotificationSubscription event'
66 def jsonData = TestUtils.getResourceFileContent('cmSubscription/cmNotificationSubscriptionNcmpInEvent.json')
67 def testEventSent = jsonObjectMapper.convertJsonString(jsonData, CmNotificationSubscriptionNcmpInEvent.class)
68 def testCloudEventSent = CloudEventBuilder.v1()
69 .withData(objectMapper.writeValueAsBytes(testEventSent))
70 .withId('subscriptionCreated')
71 .withType('subscriptionCreateRequest')
72 .withSource(URI.create('some-resource'))
73 .withExtension('correlationid', 'test-cmhandle1').build()
74 def consumerRecord = new ConsumerRecord<String, CloudEvent>('topic-name', 0, 0, 'event-key', testCloudEventSent)
75 and: 'notifications are enabled'
76 objectUnderTest.notificationFeatureEnabled = true
77 when: 'the valid event is consumed'
78 objectUnderTest.consumeSubscriptionEvent(consumerRecord)
79 then: 'an event is logged with level INFO'
80 def loggingEvent = getLoggingEvent()
81 assert loggingEvent.level == Level.INFO
82 and: 'the log indicates the task completed successfully'
83 assert loggingEvent.formattedMessage == 'Subscription for source some-resource with subscription id test-id ...'
84 and: 'the subscription handler service is called once'
85 1 * mockCmNotificationSubscriptionHandlerService.processSubscriptionCreateRequest(_)
88 def getLoggingEvent() {