summaryrefslogtreecommitdiffstats
path: root/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/events/EventPublisherSpec.groovy
blob: 59a43caf9e85967b12aa2dfa23bfba83a22f4a61 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*
 * ============LICENSE_START========================================================
 * Copyright (c) 2023 Nordix Foundation.
 *  ================================================================================
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an 'AS IS' BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 *  SPDX-License-Identifier: Apache-2.0
 *  ============LICENSE_END=========================================================
 */

package org.onap.cps.ncmp.api.impl.events

import ch.qos.logback.classic.Level
import ch.qos.logback.classic.Logger
import ch.qos.logback.core.read.ListAppender
import org.apache.kafka.clients.producer.ProducerRecord
import org.apache.kafka.clients.producer.RecordMetadata
import org.apache.kafka.common.TopicPartition
import org.onap.cps.ncmp.init.SubscriptionModelLoader
import org.slf4j.LoggerFactory
import org.springframework.kafka.support.SendResult
import spock.lang.Specification

class EventPublisherSpec extends Specification {

    def objectUnderTest = new EventsPublisher(null, null)
    def logger = (Logger) LoggerFactory.getLogger(objectUnderTest.getClass())
    def loggingListAppender

    void setup() {
        logger.setLevel(Level.DEBUG)
        loggingListAppender = new ListAppender()
        logger.addAppender(loggingListAppender)
        loggingListAppender.start()
    }

    void cleanup() {
        ((Logger) LoggerFactory.getLogger(SubscriptionModelLoader.class)).detachAndStopAllAppenders()
    }

    def 'Callback handling on success.'() {
        given: 'a send result'
            def producerRecord = new ProducerRecord('topic-1', 'my value')
            def topicPartition = new TopicPartition('topic-2', 0)
            def recordMetadata = new RecordMetadata(topicPartition, 0, 0, 0, 0, 0)
            def sendResult = new SendResult(producerRecord, recordMetadata)
        when: 'the callback handler processes success'
            def callbackHandler = objectUnderTest.handleCallback('topic-3')
            callbackHandler.onSuccess(sendResult)
        then: 'an event is logged with level DEBUG'
            def loggingEvent = getLoggingEvent()
            loggingEvent.level == Level.DEBUG
        and: 'it contains the topic (from the record metadata) and the "value" (from the producer record)'
            loggingEvent.formattedMessage.contains('topic-2')
            loggingEvent.formattedMessage.contains('my value')
    }


    def 'Callback handling on failure.'() {
        when: 'the callback handler processes a failure'
            def callbackHandler = objectUnderTest.handleCallback('my topic')
            callbackHandler.onFailure(new Exception('my exception'))
        then: 'an event is logged with level ERROR'
            def loggingEvent = getLoggingEvent()
            loggingEvent.level == Level.ERROR
        and: 'it contains the topic and exception message'
            loggingEvent.formattedMessage.contains('my topic')
            loggingEvent.formattedMessage.contains('my exception')
    }

    def getLoggingEvent() {
        return loggingListAppender.list[0]
    }


}