blob: b049774f9b2a488286ef919550ddd429e0f1f6bd [file] [log] [blame]
Shrey Baid64cda472020-04-24 18:58:18 +05301/*
Joey Armstrong2c039362024-02-04 18:51:52 -05002 * Copyright 2018-2024 Open Networking Foundation (ONF) and the ONF Contributors
Shrey Baid64cda472020-04-24 18:58:18 +05303
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7
8 * http://www.apache.org/licenses/LICENSE-2.0
9
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package common
18
19import (
20 "testing"
21
22 "github.com/Shopify/sarama"
23 "gotest.tools/assert"
24)
25
26type mockAsyncProducer struct {
27 input chan *sarama.ProducerMessage
28 successes chan *sarama.ProducerMessage
29 errors chan *sarama.ProducerError
30}
31
32func (p mockAsyncProducer) AsyncClose() {
33
34}
35
36func (p mockAsyncProducer) Close() error {
37 return nil
38}
39
40func (p mockAsyncProducer) Input() chan<- *sarama.ProducerMessage {
41 return p.input
42}
43
44func (p mockAsyncProducer) Successes() <-chan *sarama.ProducerMessage {
45 return p.successes
46}
47
48func (p mockAsyncProducer) Errors() <-chan *sarama.ProducerError {
49 return p.errors
50}
51
52type mockSarama struct{}
53
54func (m mockSarama) NewAsyncProducer(addrs []string, conf *sarama.Config) (sarama.AsyncProducer, error) {
55
56 producer := &mockAsyncProducer{
57 errors: make(chan *sarama.ProducerError),
58 input: make(chan *sarama.ProducerMessage),
59 successes: make(chan *sarama.ProducerMessage),
60 }
61
62 return producer, nil
63}
64
65func TestInitializePublisher(t *testing.T) {
66 mockLib := mockSarama{}
Matteo Scandolo4a036262020-08-17 15:56:13 -070067
68 Config = &GlobalConfig{}
69
Shrey Baid64cda472020-04-24 18:58:18 +053070 err := InitializePublisher(mockLib.NewAsyncProducer, 0)
71
72 assert.Equal(t, err, nil)
73 assert.Equal(t, topic, "BBSim-OLT-0-Events")
74
Matteo Scandolo4a036262020-08-17 15:56:13 -070075 Config.BBSim.KafkaEventTopic = "Testing-Topic"
Shrey Baid64cda472020-04-24 18:58:18 +053076 err = InitializePublisher(mockLib.NewAsyncProducer, 0)
77 assert.Equal(t, topic, "Testing-Topic")
78 assert.Equal(t, err, nil)
79}