Skip to content

Commit

Permalink
Adjustments to entity and relation models and their tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Dave Moore authored and Dave Moore committed Jun 11, 2024
1 parent 19702c2 commit f11eaa0
Show file tree
Hide file tree
Showing 5 changed files with 243 additions and 170 deletions.
19 changes: 13 additions & 6 deletions src/main/java/io/zentity/model/Validation.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,20 @@ public class Validation {
/**
* Validate that a name meets the same requirements as the Elasticsearch index name requirements.
*
* @param name The name of the relation type.
* @param name The name to validate.
* @param optional Whether the name can be empty.
* @return an optional ValidationException if the type is not in a valid format.
* @see <a href="https://www.elastic.co/guide/en/elasticsearch/reference/7.10/indices-create-index.html#indices-create-api-path-params">Elasticsearch Index Name Requirements</a>
* @see org.elasticsearch.cluster.metadata.MetadataCreateIndexService#validateIndexOrAliasName
*/
public static void validateStrictName(String name) throws ValidationException {
public static void validateStrictName(String name, Boolean optional) throws ValidationException {
BiFunction<String, String, String> msg = (invalidName, description) -> "Invalid name [" + invalidName + "], " + description;
if (name == null)
throw new ValidationException(msg.apply("", "must not be empty"));
if (Patterns.EMPTY_STRING.matcher(name).matches())
throw new ValidationException(msg.apply(name, "must not be empty"));
if (!optional) {
if (name == null)
throw new ValidationException(msg.apply("", "must not be empty"));
if (Patterns.EMPTY_STRING.matcher(name).matches())
throw new ValidationException(msg.apply(name, "must not be empty"));
}
if (!Strings.validFileName(name))
throw new ValidationException(msg.apply(name, "must not contain the following characters: " + Strings.INVALID_FILENAME_CHARS));
if (name.contains("#"))
Expand All @@ -48,4 +51,8 @@ public static void validateStrictName(String name) throws ValidationException {
if (!name.toLowerCase(Locale.ROOT).equals(name))
throw new ValidationException(msg.apply(name, "must be lowercase"));
}

public static void validateStrictName(String name) throws ValidationException {
validateStrictName(name, false);
}
}
3 changes: 3 additions & 0 deletions src/main/java/io/zentity/model/entity/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public class Model {
Arrays.asList("attributes", "resolvers", "matchers", "indices")
);

private String _id;
private Map<String, Attribute> attributes = new TreeMap<>();
private Map<String, Index> indices = new TreeMap<>();
private Map<String, Matcher> matchers = new TreeMap<>();
Expand All @@ -62,6 +63,8 @@ public Model(String json, boolean validateRunnable) throws ValidationException,
this.deserialize(json);
}

public String _id() { return this._id; }

public Map<String, Attribute> attributes() {
return this.attributes;
}
Expand Down
40 changes: 35 additions & 5 deletions src/main/java/io/zentity/model/relation/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public class Model {
Arrays.asList("a>b", "a<b", "a<>b", "")
);

private String _id;
private String index;
private String type;
private String direction;
Expand All @@ -54,6 +55,8 @@ public Model(String json) throws ValidationException, IOException {
this.deserialize(json);
}

public String _id() { return this._id; };

public String index() { return this.index; }

public String type() { return this.type; }
Expand All @@ -64,13 +67,17 @@ public Model(String json) throws ValidationException, IOException {

public String b() { return this.b; }

private void _id(String index, String a, String b) {
this._id = String.join("#", index, a, b);
}

private void index(String name) throws ValidationException {
validateStrictName(name);
this.index = name;
}

private void type(String type) throws ValidationException {
validateStrictName(type);
validateStrictName(type, true);
this.type = type;
}

Expand Down Expand Up @@ -149,12 +156,18 @@ public void deserialize(JsonNode json) throws ValidationException {
this.index(value.asText());
break;
case "type":
if (!value.isNull() && !value.asText().equals(""))
this.type(value.asText());
if (!value.isNull()) {
String s = value.asText().trim();
if (!s.equals(""))
this.type(s);
}
break;
case "direction":
if (!value.isNull() && !value.asText().equals(""))
this.direction(value.asText());
if (!value.isNull()) {
String s = value.asText().trim();
if (!s.equals(""))
this.direction(s);
}
break;
case "a":
this.a(value.asText());
Expand All @@ -166,6 +179,23 @@ public void deserialize(JsonNode json) throws ValidationException {
throw new ValidationException("'" + fieldName + "' is not a recognized field.");
}
}

// If entity A > entity B, reverse their positions and reverse the direction.
if (this.a().compareTo(this.b()) > 0) {
String a = this.a();
String b = this.b();
this.a(b);
this.b(a);
if (this.direction() != null) {
if (this.direction().equals("a>b"))
this.direction("a<b");
else if (this.direction().equals("a<b"))
this.direction("a>b");
}
}

// Create the _id
this._id(this.index(), this.a(), this.b());
}

public void deserialize(String json) throws ValidationException, IOException {
Expand Down
17 changes: 16 additions & 1 deletion src/test/java/io/zentity/model/entity/ModelTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package io.zentity.model.entity;

import io.zentity.model.ValidationException;
import org.junit.Assert;
import org.junit.Test;

public class ModelTest {
Expand All @@ -33,7 +34,21 @@ public class ModelTest {

@Test
public void testValid() throws Exception {
new Model(VALID_OBJECT);
Model model = new Model(VALID_OBJECT);
Assert.assertEquals("string", model.attributes().get("attribute_name").type());
Assert.assertEquals(0.5, model.attributes().get("attribute_name").score(), 0.0);
Assert.assertEquals("string", model.attributes().get("attribute_array").type());
Assert.assertEquals(0.5, model.attributes().get("attribute_array").score(), 0.0);
Assert.assertEquals("string", model.attributes().get("attribute_object").type());
Assert.assertEquals(0.5, model.attributes().get("attribute_object").score(), 0.0);
//Assert.assertTrue(model.resolvers().get("resolver_name_a").attributes().contains("string"));
//Assert.assertTrue(model.resolvers().get("resolver_name_b").attributes().contains("string"));
//Assert.assertTrue(model.resolvers().get("resolver_name_c").attributes().contains("string"));
Assert.assertEquals("{\"match\":{\"{{ field }}\":\"{{ value }}\"}}", model.matchers().get("matcher_name").clause());
Assert.assertEquals("foo", model.indices().get("index_name_a").fields().get("index_field_name").attribute());
Assert.assertEquals("bar", model.indices().get("index_name_a").fields().get("index_field_name").matcher());
Assert.assertEquals(1.0, model.indices().get("index_name_a").fields().get("index_field_name").quality(), 0.0);

}

@Test(expected = ValidationException.class)
Expand Down
Loading

0 comments on commit f11eaa0

Please sign in to comment.