Skip to content

Commit

Permalink
fix: NPE when frame is missing (#400)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tomvbe authored Nov 15, 2023
1 parent 6a92856 commit 2850aa4
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 4 deletions.
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 @@ -45,6 +46,23 @@ void formatModel_jsonLD() throws IOException, URISyntaxException {
assertTrue(outputModel.isIsomorphicWith(expected));
}

@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 @@ -88,7 +106,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

0 comments on commit 2850aa4

Please sign in to comment.