Skip to content

Commit 26ff085

Browse files
committed
Make get or default more explicit. Consider empty string as null and return default value.
1 parent 88f3218 commit 26ff085

File tree

3 files changed

+30
-9
lines changed

3 files changed

+30
-9
lines changed

src/main/java/cd/go/contrib/elasticagent/PluginSettings.java

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import com.google.gson.annotations.Expose;
2020
import com.google.gson.annotations.SerializedName;
21+
import org.apache.commons.lang3.StringUtils;
2122
import org.joda.time.Period;
2223

2324
import static cd.go.contrib.elasticagent.utils.Util.GSON;
@@ -29,11 +30,11 @@ public class PluginSettings {
2930

3031
@Expose
3132
@SerializedName("auto_register_timeout")
32-
private Integer autoRegisterTimeout = 10;
33+
private Integer autoRegisterTimeout;
3334

3435
@Expose
3536
@SerializedName("pending_pods_count")
36-
private Integer maxPendingPods = 10;
37+
private Integer maxPendingPods;
3738

3839
@Expose
3940
@SerializedName("kubernetes_cluster_url")
@@ -49,7 +50,7 @@ public class PluginSettings {
4950

5051
@Expose
5152
@SerializedName("namespace")
52-
private String namespace = "default";
53+
private String namespace;
5354

5455
private Period autoRegisterPeriod;
5556

@@ -74,11 +75,11 @@ public Period getAutoRegisterPeriod() {
7475
}
7576

7677
Integer getAutoRegisterTimeout() {
77-
return autoRegisterTimeout;
78+
return getOrDefault(autoRegisterTimeout, 10);
7879
}
7980

8081
public Integer getMaxPendingPods() {
81-
return Integer.valueOf(maxPendingPods);
82+
return getOrDefault(this.maxPendingPods, 10);
8283
}
8384

8485
public String getGoServerUrl() {
@@ -98,7 +99,19 @@ public String getCaCertData() {
9899
}
99100

100101
public String getNamespace() {
101-
return namespace;
102+
return getOrDefault(this.namespace, "default");
103+
}
104+
105+
private <T> T getOrDefault(T t, T defaultValue) {
106+
if (t instanceof String && StringUtils.isBlank(String.valueOf(t))) {
107+
return defaultValue;
108+
}
109+
110+
if (t == null) {
111+
return defaultValue;
112+
}
113+
114+
return t;
102115
}
103116

104117
@Override

src/main/java/cd/go/contrib/elasticagent/executors/ValidateConfigurationExecutor.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,9 @@ public GoPluginApiResponse execute() throws ServerRequestFailedException {
7676
private void validateNamespaceExistence() {
7777
try {
7878
final String namespace = validatePluginSettingsRequest.getPluginSettingsMap().getNamespace();
79-
if (isBlank(namespace)) {
80-
return;
81-
}
8279
final KubernetesClient client = factory.client(validatePluginSettingsRequest.getPluginSettingsMap());
8380
final List<Namespace> namespaceList = client.namespaces().list().getItems();
81+
8482
if (namespaceList.stream().anyMatch(n -> n.getMetadata().getName().equals(namespace))) {
8583
return;
8684
}

src/test/java/cd/go/contrib/elasticagent/PluginSettingsTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,14 @@ public void shouldHaveDefaultValueAfterDeSerialization() {
6262
assertNull(pluginSettings.getClusterUrl());
6363
assertNull(pluginSettings.getCaCertData());
6464
}
65+
66+
@Test
67+
public void shouldConsiderBlankStringAsNull() {
68+
final Map<String, Object> pluginSettingsMap = new HashMap<>();
69+
pluginSettingsMap.put("namespace", " ");
70+
71+
PluginSettings pluginSettings = PluginSettings.fromJSON(new Gson().toJson(pluginSettingsMap));
72+
73+
assertThat(pluginSettings.getNamespace(), is("default"));
74+
}
6575
}

0 commit comments

Comments
 (0)