|
| 1 | +/** |
| 2 | + * Copyright 2016 Confluent Inc. |
| 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 kafka |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "testing" |
| 22 | +) |
| 23 | + |
| 24 | +// A custom type with Stringer interface to be used to test config map APIs |
| 25 | +type HostPortType struct { |
| 26 | + Host string |
| 27 | + Port int |
| 28 | +} |
| 29 | + |
| 30 | +// implements String() interface |
| 31 | +func (hp HostPortType) String() string { |
| 32 | + return fmt.Sprintf("%s:%d", hp.Host, hp.Port) |
| 33 | +} |
| 34 | + |
| 35 | +//Test config map APIs |
| 36 | +func TestConfigMapAPIs(t *testing.T) { |
| 37 | + config := &ConfigMap{} |
| 38 | + |
| 39 | + // set a good key via SetKey() |
| 40 | + err := config.SetKey("bootstrap.servers", testconf.Brokers) |
| 41 | + if err != nil { |
| 42 | + t.Errorf("Failed to set key via SetKey(). Error: %s\n", err) |
| 43 | + } |
| 44 | + |
| 45 | + // test custom Stringer type |
| 46 | + hostPort := HostPortType{Host: "localhost", Port: 9092} |
| 47 | + err = config.SetKey("bootstrap.servers", hostPort) |
| 48 | + if err != nil { |
| 49 | + t.Errorf("Failed to set custom Stringer type via SetKey(). Error: %s\n", err) |
| 50 | + } |
| 51 | + |
| 52 | + // test boolean type |
| 53 | + err = config.SetKey("{topic}.produce.offset.report", true) |
| 54 | + if err != nil { |
| 55 | + t.Errorf("Failed to set key via SetKey(). Error: %s\n", err) |
| 56 | + } |
| 57 | + |
| 58 | + // test offset literal string |
| 59 | + err = config.SetKey("{topic}.auto.offset.reset", "earliest") |
| 60 | + if err != nil { |
| 61 | + t.Errorf("Failed to set key via SetKey(). Error: %s\n", err) |
| 62 | + } |
| 63 | + |
| 64 | + //test offset constant |
| 65 | + err = config.SetKey("{topic}.auto.offset.reset", OffsetBeginning) |
| 66 | + if err != nil { |
| 67 | + t.Errorf("Failed to set key via SetKey(). Error: %s\n", err) |
| 68 | + } |
| 69 | + |
| 70 | + //test integer offset |
| 71 | + err = config.SetKey("{topic}.message.timeout.ms", 10) |
| 72 | + if err != nil { |
| 73 | + t.Errorf("Failed to set integer value via SetKey(). Error: %s\n", err) |
| 74 | + } |
| 75 | + |
| 76 | + // set a good key-value pair via Set() |
| 77 | + err = config.Set("group.id=test.id") |
| 78 | + if err != nil { |
| 79 | + t.Errorf("Failed to set key-value pair via Set(). Error: %s\n", err) |
| 80 | + } |
| 81 | + |
| 82 | + // negative test cases |
| 83 | + // set a bad key-value pair via Set() |
| 84 | + err = config.Set("group.id:test.id") |
| 85 | + if err == nil { |
| 86 | + t.Errorf("Expected failure when setting invalid key-value pair via Set()") |
| 87 | + } |
| 88 | + |
| 89 | +} |
0 commit comments