Skip to content

Commit

Permalink
doesResourceExist function (#1608)
Browse files Browse the repository at this point in the history
* Add existence check to resourcefactory

* With and without session tests
  • Loading branch information
whikloj authored and bbpennel committed Jan 24, 2020
1 parent fead75c commit 1cdf310
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 0 deletions.
Expand Up @@ -17,6 +17,8 @@
*/
package org.fcrepo.kernel.api.models;

import java.time.Instant;

import org.fcrepo.kernel.api.Transaction;
import org.fcrepo.kernel.api.exception.PathNotFoundException;

Expand Down Expand Up @@ -73,4 +75,13 @@ public <T extends FedoraResource> T getResource(final String identifier,
*/
public <T extends FedoraResource> T getResource(final Transaction transaction, final String identifier,
final Class<T> clazz) throws PathNotFoundException;

/**
* Check if a resource exists.
* @param transaction The current transaction or null if read-only.
* @param fedoraId The internal identifier
* @param version The version datetime or null for head.
* @return True if the identifier resolves to a resource.
*/
public boolean doesResourceExist(final Transaction transaction, final String fedoraId, final Instant version);
}
Expand Up @@ -21,9 +21,12 @@
import static org.fcrepo.kernel.api.RdfLexicon.DIRECT_CONTAINER;
import static org.fcrepo.kernel.api.RdfLexicon.INDIRECT_CONTAINER;
import static org.fcrepo.kernel.api.RdfLexicon.NON_RDF_SOURCE;
import static org.slf4j.LoggerFactory.getLogger;

import javax.inject.Inject;

import java.time.Instant;

import org.fcrepo.kernel.api.Transaction;
import org.fcrepo.kernel.api.exception.PathNotFoundException;
import org.fcrepo.kernel.api.exception.RepositoryRuntimeException;
Expand All @@ -36,6 +39,7 @@
import org.fcrepo.persistence.api.PersistentStorageSessionManager;
import org.fcrepo.persistence.api.exceptions.PersistentItemNotFoundException;
import org.fcrepo.persistence.api.exceptions.PersistentStorageException;
import org.slf4j.Logger;
import org.springframework.stereotype.Component;


Expand All @@ -48,6 +52,8 @@
@Component
public class ResourceFactoryImpl implements ResourceFactory {

private static final Logger LOGGER = getLogger(ResourceFactoryImpl.class);

@Inject
private PersistentStorageSessionManager persistentStorageSessionManager;

Expand Down Expand Up @@ -75,6 +81,37 @@ public <T extends FedoraResource> T getResource(final Transaction transaction, f
return clazz.cast(getResource(transaction, identifier));
}

@Override
public boolean doesResourceExist(final Transaction transaction, final String fedoraId, final Instant version) {
// TODO: Check the index first.

final PersistentStorageSession psSession;
if (transaction == null) {
psSession = persistentStorageSessionManager.getReadOnlySession();
} else {
psSession = persistentStorageSessionManager.getSession(transaction.getId());
}
try {
psSession.getHeaders(fedoraId, version);
return true;
} catch (PersistentItemNotFoundException e) {
// Object doesn't exist.
return false;
} catch (PersistentStorageException e) {
// Other error, pass along.
throw new RepositoryRuntimeException(e);
} finally {
if (transaction == null) {
// Commit session (if read-only) so it doesn't hang around.
try {
psSession.commit();
} catch (PersistentStorageException e) {
LOGGER.error("Error committing session, message: {}", e.getMessage());
}
}
}
}

/**
* Returns the appropriate FedoraResource class for an object based on the provided headers
*
Expand Down
Expand Up @@ -22,6 +22,7 @@
import static org.fcrepo.kernel.api.RdfLexicon.INDIRECT_CONTAINER;
import static org.fcrepo.kernel.api.RdfLexicon.NON_RDF_SOURCE;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.ArgumentMatchers.nullable;
Expand All @@ -43,6 +44,7 @@
import org.fcrepo.persistence.api.PersistentStorageSession;
import org.fcrepo.persistence.api.PersistentStorageSessionManager;
import org.fcrepo.persistence.api.exceptions.PersistentItemNotFoundException;
import org.fcrepo.persistence.api.exceptions.PersistentSessionClosedException;
import org.fcrepo.persistence.api.exceptions.PersistentStorageException;
import org.fcrepo.persistence.common.ResourceHeadersImpl;
import org.junit.Before;
Expand Down Expand Up @@ -245,6 +247,44 @@ public void getResource_Binary_StorageException() throws Exception {
factory.getResource(fedoraId);
}

@Test
public void doesResourceExists_Exists_WithSession() throws Exception {
final boolean answer = factory.doesResourceExist(mockTx, fedoraId, null);
assertTrue(answer);
}

@Test
public void doesResourceExists_Exists_WithoutSession() throws Exception {
final boolean answer = factory.doesResourceExist(null, fedoraId, null);
assertTrue(answer);
}

@Test
public void doesResourceExist_DoesntExist_WithSession() throws Exception {
when(psSession.getHeaders(fedoraId, null)).thenThrow(PersistentItemNotFoundException.class);
final boolean answer = factory.doesResourceExist(mockTx, fedoraId, null);
assertFalse(answer);
}

@Test
public void doesResourceExist_DoesntExist_WithoutSession() throws Exception {
when(psSession.getHeaders(fedoraId, null)).thenThrow(PersistentItemNotFoundException.class);
final boolean answer = factory.doesResourceExist(null, fedoraId, null);
assertFalse(answer);
}

@Test(expected = RepositoryRuntimeException.class)
public void doesResourceExist_Exception_WithSession() throws Exception {
when(psSession.getHeaders(fedoraId, null)).thenThrow(PersistentSessionClosedException.class);
factory.doesResourceExist(mockTx, fedoraId, null);
}

@Test(expected = RepositoryRuntimeException.class)
public void doesResourceExist_Exception_WithoutSession() throws Exception {
when(psSession.getHeaders(fedoraId, null)).thenThrow(PersistentSessionClosedException.class);
factory.doesResourceExist(null, fedoraId, null);
}

private void assertStateFieldsMatches(final FedoraResource resc) {
assertEquals(CREATED_DATE, resc.getCreatedDate());
assertEquals(CREATED_BY, resc.getCreatedBy());
Expand Down

0 comments on commit 1cdf310

Please sign in to comment.