Shrey Baid | 64cda47 | 2020-04-24 18:58:18 +0530 | [diff] [blame] | 1 | /* |
Joey Armstrong | 2c03936 | 2024-02-04 18:51:52 -0500 | [diff] [blame] | 2 | * Copyright 2018-2024 Open Networking Foundation (ONF) and the ONF Contributors |
Shrey Baid | 64cda47 | 2020-04-24 18:58:18 +0530 | [diff] [blame] | 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 | |
| 17 | package common |
| 18 | |
| 19 | import ( |
| 20 | "testing" |
| 21 | |
| 22 | "github.com/Shopify/sarama" |
| 23 | "gotest.tools/assert" |
| 24 | ) |
| 25 | |
| 26 | type mockAsyncProducer struct { |
| 27 | input chan *sarama.ProducerMessage |
| 28 | successes chan *sarama.ProducerMessage |
| 29 | errors chan *sarama.ProducerError |
| 30 | } |
| 31 | |
| 32 | func (p mockAsyncProducer) AsyncClose() { |
| 33 | |
| 34 | } |
| 35 | |
| 36 | func (p mockAsyncProducer) Close() error { |
| 37 | return nil |
| 38 | } |
| 39 | |
| 40 | func (p mockAsyncProducer) Input() chan<- *sarama.ProducerMessage { |
| 41 | return p.input |
| 42 | } |
| 43 | |
| 44 | func (p mockAsyncProducer) Successes() <-chan *sarama.ProducerMessage { |
| 45 | return p.successes |
| 46 | } |
| 47 | |
| 48 | func (p mockAsyncProducer) Errors() <-chan *sarama.ProducerError { |
| 49 | return p.errors |
| 50 | } |
| 51 | |
| 52 | type mockSarama struct{} |
| 53 | |
| 54 | func (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 | |
| 65 | func TestInitializePublisher(t *testing.T) { |
| 66 | mockLib := mockSarama{} |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 67 | |
| 68 | Config = &GlobalConfig{} |
| 69 | |
Shrey Baid | 64cda47 | 2020-04-24 18:58:18 +0530 | [diff] [blame] | 70 | err := InitializePublisher(mockLib.NewAsyncProducer, 0) |
| 71 | |
| 72 | assert.Equal(t, err, nil) |
| 73 | assert.Equal(t, topic, "BBSim-OLT-0-Events") |
| 74 | |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 75 | Config.BBSim.KafkaEventTopic = "Testing-Topic" |
Shrey Baid | 64cda47 | 2020-04-24 18:58:18 +0530 | [diff] [blame] | 76 | err = InitializePublisher(mockLib.NewAsyncProducer, 0) |
| 77 | assert.Equal(t, topic, "Testing-Topic") |
| 78 | assert.Equal(t, err, nil) |
| 79 | } |