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

fix: NPE when frame is missing #400

Merged
merged 2 commits into from
Nov 15, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@

import static be.vlaanderen.informatievlaanderen.ldes.ldi.rdf.formatter.PrefixAdder.addPrefixesToModel;

public class JsonLdWriter implements LdiRdfWriter {
public class JsonLdFrameWriter implements LdiRdfWriter {
private final String frame;

public JsonLdWriter(LdiRdfWriterProperties properties) {
public JsonLdFrameWriter(LdiRdfWriterProperties properties) {
this.frame = properties.getJsonLdFrame();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package be.vlaanderen.informatievlaanderen.ldes.ldi.rdf.formatter;

import org.apache.jena.rdf.model.Model;
import org.apache.jena.riot.RDFFormat;
import org.apache.jena.riot.RDFWriter;

import static be.vlaanderen.informatievlaanderen.ldes.ldi.rdf.formatter.PrefixAdder.addPrefixesToModel;

public class JsonLdPrettyWriter implements LdiRdfWriter {

@Override
public String write(Model model) {
return RDFWriter.source(addPrefixesToModel(model))
.format(RDFFormat.JSONLD10_PRETTY)
.asString();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@
import org.apache.jena.rdf.model.Model;
import org.apache.jena.riot.Lang;

import static org.apache.commons.lang3.StringUtils.isBlank;

public interface LdiRdfWriter {
String write(Model model);

static LdiRdfWriter getRdfWriter(LdiRdfWriterProperties properties) {
if (Lang.JSONLD.equals(properties.getLang())) {
return new JsonLdWriter(properties);
return isBlank(properties.getJsonLdFrame())
? new JsonLdPrettyWriter()
: new JsonLdFrameWriter(properties);
} else {
return new GenericRdfWriter(properties);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import static org.junit.jupiter.api.Assertions.*;

public class LdiRdfWriterTest {

@Test
void formatModel_jsonLD() throws IOException, URISyntaxException {
String input = getFileContentString("rdf/formatter/product.jsonld");
Expand All @@ -43,6 +44,23 @@ void formatModel_jsonLD() throws IOException, URISyntaxException {
assertEquals(expected, outputJson);
}

@Test
void formatModel_jsonLD_withoutFrame() throws IOException, URISyntaxException {
String input = getFileContentString("rdf/formatter/product.jsonld");

Model model = RDFParser.fromString(input)
.lang(Lang.JSONLD)
.toModel();

LdiRdfWriterProperties writerProperties = new LdiRdfWriterProperties();

String output = LdiRdfWriter.getRdfWriter(writerProperties.withLang(Lang.JSONLD)).write(model);

JsonObject outputJson = JSON.parse(output);

assertNotNull(outputJson);
}

@Test
void formatModel_turtle() throws IOException, URISyntaxException {
String input = getFileContentString("rdf/formatter/product.jsonld");
Expand Down Expand Up @@ -86,7 +104,7 @@ void getFramedContext() {
}
""";

JsonLDWriteContext context = (JsonLDWriteContext) JsonLdWriter.getFramedContext(frame);
JsonLDWriteContext context = (JsonLDWriteContext) JsonLdFrameWriter.getFramedContext(frame);

JsonObject frameObject = JSON.parse((String) context.get(JsonLD10Writer.JSONLD_FRAME));
assertTrue(frameObject.hasKey("@type"));
Expand Down