Skip to content

Commit

Permalink
empty properties treated as null (#201)
Browse files Browse the repository at this point in the history
  • Loading branch information
azatkhakimov committed Apr 16, 2020
1 parent bb07897 commit 17a9570
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 5 deletions.
11 changes: 6 additions & 5 deletions src/main/java/com/hazelcast/kubernetes/KubernetesConfig.java
Expand Up @@ -219,22 +219,23 @@ private String getProperty(String prefix, PropertyDefinition property) {
}

private void validateConfig() {
if (serviceDns != null && (serviceName != null || serviceLabelName != null || podLabelName != null)) {
if (!StringUtil.isNullOrEmptyAfterTrim(serviceDns) && (!StringUtil.isNullOrEmptyAfterTrim(serviceName)
|| !StringUtil.isNullOrEmptyAfterTrim(serviceLabelName) || !StringUtil.isNullOrEmptyAfterTrim(podLabelName))) {
throw new InvalidConfigurationException(
String.format("Properties '%s' and ('%s' or '%s' or %s) cannot be defined at the same time",
SERVICE_DNS.key(), SERVICE_NAME.key(), SERVICE_LABEL_NAME.key(), POD_LABEL_NAME.key()));
}
if (serviceName != null && serviceLabelName != null) {
if (!StringUtil.isNullOrEmptyAfterTrim(serviceName) && !StringUtil.isNullOrEmptyAfterTrim(serviceLabelName)) {
throw new InvalidConfigurationException(
String.format("Properties '%s' and '%s' cannot be defined at the same time",
SERVICE_NAME.key(), SERVICE_LABEL_NAME.key()));
}
if (serviceName != null && podLabelName != null) {
if (!StringUtil.isNullOrEmptyAfterTrim(serviceName) && !StringUtil.isNullOrEmptyAfterTrim(podLabelName)) {
throw new InvalidConfigurationException(
String.format("Properties '%s' and '%s' cannot be defined at the same time",
SERVICE_NAME.key(), POD_LABEL_NAME.key()));
}
if (serviceLabelName != null && podLabelName != null) {
if (!StringUtil.isNullOrEmptyAfterTrim(serviceLabelName) && !StringUtil.isNullOrEmptyAfterTrim(podLabelName)) {
throw new InvalidConfigurationException(
String.format("Properties '%s' and '%s' cannot be defined at the same time",
SERVICE_LABEL_NAME.key(), POD_LABEL_NAME.key()));
Expand All @@ -254,7 +255,7 @@ private void validateConfig() {
}

DiscoveryMode getMode() {
if (serviceDns != null) {
if (!StringUtil.isNullOrEmptyAfterTrim(serviceDns)) {
return DiscoveryMode.DNS_LOOKUP;
} else {
return DiscoveryMode.KUBERNETES_API;
Expand Down
48 changes: 48 additions & 0 deletions src/test/java/com/hazelcast/kubernetes/KubernetesConfigTest.java
Expand Up @@ -275,4 +275,52 @@ private static String createTestFile(String expectedContents)
}
return temp.getAbsolutePath();
}

@Test
public void propertyServiceNameIsEmpty() {
// given
Map<String, Comparable> properties = createProperties();
properties.put(SERVICE_NAME.key(), " ");
String serviceDns = "service-dns";
properties.put(SERVICE_DNS.key(), serviceDns);

//when
KubernetesConfig config = new KubernetesConfig(properties);

//then
assertEquals(serviceDns, config.getServiceDns());

}

@Test
public void propertyServiceDnsIsNull() {
// given
Map<String, Comparable> properties = createProperties();
String serviceName = "service-name";
properties.put(SERVICE_NAME.key(), serviceName);
properties.put(SERVICE_DNS.key(), null);

//when
KubernetesConfig config = new KubernetesConfig(properties);

//then
assertEquals(serviceName, config.getServiceName());

}

@Test
public void emptyProperties() {
// given
Map<String, Comparable> properties = createProperties();
properties.put(SERVICE_LABEL_NAME.key(), " ");
String serviceLabelValue = "service-label-value";
properties.put(SERVICE_LABEL_VALUE.key(), serviceLabelValue);
properties.put(SERVICE_DNS.key(), "");

//when
KubernetesConfig config = new KubernetesConfig(properties);

//then
assertEquals(serviceLabelValue, config.getServiceLabelValue());
}
}

0 comments on commit 17a9570

Please sign in to comment.