Просмотр исходного кода

ISSUE-993: Cluster names validation moved to ClustersProperties, added default n… (#1010)

Ilya Kuramshin 3 лет назад
Родитель
Сommit
335e0ea983

+ 31 - 0
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<String> 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");
+      }
+    }
+  }
+
 }

+ 0 - 4
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(),

+ 51 - 0
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");
+  }
+
+}