From a6a9066d730071b790bd756898c1fbeffcd1affc Mon Sep 17 00:00:00 2001 From: pwinckles Date: Wed, 22 Apr 2020 21:49:16 +0000 Subject: [PATCH] fix NPE in event serialization (#1665) --- .../serialization/JsonLDEventMessage.java | 3 +- .../EventSerializerTestBase.java | 20 ++++++++--- .../serialization/JsonLDSerializerTest.java | 36 +++++++++++-------- 3 files changed, 39 insertions(+), 20 deletions(-) diff --git a/fcrepo-event-serialization/src/main/java/org/fcrepo/event/serialization/JsonLDEventMessage.java b/fcrepo-event-serialization/src/main/java/org/fcrepo/event/serialization/JsonLDEventMessage.java index 072a2dc04a..9e4dd181c2 100644 --- a/fcrepo-event-serialization/src/main/java/org/fcrepo/event/serialization/JsonLDEventMessage.java +++ b/fcrepo-event-serialization/src/main/java/org/fcrepo/event/serialization/JsonLDEventMessage.java @@ -27,6 +27,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import java.util.Objects; import org.fcrepo.kernel.api.observer.Event; @@ -183,7 +184,7 @@ public static JsonLDEventMessage from(final Event evt) { // build actors list final List actor = new ArrayList<>(); - actor.add(new Person(evt.getUserURI().toString(), singletonList("Person"))); + actor.add(new Person(Objects.toString(evt.getUserURI()), singletonList("Person"))); final String softwareAgent = evt.getUserAgent(); if (softwareAgent != null) { actor.add(new Application(softwareAgent, singletonList("Application"))); diff --git a/fcrepo-event-serialization/src/test/java/org/fcrepo/event/serialization/EventSerializerTestBase.java b/fcrepo-event-serialization/src/test/java/org/fcrepo/event/serialization/EventSerializerTestBase.java index c0259438bb..f02a902702 100644 --- a/fcrepo-event-serialization/src/test/java/org/fcrepo/event/serialization/EventSerializerTestBase.java +++ b/fcrepo-event-serialization/src/test/java/org/fcrepo/event/serialization/EventSerializerTestBase.java @@ -73,7 +73,7 @@ public class EventSerializerTestBase { protected final String softwareAgent = "fcrepo-java-client"; - protected void mockEvent(final String path) { + private void mockEventCommon(final String path) { final Set typeSet = new HashSet<>(); typeSet.add(EventType.RESOURCE_MODIFICATION); final Set resourceTypeSet = new HashSet<>(); @@ -85,7 +85,7 @@ protected void mockEvent(final String path) { when(mockEvent.getResourceTypes()).thenReturn(resourceTypeSet); when(mockEvent.getPath()).thenReturn(path); when(mockEvent.getUserID()).thenReturn(username); - when(mockEvent.getUserURI()).thenReturn(URI.create(getAgentIRI())); + when(mockEvent.getUserURI()).thenReturn(URI.create(getAgentURI())); when(mockEvent.getDate()).thenReturn(timestamp); when(mockEvent.getEventID()).thenReturn(eventResourceId); @@ -93,6 +93,18 @@ protected void mockEvent(final String path) { when(mockEvent.getBaseUrl()).thenReturn(baseUrl); } + protected void mockEvent(final String path) { + mockEventCommon(path); + when(mockEvent.getUserID()).thenReturn(username); + when(mockEvent.getUserURI()).thenReturn(URI.create(getAgentURI())); + } + + protected void mockEventNullUser(final String path) { + mockEventCommon(path); + when(mockEvent.getUserID()).thenReturn(null); + when(mockEvent.getUserURI()).thenReturn(null); + } + protected void testModel(final Model model) { final Resource resourceSubject = createResource(baseUrl + path); final Resource eventSubject = createResource(eventResourceId); @@ -117,7 +129,7 @@ protected void testModel(final Model model) { final Resource r = statement.getResource(); if (r.hasProperty(type, createResource(ACTIVITY_STREAMS_NAMESPACE + "Person"))) { assertTrue(r.hasProperty(type, createResource(ACTIVITY_STREAMS_NAMESPACE + "Person"))); - assertEquals(getAgentIRI(), r.toString()); + assertEquals(getAgentURI(), r.toString()); } else { assertTrue(r.hasProperty(type, createResource(ACTIVITY_STREAMS_NAMESPACE + "Application"))); assertTrue(r.hasProperty(createProperty(ACTIVITY_STREAMS_NAMESPACE + "name"), softwareAgent)); @@ -136,7 +148,7 @@ protected void testModel(final Model model) { assertEquals(1, eventName.get()); } - protected String getAgentIRI() { + protected String getAgentURI() { return userAgentBaseUri + username; } } diff --git a/fcrepo-event-serialization/src/test/java/org/fcrepo/event/serialization/JsonLDSerializerTest.java b/fcrepo-event-serialization/src/test/java/org/fcrepo/event/serialization/JsonLDSerializerTest.java index 0215fff511..6793693ae3 100644 --- a/fcrepo-event-serialization/src/test/java/org/fcrepo/event/serialization/JsonLDSerializerTest.java +++ b/fcrepo-event-serialization/src/test/java/org/fcrepo/event/serialization/JsonLDSerializerTest.java @@ -18,23 +18,23 @@ package org.fcrepo.event.serialization; -import static java.nio.charset.StandardCharsets.UTF_8; -import static org.apache.jena.rdf.model.ModelFactory.createDefaultModel; -import static org.fcrepo.kernel.api.RdfLexicon.PROV_NAMESPACE; -import static org.fcrepo.kernel.api.RdfLexicon.REPOSITORY_NAMESPACE; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.apache.jena.rdf.model.Model; +import org.fcrepo.kernel.api.observer.EventType; +import org.junit.Test; import java.io.ByteArrayInputStream; import java.io.IOException; import java.util.ArrayList; import java.util.List; -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; -import org.apache.jena.rdf.model.Model; -import org.fcrepo.kernel.api.observer.EventType; -import org.junit.Test; +import static java.nio.charset.StandardCharsets.UTF_8; +import static org.apache.jena.rdf.model.ModelFactory.createDefaultModel; +import static org.fcrepo.kernel.api.RdfLexicon.PROV_NAMESPACE; +import static org.fcrepo.kernel.api.RdfLexicon.REPOSITORY_NAMESPACE; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; /** *

@@ -58,11 +58,17 @@ public void testJsonSerializationAsModel() { @Test public void testJsonSerializationAsJson() throws IOException { - testJsonSerializationAsJson(path, path); + mockEvent(path); + testJsonSerializationAsJson(path, getAgentURI()); + } + + @Test + public void testJsonSerializationAsJsonWithNullUser() throws IOException { + mockEventNullUser(path); + testJsonSerializationAsJson(path, "null"); } - private void testJsonSerializationAsJson(final String inputPath, final String outputPath) throws IOException { - mockEvent(inputPath); + private void testJsonSerializationAsJson(final String outputPath, final String user) throws IOException { final EventSerializer serializer = new JsonLDSerializer(); final String json = serializer.serialize(mockEvent); @@ -79,7 +85,7 @@ private void testJsonSerializationAsJson(final String inputPath, final String ou assertEquals(eventResourceId, node.get("id").textValue()); assertEquals(EventType.RESOURCE_MODIFICATION.getName(), node.get("name").textValue()); assertEquals(EventType.RESOURCE_MODIFICATION.getTypeAbbreviated(), node.get("type").get(0).asText()); - assertEquals(getAgentIRI(), node.get("actor").get(0).get("id").asText()); + assertEquals(user, node.get("actor").get(0).get("id").asText()); assertEquals("Person", node.get("actor").get(0).get("type").get(0).asText()); assertEquals(softwareAgent, node.get("actor").get(1).get("name").asText()); assertEquals("Application", node.get("actor").get(1).get("type").get(0).asText());