diff --git a/kafka-ui-api/src/main/java/com/provectus/kafka/ui/config/ClustersProperties.java b/kafka-ui-api/src/main/java/com/provectus/kafka/ui/config/ClustersProperties.java index 04efdae3c3..8befe58efc 100644 --- a/kafka-ui-api/src/main/java/com/provectus/kafka/ui/config/ClustersProperties.java +++ b/kafka-ui-api/src/main/java/com/provectus/kafka/ui/config/ClustersProperties.java @@ -1,12 +1,16 @@ package com.provectus.kafka.ui.config; import java.util.ArrayList; +import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Properties; +import java.util.Set; +import javax.annotation.PostConstruct; import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Configuration; +import org.springframework.util.StringUtils; @Configuration @ConfigurationProperties("kafka") @@ -49,4 +53,31 @@ public class ClustersProperties { String username; String password; } + + @PostConstruct + public void validateAndSetDefaults() { + validateClusterNames(); + } + + private void validateClusterNames() { + // if only one cluster provided it is ok not to set name + if (clusters.size() == 1 && StringUtils.isEmpty(clusters.get(0).getName())) { + clusters.get(0).setName("Default"); + return; + } + + Set clusterNames = new HashSet<>(); + for (Cluster clusterProperties : clusters) { + if (StringUtils.isEmpty(clusterProperties.getName())) { + throw new IllegalStateException( + "Application config isn't valid. " + + "Cluster names should be provided in case of multiple clusters present"); + } + if (!clusterNames.add(clusterProperties.getName())) { + throw new IllegalStateException( + "Application config isn't valid. Two clusters can't have the same name"); + } + } + } + } diff --git a/kafka-ui-api/src/main/java/com/provectus/kafka/ui/service/ClustersStorage.java b/kafka-ui-api/src/main/java/com/provectus/kafka/ui/service/ClustersStorage.java index a1fb3bea01..61b6e15510 100644 --- a/kafka-ui-api/src/main/java/com/provectus/kafka/ui/service/ClustersStorage.java +++ b/kafka-ui-api/src/main/java/com/provectus/kafka/ui/service/ClustersStorage.java @@ -27,10 +27,6 @@ public class ClustersStorage { @PostConstruct public void init() { for (ClustersProperties.Cluster clusterProperties : clusterProperties.getClusters()) { - if (kafkaClusters.get(clusterProperties.getName()) != null) { - throw new IllegalStateException( - "Application config isn't correct. Two clusters can't have the same name"); - } KafkaCluster cluster = clusterMapper.toKafkaCluster(clusterProperties); kafkaClusters.put( clusterProperties.getName(), diff --git a/kafka-ui-api/src/test/java/com/provectus/kafka/ui/config/ClustersPropertiesTest.java b/kafka-ui-api/src/test/java/com/provectus/kafka/ui/config/ClustersPropertiesTest.java new file mode 100644 index 0000000000..be8a6bdf89 --- /dev/null +++ b/kafka-ui-api/src/test/java/com/provectus/kafka/ui/config/ClustersPropertiesTest.java @@ -0,0 +1,51 @@ +package com.provectus.kafka.ui.config; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import java.util.Collections; +import org.junit.jupiter.api.Test; + +class ClustersPropertiesTest { + + @Test + void clusterNamesShouldBeUniq() { + ClustersProperties properties = new ClustersProperties(); + var c1 = new ClustersProperties.Cluster(); + c1.setName("test"); + var c2 = new ClustersProperties.Cluster(); + c2.setName("test"); //same name + + Collections.addAll(properties.getClusters(), c1, c2); + + assertThatThrownBy(properties::validateAndSetDefaults) + .hasMessageContaining("Application config isn't valid"); + } + + @Test + void clusterNamesShouldSetIfMultipleClustersProvided() { + ClustersProperties properties = new ClustersProperties(); + var c1 = new ClustersProperties.Cluster(); + c1.setName("test1"); + var c2 = new ClustersProperties.Cluster(); //name not set + + Collections.addAll(properties.getClusters(), c1, c2); + + assertThatThrownBy(properties::validateAndSetDefaults) + .hasMessageContaining("Application config isn't valid"); + } + + @Test + void ifOnlyOneClusterProvidedNameIsOptionalAndSetToDefault() { + ClustersProperties properties = new ClustersProperties(); + properties.getClusters().add(new ClustersProperties.Cluster()); + + properties.validateAndSetDefaults(); + + assertThat(properties.getClusters()) + .element(0) + .extracting("name") + .isEqualTo("Default"); + } + +}