Skip to content

Commit

Permalink
NodeService#deleteObject should also clean up inbound references to a…
Browse files Browse the repository at this point in the history
… node
  • Loading branch information
cbeer committed May 6, 2014
1 parent d82a27f commit 67d8771
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 1 deletion.
Expand Up @@ -26,6 +26,7 @@

import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.nodetype.NodeTypeIterator;
Expand All @@ -35,6 +36,7 @@
import org.fcrepo.kernel.FedoraResource;
import org.fcrepo.kernel.FedoraResourceImpl;
import org.fcrepo.kernel.rdf.impl.NodeTypeRdfContext;
import org.fcrepo.kernel.utils.iterators.PropertyIterator;
import org.fcrepo.kernel.utils.iterators.RdfStream;
import org.modeshape.jcr.api.nodetype.NodeTypeManager;
import org.slf4j.Logger;
Expand Down Expand Up @@ -154,6 +156,12 @@ public Set<String> getObjectNames(final Session session, final String path, fina
@Override
public void deleteObject(final Session session, final String path) throws RepositoryException {
final Node obj = session.getNode(path);
final PropertyIterator inboundProperties = new PropertyIterator(obj.getReferences());

for (final Property inboundProperty : inboundProperties) {
inboundProperty.remove();
}

obj.remove();
}

Expand Down
Expand Up @@ -22,6 +22,8 @@
import org.slf4j.Logger;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.UUID;

/**
* @author ajs6f
* @date 2013
Expand All @@ -36,4 +38,8 @@ public void setLogger() {
logger = getLogger(this.getClass());
}

public String getRandomPid() {
return UUID.randomUUID().toString();
}

}
@@ -0,0 +1,86 @@
/**
* Copyright 2013 DuraSpace, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.fcrepo.integration.kernel.services;

import org.fcrepo.integration.kernel.AbstractIT;
import org.fcrepo.kernel.FedoraResource;
import org.fcrepo.kernel.services.NodeService;
import org.fcrepo.kernel.services.ObjectService;
import org.junit.Test;
import org.springframework.test.context.ContextConfiguration;

import javax.inject.Inject;
import javax.jcr.Repository;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.Value;

import static org.junit.Assert.assertFalse;

/**
* @author cabeer
*/

@ContextConfiguration({"/spring-test/repo.xml"})
public class NodeServiceImplIT extends AbstractIT {

@Inject
private Repository repository;

@Inject
NodeService nodeService;

@Inject
ObjectService objectService;



@Test
public void testDeleteObject() throws RepositoryException {
final Session session = repository.login();
final String pid = getRandomPid();
nodeService.findOrCreateObject(session, "/" + pid);
session.save();

nodeService.deleteObject(session, "/" + pid);
session.save();

assertFalse(session.nodeExists("/" + pid));
}

@Test
public void testDeleteObjectWithInboundReferences() throws RepositoryException {

final Session session = repository.login();
final String pid = getRandomPid();
final FedoraResource resourceA = objectService.createObject(session, "/" + pid + "/a");
final FedoraResource resourceB = objectService.createObject(session, "/" + pid + "/b");

final Value value = session.getValueFactory().createValue(resourceB.getNode());
resourceA.getNode().setProperty("fedorarelsext:hasMember", new Value[] { value });

session.save();
nodeService.deleteObject(session, "/" + pid + "/a");
session.save();

nodeService.deleteObject(session, "/" + pid + "/b");
session.save();

assertFalse(session.nodeExists("/" + pid + "/b"));
// assertFalse(resourceA.getNode().hasProperty("fedorarelsext:isPartOf"));

}
}
Expand Up @@ -28,6 +28,7 @@

import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.PropertyIterator;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.Workspace;
Expand Down Expand Up @@ -65,6 +66,9 @@ public class NodeServiceImplTest {
@Mock
private Workspace mockWorkspace;

@Mock
private PropertyIterator mockEmptyIterator;

private NodeService testObj;

@Before
Expand All @@ -76,6 +80,7 @@ public void setUp() throws RepositoryException {
when(mockSession.getWorkspace()).thenReturn(mockWorkspace);
when(mockWorkspace.getNodeTypeManager()).thenReturn(mockNodeTypeManager);
when(mockNodeTypeManager.getAllNodeTypes()).thenReturn(mockNTI);
when(mockEmptyIterator.hasNext()).thenReturn(false);
}

@SuppressWarnings("unchecked")
Expand All @@ -97,12 +102,13 @@ public void testGetObjectNames() throws RepositoryException {
}

@Test
public void testDeleteOBject() throws RepositoryException {
public void testDeleteObject() throws RepositoryException {
final String objPath = "foo";
final Node mockObjectsNode = mock(Node.class);
when(mockSession.getRootNode()).thenReturn(mockRoot);
when(mockRoot.getNode("objects")).thenReturn(mockObjectsNode);
when(mockSession.getNode(objPath)).thenReturn(mockObjNode);
when(mockObjNode.getReferences()).thenReturn(mockEmptyIterator);
mockStatic(ServiceHelpers.class);
testObj.deleteObject(mockSession, "foo");
verify(mockSession).getNode(objPath);
Expand Down

0 comments on commit 67d8771

Please sign in to comment.