Skip to content
Permalink
Browse files

Added unit and integration tests for resolver priority.

  • Loading branch information...
davemoore- committed Jul 4, 2019
1 parent a57958d commit 0d76da05a810eb58b9537e1545dc259a3aa90a53
@@ -577,7 +577,6 @@ private void traverse() throws IOException, ValidationException {
List<String> queryClauses = new ArrayList<>();
List<String> queryMustNotClauses = new ArrayList<>();
List<String> queryFilterClauses = new ArrayList<>();
List<String> queryResolverClauses = new ArrayList<>();
List<String> topLevelClauses = new ArrayList<>();
topLevelClauses.add("\"_source\":true");

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

// Construct the resolvers clause.
String resolversClause = "{}";
TreeMap<String, TreeMap> resolversFilterTree = new TreeMap<>();
TreeMap<Integer, TreeMap<String, TreeMap>> resolversFilterTreeGrouped= new TreeMap<>();
TreeMap<String, TreeMap> resolversFilterTree;
TreeMap<Integer, TreeMap<String, TreeMap>> resolversFilterTreeGrouped= new TreeMap<>(Collections.reverseOrder());
if (!this.attributes.isEmpty()) {

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

// If there are multiple levels of priority, then each lower priority group of resolvers must ensure
@@ -12,6 +12,9 @@
public void testValid() throws Exception {
new Resolver("resolver_name", VALID_OBJECT);
new Resolver("resolver_name", "{\"attributes\":[\"attribute_a\",\"attribute_b\"]}");
new Resolver("resolver_name", "{\"attributes\":[\"attribute_a\"],\"priority\":1}");
new Resolver("resolver_name", "{\"attributes\":[\"attribute_a\"],\"priority\":0}");
new Resolver("resolver_name", "{\"attributes\":[\"attribute_a\"],\"priority\":-1}");
}

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

@Test(expected = ValidationException.class)
public void testInvalidAttributesEmpty() throws Exception {
new Resolver("resolver_name", "[]");
new Resolver("resolver_name", "{\"attributes\":[]}");
}

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

//// "resolvers".RESOLVER_NAME."priority" //////////////////////////////////////////////////////////////////////

@Test(expected = ValidationException.class)
public void testInvalidPriorityTypeArray() throws Exception {
new Resolver("resolver_name", "{\"attributes\":[\"attribute_a\"],\"priority\":[]}");
}

@Test(expected = ValidationException.class)
public void testInvalidPriorityTypeBoolean() throws Exception {
new Resolver("resolver_name", "{\"attributes\":[\"attribute_a\"],\"priority\":true}");
}

@Test(expected = ValidationException.class)
public void testInvalidPriorityTypeFloat() throws Exception {
new Resolver("resolver_name", "{\"attributes\":[\"attribute_a\"],\"priority\":1.0}");
}

@Test(expected = ValidationException.class)
public void testInvalidPriorityTypeNull() throws Exception {
new Resolver("resolver_name", "{\"attributes\":[\"attribute_a\"],\"priority\":null}");
}

@Test(expected = ValidationException.class)
public void testInvalidPriorityTypeObject() throws Exception {
new Resolver("resolver_name", "{\"attributes\":[\"attribute_a\"],\"priority\":{}}");
}

@Test(expected = ValidationException.class)
public void testInvalidPriorityTypeString() throws Exception {
new Resolver("resolver_name", "{\"attributes\":[\"attribute_a\"],\"priority\":\"1\"}");
}

}
@@ -250,6 +250,18 @@
" }\n" +
"}", ContentType.APPLICATION_JSON);

private final StringEntity TEST_PAYLOAD_JOB_PRIORITY = new StringEntity("{\n" +
" \"attributes\": {\n" +
" \"attribute_a\": [ \"a_10\" ],\n" +
" \"attribute_b\": [ \"b_10\" ]\n" +
" },\n" +
" \"scope\": {\n" +
" \"include\": {\n" +
" \"indices\": [ \".zentity_test_index_a\" ]\n" +
" }\n" +
" }\n" +
"}", ContentType.APPLICATION_JSON);

