Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
unit test BagItExtraPropertiesStore; add IT for property from bag-inf…
…o.txt
  • Loading branch information
barmintor committed Apr 11, 2013
1 parent 6946509 commit 40628f1
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 24 deletions.
4 changes: 3 additions & 1 deletion src/main/java/org/fcrepo/federation/bagit/BagInfo.java
Expand Up @@ -68,7 +68,9 @@ public void save() throws IOException {
}

public boolean delete() throws IOException {
return new File(this.getFilepath()).delete();
int len = getProperties().size();
setProperties(BagItExtraPropertiesStore.EMPTY);
return len > 0;
}

private Property makeProperty(Name name, String value) {
Expand Down
Expand Up @@ -4,6 +4,7 @@
import static org.slf4j.LoggerFactory.getLogger;

import java.io.File;
import java.io.IOException;
import java.util.Map;

import org.modeshape.jcr.cache.DocumentStoreException;
Expand All @@ -25,23 +26,27 @@ public class BagItExtraPropertiesStore implements ExtraPropertiesStore {

private BagItConnector connector;

private static final Map<Name, Property> EMPTY = emptyMap();
protected static final Map<Name, Property> EMPTY = emptyMap();

protected BagItExtraPropertiesStore(BagItConnector connector) {
this.connector = connector;
}

@Override
public void storeProperties(String id, Map<Name, Property> properties) {
try {
BagInfo bagInfo = connector.getBagInfo(id);
if (bagInfo == null) return;
bagInfo.setProperties(properties);
bagInfo.save();
storeProperties(connector.getBagInfo(id), properties);
}

private void storeProperties(BagInfo bagInfo, Map<Name, Property> properties) {
if (bagInfo == null) return;

try{
bagInfo.setProperties(properties);
bagInfo.save();
} catch (Exception ex) {
throw new DocumentStoreException(
"Error in storing properties for " + id + " at " +
connector.fileFor(id), ex);
"Error in storing properties for " + bagInfo.bagID + " at " +
bagInfo.getFilepath(), ex);
}
}

Expand All @@ -59,7 +64,7 @@ public void updateProperties(String id, Map<Name, Property> properties) {
existing.put(name, prop);
}
}
storeProperties(id, existing);
storeProperties(bagInfo, existing);
}

@Override
Expand All @@ -81,11 +86,19 @@ public Map<Name, Property> getProperties(String id) {

@Override
public boolean removeProperties(String id) {
File bagInfo = connector.bagInfoFileFor(id);
BagInfo bagInfo = connector.getBagInfo(id);
if (!bagInfo.exists()) {
return false;
} else {
return bagInfo.delete();
try {
boolean result = bagInfo.delete();
bagInfo.save();
return result;
} catch (IOException ex) {
throw new DocumentStoreException(
"Error in removing properties for " + bagInfo.bagID + " at " +
bagInfo.getFilepath(), ex);
}
}
}

Expand Down
Expand Up @@ -2,12 +2,15 @@
package org.fcrepo.federation.bagit;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.io.IOException;

import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.Property;
import javax.jcr.PropertyIterator;
import javax.jcr.RepositoryException;

import org.apache.http.HttpResponse;
Expand All @@ -27,6 +30,12 @@ public void tryProgrammaticAccess() throws RepositoryException {
Session session = (Session)repo.login();
Node node = session.getNode("/objects/BagItFed1");
logger.info("Got node at " + node.getPath());
PropertyIterator properties = node.getProperties("bagit:*");
assertTrue(properties.hasNext());
// Bag-Count: 1 of 1
Property property = node.getProperty("bagit:Bag.Count");
assertNotNull(property);
assertEquals("1 of 1", property.getString());
NodeIterator nodes = node.getNodes();
assertTrue("/objects/testDS had no child nodes!", nodes.hasNext());
Node child = nodes.nextNode();
Expand Down
@@ -1,14 +1,22 @@

package org.fcrepo.federation.bagit;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.io.IOException;
import java.util.Map;

import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.junit.Before;
import org.junit.Test;
import org.mockito.internal.matchers.Equals;
import org.modeshape.jcr.value.Name;
import org.modeshape.jcr.value.Property;
import org.modeshape.jcr.value.PropertyFactory;
Expand All @@ -33,26 +41,54 @@ public void setUp() {

@Test
public void testRead() {
// TODO read a properties file from disk
BagInfo mockBI = getMockBagInfo();
when(connector.getBagInfo("/foo")).thenReturn(mockBI);
Map<Name, Property> props = store.getProperties("/foo");
verify(connector).getBagInfo("/foo");
verify(connector).getBagInfo("/foo");
verify(mockBI).getProperties();
props = store.getProperties("/non/existent");
assertEquals(BagItExtraPropertiesStore.EMPTY, props);
}

/*

@Test
public void testWrite() {
// TODO write a properties file to disk then read it back
public void testUpdateProperties() throws IOException {
BagInfo mockBI = getMockBagInfo();
when(connector.getBagInfo("/foo")).thenReturn(mockBI);
Map<Name, Property> mockProps = mock(Map.class);
store.updateProperties("/foo", mockProps);
verify(connector).getBagInfo("/foo");
verify(mockBI).setProperties(any(Map.class));
verify(mockBI).save();
verify(mockBI).getProperties();
}

@Test
public void testUpdate() {
// TODO update a properties file and make sure new props saved, delete dprops removed
public void testStoreProperties() throws IOException {
BagInfo mockBI = getMockBagInfo();
when(connector.getBagInfo("/foo")).thenReturn(mockBI);
Map<Name, Property> mockProps = mock(Map.class);
store.storeProperties("/foo", mockProps);
verify(connector).getBagInfo("/foo");
verify(mockBI).setProperties(any(Map.class));
verify(mockBI).save();
}


@Test
public void testRemove() {
// TODO remove a properties file
public void testRemove() throws IOException {
BagInfo mockBI = getMockBagInfo();
when(mockBI.exists()).thenReturn(true);
when(connector.getBagInfo("/foo")).thenReturn(mockBI);
Map<Name, Property> mockProps = mock(Map.class);
store.removeProperties("/foo");
verify(connector).getBagInfo("/foo");
verify(mockBI).delete();
verify(mockBI).save();
}
*/

private BagInfo getMockBagInfo() {
BagInfo mock = mock(BagInfo.class);
return mock;
}

}

0 comments on commit 40628f1

Please sign in to comment.