Skip to content

Commit

Permalink
add more fcrepo-connector-file unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
acoburn authored and Andrew Woods committed Sep 10, 2015
1 parent 38f9fdb commit 51d9202
Show file tree
Hide file tree
Showing 2 changed files with 293 additions and 6 deletions.
Expand Up @@ -15,17 +15,37 @@
*/
package org.fcrepo.connector.file;

import org.fcrepo.kernel.api.exception.RepositoryRuntimeException;
import org.infinispan.schematic.document.Document;
import org.infinispan.schematic.document.EditableDocument;
import org.infinispan.schematic.document.Json;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.modeshape.jcr.cache.document.DocumentTranslator;
import org.modeshape.jcr.value.Name;
import org.modeshape.jcr.value.Property;
import org.modeshape.jcr.value.basic.BasicName;
import org.modeshape.jcr.value.basic.BasicSingleValueProperty;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import static java.nio.file.Files.createTempDirectory;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyObject;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

/**
Expand All @@ -42,24 +62,247 @@ public class ExternalJsonSidecarExtraPropertyStoreTest {

private static final String FEDERATION_ROOT = "/federation-root";
private static final String FILE_PATH = "/federation-root/file";
@Test
public void testSidecarFile() throws IOException {
final File tmp = createTempDirectory("filesystem-federation").toFile();
tmp.deleteOnExit();
private static final Name KEY1 = new BasicName("info", "one");
private static final Name KEY2 = new BasicName("info", "two");
private static final Name KEY3 = new BasicName("info", "three");
private static final Name KEY4 = new BasicName("info", "four");
private static final Name LANG_PROP = new BasicName("lang", "de");
private static final Property PROP1 = new BasicSingleValueProperty(LANG_PROP, "eins");
private static final Property PROP2 = new BasicSingleValueProperty(LANG_PROP, "zwei");
private static final Property PROP3 = new BasicSingleValueProperty(LANG_PROP, "drei");

@Before
public void setUp() {
when(mockConnector.fileFor("/")).thenReturn(new File(FEDERATION_ROOT));
when(mockConnector.isContentNode("/")).thenReturn(false);
when(mockConnector.isRoot("/")).thenReturn(true);
when(mockConnector.fileFor("/file")).thenReturn(new File(FILE_PATH));
when(mockConnector.isContentNode("/file")).thenReturn(false);
when(mockConnector.fileFor("/file/fcr:content")).thenReturn(new File(FILE_PATH));
when(mockConnector.isContentNode("/file/fcr:content")).thenReturn(true);
when(mockConnector.fileFor("/test/test")).thenReturn(new File(FEDERATION_ROOT + "/test/test"));
when(mockConnector.isContentNode("/test/test")).thenReturn(false);
}

@Test
public void testSidecarFile() throws IOException {
final File tmp = createTempDirectory("filesystem-federation").toFile();
tmp.deleteOnExit();

final ExternalJsonSidecarExtraPropertyStore store =
new ExternalJsonSidecarExtraPropertyStore(mockConnector, mockTranslator, tmp);
assertEquals(new File(tmp, "federation-root.modeshape.json"), store.sidecarFile("/"));
assertEquals(new File(tmp, "file.modeshape.json"), store.sidecarFile("/file"));
assertEquals(new File(tmp, "file.content.modeshape.json"), store.sidecarFile("/file/fcr:content"));
assertEquals(new File(tmp + "/test", "test.modeshape.json"), store.sidecarFile("/test/test"));
}

@Test
public void testEmptyPropertyFile() throws IOException {
final File tmp = createTempDirectory("filesystem-federation").toFile();
tmp.deleteOnExit();

final ExternalJsonSidecarExtraPropertyStore store =
new ExternalJsonSidecarExtraPropertyStore(mockConnector, mockTranslator, tmp);

assertFalse(store.contains("/file"));
assertTrue(store.getProperties("/file").isEmpty());
}

@Test
public void testStoreProperties() throws IOException {
final File tmp = createTempDirectory("filesystem-federation").toFile();
tmp.deleteOnExit();

final ExternalJsonSidecarExtraPropertyStore store =
new ExternalJsonSidecarExtraPropertyStore(mockConnector, mockTranslator, tmp);

final Map<Name, Property> properties = new HashMap<>();

store.storeProperties("/file", properties);

assertFalse(store.contains("/file"));
assertTrue(store.getProperties("/file").isEmpty());

properties.put(KEY1, PROP1);
properties.put(KEY2, PROP2);
properties.put(KEY3, PROP3);
properties.put(KEY4, null);

store.storeProperties("/file", properties);
assertTrue(store.contains("/file"));

properties.forEach((key, property) -> {
if (property != null) {
verify(mockTranslator).setProperty(any(EditableDocument.class), eq(property), anyObject(), anyObject());
}
});
verify(mockTranslator, times(3)).setProperty(any(EditableDocument.class),
any(Property.class), anyObject(), anyObject());
}

@Test
public void testStoreExistingProperties() throws IOException {
final File tmp = createTempDirectory("filesystem-federation").toFile();
final File sidecarFile = new File(tmp, "file.modeshape.json");
tmp.deleteOnExit();
final String jsonString = "{}";
final FileOutputStream fos = new FileOutputStream(sidecarFile);
fos.write(jsonString.getBytes("UTF-8"));
fos.close();

final ExternalJsonSidecarExtraPropertyStore store =
new ExternalJsonSidecarExtraPropertyStore(mockConnector, mockTranslator, tmp);

final Map<Name, Property> properties = new HashMap<>();

assertTrue(store.contains("/file"));

properties.put(KEY1, PROP1);
properties.put(KEY2, PROP2);
properties.put(KEY3, PROP3);
properties.put(KEY4, null);

store.storeProperties("/file", properties);

properties.forEach((key, property) -> {
if (property != null) {
verify(mockTranslator).setProperty(any(EditableDocument.class), eq(property), anyObject(), anyObject());
}
});
verify(mockTranslator, times(3)).setProperty(any(EditableDocument.class),
any(Property.class), anyObject(), anyObject());
}


@Test
public void testUpdateProperties() throws IOException {
final File tmp = createTempDirectory("filesystem-federation").toFile();
tmp.deleteOnExit();

final ExternalJsonSidecarExtraPropertyStore store =
new ExternalJsonSidecarExtraPropertyStore(mockConnector, mockTranslator, tmp);

final Map<Name, Property> properties = new HashMap<>();

store.updateProperties("/file", properties);

assertFalse(store.contains("/file"));
assertTrue(store.getProperties("/file").isEmpty());

properties.put(KEY1, PROP1);
properties.put(KEY2, PROP2);
properties.put(KEY3, null);

store.updateProperties("/file", properties);

properties.forEach((key, property) -> {
if (property == null) {
verify(mockTranslator).removeProperty(any(EditableDocument.class), eq(key), anyObject(), anyObject());
} else {
verify(mockTranslator).setProperty(any(EditableDocument.class), eq(property), anyObject(), anyObject());
}
});
}

@Test
public void testUpdateExistingProperties() throws IOException {
final File tmp = createTempDirectory("filesystem-federation").toFile();
tmp.deleteOnExit();
final File sidecarFile = new File(tmp, "file.modeshape.json");
final String jsonString = "{}";
final FileOutputStream fos = new FileOutputStream(sidecarFile);
fos.write(jsonString.getBytes("UTF-8"));
fos.close();

final ExternalJsonSidecarExtraPropertyStore store =
new ExternalJsonSidecarExtraPropertyStore(mockConnector, mockTranslator, tmp);

assertTrue(store.contains("/file"));

final Map<Name, Property> properties = new HashMap<>();

properties.put(KEY1, PROP1);
properties.put(KEY2, PROP2);
properties.put(KEY3, null);

store.updateProperties("/file", properties);

properties.forEach((key, property) -> {
if (property == null) {
verify(mockTranslator).removeProperty(any(EditableDocument.class), eq(key), anyObject(), anyObject());
} else {
verify(mockTranslator).setProperty(any(EditableDocument.class), eq(property), anyObject(), anyObject());
}
});
}

@Test
public void testRemoveProperties() throws IOException {
final File tmp = createTempDirectory("filesystem-federation").toFile();
tmp.deleteOnExit();

final ExternalJsonSidecarExtraPropertyStore store =
new ExternalJsonSidecarExtraPropertyStore(mockConnector, mockTranslator, tmp);

final Map<Name, Property> properties = new HashMap<>();

properties.put(KEY1, PROP1);
properties.put(KEY2, PROP2);
properties.put(KEY3, PROP3);

store.updateProperties("/file", properties);
assertTrue(store.contains("/file"));

store.removeProperties("/file");
assertFalse(store.contains("/file"));
}

@Test
public void testGetProperties() throws IOException {
final File tmp = createTempDirectory("filesystem-federation").toFile();
tmp.deleteOnExit();
final File sidecarFile = new File(tmp, "file.content.modeshape.json");
final String jsonString = "{" +
"\"properties\" : { " +
"\"http://www.jcp.org/jcr/1.0\" : {" +
"\"created\" : { " +
"\"$date\" : \"2008-09-23T15:19:20.000-04:00\" } } ," +
"\"http://fedora.info/definitions/v4/repository#\" : {" +
"\"digest\" : { " +
"\"$uri\" : \"urn:sha1:6e1a2e24a4cc3dde495877019f53830b8f1d20e3\" } } } }";
final FileOutputStream fos = new FileOutputStream(sidecarFile);
fos.write(jsonString.getBytes("UTF-8"));
fos.close();

final ExternalJsonSidecarExtraPropertyStore store =
new ExternalJsonSidecarExtraPropertyStore(mockConnector, mockTranslator, tmp);

assertTrue(store.contains("/file/fcr:content"));

final FileInputStream sidecarStream = new FileInputStream(sidecarFile);
final Document document = Json.read(sidecarStream, false);
final Map<Name, Property> results = new HashMap<>();

store.getProperties("/file/fcr:content");

verify(mockTranslator).getProperties(eq(document), eq(results));
}

@Test(expected = RepositoryRuntimeException.class)
public void testGetPropertiesWithException() throws IOException {
final File tmp = createTempDirectory("filesystem-federation").toFile();
tmp.deleteOnExit();
final File sidecarFile = new File(tmp, "file.modeshape.json");
final String jsonString = "{ THIS ISN'T JSON !";
final FileOutputStream fos = new FileOutputStream(sidecarFile);
fos.write(jsonString.getBytes("UTF-8"));
fos.close();

final ExternalJsonSidecarExtraPropertyStore store =
new ExternalJsonSidecarExtraPropertyStore(mockConnector, mockTranslator, tmp);

store.getProperties("/file");
}

}
Expand Up @@ -19,6 +19,7 @@
import static java.nio.file.Files.createTempFile;
import static org.fcrepo.kernel.api.FedoraJcrTypes.CONTENT_DIGEST;
import static org.fcrepo.kernel.api.RdfLexicon.REPOSITORY_NAMESPACE;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
Expand All @@ -32,6 +33,7 @@
import static org.mockito.Mockito.when;
import static org.modeshape.jcr.api.JcrConstants.JCR_DATA;
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 static org.slf4j.LoggerFactory.getLogger;
import static org.springframework.test.util.ReflectionTestUtils.setField;
Expand Down Expand Up @@ -76,6 +78,9 @@ public class FedoraFileSystemConnectorTest {

private static File tmpFile;
private static File tmpFile2;
private static final String ROOT_PATH = "/";
private static final String PARENT_FIELD = "parent";
private static final String CHILD_FIELD = "children";

@Mock
private NamespaceRegistry mockRegistry;
Expand Down Expand Up @@ -160,8 +165,42 @@ public void testGetDocumentByIdDatastream() {
when(mockNameFactory.create(anyString())).thenReturn(
new BasicName("", tmpFile.getName()));

final Document doc = connector.getDocumentById("/" + tmpFile.getName());
final Document doc = connector.getDocumentById(ROOT_PATH + tmpFile.getName());
assertNotNull(doc);
assertTrue(doc.containsField(PARENT_FIELD));
assertTrue(doc.containsField(CHILD_FIELD));
assertEquals(ROOT_PATH, doc.getString(PARENT_FIELD));
assertNull(doc.getString(CHILD_FIELD));
}

@Test
public void testGetDocumentByIdObject() {
when(mockTranslator.getPrimaryTypeName(any(Document.class)))
.thenReturn(NT_FOLDER);
when(mockNameFactory.create(anyString())).thenReturn(
new BasicName("", tmpFile.getName()));

final Document doc = connector.getDocumentById(ROOT_PATH + tmpFile.getName());
assertNotNull(doc);
assertTrue(doc.containsField(PARENT_FIELD));
assertTrue(doc.containsField(CHILD_FIELD));
assertEquals(ROOT_PATH, doc.getString(PARENT_FIELD));
assertNull(doc.getString(CHILD_FIELD));
}

@Test
public void testGetDocumentByIdNone() {
when(mockTranslator.getPrimaryTypeName(any(Document.class)))
.thenReturn("");
when(mockNameFactory.create(anyString())).thenReturn(
new BasicName("", tmpFile.getName()));

final Document doc = connector.getDocumentById(ROOT_PATH + tmpFile.getName());
assertNotNull(doc);
assertTrue(doc.containsField(PARENT_FIELD));
assertTrue(doc.containsField(CHILD_FIELD));
assertEquals(ROOT_PATH, doc.getString(PARENT_FIELD));
assertNull(doc.getString(CHILD_FIELD));
}

@Test
Expand All @@ -175,8 +214,13 @@ public void testGetDocumentByIdContent() {
when(mockTranslator.getProperty(any(Document.class), eq(JCR_DATA)))
.thenReturn(binaryProperty);

final Document doc = connector.getDocumentById("/" + tmpFile.getName());
final Document doc = connector.getDocumentById(ROOT_PATH + tmpFile.getName());

assertNotNull(doc);
assertTrue(doc.containsField(PARENT_FIELD));
assertTrue(doc.containsField(CHILD_FIELD));
assertEquals(ROOT_PATH, doc.getString(PARENT_FIELD));
assertNull(doc.getString(CHILD_FIELD));
}

@Test
Expand Down

0 comments on commit 51d9202

Please sign in to comment.