private byte[] readFile(String filename) throws IOException {
InputStream stream = this.getClass().getResourceAsStream("/" + filename);
return IOUtils.toByteArray(stream);
@@ -265,14 +277,16 @@ private void destroyTestResources() throws IOException {

// Delete entity model
client.performRequest(new Request("DELETE", "_zentity/models/zentity_test_entity_a"));
client.performRequest(new Request("DELETE", "_zentity/models/zentity_test_entity_b"));
}

private void prepareTestResources() throws Exception {

// Load files
ByteArrayEntity testIndex;
ByteArrayEntity testData;
ByteArrayEntity testEntityModel;
ByteArrayEntity testEntityModelA;
ByteArrayEntity testEntityModelB;
// Elasticsearch 7.0.0+ removes mapping types
Properties props = new Properties();
props.load(ZentityPlugin.class.getResourceAsStream("/plugin-descriptor.properties"));
@@ -283,7 +297,8 @@ private void prepareTestResources() throws Exception {
testIndex = new ByteArrayEntity(readFile("TestIndexElasticsearch6.json"), ContentType.APPLICATION_JSON);
testData = new ByteArrayEntity(readFile("TestDataElasticsearch6.txt"), ContentType.create("application/x-ndjson"));
}
testEntityModel = new ByteArrayEntity(readFile("TestEntityModel.json"), ContentType.APPLICATION_JSON);
testEntityModelA = new ByteArrayEntity(readFile("TestEntityModelA.json"), ContentType.APPLICATION_JSON);
testEntityModelB = new ByteArrayEntity(readFile("TestEntityModelB.json"), ContentType.APPLICATION_JSON);

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

// Create entity model
Request postModel = new Request("POST", "_zentity/models/zentity_test_entity_a");
postModel.setEntity(testEntityModel);
client.performRequest(postModel);
// Create entity models
Request postModelA = new Request("POST", "_zentity/models/zentity_test_entity_a");
postModelA.setEntity(testEntityModelA);
client.performRequest(postModelA);
Request postModelB = new Request("POST", "_zentity/models/zentity_test_entity_b");
postModelB.setEntity(testEntityModelB);
client.performRequest(postModelB);
}

private Set<String> getActual(JsonNode json) {
@@ -772,4 +790,26 @@ public void testJobScopeExcludeAndIncludeAttributes() throws Exception {
destroyTestResources();
}
}

public void testJobPriority() throws Exception {
prepareTestResources();
try {
String endpoint = "_zentity/resolution/zentity_test_entity_b";
Request postResolution = new Request("POST", endpoint);
postResolution.setEntity(TEST_PAYLOAD_JOB_PRIORITY);
Response response = client.performRequest(postResolution);
JsonNode json = Json.MAPPER.readTree(response.getEntity().getContent());
assertEquals(json.get("hits").get("total").asInt(), 4);

Set<String> docsExpected = new TreeSet<>();
docsExpected.add("a2,0");
docsExpected.add("a3,0");
docsExpected.add("a4,1");
docsExpected.add("a5,1");

assertEquals(docsExpected, getActual(json));
} finally {
destroyTestResources();
}
}
}
File renamed without changes.
@@ -0,0 +1,92 @@
{
"attributes": {
"attribute_a": {
"type": "string"
},
"attribute_b": {
"type": "string"
},
"attribute_c": {
"type": "string"
},
"attribute_d": {
"type": "string"
},
"attribute_x": {
"type": "string"
}
},
"resolvers": {
"resolver_ab": {
"attributes": [
"attribute_a", "attribute_b"
],
"priority": -1
},
"resolver_ac": {
"attributes": [
"attribute_a", "attribute_c"
],
"priority": -1
},
"resolver_bc": {
"attributes": [
"attribute_b", "attribute_c"
],
"priority": 1
},
"resolver_cd": {
"attributes": [
"attribute_c", "attribute_d"
],
"priority": -1
},
"resolver_x": {
"attributes": [
"attribute_x"
]
}
},
"matchers": {
"matcher_a": {
"clause": {
"match": {
"{{ field }}": "{{ value }}"
}
}
},
"matcher_b": {
"clause": {
"term": {
"{{ field }}": "{{ value }}"
}
}
}
},
"indices": {
".zentity_test_index_a": {
"fields": {
"field_a.clean": {
"attribute": "attribute_a",
"matcher": "matcher_a"
},
"field_b.clean": {
"attribute": "attribute_b",
"matcher": "matcher_a"
},
"field_c.clean": {
"attribute": "attribute_c",
"matcher": "matcher_a"
},
"field_d.clean": {
"attribute": "attribute_d",
"matcher": "matcher_a"
},
"object.a.b.c.keyword": {
"attribute": "attribute_x",
"matcher": "matcher_b"
}
}
}
}
}

0 comments on commit 0d76da0

Please sign in to comment.
You can’t perform that action at this time.