|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +package utils |
| 19 | + |
| 20 | +import ( |
| 21 | + "encoding/json" |
| 22 | + "testing" |
| 23 | + |
| 24 | + "github.com/stretchr/testify/assert" |
| 25 | + "github.com/stretchr/testify/require" |
| 26 | +) |
| 27 | + |
| 28 | +func TestConsumerConfigReceiverQueueSizeJSON(t *testing.T) { |
| 29 | + tests := []struct { |
| 30 | + name string |
| 31 | + config ConsumerConfig |
| 32 | + want string |
| 33 | + }{ |
| 34 | + { |
| 35 | + name: "unset is omitted", |
| 36 | + config: ConsumerConfig{}, |
| 37 | + want: `{}`, |
| 38 | + }, |
| 39 | + { |
| 40 | + name: "direct non-zero assignment and other fields remain supported", |
| 41 | + config: ConsumerConfig{ |
| 42 | + SchemaType: "STRING", |
| 43 | + RegexPattern: true, |
| 44 | + ReceiverQueueSize: 100, |
| 45 | + SchemaProperties: map[string]string{"schema": "value"}, |
| 46 | + ConsumerProperties: map[string]string{"consumer": "value"}, |
| 47 | + PoolMessages: true, |
| 48 | + }, |
| 49 | + want: `{ |
| 50 | + "schemaType":"STRING", |
| 51 | + "regexPattern":true, |
| 52 | + "receiverQueueSize":100, |
| 53 | + "schemaProperties":{"schema":"value"}, |
| 54 | + "consumerProperties":{"consumer":"value"}, |
| 55 | + "poolMessages":true |
| 56 | + }`, |
| 57 | + }, |
| 58 | + { |
| 59 | + name: "explicit zero is included", |
| 60 | + config: func() ConsumerConfig { |
| 61 | + config := ConsumerConfig{} |
| 62 | + config.SetReceiverQueueSize(0) |
| 63 | + return config |
| 64 | + }(), |
| 65 | + want: `{"receiverQueueSize":0}`, |
| 66 | + }, |
| 67 | + } |
| 68 | + |
| 69 | + for _, test := range tests { |
| 70 | + t.Run(test.name, func(t *testing.T) { |
| 71 | + data, err := json.Marshal(test.config) |
| 72 | + require.NoError(t, err) |
| 73 | + assert.JSONEq(t, test.want, string(data)) |
| 74 | + }) |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +func TestConsumerConfigReceiverQueueSizeUnmarshalJSON(t *testing.T) { |
| 79 | + tests := []struct { |
| 80 | + name string |
| 81 | + payload string |
| 82 | + want int |
| 83 | + isSet bool |
| 84 | + schema string |
| 85 | + }{ |
| 86 | + {name: "unset", payload: `{}`, want: 0, isSet: false}, |
| 87 | + {name: "explicit zero", payload: `{"receiverQueueSize":0}`, want: 0, isSet: true}, |
| 88 | + { |
| 89 | + name: "non-zero with other fields", |
| 90 | + payload: `{"receiverQueueSize":100,"schemaType":"STRING"}`, |
| 91 | + want: 100, |
| 92 | + isSet: true, |
| 93 | + schema: "STRING", |
| 94 | + }, |
| 95 | + } |
| 96 | + |
| 97 | + for _, test := range tests { |
| 98 | + t.Run(test.name, func(t *testing.T) { |
| 99 | + var config ConsumerConfig |
| 100 | + require.NoError(t, json.Unmarshal([]byte(test.payload), &config)) |
| 101 | + assert.Equal(t, test.want, config.ReceiverQueueSize) |
| 102 | + assert.Equal(t, test.isSet, config.HasReceiverQueueSize()) |
| 103 | + assert.Equal(t, test.schema, config.SchemaType) |
| 104 | + }) |
| 105 | + } |
| 106 | +} |
0 commit comments