Skip to content

Commit 3fa0e59

Browse files
authored
Remove invalid hints from Properties inputs. (#119)
1 parent 279b3d4 commit 3fa0e59

File tree

2 files changed

+65
-3
lines changed

2 files changed

+65
-3
lines changed

hoptimator-util/src/main/java/com/linkedin/hoptimator/util/DeploymentService.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929

3030
public final class DeploymentService {
3131

32-
private static final String HINT_OPTION = "hints";
32+
static final String HINT_OPTION = "hints";
3333
public static final String PIPELINE_OPTION = "pipeline";
3434

3535
private DeploymentService() {
@@ -97,11 +97,19 @@ public static PipelineRel.Implementor plan(RelRoot root, List<RelOptMaterializat
9797
public static Map<String, String> parseHints(Properties connectionProperties) {
9898
Map<String, String> hints = new LinkedHashMap<>();
9999
if (connectionProperties.containsKey(HINT_OPTION)) {
100-
hints.putAll(Splitter.on(',').withKeyValueSeparator('=').split(connectionProperties.getProperty(HINT_OPTION)));
100+
String property = connectionProperties.getProperty(HINT_OPTION);
101+
if (property != null && !property.isEmpty()) {
102+
hints.putAll(Splitter.on(',').withKeyValueSeparator('=').split(property));
103+
}
101104
}
105+
102106
if (connectionProperties.containsKey(PIPELINE_OPTION)) {
103-
hints.put(PIPELINE_OPTION, connectionProperties.getProperty(PIPELINE_OPTION));
107+
String property = connectionProperties.getProperty(PIPELINE_OPTION);
108+
if (property != null && !property.isEmpty()) {
109+
hints.put(PIPELINE_OPTION, property);
110+
}
104111
}
112+
105113
return hints;
106114
}
107115
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.linkedin.hoptimator.util;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.Collections;
6+
import java.util.List;
7+
import java.util.Map;
8+
import java.util.Properties;
9+
10+
import org.junit.jupiter.api.Test;
11+
12+
import static com.linkedin.hoptimator.util.DeploymentService.HINT_OPTION;
13+
import static com.linkedin.hoptimator.util.DeploymentService.PIPELINE_OPTION;
14+
import static org.junit.jupiter.api.Assertions.assertEquals;
15+
import static org.junit.jupiter.api.Assertions.assertTrue;
16+
import static org.junit.jupiter.api.Assertions.assertFalse;
17+
18+
19+
class DeploymentServiceTest {
20+
21+
/**
22+
* "hint" keys <b>and</b> values are required to be non-{@code null}. An
23+
* empty {@link Map} is considered invalid and should <b>not</b> be added
24+
* to the {@link Properties} object.
25+
* <br/>
26+
* nb. "pipeline" values are <b>always</b> added when present.
27+
*/
28+
@Test
29+
void parseHints() {
30+
Map<String, String> empty = DeploymentService.parseHints(new Properties());
31+
assertTrue(empty.isEmpty(), "An empty map should not add `hints`.");
32+
33+
Map<String, String> defined = DeploymentService.parseHints(new Properties() {{
34+
put(HINT_OPTION, "key=value");
35+
}});
36+
assertEquals("value", defined.get("key"), "Did not match expected key value pair: `key=value`.");
37+
38+
Map<String, String> nokey = DeploymentService.parseHints(new Properties() {{
39+
put(HINT_OPTION, "flag0=,flag1=");
40+
}});
41+
assertTrue(nokey.keySet().containsAll(Arrays.asList("flag0", "flag1")), "Expected to find flags.");
42+
43+
Map<String, String> pipelineOnly = DeploymentService.parseHints(new Properties() {{
44+
put(PIPELINE_OPTION, "pipeline");
45+
}});
46+
assertEquals("pipeline", pipelineOnly.get(PIPELINE_OPTION), "Did not match expected `pipeline` value.");
47+
48+
Map<String, String> both = DeploymentService.parseHints(new Properties() {{
49+
putAll(defined);
50+
put(PIPELINE_OPTION, "pipeline");
51+
}});
52+
assertEquals("pipeline", both.get(PIPELINE_OPTION), "Did not match expected `pipeline` value.");
53+
}
54+
}

0 commit comments

Comments
 (0)