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

Expose exceptions #539

Merged
merged 3 commits into from
Feb 24, 2021
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
@@ -1,5 +1,3 @@
package org.wikidata.wdtk.datamodel.helpers;

/*-
* #%L
* Wikidata Toolkit Data Model
Expand All @@ -20,13 +18,24 @@
* #L%
*/

import java.io.IOException;
package org.wikidata.wdtk.datamodel.helpers;

import com.fasterxml.jackson.databind.ObjectReader;
import org.wikidata.wdtk.datamodel.implementation.*;
import org.wikidata.wdtk.datamodel.interfaces.*;
import org.wikidata.wdtk.datamodel.implementation.EntityDocumentImpl;
import org.wikidata.wdtk.datamodel.implementation.EntityRedirectDocumentImpl;
import org.wikidata.wdtk.datamodel.implementation.ItemDocumentImpl;
import org.wikidata.wdtk.datamodel.implementation.LexemeDocumentImpl;
import org.wikidata.wdtk.datamodel.implementation.MediaInfoDocumentImpl;
import org.wikidata.wdtk.datamodel.implementation.PropertyDocumentImpl;
import org.wikidata.wdtk.datamodel.interfaces.EntityDocument;
import org.wikidata.wdtk.datamodel.interfaces.EntityRedirectDocument;
import org.wikidata.wdtk.datamodel.interfaces.ItemDocument;
import org.wikidata.wdtk.datamodel.interfaces.LexemeDocument;
import org.wikidata.wdtk.datamodel.interfaces.MediaInfoDocument;
import org.wikidata.wdtk.datamodel.interfaces.PropertyDocument;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectReader;

