blob: bd3dd2e55c867589715c646dcf37cf0b4278cb1c [file] [log] [blame]
Shrey Baid64cda472020-04-24 18:58:18 +05301/*
2 * Copyright 2018-present Open Networking Foundation
3
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{}
67 err := InitializePublisher(mockLib.NewAsyncProducer, 0)
68
69 assert.Equal(t, err, nil)
70 assert.Equal(t, topic, "BBSim-OLT-0-Events")
71
72 Options.BBSim.KafkaEventTopic = "Testing-Topic"
73 err = InitializePublisher(mockLib.NewAsyncProducer, 0)
74 assert.Equal(t, topic, "Testing-Topic")
75 assert.Equal(t, err, nil)
76}