|
| 1 | +/* |
| 2 | +Copyright 2025. |
| 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 v1beta1 |
| 18 | + |
| 19 | +import "testing" |
| 20 | + |
| 21 | +func TestDefaultRabbitMqConfig(t *testing.T) { |
| 22 | + tests := []struct { |
| 23 | + name string |
| 24 | + config *RabbitMqConfig |
| 25 | + defaultClusterName string |
| 26 | + wantClusterName string |
| 27 | + wantUser string |
| 28 | + wantVhost string |
| 29 | + }{ |
| 30 | + { |
| 31 | + name: "should set Cluster when it's empty and defaultClusterName is provided", |
| 32 | + config: &RabbitMqConfig{ |
| 33 | + User: "testuser", |
| 34 | + Vhost: "testvhost", |
| 35 | + // Cluster is empty |
| 36 | + }, |
| 37 | + defaultClusterName: "default-rabbitmq", |
| 38 | + wantClusterName: "default-rabbitmq", |
| 39 | + wantUser: "testuser", |
| 40 | + wantVhost: "testvhost", |
| 41 | + }, |
| 42 | + { |
| 43 | + name: "should not override Cluster when it's already set", |
| 44 | + config: &RabbitMqConfig{ |
| 45 | + User: "testuser", |
| 46 | + Vhost: "testvhost", |
| 47 | + Cluster: "existing-cluster", |
| 48 | + }, |
| 49 | + defaultClusterName: "default-rabbitmq", |
| 50 | + wantClusterName: "existing-cluster", |
| 51 | + wantUser: "testuser", |
| 52 | + wantVhost: "testvhost", |
| 53 | + }, |
| 54 | + { |
| 55 | + name: "should not set Cluster when defaultClusterName is empty", |
| 56 | + config: &RabbitMqConfig{ |
| 57 | + User: "testuser", |
| 58 | + Vhost: "testvhost", |
| 59 | + // Cluster is empty |
| 60 | + }, |
| 61 | + defaultClusterName: "", |
| 62 | + wantClusterName: "", |
| 63 | + wantUser: "testuser", |
| 64 | + wantVhost: "testvhost", |
| 65 | + }, |
| 66 | + { |
| 67 | + name: "should not set Cluster when both Cluster and defaultClusterName are empty", |
| 68 | + config: &RabbitMqConfig{ |
| 69 | + User: "testuser", |
| 70 | + Vhost: "testvhost", |
| 71 | + // Cluster is empty |
| 72 | + }, |
| 73 | + defaultClusterName: "", |
| 74 | + wantClusterName: "", |
| 75 | + wantUser: "testuser", |
| 76 | + wantVhost: "testvhost", |
| 77 | + }, |
| 78 | + } |
| 79 | + |
| 80 | + for _, tt := range tests { |
| 81 | + t.Run(tt.name, func(t *testing.T) { |
| 82 | + // Make a copy of config to avoid modifying the original |
| 83 | + configCopy := *tt.config |
| 84 | + config := &configCopy |
| 85 | + |
| 86 | + DefaultRabbitMqConfig(config, tt.defaultClusterName) |
| 87 | + |
| 88 | + if config.Cluster != tt.wantClusterName { |
| 89 | + t.Errorf("Cluster = %q, want %q", config.Cluster, tt.wantClusterName) |
| 90 | + } |
| 91 | + if config.User != tt.wantUser { |
| 92 | + t.Errorf("User = %q, want %q", config.User, tt.wantUser) |
| 93 | + } |
| 94 | + if config.Vhost != tt.wantVhost { |
| 95 | + t.Errorf("Vhost = %q, want %q", config.Vhost, tt.wantVhost) |
| 96 | + } |
| 97 | + }) |
| 98 | + } |
| 99 | +} |
0 commit comments