ISSUE-993: Cluster names validation moved to ClustersProperties, added default n… (#1010)
This commit is contained in:
parent
77226a2144
commit
335e0ea983
3 changed files with 82 additions and 4 deletions
|
@ -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");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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(),
|
||||
|
|
|
@ -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");
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Reference in a new issue