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
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
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.CmDataSubscriptionModelLoader
30 import org.slf4j.LoggerFactory
31 import org.springframework.kafka.support.SendResult
32 import spock.lang.Ignore
33 import spock.lang.Specification
35 class EventPublisherSpec extends Specification {
37 def objectUnderTest = new EventsPublisher(null, null)
38 def logger = (Logger) LoggerFactory.getLogger(objectUnderTest.getClass())
39 def loggingListAppender
42 logger.setLevel(Level.DEBUG)
43 loggingListAppender = new ListAppender()
44 logger.addAppender(loggingListAppender)
45 loggingListAppender.start()
49 ((Logger) LoggerFactory.getLogger(CmDataSubscriptionModelLoader.class)).detachAndStopAllAppenders()
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')
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')
84 def getLoggingEvent() {
85 return loggingListAppender.list[0]