Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Using FedoraEvent logic to trim property names from node paths, addin…
…g IT demonstrating only a single event is emitted when content node is added
  • Loading branch information
escowles committed Apr 7, 2015
1 parent 6ff5e6e commit 3066035
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 4 deletions.
Expand Up @@ -62,8 +62,7 @@ public class AllNodeEventsOneEvent implements InternalExternalEventMapper {
public String apply(final Event ev) {
try {
// build id from nodepath+user to collapse multiple nodes from adding/removing content nodes
final String id = ev.getPath().replaceAll("/jcr:content/","/").replaceAll(propPattern,"")
+ "-" + ev.getUserID();
final String id = FedoraEvent.getPath(ev).replaceAll("/fcr:content","") + "-" + ev.getUserID();
LOGGER.debug("Sorting an event by identifier: {}", id);
return id;
} catch (final RepositoryException e) {
Expand Down
Expand Up @@ -15,13 +15,17 @@
*/
package org.fcrepo.integration.kernel.impl.observer;

import static org.fcrepo.kernel.FedoraJcrTypes.FEDORA_BINARY;
import static org.fcrepo.kernel.FedoraJcrTypes.FEDORA_CONTAINER;
import static org.fcrepo.kernel.RdfLexicon.REPOSITORY_NAMESPACE;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.modeshape.jcr.api.JcrConstants.JCR_DATA;

import java.io.ByteArrayInputStream;
import javax.inject.Inject;
import javax.jcr.Node;
import javax.jcr.Repository;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
Expand All @@ -31,6 +35,8 @@
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.modeshape.jcr.api.Binary;
import org.modeshape.jcr.api.ValueFactory;
import org.springframework.test.context.ContextConfiguration;

import com.google.common.eventbus.EventBus;
Expand Down Expand Up @@ -77,6 +83,28 @@ public void TestEventBusPublishing() throws RepositoryException {

}

@Test
public void contentEventCollapsing() throws RepositoryException {

final Session se = repository.login();
final Binary bin = ((ValueFactory)se.getValueFactory()).createBinary(
new ByteArrayInputStream("test content".getBytes()), null );
final Node node = se.getRootNode().addNode("/object1");
node.addMixin(FEDORA_BINARY);
node.setProperty(JCR_DATA, bin);
se.save();
se.logout();

try {
Thread.sleep(500);
} catch (final InterruptedException e) {
e.printStackTrace();
}

assertEquals("Node and content events not collapsed!", (Integer) 1, eventBusMessageCount);

}

@Subscribe
public void countMessages(final FedoraEvent e) {
eventBusMessageCount++;
Expand Down
Expand Up @@ -18,6 +18,7 @@
import static com.google.common.collect.Iterators.getLast;
import static com.google.common.collect.Iterators.size;
import static javax.jcr.observation.Event.NODE_ADDED;
import static javax.jcr.observation.Event.PROPERTY_ADDED;
import static javax.jcr.observation.Event.PROPERTY_CHANGED;
import static org.jgroups.util.UUID.randomUUID;
import static org.junit.Assert.assertEquals;
Expand Down Expand Up @@ -94,10 +95,10 @@ public void setUp() throws RepositoryException {
initMocks(this);
when(mockEvent1.getIdentifier()).thenReturn(TEST_IDENTIFIER1);
when(mockEvent1.getPath()).thenReturn(TEST_PATH1);
when(mockEvent1.getType()).thenReturn(PROPERTY_CHANGED);
when(mockEvent1.getType()).thenReturn(NODE_ADDED);
when(mockEvent2.getIdentifier()).thenReturn(TEST_IDENTIFIER2);
when(mockEvent2.getPath()).thenReturn(TEST_PATH2);
when(mockEvent2.getType()).thenReturn(PROPERTY_CHANGED);
when(mockEvent2.getType()).thenReturn(PROPERTY_ADDED);
when(mockEvent3.getIdentifier()).thenReturn(TEST_IDENTIFIER3);
when(mockEvent3.getPath()).thenReturn(TEST_PATH3);
when(mockEvent3.getType()).thenReturn(PROPERTY_CHANGED);
Expand Down
Expand Up @@ -111,6 +111,15 @@ public FedoraEvent addProperty( final String property ) {
* @throws RepositoryException if the repository exception occurred
*/
public String getPath() throws RepositoryException {
return getPath(e);
}

/**
* Get the path of the node related to this event (removing property names
* from the end of property nodes).
* @param e JCR Event
**/
public static String getPath(final Event e) throws RepositoryException {
if (e.getType() == PROPERTY_ADDED ||
e.getType() == PROPERTY_CHANGED ||
e.getType() == PROPERTY_REMOVED) {
Expand Down

0 comments on commit 3066035

Please sign in to comment.