Skip to content
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
5 changes: 4 additions & 1 deletion docs/library-template-model.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ package are unchanged.
([nanopub-java#218](https://github.com/Nanopublication/nanopub-java/issues/218)).
- **Retrieval goes through Nanodash.** A template gets its own nanopublication, and those that
hold the values of a placeholder (`nt:possibleValuesFrom`), from `TemplateData.RESOLVER`,
which is `Utils.getNanopub` with its cache. The value lists are now loaded when first asked
which is `Utils.getNanopub` with its cache. It first strips an IRI within a nanopublication
down to the nanopublication (`Utils.stripToNanopubId`), as the compliance check asks for a
template by the IRI a nanopublication names it with, which can be its node
(`<np>/templateVersion`). The value lists are now loaded when first asked
for, not when the template is read.

One difference in behaviour: a label the template itself gives to an IRI now wins over a label
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@
<dependency>
<groupId>org.nanopub</groupId>
<artifactId>nanopub</artifactId>
<version>1.97.1</version>
<version>1.98.2</version>
</dependency>
<dependency>
<groupId>io.github.openhtmltopdf</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,13 @@ public class TemplateData implements Serializable {
* How a template retrieves nanopublications: its own, and those that hold the values a
* placeholder can take ({@code nt:possibleValuesFrom}). They come from where Nanodash
* gets all its nanopublications, so that they are cached like the others.
* <p>
* An IRI within a nanopublication retrieves that nanopublication. A nanopublication made
* from a template with an identity of its own names the template by its node
* ({@code <np>/templateVersion}), and that is what the compliance check passes on, while
* {@link Utils#getNanopub(String)} only takes a nanopublication URI or an artifact code.
*/
public static final NanopubResolver RESOLVER = Utils::getNanopub;
public static final NanopubResolver RESOLVER = id -> Utils.getNanopub(Utils.stripToNanopubId(id));

private static TemplateData instance;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
import org.eclipse.rdf4j.model.vocabulary.PROV;
import org.eclipse.rdf4j.model.vocabulary.RDF;
import org.eclipse.rdf4j.model.vocabulary.RDFS;
import com.knowledgepixels.nanodash.Utils;
import org.junit.jupiter.api.Test;
import org.mockito.MockedStatic;
import org.nanopub.Nanopub;
import org.nanopub.NanopubCreator;
import org.nanopub.templates.ComplianceIssue;
Expand All @@ -20,6 +22,8 @@
import java.util.Map;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mockStatic;

/**
* Tests for {@link TemplateComplianceGuard}: it reports what nanopub-java finds, and never
Expand Down Expand Up @@ -73,11 +77,16 @@ private void registerTemplates() throws Exception {

// A nanopublication as the form makes it from these templates, with the given assertion.
private static Nanopub nanopub(IRI predicate) throws Exception {
return nanopub(predicate, ASSERTION_TEMPLATE);
}

// The same, naming its assertion template with the given IRI.
private static Nanopub nanopub(IRI predicate, String assertionTemplateLink) throws Exception {
NanopubCreator creator = new NanopubCreator(NP);
creator.addAssertionStatement(THING, predicate, vf.createLiteral("A thing"));
creator.addProvenanceStatement(PROV.WAS_ATTRIBUTED_TO, ME);
creator.addPubinfoStatement(DCTERMS.CREATOR, ME);
creator.addPubinfoStatement(NTEMPLATE.WAS_CREATED_FROM_TEMPLATE, vf.createIRI(ASSERTION_TEMPLATE));
creator.addPubinfoStatement(NTEMPLATE.WAS_CREATED_FROM_TEMPLATE, vf.createIRI(assertionTemplateLink));
creator.addPubinfoStatement(NTEMPLATE.WAS_CREATED_FROM_PROVENANCE_TEMPLATE, vf.createIRI(PROVENANCE_TEMPLATE));
creator.addPubinfoStatement(NTEMPLATE.WAS_CREATED_FROM_PUBINFO_TEMPLATE, vf.createIRI(PUBINFO_TEMPLATE));
return creator.finalizeNanopub();
Expand All @@ -101,6 +110,27 @@ void nanopublicationThatDoesNotFollowItsTemplatesIsReported() throws Exception {
report.getErrors().stream().map(ComplianceIssue::kind).toList());
}

@Test
void templateNamedByItsNodeIsRetrievedThroughNanodash() throws Exception {
registerTemplates();
// The check runs with Nanodash's resolver, as on the publish form. Utils.getNanopub is
// stood in for by one that, like the real one, takes only a nanopublication URI or an
// artifact code.
try (MockedStatic<Utils> utils = mockStatic(Utils.class)) {
utils.when(() -> Utils.stripToNanopubId(anyString())).thenCallRealMethod();
utils.when(() -> Utils.getNanopub(anyString())).thenAnswer(call -> {
String id = call.getArgument(0);
if (!id.matches("(.*[^A-Za-z0-9_-])?RA[A-Za-z0-9_-]{43}")) throw new IllegalArgumentException("Not a well-formed trusty URI");
return templates.get(id);
});
// A nanopublication made from a template with an identity of its own names the
// template by its node:
ComplianceReport report = TemplateComplianceGuard.check(nanopub(RDFS.LABEL, ASSERTION_TEMPLATE + "/templateVersion"));
assertNotNull(report);
assertEquals(List.of(), report.getIssues());
}
}

@Test
void templateThatCannotBeRetrievedIsAFindingNotAFailure() throws Exception {
ComplianceReport report = TemplateComplianceGuard.check(nanopub(RDFS.LABEL), resolver);
Expand Down