|
| 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 | +) |
| 26 | + |
| 27 | +func TestNewAutoSubscriptionCreationOverride(t *testing.T) { |
| 28 | + // Test that constructor returns non-nil pointer |
| 29 | + override := NewAutoSubscriptionCreationOverride() |
| 30 | + assert.NotNil(t, override) |
| 31 | + |
| 32 | + // Test that default value is set correctly |
| 33 | + assert.Equal(t, false, override.AllowAutoSubscriptionCreation) |
| 34 | + |
| 35 | + // Test that it returns pointer to struct |
| 36 | + assert.IsType(t, &AutoSubscriptionCreationOverride{}, override) |
| 37 | +} |
| 38 | + |
| 39 | +func TestAutoSubscriptionCreationOverride_JSONSerialization(t *testing.T) { |
| 40 | + tests := []struct { |
| 41 | + name string |
| 42 | + override AutoSubscriptionCreationOverride |
| 43 | + expected string |
| 44 | + }{ |
| 45 | + { |
| 46 | + name: "False value serialization", |
| 47 | + override: AutoSubscriptionCreationOverride{AllowAutoSubscriptionCreation: false}, |
| 48 | + expected: `{"allowAutoSubscriptionCreation":false}`, |
| 49 | + }, |
| 50 | + { |
| 51 | + name: "True value serialization", |
| 52 | + override: AutoSubscriptionCreationOverride{AllowAutoSubscriptionCreation: true}, |
| 53 | + expected: `{"allowAutoSubscriptionCreation":true}`, |
| 54 | + }, |
| 55 | + { |
| 56 | + name: "Default constructor serialization", |
| 57 | + override: *NewAutoSubscriptionCreationOverride(), |
| 58 | + expected: `{"allowAutoSubscriptionCreation":false}`, |
| 59 | + }, |
| 60 | + } |
| 61 | + |
| 62 | + for _, tt := range tests { |
| 63 | + t.Run(tt.name, func(t *testing.T) { |
| 64 | + jsonData, err := json.Marshal(tt.override) |
| 65 | + assert.NoError(t, err) |
| 66 | + assert.Equal(t, tt.expected, string(jsonData)) |
| 67 | + }) |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +func TestAutoSubscriptionCreationOverride_JSONDeserialization(t *testing.T) { |
| 72 | + tests := []struct { |
| 73 | + name string |
| 74 | + jsonData string |
| 75 | + expected AutoSubscriptionCreationOverride |
| 76 | + wantErr bool |
| 77 | + }{ |
| 78 | + { |
| 79 | + name: "False value deserialization", |
| 80 | + jsonData: `{"allowAutoSubscriptionCreation":false}`, |
| 81 | + expected: AutoSubscriptionCreationOverride{AllowAutoSubscriptionCreation: false}, |
| 82 | + wantErr: false, |
| 83 | + }, |
| 84 | + { |
| 85 | + name: "True value deserialization", |
| 86 | + jsonData: `{"allowAutoSubscriptionCreation":true}`, |
| 87 | + expected: AutoSubscriptionCreationOverride{AllowAutoSubscriptionCreation: true}, |
| 88 | + wantErr: false, |
| 89 | + }, |
| 90 | + { |
| 91 | + name: "Empty JSON object", |
| 92 | + jsonData: `{}`, |
| 93 | + expected: AutoSubscriptionCreationOverride{AllowAutoSubscriptionCreation: false}, |
| 94 | + wantErr: false, |
| 95 | + }, |
| 96 | + { |
| 97 | + name: "Extra fields ignored", |
| 98 | + jsonData: `{"allowAutoSubscriptionCreation":true,"extraField":"ignored"}`, |
| 99 | + expected: AutoSubscriptionCreationOverride{AllowAutoSubscriptionCreation: true}, |
| 100 | + wantErr: false, |
| 101 | + }, |
| 102 | + } |
| 103 | + |
| 104 | + for _, tt := range tests { |
| 105 | + t.Run(tt.name, func(t *testing.T) { |
| 106 | + var override AutoSubscriptionCreationOverride |
| 107 | + err := json.Unmarshal([]byte(tt.jsonData), &override) |
| 108 | + |
| 109 | + if tt.wantErr { |
| 110 | + assert.Error(t, err) |
| 111 | + } else { |
| 112 | + assert.NoError(t, err) |
| 113 | + assert.Equal(t, tt.expected, override) |
| 114 | + } |
| 115 | + }) |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +func TestAutoSubscriptionCreationOverride_FieldModification(t *testing.T) { |
| 120 | + // Test field modification on constructor-created instance |
| 121 | + override := NewAutoSubscriptionCreationOverride() |
| 122 | + |
| 123 | + // Initial state should be false |
| 124 | + assert.Equal(t, false, override.AllowAutoSubscriptionCreation) |
| 125 | + |
| 126 | + // Modify to true |
| 127 | + override.AllowAutoSubscriptionCreation = true |
| 128 | + assert.Equal(t, true, override.AllowAutoSubscriptionCreation) |
| 129 | + |
| 130 | + // Modify back to false |
| 131 | + override.AllowAutoSubscriptionCreation = false |
| 132 | + assert.Equal(t, false, override.AllowAutoSubscriptionCreation) |
| 133 | + |
| 134 | + // Test field modification on manually created instance |
| 135 | + manual := &AutoSubscriptionCreationOverride{ |
| 136 | + AllowAutoSubscriptionCreation: true, |
| 137 | + } |
| 138 | + assert.Equal(t, true, manual.AllowAutoSubscriptionCreation) |
| 139 | + |
| 140 | + manual.AllowAutoSubscriptionCreation = false |
| 141 | + assert.Equal(t, false, manual.AllowAutoSubscriptionCreation) |
| 142 | +} |
| 143 | + |
| 144 | +func TestAutoSubscriptionCreationOverride_InvalidJSON(t *testing.T) { |
| 145 | + tests := []struct { |
| 146 | + name string |
| 147 | + jsonData string |
| 148 | + }{ |
| 149 | + { |
| 150 | + name: "Invalid JSON syntax", |
| 151 | + jsonData: `{"allowAutoSubscriptionCreation":false`, |
| 152 | + }, |
| 153 | + { |
| 154 | + name: "Invalid boolean value", |
| 155 | + jsonData: `{"allowAutoSubscriptionCreation":"invalid"}`, |
| 156 | + }, |
| 157 | + { |
| 158 | + name: "Invalid JSON structure", |
| 159 | + jsonData: `[{"allowAutoSubscriptionCreation":true}]`, |
| 160 | + }, |
| 161 | + { |
| 162 | + name: "Completely invalid JSON", |
| 163 | + jsonData: `invalid json`, |
| 164 | + }, |
| 165 | + } |
| 166 | + |
| 167 | + for _, tt := range tests { |
| 168 | + t.Run(tt.name, func(t *testing.T) { |
| 169 | + var override AutoSubscriptionCreationOverride |
| 170 | + err := json.Unmarshal([]byte(tt.jsonData), &override) |
| 171 | + assert.Error(t, err) |
| 172 | + }) |
| 173 | + } |
| 174 | +} |
0 commit comments