Skip to content

Commit

Permalink
Add a fix and a test for issue #19.
Browse files Browse the repository at this point in the history
Since event subject traversal didn't filter its nodes for valid
types, deletion of a subject would result in the last event link
node being put in the result set, which caused a crash when we tried
to serialize it.
  • Loading branch information
mikesname committed Feb 7, 2014
1 parent 78524da commit fb38c67
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,7 @@ public Iterable<AccessibleEntity> getSubjects() {
.loop("n", JavaHandlerUtils.noopLoopFunc, new PipeFunction<LoopPipe.LoopBundle<Vertex>, Boolean>() {
@Override
public Boolean compute(LoopPipe.LoopBundle<Vertex> vertexLoopBundle) {
return !vertexLoopBundle.getObject().getVertices(Direction.IN,
Ontology.ENTITY_HAS_LIFECYCLE_EVENT).iterator().hasNext();
return isValidTarget(vertexLoopBundle.getObject());
}
}));
}
Expand All @@ -88,9 +87,7 @@ public AccessibleEntity getFirstSubject() {
.loop("n", JavaHandlerUtils.noopLoopFunc, new PipeFunction<LoopPipe.LoopBundle<Vertex>, Boolean>() {
@Override
public Boolean compute(LoopPipe.LoopBundle<Vertex> vertexLoopBundle) {
return (!vertexLoopBundle.getObject().getVertices(Direction.IN,
Ontology.ENTITY_HAS_LIFECYCLE_EVENT).iterator().hasNext())
&& vertexLoopBundle.getObject().getProperty(EntityType.TYPE_KEY) != null;
return isValidTarget(vertexLoopBundle.getObject());
}
});
return (AccessibleEntity)(subjects.iterator().hasNext()
Expand All @@ -109,5 +106,11 @@ public Boolean compute(LoopPipe.LoopBundle<Vertex> vertexLoopBundle) {
}
}));
}

private boolean isValidTarget(Vertex vertex) {
return (!vertex.getVertices(Direction.IN,
Ontology.ENTITY_HAS_LIFECYCLE_EVENT).iterator().hasNext())
&& vertex.getProperty(EntityType.TYPE_KEY) != null;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package eu.ehri.project.models.events;

import eu.ehri.project.definitions.EventTypes;
import eu.ehri.project.models.UserProfile;
import eu.ehri.project.models.base.Actioner;
import eu.ehri.project.persistence.ActionManager;
import eu.ehri.project.persistence.Bundle;
import eu.ehri.project.persistence.BundleDAO;
import eu.ehri.project.persistence.Serializer;
import eu.ehri.project.test.AbstractFixtureTest;
import eu.ehri.project.test.TestData;
import org.junit.Test;
import org.neo4j.helpers.collection.Iterables;

import static org.junit.Assert.assertEquals;

/**
* @author Mike Bryant (http://github.com/mikesname)
*/
public class SystemEventTest extends AbstractFixtureTest {
@Test
public void testGetSubjects() throws Exception {
ActionManager am = new ActionManager(graph);
BundleDAO dao = new BundleDAO(graph);
Serializer serializer = new Serializer(graph);
// Create a user and log it
Bundle userBundle = Bundle.fromData(TestData.getTestUserBundle());
UserProfile user = dao.create(userBundle, UserProfile.class);

SystemEvent first = am.logEvent(user,
graph.frame(validUser.asVertex(), Actioner.class),
EventTypes.creation).getSystemEvent();
assertEquals(1, Iterables.count(first.getSubjects()));

// Delete the user and log it
SystemEvent second = am.logEvent(user,
graph.frame(validUser.asVertex(), Actioner.class),
EventTypes.deletion).getSystemEvent();
dao.delete(serializer.vertexFrameToBundle(user));

// First event should now have 0 subjects, since it's
// been deleted.
assertEquals(0, Iterables.count(first.getSubjects()));
}
}

0 comments on commit fb38c67

Please sign in to comment.