d0f1afd5d5b6212fe97d729edc1c34b737883601
[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.Ignore
33 import spock.lang.Specification
34
35 class EventPublisherSpec extends Specification {
36
37     def objectUnderTest = new EventsPublisher(null, null)
38     def logger = (Logger) LoggerFactory.getLogger(objectUnderTest.getClass())
39     def loggingListAppender
40
41     void setup() {
42         logger.setLevel(Level.DEBUG)
43         loggingListAppender = new ListAppender()
44         logger.addAppender(loggingListAppender)
45         loggingListAppender.start()
46     }
47
48     void cleanup() {
49         ((Logger) LoggerFactory.getLogger(SubscriptionModelLoader.class)).detachAndStopAllAppenders()
50     }
51
52     @Ignore
53     def 'Callback handling on success.'() {
54         given: 'a send result'
55             def producerRecord = new ProducerRecord('topic-1', 'my value')
56             def topicPartition = new TopicPartition('topic-2', 0)
57             def recordMetadata = new RecordMetadata(topicPartition, 0, 0, 0, 0, 0)
58             def sendResult = new SendResult(producerRecord, recordMetadata)
59         when: 'the callback handler processes success'
60             def callbackHandler = objectUnderTest.handleCallback('topic-3')
61             callbackHandler.onSuccess(sendResult)
62         then: 'an event is logged with level DEBUG'
63             def loggingEvent = getLoggingEvent()
64             loggingEvent.level == Level.DEBUG
65         and: 'it contains the topic (from the record metadata) and the "value" (from the producer record)'
66             loggingEvent.formattedMessage.contains('topic-2')
67             loggingEvent.formattedMessage.contains('my value')
68     }
69
70
71     @Ignore
72     def 'Callback handling on failure.'() {
73         when: 'the callback handler processes a failure'
74             def callbackHandler = objectUnderTest.handleCallback('my topic')
75             callbackHandler.onFailure(new Exception('my exception'))
76         then: 'an event is logged with level ERROR'
77             def loggingEvent = getLoggingEvent()
78             loggingEvent.level == Level.ERROR
79         and: 'it contains the topic and exception message'
80             loggingEvent.formattedMessage.contains('my topic')
81             loggingEvent.formattedMessage.contains('my exception')
82     }
83
84     def getLoggingEvent() {
85         return loggingListAppender.list[0]
86     }
87
88
89 }