Skip to content
Permalink
Browse files

NodeService#deleteObject should also clean up inbound references to a…

  • Loading branch information...
cbeer authored and awoods committed May 6, 2014
1 parent d77194c commit ed41237e2ee9d7d5cec4c8dad6d599f30230c5db
@@ -25,6 +25,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;
@@ -34,6 +35,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;
@@ -153,6 +155,12 @@ public FedoraResource getObject(final Session session, final String path, final
@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();
}

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

import java.util.UUID;

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

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

}
@@ -0,0 +1,85 @@
/**
* Copyright 2014 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"));

}
}
@@ -27,6 +27,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;
@@ -64,6 +65,9 @@
@Mock
private Workspace mockWorkspace;

@Mock
private PropertyIterator mockEmptyIterator;

private NodeService testObj;

@Before
@@ -75,6 +79,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")
@@ -96,12 +101,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);

0 comments on commit ed41237

Please sign in to comment.
You can’t perform that action at this time.