Skip to content

Commit

Permalink
Updating event machinery to collapse events about content nodes and t…
Browse files Browse the repository at this point in the history
…he fcr:content property so adding/removing content only produces a single event
  • Loading branch information
escowles committed Apr 3, 2015
1 parent f374e80 commit 6ff5e6e
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
Expand Up @@ -51,6 +51,8 @@ public class AllNodeEventsOneEvent implements InternalExternalEventMapper {
private static final List<Integer> PROPERTY_EVENT_TYPES = asList(PROPERTY_ADDED, PROPERTY_CHANGED,
PROPERTY_REMOVED);

private final static String propPattern = "/[a-zA-Z0-9\\.]+:[a-zA-Z0-9\\.]+";

/**
* Extracts the node identifier from a JCR {@link Event}.
*/
Expand All @@ -59,7 +61,9 @@ public class AllNodeEventsOneEvent implements InternalExternalEventMapper {
@Override
public String apply(final Event ev) {
try {
final String id = ev.getIdentifier();
// 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();
LOGGER.debug("Sorting an event by identifier: {}", id);
return id;
} catch (final RepositoryException e) {
Expand Down
Expand Up @@ -17,6 +17,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_CHANGED;
import static org.jgroups.util.UUID.randomUUID;
import static org.junit.Assert.assertEquals;
Expand Down Expand Up @@ -51,12 +52,20 @@ public class AllNodeEventsOneEventTest {

private static final String TEST_IDENTIFIER2 = TEST_IDENTIFIER1;

private static final String TEST_PATH2 = TEST_PATH1 + "/property";
private static final String TEST_PATH2 = TEST_PATH1 + "/dc:title";

private static final String TEST_IDENTIFIER3 = randomUUID().toString();

private static final String TEST_PATH3 = "/test/node2";

private static final String TEST_IDENTIFIER4 = randomUUID().toString();

private static final String TEST_PATH4 = "/test/node3";

private static final String TEST_IDENTIFIER5 = randomUUID().toString();

private static final String TEST_PATH5 = "/test/node3/fcr:content";

private final AllNodeEventsOneEvent testMapping = new AllNodeEventsOneEvent();

@Mock
Expand All @@ -68,21 +77,41 @@ public class AllNodeEventsOneEventTest {
@Mock
private Event mockEvent3;

@Mock
private Event mockEvent4;

@Mock
private Event mockEvent5;

@Mock
private Iterator<Event> mockIterator;

@Mock
private Iterator<Event> mockIterator2;

@Before
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(mockEvent2.getIdentifier()).thenReturn(TEST_IDENTIFIER2);
when(mockEvent2.getPath()).thenReturn(TEST_PATH2);
when(mockEvent2.getType()).thenReturn(PROPERTY_CHANGED);
when(mockEvent3.getIdentifier()).thenReturn(TEST_IDENTIFIER3);
when(mockEvent3.getPath()).thenReturn(TEST_PATH3);
when(mockEvent3.getType()).thenReturn(PROPERTY_CHANGED);
when(mockIterator.next()).thenReturn(mockEvent1, mockEvent2, mockEvent3);
when(mockIterator.hasNext()).thenReturn(true, true, true, false);

when(mockEvent4.getIdentifier()).thenReturn(TEST_IDENTIFIER4);
when(mockEvent4.getPath()).thenReturn(TEST_PATH4);
when(mockEvent4.getType()).thenReturn(NODE_ADDED);
when(mockEvent5.getIdentifier()).thenReturn(TEST_IDENTIFIER5);
when(mockEvent5.getPath()).thenReturn(TEST_PATH5);
when(mockEvent5.getType()).thenReturn(NODE_ADDED);
when(mockIterator2.next()).thenReturn(mockEvent4, mockEvent5);
when(mockIterator2.hasNext()).thenReturn(true, true, false);
}

@Test
Expand All @@ -91,6 +120,11 @@ public void testCardinality() {
size(testMapping.apply(mockIterator)));
}

@Test
public void testCollapseContentEvents() {
assertEquals("Didn't collapse content node and fcr:content events!", 1, size(testMapping.apply(mockIterator2)));
}

@Test(expected = UnsupportedOperationException.class)
public void testBadOPeration() {
testMapping.apply(mockIterator).remove();
Expand Down

0 comments on commit 6ff5e6e

Please sign in to comment.