Skip to content

Commit 0d76da0

Browse files
committed
Added unit and integration tests for resolver priority.
1 parent a57958d commit 0d76da0

File tree

5 files changed

+179
-12
lines changed

5 files changed

+179
-12
lines changed

src/main/java/io/zentity/resolution/Job.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,6 @@ private void traverse() throws IOException, ValidationException {
577577
List<String> queryClauses = new ArrayList<>();
578578
List<String> queryMustNotClauses = new ArrayList<>();
579579
List<String> queryFilterClauses = new ArrayList<>();
580-
List<String> queryResolverClauses = new ArrayList<>();
581580
List<String> topLevelClauses = new ArrayList<>();
582581
topLevelClauses.add("\"_source\":true");
583582

@@ -619,8 +618,8 @@ else if (size == 1)
619618

620619
// Construct the resolvers clause.
621620
String resolversClause = "{}";
622-
TreeMap<String, TreeMap> resolversFilterTree = new TreeMap<>();
623-
TreeMap<Integer, TreeMap<String, TreeMap>> resolversFilterTreeGrouped= new TreeMap<>();
621+
TreeMap<String, TreeMap> resolversFilterTree;
622+
TreeMap<Integer, TreeMap<String, TreeMap>> resolversFilterTreeGrouped= new TreeMap<>(Collections.reverseOrder());
624623
if (!this.attributes.isEmpty()) {
625624

626625
// Group the resolvers by their priority level.
@@ -629,13 +628,14 @@ else if (size == 1)
629628
// Construct a clause for each priority level in descending order of priority.
630629
List<Integer> priorities = new ArrayList<>(resolverGroups.keySet());
631630
Collections.reverse(priorities);
632-
for (int level = 0; level < priorities.size(); level++) {
631+
int numPriorityLevels= priorities.size();
632+
for (int level = 0; level < numPriorityLevels; level++) {
633633
Integer priority = priorities.get(level);
634634
List<String> resolversGroup = resolverGroups.get(priority);
635635
Map<String, Integer> counts = countAttributesAcrossResolvers(this.input.model(), resolversGroup);
636636
List<List<String>> resolversSorted = sortResolverAttributes(this.input.model(), resolversGroup, counts);
637637
resolversFilterTree = makeResolversFilterTree(resolversSorted);
638-
resolversFilterTreeGrouped.put(level, resolversFilterTree);
638+
resolversFilterTreeGrouped.put(numPriorityLevels - level - 1, resolversFilterTree);
639639
resolversClause = populateResolversFilterTree(this.input.model(), indexName, resolversFilterTree, this.attributes);
640640

641641
// If there are multiple levels of priority, then each lower priority group of resolvers must ensure

src/test/java/io/zentity/model/ResolverTest.java

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ public class ResolverTest {
1212
public void testValid() throws Exception {
1313
new Resolver("resolver_name", VALID_OBJECT);
1414
new Resolver("resolver_name", "{\"attributes\":[\"attribute_a\",\"attribute_b\"]}");
15+
new Resolver("resolver_name", "{\"attributes\":[\"attribute_a\"],\"priority\":1}");
16+
new Resolver("resolver_name", "{\"attributes\":[\"attribute_a\"],\"priority\":0}");
17+
new Resolver("resolver_name", "{\"attributes\":[\"attribute_a\"],\"priority\":-1}");
1518
}
1619

1720
@Test(expected = ValidationException.class)
@@ -35,7 +38,7 @@ public void testInvalidAttributesMissing() throws Exception {
3538

3639
@Test(expected = ValidationException.class)
3740
public void testInvalidAttributesEmpty() throws Exception {
38-
new Resolver("resolver_name", "[]");
41+
new Resolver("resolver_name", "{\"attributes\":[]}");
3942
}
4043

4144
@Test(expected = ValidationException.class)
@@ -105,4 +108,36 @@ public void testInvalidAttributesNameTypeObject() throws Exception {
105108
new Resolver("resolver_name", "{\"attributes\":[\"attribute_a\",{}]}");
106109
}
107110

111+
//// "resolvers".RESOLVER_NAME."priority" //////////////////////////////////////////////////////////////////////
112+
113+
@Test(expected = ValidationException.class)
114+
public void testInvalidPriorityTypeArray() throws Exception {
115+
new Resolver("resolver_name", "{\"attributes\":[\"attribute_a\"],\"priority\":[]}");
116+
}
117+
118+
@Test(expected = ValidationException.class)
119+
public void testInvalidPriorityTypeBoolean() throws Exception {
120+
new Resolver("resolver_name", "{\"attributes\":[\"attribute_a\"],\"priority\":true}");
121+
}
122+
123+
@Test(expected = ValidationException.class)
124+
public void testInvalidPriorityTypeFloat() throws Exception {
125+
new Resolver("resolver_name", "{\"attributes\":[\"attribute_a\"],\"priority\":1.0}");
126+
}
127+
128+
@Test(expected = ValidationException.class)
129+
public void testInvalidPriorityTypeNull() throws Exception {
130+
new Resolver("resolver_name", "{\"attributes\":[\"attribute_a\"],\"priority\":null}");
131+
}
132+
133+
@Test(expected = ValidationException.class)
134+
public void testInvalidPriorityTypeObject() throws Exception {
135+
new Resolver("resolver_name", "{\"attributes\":[\"attribute_a\"],\"priority\":{}}");
136+
}
137+
138+
@Test(expected = ValidationException.class)
139+
public void testInvalidPriorityTypeString() throws Exception {
140+
new Resolver("resolver_name", "{\"attributes\":[\"attribute_a\"],\"priority\":\"1\"}");
141+
}
142+
108143
}

src/test/java/io/zentity/resolution/JobIT.java

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,18 @@ public class JobIT extends AbstractITCase {
250250
" }\n" +
251251
"}", ContentType.APPLICATION_JSON);
252252

253+
private final StringEntity TEST_PAYLOAD_JOB_PRIORITY = new StringEntity("{\n" +
254+
" \"attributes\": {\n" +
255+
" \"attribute_a\": [ \"a_10\" ],\n" +
256+
" \"attribute_b\": [ \"b_10\" ]\n" +
257+
" },\n" +
258+
" \"scope\": {\n" +
259+
" \"include\": {\n" +
260+
" \"indices\": [ \".zentity_test_index_a\" ]\n" +
261+
" }\n" +
262+
" }\n" +
263+
"}", ContentType.APPLICATION_JSON);
264+
253265
private byte[] readFile(String filename) throws IOException {
254266
InputStream stream = this.getClass().getResourceAsStream("/" + filename);
255267
return IOUtils.toByteArray(stream);
@@ -265,14 +277,16 @@ private void destroyTestResources() throws IOException {
265277

266278
// Delete entity model
267279
client.performRequest(new Request("DELETE", "_zentity/models/zentity_test_entity_a"));
280+
client.performRequest(new Request("DELETE", "_zentity/models/zentity_test_entity_b"));
268281
}
269282

270283
private void prepareTestResources() throws Exception {
271284

272285
// Load files
273286
ByteArrayEntity testIndex;
274287
ByteArrayEntity testData;
275-
ByteArrayEntity testEntityModel;
288+
ByteArrayEntity testEntityModelA;
289+
ByteArrayEntity testEntityModelB;
276290
// Elasticsearch 7.0.0+ removes mapping types
277291
Properties props = new Properties();
278292
props.load(ZentityPlugin.class.getResourceAsStream("/plugin-descriptor.properties"));
@@ -283,7 +297,8 @@ private void prepareTestResources() throws Exception {
283297
testIndex = new ByteArrayEntity(readFile("TestIndexElasticsearch6.json"), ContentType.APPLICATION_JSON);
284298
testData = new ByteArrayEntity(readFile("TestDataElasticsearch6.txt"), ContentType.create("application/x-ndjson"));
285299
}
286-
testEntityModel = new ByteArrayEntity(readFile("TestEntityModel.json"), ContentType.APPLICATION_JSON);
300+
testEntityModelA = new ByteArrayEntity(readFile("TestEntityModelA.json"), ContentType.APPLICATION_JSON);
301+
testEntityModelB = new ByteArrayEntity(readFile("TestEntityModelB.json"), ContentType.APPLICATION_JSON);
287302

288303
// Create indices
289304
Request putTestIndexA = new Request("PUT", ".zentity_test_index_a");
@@ -305,10 +320,13 @@ private void prepareTestResources() throws Exception {
305320
postBulk.setEntity(testData);
306321
client.performRequest(postBulk);
307322

308-
// Create entity model
309-
Request postModel = new Request("POST", "_zentity/models/zentity_test_entity_a");
310-
postModel.setEntity(testEntityModel);
311-
client.performRequest(postModel);
323+
// Create entity models
324+
Request postModelA = new Request("POST", "_zentity/models/zentity_test_entity_a");
325+
postModelA.setEntity(testEntityModelA);
326+
client.performRequest(postModelA);
327+
Request postModelB = new Request("POST", "_zentity/models/zentity_test_entity_b");
328+
postModelB.setEntity(testEntityModelB);
329+
client.performRequest(postModelB);
312330
}
313331

314332
private Set<String> getActual(JsonNode json) {
@@ -772,4 +790,26 @@ public void testJobScopeExcludeAndIncludeAttributes() throws Exception {
772790
destroyTestResources();
773791
}
774792
}
793+
794+
public void testJobPriority() throws Exception {
795+
prepareTestResources();
796+
try {
797+
String endpoint = "_zentity/resolution/zentity_test_entity_b";
798+
Request postResolution = new Request("POST", endpoint);
799+
postResolution.setEntity(TEST_PAYLOAD_JOB_PRIORITY);
800+
Response response = client.performRequest(postResolution);
801+
JsonNode json = Json.MAPPER.readTree(response.getEntity().getContent());
802+
assertEquals(json.get("hits").get("total").asInt(), 4);
803+
804+
Set<String> docsExpected = new TreeSet<>();
805+
docsExpected.add("a2,0");
806+
docsExpected.add("a3,0");
807+
docsExpected.add("a4,1");
808+
docsExpected.add("a5,1");
809+
810+
assertEquals(docsExpected, getActual(json));
811+
} finally {
812+
destroyTestResources();
813+
}
814+
}
775815
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
{
2+
"attributes": {
3+
"attribute_a": {
4+
"type": "string"
5+
},
6+
"attribute_b": {
7+
"type": "string"
8+
},
9+
"attribute_c": {
10+
"type": "string"
11+
},
12+
"attribute_d": {
13+
"type": "string"
14+
},
15+
"attribute_x": {
16+
"type": "string"
17+
}
18+
},
19+
"resolvers": {
20+
"resolver_ab": {
21+
"attributes": [
22+
"attribute_a", "attribute_b"
23+
],
24+
"priority": -1
25+
},
26+
"resolver_ac": {
27+
"attributes": [
28+
"attribute_a", "attribute_c"
29+
],
30+
"priority": -1
31+
},
32+
"resolver_bc": {
33+
"attributes": [
34+
"attribute_b", "attribute_c"
35+
],
36+
"priority": 1
37+
},
38+
"resolver_cd": {
39+
"attributes": [
40+
"attribute_c", "attribute_d"
41+
],
42+
"priority": -1
43+
},
44+
"resolver_x": {
45+
"attributes": [
46+
"attribute_x"
47+
]
48+
}
49+
},
50+
"matchers": {
51+
"matcher_a": {
52+
"clause": {
53+
"match": {
54+
"{{ field }}": "{{ value }}"
55+
}
56+
}
57+
},
58+
"matcher_b": {
59+
"clause": {
60+
"term": {
61+
"{{ field }}": "{{ value }}"
62+
}
63+
}
64+
}
65+
},
66+
"indices": {
67+
".zentity_test_index_a": {
68+
"fields": {
69+
"field_a.clean": {
70+
"attribute": "attribute_a",
71+
"matcher": "matcher_a"
72+
},
73+
"field_b.clean": {
74+
"attribute": "attribute_b",
75+
"matcher": "matcher_a"
76+
},
77+
"field_c.clean": {
78+
"attribute": "attribute_c",
79+
"matcher": "matcher_a"
80+
},
81+
"field_d.clean": {
82+
"attribute": "attribute_d",
83+
"matcher": "matcher_a"
84+
},
85+
"object.a.b.c.keyword": {
86+
"attribute": "attribute_x",
87+
"matcher": "matcher_b"
88+
}
89+
}
90+
}
91+
}
92+
}

0 commit comments

Comments
 (0)