Skip to content

Commit

Permalink
Fix fcr:content/jcr:content mixup
Browse files Browse the repository at this point in the history
- Tag content events with fedora:hasContent property
- Improve IT to correctly trigger duplicate events

Resolves: https://jira.duraspace.org/browse/FCREPO-1445
Follow-on resolution to: https://jira.duraspace.org/browse/FCREPO-1440
  • Loading branch information
escowles authored and Andrew Woods committed Apr 9, 2015
1 parent 3de050c commit 6749488
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 8 deletions.
Expand Up @@ -16,6 +16,7 @@
package org.fcrepo.kernel.impl.observer.eventmappings;

import static com.google.common.collect.Multimaps.index;
import static org.modeshape.jcr.api.JcrConstants.JCR_CONTENT;
import static org.slf4j.LoggerFactory.getLogger;
import static java.util.Arrays.asList;
import static javax.jcr.observation.Event.PROPERTY_ADDED;
Expand Down Expand Up @@ -60,7 +61,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 = FedoraEvent.getPath(ev).replaceAll("/fcr:content","") + "-" + ev.getUserID();
final String id = FedoraEvent.getPath(ev).replaceAll("/" + JCR_CONTENT,"") + "-" + ev.getUserID();
LOGGER.debug("Sorting an event by identifier: {}", id);
return id;
} catch (final RepositoryException e) {
Expand Down Expand Up @@ -123,6 +124,9 @@ public void remove() {

private void addProperty( final FedoraEvent fedoraEvent, final Event ev ) {
try {
if ( ev.getPath().contains(JCR_CONTENT)) {
fedoraEvent.addProperty("fedora:hasContent");
}
if (PROPERTY_EVENT_TYPES.contains(ev.getType())) {
final String eventPath = ev.getPath();
fedoraEvent.addProperty(eventPath.substring(eventPath.lastIndexOf('/') + 1));
Expand Down
Expand Up @@ -17,11 +17,16 @@

import static org.fcrepo.kernel.FedoraJcrTypes.FEDORA_BINARY;
import static org.fcrepo.kernel.FedoraJcrTypes.FEDORA_CONTAINER;
import static org.fcrepo.kernel.FedoraJcrTypes.FEDORA_RESOURCE;
import static org.fcrepo.kernel.RdfLexicon.REPOSITORY_NAMESPACE;
import static org.fcrepo.kernel.utils.ContentDigest.asURI;
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 static org.modeshape.jcr.api.JcrConstants.JCR_CONTENT;
import static org.modeshape.jcr.api.JcrConstants.NT_FILE;
import static org.modeshape.jcr.api.JcrConstants.NT_FOLDER;
import static org.modeshape.jcr.api.JcrConstants.NT_RESOURCE;

import java.io.ByteArrayInputStream;
import javax.inject.Inject;
Expand All @@ -31,11 +36,15 @@
import javax.jcr.Session;

import org.fcrepo.integration.kernel.impl.AbstractIT;
import org.fcrepo.kernel.exception.InvalidChecksumException;
import org.fcrepo.kernel.impl.FedoraBinaryImpl;
import org.fcrepo.kernel.models.FedoraBinary;
import org.fcrepo.kernel.observer.FedoraEvent;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.modeshape.jcr.api.Binary;
import org.modeshape.jcr.api.JcrTools;
import org.modeshape.jcr.api.ValueFactory;
import org.springframework.test.context.ContextConfiguration;

Expand Down Expand Up @@ -84,14 +93,25 @@ public void TestEventBusPublishing() throws RepositoryException {
}

@Test
public void contentEventCollapsing() throws RepositoryException {
public void contentEventCollapsing() throws RepositoryException, InvalidChecksumException {

final Session se = repository.login();
final JcrTools jcrTools = new JcrTools();

final Node n = jcrTools.findOrCreateNode(se.getRootNode(), "/object3", NT_FOLDER, NT_FILE);
n.addMixin(FEDORA_RESOURCE);

final String content = "test content";
final String checksum = "1eebdf4fdc9fc7bf283031b93f9aef3338de9052";
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);
new ByteArrayInputStream(content.getBytes()), null );

final Node contentNode = jcrTools.findOrCreateChild(n, JCR_CONTENT, NT_RESOURCE);
contentNode.addMixin(FEDORA_BINARY);
final FedoraBinary binary = new FedoraBinaryImpl(contentNode);
binary.setContent( new ByteArrayInputStream(content.getBytes()), "text/plain",
asURI("SHA-1", checksum), "text.txt", null);

se.save();
se.logout();

Expand Down
Expand Up @@ -27,6 +27,7 @@
import static org.mockito.Mockito.reset;
import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.initMocks;
import static org.modeshape.jcr.api.JcrConstants.JCR_CONTENT;

import javax.jcr.RepositoryException;
import javax.jcr.observation.Event;
Expand Down Expand Up @@ -65,7 +66,7 @@ public class AllNodeEventsOneEventTest {

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

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

private final AllNodeEventsOneEvent testMapping = new AllNodeEventsOneEvent();

Expand All @@ -90,6 +91,9 @@ public class AllNodeEventsOneEventTest {
@Mock
private Iterator<Event> mockIterator2;

@Mock
private Iterator<Event> mockIterator3;

@Before
public void setUp() throws RepositoryException {
initMocks(this);
Expand All @@ -113,6 +117,9 @@ public void setUp() throws RepositoryException {
when(mockEvent5.getType()).thenReturn(NODE_ADDED);
when(mockIterator2.next()).thenReturn(mockEvent4, mockEvent5);
when(mockIterator2.hasNext()).thenReturn(true, true, false);

when(mockIterator3.next()).thenReturn(mockEvent4, mockEvent5);
when(mockIterator3.hasNext()).thenReturn(true, true, false);
}

@Test
Expand All @@ -126,6 +133,13 @@ public void testCollapseContentEvents() {
assertEquals("Didn't collapse content node and fcr:content events!", 1, size(testMapping.apply(mockIterator2)));
}

@Test
public void testFileEventProperties() {
final FedoraEvent e = testMapping.apply(mockIterator3).next();
assertTrue("Didn't add fedora:hasContent property to fcr:content events!: " + e.getProperties(),
e.getProperties().contains("fedora:hasContent"));
}

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

0 comments on commit 6749488

Please sign in to comment.