/**
* Helper to deserialize datamodel objects from their
Expand Down Expand Up @@ -72,55 +81,55 @@ public JsonDeserializer(String siteIri) {

/**
* Deserializes a JSON string into an {@link ItemDocument}.
* @throws IOException
* @throws JsonProcessingException
if the JSON payload is invalid
*/
public ItemDocument deserializeItemDocument(String json) throws IOException {
public ItemDocument deserializeItemDocument(String json) throws JsonProcessingException {
return itemReader.readValue(json);
}

/**
* Deserializes a JSON string into a {@link PropertyDocument}.
* @throws IOException
* @throws JsonProcessingException
if the JSON payload is invalid
*/
public PropertyDocument deserializePropertyDocument(String json) throws IOException {
public PropertyDocument deserializePropertyDocument(String json) throws JsonProcessingException {
return propertyReader.readValue(json);
}

/**
* Deserializes a JSON string into a {@link LexemeDocument}.
* @throws IOException
* @throws JsonProcessingException
if the JSON payload is invalid
*/
public LexemeDocument deserializeLexemeDocument(String json) throws IOException {
public LexemeDocument deserializeLexemeDocument(String json) throws JsonProcessingException {
return lexemeReader.readValue(json);
}

/**
* Deserializes a JSON string into a {@link MediaInfoDocument}.
* @throws IOException
* @throws JsonProcessingException
if the JSON payload is invalid
*/
public MediaInfoDocument deserializeMediaInfoDocument(String json) throws IOException {
public MediaInfoDocument deserializeMediaInfoDocument(String json) throws JsonProcessingException {
return mediaInfoReader.readValue(json);
}

/**
* Deserializes a JSON string into a {@link EntityDocument}.
* @throws IOException
* @throws JsonProcessingException
if the JSON payload is invalid
*/
public EntityDocument deserializeEntityDocument(String json) throws IOException {
public EntityDocument deserializeEntityDocument(String json) throws JsonProcessingException {
return entityDocumentReader.readValue(json);
}

/**
* Deserializes a JSON string into a {@link EntityRedirectDocument}.
* @throws IOException
* @throws JsonProcessingException
if the JSON payload is invalid
*/
public EntityRedirectDocument deserializeEntityRedirectDocument(String json) throws IOException {
public EntityRedirectDocument deserializeEntityRedirectDocument(String json) throws JsonProcessingException {
return entityRedirectReader.readValue(json);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
package org.wikidata.wdtk.datamodel.helpers;

/*
* #%L
* Wikidata Toolkit Data Model
Expand All @@ -20,6 +18,8 @@
* #L%
*/

package org.wikidata.wdtk.datamodel.helpers;

import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
Expand Down Expand Up @@ -173,69 +173,54 @@ private void serializeEntityDocument(EntityDocument entityDocument) {
}

/**
* Serializes the given object in JSON and returns the resulting string. In
* case of errors, null is returned.
* Serializes the given object in JSON and returns the resulting string.
* Throws if the serialization fails.
*
* @param itemDocument
* object to serialize
* @return JSON serialization or null
* @return JSON serialization
* @throws JsonProcessingException if the object cannot be serialized
*/
public static String getJsonString(ItemDocument itemDocument) {
return jacksonObjectToString(itemDocument);
public static String getJsonString(ItemDocument itemDocument) throws JsonProcessingException {
return mapper.writeValueAsString(itemDocument);
}

/**
* Serializes the given object in JSON and returns the resulting string. In
* case of errors, null is returned.
* Serializes the given object in JSON and returns the resulting string.
* Throws if the serialization fails.
*
* @param propertyDocument
* object to serialize
* @return JSON serialization or null
* @return JSON serialization
* @throws JsonProcessingException if the object cannot be serialized
*/
public static String getJsonString(PropertyDocument propertyDocument) {
return jacksonObjectToString(propertyDocument);
public static String getJsonString(PropertyDocument propertyDocument) throws JsonProcessingException {
return mapper.writeValueAsString(propertyDocument);
}

/**
* Serializes the given object in JSON and returns the resulting string. In
* case of errors, null is returned.
* Serializes the given object in JSON and returns the resulting string.
* Throws if the serialization fails.
*
* @param mediaInfoDocument
* object to serialize
* @return JSON serialization or null
* @return JSON serialization
* @throws JsonProcessingException if the object cannot be serialized
*/
public static String getJsonString(MediaInfoDocument mediaInfoDocument) {
return jacksonObjectToString(mediaInfoDocument);
public static String getJsonString(MediaInfoDocument mediaInfoDocument) throws JsonProcessingException {
return mapper.writeValueAsString(mediaInfoDocument);
}

/**
* Serializes the given object in JSON and returns the resulting string. In
* case of errors, null is returned.
* Serializes the given object in JSON and returns the resulting string.
* Throws if the serialization fails.
*
* @param statement
* object to serialize
* @return JSON serialization or null
*/
public static String getJsonString(Statement statement) {
return jacksonObjectToString(statement);
}

/**
* Serializes the given object in JSON and returns the resulting string. In
* case of errors, null is returned. In particular, this happens if the
* object is not based on a Jackson-annotated class. An error is logged in
* this case.
*
* @param object
* object to serialize
* @return JSON serialization or null
* @return JSON serialization
* @throws JsonProcessingException if the object cannot be serialized
*/
protected static String jacksonObjectToString(Object object) {
try {
return mapper.writeValueAsString(object);
} catch (JsonProcessingException e) {
logger.error("Failed to serialize JSON data: " + e.toString());
return null;
}
public static String getJsonString(Statement statement) throws JsonProcessingException {
return mapper.writeValueAsString(statement);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,16 @@
package org.wikidata.wdtk.datamodel.helpers;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.junit.jupiter.api.Test;
import org.wikidata.wdtk.datamodel.implementation.EntityDocumentImpl;
Expand All @@ -37,10 +39,14 @@
import org.wikidata.wdtk.datamodel.interfaces.EntityDocument;
import org.wikidata.wdtk.datamodel.interfaces.ItemDocument;
import org.wikidata.wdtk.datamodel.interfaces.ItemIdValue;
import org.wikidata.wdtk.datamodel.interfaces.MonolingualTextValue;
import org.wikidata.wdtk.datamodel.interfaces.PropertyDocument;
import org.wikidata.wdtk.datamodel.interfaces.SiteLink;
import org.wikidata.wdtk.datamodel.interfaces.Statement;
import org.wikidata.wdtk.datamodel.interfaces.StatementGroup;
import org.wikidata.wdtk.datamodel.interfaces.StatementRank;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.MappingIterator;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectReader;
Expand Down Expand Up @@ -97,7 +103,7 @@ public void testSerializer() throws IOException {
}

@Test
public void testItemDocumentToJson() {
public void testItemDocumentToJson() throws JsonProcessingException {
ItemDocument id = Datamodel.makeItemDocument(
Datamodel.makeWikidataItemIdValue("Q42"),
Collections.emptyList(),
Expand All @@ -110,7 +116,7 @@ public void testItemDocumentToJson() {
}

@Test
public void testPropertyDocumentToJson() {
public void testPropertyDocumentToJson() throws JsonProcessingException {
PropertyDocument pd = Datamodel.makePropertyDocument(
Datamodel.makeWikidataPropertyIdValue("P1"),
Collections. emptyList(),
Expand All @@ -123,7 +129,7 @@ Collections. emptyList(),
}

@Test
public void testStatementToJson() {
public void testStatementToJson() throws JsonProcessingException {
Statement s = Datamodel.makeStatement(ItemIdValue.NULL,
Datamodel.makeNoValueSnak(Datamodel.makeWikidataPropertyIdValue("P1")),
Collections.emptyList(), Collections.emptyList(),
Expand All @@ -134,14 +140,74 @@ public void testStatementToJson() {

@Test
public void testJacksonObjectToJsonError() {
Object obj = new Object() {
@SuppressWarnings("unused")
public String getData() {
throw new RuntimeException("Test exception");
ItemDocument obj = new ItemDocument() {

@Override
public List<StatementGroup> getStatementGroups() {
return null;
}

@Override
public long getRevisionId() {
return 0;
}

@Override
public Map<String, MonolingualTextValue> getLabels() {
return null;
}

@Override
public Map<String, MonolingualTextValue> getDescriptions() {
return null;
}

@Override
public Map<String, List<MonolingualTextValue>> getAliases() {
return null;
}

@Override
public ItemDocument withoutStatementIds(Set<String> statementIds) {
return null;
}

@Override
public ItemDocument withStatement(Statement statement) {
return null;
}

@Override
public ItemDocument withRevisionId(long newRevisionId) {
return null;
}

@Override
public ItemDocument withLabel(MonolingualTextValue newLabel) {
return null;
}

@Override
public ItemDocument withDescription(MonolingualTextValue newDescription) {
return null;
}

@Override
public ItemDocument withAliases(String language, List<MonolingualTextValue> aliases) {
return null;
}

@Override
public Map<String, SiteLink> getSiteLinks() {
return null;
}

@Override
public ItemIdValue getEntityId() {
return null;
}
};

String result = JsonSerializer.jacksonObjectToString(obj);
assertNull(result);
assertThrows(JsonProcessingException.class, () -> JsonSerializer.getJsonString(obj));
}
}
Loading