Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle missing Predicates in TestThing #723

Merged
merged 1 commit into from
Jun 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 14 additions & 15 deletions java/dev/enola/thing/java/TestThing.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.google.errorprone.annotations.Immutable;
import com.google.errorprone.annotations.ThreadSafe;

import dev.enola.datatype.Datatype;
import dev.enola.thing.*;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
Expand All @@ -47,19 +48,19 @@ protected TestThing(
@Nullable Integer number,
ImmutableMap<String, Object> dynamic_properties,
ImmutableMap<String, String> dynamic_datatypes) {
super(
iri,
ImmutableMap.of(
KIRI.RDFS.LABEL,
dev.enola.datatype.Datatypes.STRING,
NUMBER_URI,
dev.enola.model.xsd.Datatypes.INT),
dynamic_properties,
dynamic_datatypes);
super(iri, datatypesOfNonNullFields(label, number), dynamic_properties, dynamic_datatypes);
this.label = label;
this.number = number;
}

private static ImmutableMap<String, Datatype<?>> datatypesOfNonNullFields(
@Nullable String label, @Nullable Integer number) {
var builder = ImmutableMap.<String, Datatype<?>>builderWithExpectedSize(2);
if (label != null) builder.put(KIRI.RDFS.LABEL, dev.enola.datatype.Datatypes.STRING);
if (number != null) builder.put(NUMBER_URI, dev.enola.model.xsd.Datatypes.INT);
return builder.build();
}

public static TestThing create(String iri, String label, int number) {
return new TestThing(iri, label, number, ImmutableMap.of(), ImmutableMap.of());
}
Expand All @@ -83,12 +84,10 @@ public ImmutableMap<String, Object> properties() {
// TODO Compute this lazily once only, and cache it in a field?
if (super.properties.isEmpty())
return ImmutableMap.of(KIRI.RDFS.LABEL, label, NUMBER_URI, number);
else
return ImmutableMap.<String, Object>builder()
.putAll(super.properties)
.put(KIRI.RDFS.LABEL, label)
.put(NUMBER_URI, number)
.build();
var builder = ImmutableMap.<String, Object>builder().putAll(super.properties);
if (label != null) builder.put(KIRI.RDFS.LABEL, label);
if (number != null) builder.put(NUMBER_URI, number);
return builder.build();
}

