Revert "Migrate CPS to Spring-boot 3.0"
[cps.git] / cps-ncmp-service / src / test / groovy / org / onap / cps / ncmp / api / impl / events / EventPublisherSpec.groovy
1 /*
2  * ============LICENSE_START========================================================
3  * Copyright (c) 2023 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
22
23 import ch.qos.logback.classic.Level
24 import ch.qos.logback.classic.Logger
25 import ch.qos.logback.core.read.ListAppender
26 import org.apache.kafka.clients.producer.ProducerRecord
27 import org.apache.kafka.clients.producer.RecordMetadata
28 import org.apache.kafka.common.TopicPartition
29 import org.onap.cps.ncmp.init.SubscriptionModelLoader
30 import org.slf4j.LoggerFactory
31 import org.springframework.kafka.support.SendResult
32 import spock.lang.Specification
33
34 class EventPublisherSpec extends Specification {
35
36     def objectUnderTest = new EventsPublisher(null, null)
37     def logger = (Logger) LoggerFactory.getLogger(objectUnderTest.getClass())
38     def loggingListAppender
39
40     void setup() {
41         logger.setLevel(Level.DEBUG)
42         loggingListAppender = new ListAppender()
43         logger.addAppender(loggingListAppender)
44         loggingListAppender.start()
45     }
46
47     void cleanup() {
48         ((Logger) LoggerFactory.getLogger(SubscriptionModelLoader.class)).detachAndStopAllAppenders()
49     }
50
51     def 'Callback handling on success.'() {
52         given: 'a send result'
53             def producerRecord = new ProducerRecord('topic-1', 'my value')
54             def topicPartition = new TopicPartition('topic-2', 0)
55             def recordMetadata = new RecordMetadata(topicPartition, 0, 0, 0, 0, 0)
56             def sendResult = new SendResult(producerRecord, recordMetadata)
57         when: 'the callback handler processes success'
58             def callbackHandler = objectUnderTest.handleCallback('topic-3')
59             callbackHandler.onSuccess(sendResult)
60         then: 'an event is logged with level DEBUG'
61             def loggingEvent = getLoggingEvent()
62             loggingEvent.level == Level.DEBUG
63         and: 'it contains the topic (from the record metadata) and the "value" (from the producer record)'
64             loggingEvent.formattedMessage.contains('topic-2')
65             loggingEvent.formattedMessage.contains('my value')
66     }
67
68
69     def 'Callback handling on failure.'() {
70         when: 'the callback handler processes a failure'
71             def callbackHandler = objectUnderTest.handleCallback('my topic')
72             callbackHandler.onFailure(new Exception('my exception'))
73         then: 'an event is logged with level ERROR'
74             def loggingEvent = getLoggingEvent()
75             loggingEvent.level == Level.ERROR
76         and: 'it contains the topic and exception message'
77             loggingEvent.formattedMessage.contains('my topic')
78             loggingEvent.formattedMessage.contains('my exception')
79     }
80
81     def getLoggingEvent() {
82         return loggingListAppender.list[0]
83     }
84
85
86 }