Skip to content

Commit bade38e

Browse files
authored
Merge pull request #325 from oracle/targetnamespaces-trim-spaces
trim heading and trailing spaces from targetNamespaces input value
2 parents 6c1ac8a + 0325112 commit bade38e

File tree

2 files changed

+81
-4
lines changed

2 files changed

+81
-4
lines changed

operator/src/main/java/oracle/kubernetes/operator/Main.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,8 @@ private static void begin() {
189189
// read the operator configuration
190190
String namespace = getOperatorNamespace();
191191

192-
Collection<String> targetNamespaces = getTargetNamespaces(namespace);
192+
Collection<String> targetNamespaces =
193+
getTargetNamespaces(tuningAndConfig.get("targetNamespaces"), namespace);
193194

194195
String serviceAccountName = tuningAndConfig.get("serviceaccount");
195196
if (serviceAccountName == null) {
@@ -683,14 +684,13 @@ public void onThrowable(Packet packet, Throwable throwable) {
683684
*
684685
* @return the collection of target namespace names
685686
*/
686-
private static Collection<String> getTargetNamespaces(String namespace) {
687+
private static Collection<String> getTargetNamespaces(String tnValue, String namespace) {
687688
Collection<String> targetNamespaces = new ArrayList<>();
688689

689-
String tnValue = tuningAndConfig.get("targetNamespaces");
690690
if (tnValue != null) {
691691
StringTokenizer st = new StringTokenizer(tnValue, ",");
692692
while (st.hasMoreTokens()) {
693-
targetNamespaces.add(st.nextToken());
693+
targetNamespaces.add(st.nextToken().trim());
694694
}
695695
}
696696

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// Copyright 2018, Oracle Corporation and/or its affiliates. All rights reserved.
2+
// Licensed under the Universal Permissive License v 1.0 as shown at
3+
// http://oss.oracle.com/licenses/upl.
4+
5+
package oracle.kubernetes.operator;
6+
7+
import static org.junit.Assert.assertFalse;
8+
import static org.junit.Assert.assertTrue;
9+
10+
import java.lang.reflect.InvocationTargetException;
11+
import java.lang.reflect.Method;
12+
import java.util.Collection;
13+
import org.junit.Test;
14+
15+
public class MainTest {
16+
17+
@Test
18+
public void getTargetNamespaces_withEmptyValue_should_return_default()
19+
throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
20+
Collection<String> namespaces = invoke_getTargetNamespaces("", "default");
21+
assertTrue(namespaces.contains("default"));
22+
}
23+
24+
@Test
25+
public void getTargetNamespaces_withNonEmptyValue_should_not_return_default()
26+
throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
27+
Collection<String> namespaces = invoke_getTargetNamespaces("dev-domain", "default");
28+
assertFalse(namespaces.contains("default"));
29+
}
30+
31+
@Test
32+
public void getTargetNamespaces_with_single_target_should_return_it()
33+
throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
34+
Collection<String> namespaces = invoke_getTargetNamespaces("dev-domain", "default");
35+
assertTrue(namespaces.contains("dev-domain"));
36+
}
37+
38+
@Test
39+
public void getTargetNamespaces_with_multiple_targets_should_include_all()
40+
throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
41+
Collection<String> namespaces =
42+
invoke_getTargetNamespaces("dev-domain,domain1,test-domain", "default");
43+
assertTrue(namespaces.contains("dev-domain"));
44+
assertTrue(namespaces.contains("domain1"));
45+
assertTrue(namespaces.contains("test-domain"));
46+
}
47+
48+
@Test
49+
public void getTargetNamespaces_should_remove_leading_spaces()
50+
throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
51+
Collection<String> namespaces =
52+
invoke_getTargetNamespaces(" test-domain, dev-domain", "default");
53+
assertTrue(namespaces.contains("dev-domain"));
54+
assertTrue(namespaces.contains("test-domain"));
55+
}
56+
57+
@Test
58+
public void getTargetNamespaces_should_remove_trailing_spaces()
59+
throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
60+
Collection<String> namespaces =
61+
invoke_getTargetNamespaces("dev-domain ,test-domain ", "default");
62+
assertTrue(namespaces.contains("dev-domain"));
63+
assertTrue(namespaces.contains("test-domain"));
64+
}
65+
66+
Method getTargetNamespaces;
67+
68+
Collection<String> invoke_getTargetNamespaces(String tnValue, String namespace)
69+
throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
70+
if (getTargetNamespaces == null) {
71+
getTargetNamespaces =
72+
Main.class.getDeclaredMethod("getTargetNamespaces", String.class, String.class);
73+
getTargetNamespaces.setAccessible(true);
74+
}
75+
return (Collection<String>) getTargetNamespaces.invoke(null, tnValue, namespace);
76+
}
77+
}

0 commit comments

Comments
 (0)