Skip to content

Commit

Permalink
feat (core): Introduce ThingsSubject testutil
Browse files Browse the repository at this point in the history
  • Loading branch information
vorburger committed Sep 24, 2024
1 parent f561034 commit 29e8d0e
Show file tree
Hide file tree
Showing 7 changed files with 233 additions and 1 deletion.
2 changes: 1 addition & 1 deletion java/dev/enola/rdf/io/ModelSubject.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

final class ModelSubject extends Subject {

// TODO Move this class to a //common/testlib
// TODO Move this class to a //common/testlib (and rm copy/paste in thing.testlib)

public static ModelSubject assertThat(Model actual) {
return assertAbout(resources()).that(actual);
Expand Down
50 changes: 50 additions & 0 deletions java/dev/enola/thing/testlib/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# SPDX-License-Identifier: Apache-2.0
#
# Copyright 2023 The Enola <https://enola.dev> Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

load("@rules_java//java:defs.bzl", "java_library")
load("//tools/bazel:junit.bzl", "junit_tests")

java_library(
name = "testlib",
srcs = glob(
["*.java"],
exclude = ["*Test.java"],
),
visibility = ["//:__subpackages__"],
deps = [
"//java/dev/enola/common/io",
"//java/dev/enola/rdf/io",
"//java/dev/enola/thing:thing_java",
"@maven//:com_google_errorprone_error_prone_annotations",
"@maven//:com_google_guava_guava",
"@maven//:com_google_truth_truth",
"@maven//:org_eclipse_rdf4j_rdf4j_model",
"@maven//:org_eclipse_rdf4j_rdf4j_model_api",
"@maven//:org_jspecify_jspecify",
],
)

junit_tests(
name = "tests",
srcs = glob(["*Test.java"]),
deps = [
":testlib",
"//java/dev/enola/common/io",
"//java/dev/enola/thing:thing_java",
"//test",
"@maven//:org_jspecify_jspecify",
],
)
54 changes: 54 additions & 0 deletions java/dev/enola/thing/testlib/ModelSubject.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* Copyright 2024 The Enola <https://enola.dev> Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dev.enola.thing.testlib;

import static com.google.common.truth.Truth.assertAbout;

import com.google.common.truth.FailureMetadata;
import com.google.common.truth.Subject;
import com.google.common.truth.Truth;

import org.eclipse.rdf4j.model.Model;
import org.eclipse.rdf4j.model.util.Models;

final class ModelSubject extends Subject {

// TODO Remove this copy/paste duplicate of rdf.io.ModelSubject, once that's in a testlib

public static ModelSubject assertThat(Model actual) {
return assertAbout(resources()).that(actual);
}

public static Factory<ModelSubject, Model> resources() {
return ModelSubject::new;
}

private final Model actual;

public ModelSubject(FailureMetadata metadata, Model actual) {
super(metadata, actual);
this.actual = actual;
}

public void isEqualTo(Model expected) {
if (!Models.isomorphic(actual, expected)) {
// TODO Canonicalizer-like sorting of Statements by IRI
Truth.assertThat(actual).isEqualTo(expected);
}
}
}
66 changes: 66 additions & 0 deletions java/dev/enola/thing/testlib/ThingsSubject.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* Copyright 2024 The Enola <https://enola.dev> Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dev.enola.thing.testlib;

import static com.google.common.truth.Truth.assertAbout;

import com.google.common.collect.Streams;
import com.google.common.truth.FailureMetadata;
import com.google.common.truth.Subject;

import dev.enola.common.io.resource.ClasspathResource;
import dev.enola.common.io.resource.ResourceProvider;
import dev.enola.rdf.io.JavaThingRdfConverter;
import dev.enola.rdf.io.RdfReaderConverter;
import dev.enola.thing.repo.ThingRepository;

import org.eclipse.rdf4j.model.Model;
import org.eclipse.rdf4j.model.impl.DynamicModel;
import org.eclipse.rdf4j.model.impl.LinkedHashModelFactory;

final class ThingsSubject extends Subject {

// TODO add assertThat(Thing actual) - with a SingleThingRepository ?

public static ThingsSubject assertThat(ThingRepository actual) {
return assertAbout(resources()).that(actual);
}

public static Factory<ThingsSubject, ThingRepository> resources() {
return ThingsSubject::new;
}

private static final Model EMPTY_MODEL = new DynamicModel(new LinkedHashModelFactory());

private final Model actualModel;
private final ResourceProvider rp = new ClasspathResource.Provider();
private final RdfReaderConverter rdfReaderConverter = new RdfReaderConverter(rp);
private final JavaThingRdfConverter javaThingRdfConverter = new JavaThingRdfConverter();

public ThingsSubject(FailureMetadata metadata, ThingRepository actual) {
super(metadata, actual);
actualModel = javaThingRdfConverter.convert(Streams.stream(actual.list()));
}

public void isEqualTo(String classpathResourcePath) {
var resource = rp.getReadableResource(classpathResourcePath);
if (resource == null) throw new IllegalArgumentException(classpathResourcePath);
var expectedModel = rdfReaderConverter.convert(resource).orElse(EMPTY_MODEL);
ModelSubject.assertThat(actualModel).isEqualTo(expectedModel);
}
}
40 changes: 40 additions & 0 deletions java/dev/enola/thing/testlib/ThingsSubjectTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* Copyright 2024 The Enola <https://enola.dev> Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dev.enola.thing.testlib;

import dev.enola.thing.repo.ThingsBuilder;
import dev.enola.thing.repo.ThingsRepository;

import org.junit.Test;

public class ThingsSubjectTest {

@Test
public void empty() {
ThingsRepository r = new ThingsBuilder();
ThingsSubject.assertThat(r).isEqualTo("classpath:/empty.yaml");
}

@Test
public void ttl() {
ThingsBuilder r = new ThingsBuilder();
r.getBuilder("https://example.org/greeting1")
.set("https://example.org/message", "hello, world");
ThingsSubject.assertThat(r).isEqualTo("classpath:/greeting1.ttl");
}
}
21 changes: 21 additions & 0 deletions java/dev/enola/thing/testlib/package-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* Copyright 2024 The Enola <https://enola.dev> Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
@NullMarked
package dev.enola.thing.testlib;

import org.jspecify.annotations.NullMarked;
1 change: 1 addition & 0 deletions test/greeting1.ttl

0 comments on commit 29e8d0e

Please sign in to comment.