-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat (core): Introduce ThingsSubject testutil
- Loading branch information
Showing
7 changed files
with
233 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../models/example.org/greeting1.ttl |