@Override
Expand Down
47 changes: 37 additions & 10 deletions java/dev/enola/thing/java/TestThingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ public class TestThingTest {

final TestThing thing = TestThing.create("https://enola.dev/test1", "hello, world", 43);
final TestThing thing2 = TestThing.create("https://enola.dev/test1", "hello, world", 43);
final TestThing bthing =
final TestThing bthing = // Thing from Builder with static setter methods
TestThing.builder()
.iri("https://enola.dev/test1")
.label("hello, world")
.number(43)
.build();
final TestThing dthing =
final TestThing dthing = // Thing from Builder with "dynamic" set()
TestThing.builder()
.iri("https://enola.dev/test1")
.set(KIRI.RDFS.LABEL, "hello, world")
Expand All @@ -46,31 +46,44 @@ public class TestThingTest {

final String EXTRA = "https://enola.dev/another-predicate";
final Instant INSTANT = Instant.parse("2023-10-26T15:29:31.123456-05:00");
final TestThing ething =
final TestThing
ething = // Thing from Builder with an EXTRA predicate, which is not a static field
TestThing.builder()
.iri("https://enola.dev/test1")
.label("hello, world")
.number(43)
.set(EXTRA, INSTANT, Datatypes.DATE_TIME.iri())
.build();
.iri("https://enola.dev/test1")
.label("hello, world")
.number(43)
.set(EXTRA, INSTANT, Datatypes.DATE_TIME.iri())
.build();

final TestThing
mthing = // Thing without one of the static (TestThing.NUMBER_URI predicate) fields set!
TestThing.builder()
.iri("https://enola.dev/test1")
.label("hello, world")
// NO .number(43)
.set(EXTRA, INSTANT, Datatypes.DATE_TIME.iri())
.build();

@Test
public void iri() {
assertThat(thing.iri()).isEqualTo("https://enola.dev/test1");
assertThat(bthing.iri()).isEqualTo("https://enola.dev/test1");
assertThat(dthing.iri()).isEqualTo("https://enola.dev/test1");
assertThat(ething.iri()).isEqualTo("https://enola.dev/test1");
assertThat(mthing.iri()).isEqualTo("https://enola.dev/test1");
}

@Test
public void static_getters() {
assertThat(thing.label()).isEqualTo("hello, world");
assertThat(bthing.label()).isEqualTo("hello, world");
assertThat(dthing.label()).isEqualTo("hello, world");
assertThat(mthing.label()).isEqualTo("hello, world");

assertThat(thing.number()).isEqualTo(43);
assertThat(bthing.number()).isEqualTo(43);
assertThat(dthing.number()).isEqualTo(43);
assertThat(mthing.number()).isNull();
}

@Test
Expand All @@ -80,23 +93,28 @@ public void predicateIRIs() {
assertThat(dthing.predicateIRIs()).containsExactly(KIRI.RDFS.LABEL, TestThing.NUMBER_URI);
assertThat(ething.predicateIRIs())
.containsExactly(KIRI.RDFS.LABEL, TestThing.NUMBER_URI, EXTRA);
assertThat(mthing.predicateIRIs()).containsExactly(KIRI.RDFS.LABEL, EXTRA);
}

@Test
public void thing_getters() {
assertThat((String) thing.get(KIRI.RDFS.LABEL)).isEqualTo("hello, world");
assertThat((String) bthing.get(KIRI.RDFS.LABEL)).isEqualTo("hello, world");
assertThat((String) dthing.get(KIRI.RDFS.LABEL)).isEqualTo("hello, world");
assertThat((String) mthing.get(KIRI.RDFS.LABEL)).isEqualTo("hello, world");

assertThat((Integer) thing.get(TestThing.NUMBER_URI)).isEqualTo(43);
assertThat((Integer) bthing.get(TestThing.NUMBER_URI)).isEqualTo(43);
assertThat((Integer) dthing.get(TestThing.NUMBER_URI)).isEqualTo(43);
assertThat((Integer) mthing.get(TestThing.NUMBER_URI)).isNull();

assertThat((Integer) thing.get("n/a")).isNull();
assertThat((Integer) bthing.get("n/a")).isNull();
assertThat((Integer) dthing.get("n/a")).isNull();
assertThat((Integer) mthing.get("n/a")).isNull();

assertThat((Instant) ething.get(EXTRA)).isEqualTo(INSTANT);
assertThat((Instant) mthing.get(EXTRA)).isEqualTo(INSTANT);
}

@Test
Expand All @@ -106,6 +124,7 @@ public void thing_getters_asclass() {
assertThat(ething.get(EXTRA, String.class)).isEqualTo(INSTANT.toString());

assertThat(thing.get("n/a", String.class)).isNull();
assertThat(mthing.get(TestThing.NUMBER_URI, Integer.class)).isNull();
}

@Test
Expand All @@ -119,6 +138,8 @@ public void properties() {
assertThat(ething.properties())
.containsExactly(
KIRI.RDFS.LABEL, "hello, world", TestThing.NUMBER_URI, 43, EXTRA, INSTANT);
assertThat(mthing.properties())
.containsExactly(KIRI.RDFS.LABEL, "hello, world", EXTRA, INSTANT);
}

@Test
Expand Down Expand Up @@ -152,6 +173,13 @@ public void datatypes() {
dev.enola.model.xsd.Datatypes.INT.iri(),
EXTRA,
Datatypes.DATE_TIME.iri());

assertThat(mthing.datatypes())
.containsExactly(
KIRI.RDFS.LABEL,
dev.enola.datatype.Datatypes.STRING.iri(),
EXTRA,
Datatypes.DATE_TIME.iri());
}

@Test
Expand Down Expand Up @@ -186,7 +214,6 @@ public void equals() {
public void copy() {
// TODO assertThat(thing.copy().build()).isEqualTo(thing);
// TODO assertThat(ething.copy().build()).isEqualTo(ething);
// TODO assertThat(mthing.copy().build()).isEqualTo(mthing);
}

// TODO missing number - should not be in properties!
}