Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
finish unit testing LowLevelStorageService and accompanying Function …
…classes
  • Loading branch information
barmintor committed Apr 3, 2013
1 parent 1be7a5a commit e133bf0
Show file tree
Hide file tree
Showing 6 changed files with 223 additions and 31 deletions.
@@ -1,7 +1,6 @@

package org.fcrepo.services;

import static com.google.common.collect.Collections2.filter;
import static com.google.common.collect.Collections2.transform;
import static com.google.common.collect.ImmutableSet.builder;
import static com.google.common.collect.ImmutableSet.copyOf;
Expand Down Expand Up @@ -32,10 +31,10 @@
import org.fcrepo.services.functions.GetBinaryKey;
import org.fcrepo.services.functions.GetBinaryStore;
import org.fcrepo.services.functions.GetCacheStore;
import org.fcrepo.services.functions.GetGoodFixityResults;
import org.fcrepo.utils.FixityResult;
import org.fcrepo.utils.LowLevelCacheEntry;
import org.infinispan.Cache;
import org.infinispan.loaders.CacheLoaderManager;
import org.infinispan.loaders.CacheStore;
import org.infinispan.loaders.decorators.ChainingCacheStore;
import org.modeshape.jcr.value.BinaryKey;
Expand All @@ -44,7 +43,6 @@
import org.slf4j.Logger;

import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.collect.ImmutableSet;
import com.yammer.metrics.Counter;
import com.yammer.metrics.Timer;
Expand Down Expand Up @@ -73,6 +71,8 @@ public class LowLevelStorageService {
GetBinaryKey getBinaryKey = new GetBinaryKey();

GetCacheStore getCacheStore = new GetCacheStore();

GetGoodFixityResults getGoodFixityResults = new GetGoodFixityResults();

/**
* For use with non-mutating methods.
Expand Down Expand Up @@ -200,15 +200,7 @@ public Collection<FixityResult> runFixityAndFixProblems(Datastream datastream)
try {
fixityResults = copyOf(getFixity(datastream.getNode(), digest, digestUri, size));

goodEntries =
copyOf(filter(fixityResults, new Predicate<FixityResult>() {

@Override
public boolean apply(FixityResult result) {
return result.computedChecksum.equals(digestUri) &&
result.computedSize == size;
};
}));
goodEntries = getGoodFixityResults.apply(fixityResults);
} finally {
context.stop();
}
Expand Down
@@ -0,0 +1,36 @@
package org.fcrepo.services.functions;

import static com.google.common.collect.Collections2.filter;
import static com.google.common.collect.ImmutableSet.copyOf;

import java.util.Collection;
import java.util.Set;

import org.fcrepo.utils.FixityResult;

import com.google.common.base.Function;
import com.google.common.base.Predicate;

public class GetGoodFixityResults implements Function<Collection<FixityResult>, Set<FixityResult>> {
IsGoodFixity predicate = new IsGoodFixity();

public boolean isGood(FixityResult input) {
return predicate.apply(input);
}

@Override
public Set<FixityResult> apply(Collection<FixityResult> input) {
return copyOf(filter(input, predicate));
}

static class IsGoodFixity implements Predicate<FixityResult> {

@Override
public boolean apply(FixityResult input) {
return input.computedChecksum.equals(input.dsChecksum) &&
input.computedSize == input.dsSize;
}

}

}
@@ -1,16 +1,20 @@
package org.fcrepo.services;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.security.MessageDigest;
import java.util.Arrays;
import java.util.Collection;
import java.util.EnumSet;
import java.util.HashSet;
import java.util.Set;

import javax.jcr.LoginException;
Expand All @@ -19,18 +23,22 @@
import javax.jcr.RepositoryException;
import javax.jcr.Session;

import org.fcrepo.Datastream;
import org.fcrepo.FedoraObject;
import org.fcrepo.services.functions.GetBinaryKey;
import org.fcrepo.services.functions.GetBinaryStore;
import org.fcrepo.services.functions.GetCacheStore;
import org.fcrepo.services.functions.GetGoodFixityResults;
import org.fcrepo.utils.FixityResult;
import org.fcrepo.utils.FixityResult.FixityState;
import org.fcrepo.utils.LowLevelCacheEntry;
import org.infinispan.Cache;
import org.infinispan.loaders.CacheStore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.modeshape.jcr.JcrRepository;
import org.modeshape.jcr.JcrSession;
import org.modeshape.jcr.RepositoryConfiguration;
import org.modeshape.jcr.RepositoryConfiguration.BinaryStorage;
import org.modeshape.jcr.value.BinaryKey;
import org.modeshape.jcr.value.binary.BinaryStore;
import org.modeshape.jcr.value.binary.infinispan.InfinispanBinaryStore;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
Expand Down Expand Up @@ -153,8 +161,69 @@ public void testSetRepository() throws LoginException, RepositoryException {
}

@Test
public void testRunFixityAndFixProblems() {

public void testRunFixityAndFixProblems() throws RepositoryException, IOException {
GetBinaryStore mockStoreFunc = mock(GetBinaryStore.class);
GetBinaryKey mockKeyFunc = mock(GetBinaryKey.class);
Node mockNode = mock(Node.class);
Repository mockRepo = mock(Repository.class);
BinaryKey mockKey = mock(BinaryKey.class);
InfinispanBinaryStore mockStore = mock(InfinispanBinaryStore.class);
when(mockStore.toString()).thenReturn("foo");
Cache<?, ?> mockGoodCache = mock(Cache.class);
Cache<?, ?> mockBadCache = mock(Cache.class);
Cache<?, ?>[] mockCaches = new Cache[]{mockGoodCache, mockBadCache};
GetCacheStore mockCacheStoreFunc = mock(GetCacheStore.class);
CacheStore mockGoodCacheStore = mock(CacheStore.class);
CacheStore mockBadCacheStore = mock(CacheStore.class);
when(mockCacheStoreFunc.apply(mockGoodCache)).thenReturn(mockGoodCacheStore);
when(mockCacheStoreFunc.apply(mockBadCache)).thenReturn(mockBadCacheStore);
when(mockStore.getCaches()).thenReturn(Arrays.asList(mockCaches));
when(mockKeyFunc.apply(mockNode)).thenReturn(mockKey);
when(mockStoreFunc.apply(mockRepo)).thenReturn(mockStore);
LowLevelStorageService testObj = new LowLevelStorageService();
testObj.getBinaryStore = mockStoreFunc;
testObj.getBinaryKey = mockKeyFunc;
testObj.setRepository(mockRepo);
MessageDigest mockDigest = mock(MessageDigest.class);
URI mockUri = URI.create("urn:foo:bar"); // can't mock final classes
long testSize = 4L;
Function<LowLevelCacheEntry, FixityResult> mockFixityFunc = mock(Function.class);
PowerMockito.mockStatic(ServiceHelpers.class);
when(ServiceHelpers.getCheckCacheFixityFunction(any(MessageDigest.class), any(URI.class), any(Long.class)))
.thenReturn(mockFixityFunc);
GetGoodFixityResults goodMock = mock(GetGoodFixityResults.class);
testObj.getGoodFixityResults = goodMock;
testObj.getCacheStore = mockCacheStoreFunc;
Datastream mockDs = mock(Datastream.class);
FedoraObject mockObj = mock(FedoraObject.class);
when(mockObj.getName()).thenReturn("mockObject");
when(mockDs.getObject()).thenReturn(mockObj);
when(mockDs.getDsId()).thenReturn("mockDs");
when(mockDs.getNode()).thenReturn(mockNode);
when(mockDs.getContentSize()).thenReturn(testSize);
when(mockDs.getContentDigestType()).thenReturn("MD5"); // whatever, just be quiet
when(mockDs.getContentDigest()).thenReturn(mockUri);

FixityResult mockGoodFixity = mock(FixityResult.class);
FixityResult mockBadFixity = mock(FixityResult.class);
when(mockFixityFunc.apply(any(LowLevelCacheEntry.class)))
.thenReturn(mockGoodFixity, mockBadFixity);
FixityResult[] results = new FixityResult[]{mockGoodFixity};
when(goodMock.apply(any(Collection.class))).thenReturn(new HashSet<FixityResult>(Arrays.asList(results)));
LowLevelCacheEntry goodEntry = mock(LowLevelCacheEntry.class);
LowLevelCacheEntry badEntry = mock(LowLevelCacheEntry.class);
when(mockGoodFixity.getEntry()).thenReturn(goodEntry);
when(mockBadFixity.getEntry()).thenReturn(badEntry);
mockBadFixity.status = EnumSet.noneOf(FixityState.class);
InputStream mockIS = mock(InputStream.class);
when(goodEntry.getInputStream()).thenReturn(mockIS);
when(badEntry.checkFixity(any(URI.class), any(Long.class), any(MessageDigest.class))).thenReturn(mockBadFixity);
Collection<FixityResult> actual =
testObj.runFixityAndFixProblems(mockDs);
FixityResult result = actual.iterator().next();
verify(mockFixityFunc, times(2)).apply(any(LowLevelCacheEntry.class));
verify(goodMock).apply(any(Collection.class));
verify(badEntry).storeValue(mockIS);
}

}
@@ -0,0 +1,27 @@
package org.fcrepo.services.functions;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;

import java.net.URI;
import java.security.MessageDigest;
import java.security.SecureRandom;

import org.fcrepo.utils.FixityResult;
import org.fcrepo.utils.LowLevelCacheEntry;
import org.junit.Test;
import org.modeshape.jcr.value.binary.BinaryStoreException;

public class CheckCacheEntryFixityTest {

@Test
public void testApply() throws BinaryStoreException {
MessageDigest mockDigest = mock(MessageDigest.class);
URI testUri = URI.create("urn:foo:bar");
long testSize = new SecureRandom().nextLong();
CheckCacheEntryFixity testObj = new CheckCacheEntryFixity(mockDigest, testUri, testSize);
LowLevelCacheEntry mockEntry = mock(LowLevelCacheEntry.class);
FixityResult actual = testObj.apply(mockEntry);
verify(mockEntry).checkFixity(testUri, testSize, mockDigest);
}
}
@@ -1,7 +1,8 @@
package org.fcrepo.services.functions;

import static org.mockito.Mockito.any;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import javax.jcr.LoginException;
Expand All @@ -10,33 +11,33 @@
import org.infinispan.AdvancedCache;
import org.infinispan.Cache;
import org.infinispan.factories.ComponentRegistry;
import org.infinispan.factories.GlobalComponentRegistry;
import org.infinispan.factories.components.ComponentMetadata;
import org.infinispan.factories.components.ComponentMetadataRepo;
import org.infinispan.loaders.CacheLoaderManager;
import org.infinispan.loaders.CacheStore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

@RunWith(PowerMockRunner.class)
@PrepareForTest({ ComponentRegistry.class })
public class GetCacheStoreTest {

@Test
public void testApply() throws LoginException, RepositoryException {
Cache<?, ?> mockCache = mock(Cache.class);
AdvancedCache mockAC = mock(AdvancedCache.class);
GlobalComponentRegistry mockG = mock(GlobalComponentRegistry.class);
ComponentMetadataRepo mockCMR = mock(ComponentMetadataRepo.class);
when(mockG.getComponentMetadataRepo()).thenReturn(mockCMR);
ComponentRegistry mockCR = null; //new ComponentRegistry("foo", null, mockAC, mockG, getClass().getClassLoader());
ComponentMetadata mockMD = mock(ComponentMetadata.class);
when(mockCMR.findComponentMetadata(any(Class.class))).thenReturn(mockMD);
ComponentRegistry mockCR = mock(ComponentRegistry.class);
CacheLoaderManager mockCLM = mock(CacheLoaderManager.class);
CacheStore mockStore = mock(CacheStore.class);

when(mockCLM.getCacheStore()).thenReturn(mockStore);
//when(mockCR.getComponent(CacheLoaderManager.class)).thenReturn(mockCLM);
when(mockCR.getComponent(CacheLoaderManager.class)).thenReturn(mockCLM);
when(mockAC.getComponentRegistry()).thenReturn(mockCR);
when(mockCache.getAdvancedCache()).thenReturn(mockAC);

GetCacheStore testObj = new GetCacheStore();
//testObj.apply(mockCache);
testObj.apply(mockCache);
verify(mockCR).getComponent(any(Class.class));
}

}
@@ -0,0 +1,67 @@
package org.fcrepo.services.functions;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;

import java.net.URI;
import java.util.Arrays;
import java.util.List;
import java.util.Set;

import org.fcrepo.services.functions.GetGoodFixityResults.IsGoodFixity;
import org.fcrepo.utils.FixityResult;
import org.junit.Test;

public class GetGoodFixityResultsTest {

@Test
public void testPredicate() {
IsGoodFixity testPred = new IsGoodFixity();
FixityResult mockFixity = mock(FixityResult.class);
URI uri = URI.create("urn:foo:bar");
URI otherUri = URI.create("urn:does:not:match");
mockFixity.computedChecksum = uri;
mockFixity.dsChecksum = otherUri;
assertFalse(testPred.apply(mockFixity));
mockFixity.dsChecksum = uri;
mockFixity.computedSize = 1L;
assertFalse(testPred.apply(mockFixity));
mockFixity.dsSize = 1L;
assertTrue(testPred.apply(mockFixity));
}

@Test
public void testIsGood() {
GetGoodFixityResults testObj = new GetGoodFixityResults();
FixityResult mockFixity = mock(FixityResult.class);
URI uri = URI.create("urn:foo:bar");
URI otherUri = URI.create("urn:does:not:match");
mockFixity.computedChecksum = uri;
mockFixity.dsChecksum = otherUri;
assertFalse(testObj.isGood(mockFixity));
mockFixity.dsChecksum = uri;
mockFixity.computedSize = 1L;
assertFalse(testObj.isGood(mockFixity));
mockFixity.dsSize = 1L;
assertTrue(testObj.isGood(mockFixity));
}

@Test
public void testFunction() {
GetGoodFixityResults testObj = new GetGoodFixityResults();
FixityResult mockBadFixity = mock(FixityResult.class);
FixityResult mockGoodFixity = mock(FixityResult.class);
List<FixityResult> mockList = Arrays.asList(new FixityResult[]{mockBadFixity, mockGoodFixity});
URI uri = URI.create("urn:foo:bar");
URI otherUri = URI.create("urn:does:not:match");
mockBadFixity.computedChecksum = uri;
mockBadFixity.dsChecksum = otherUri;
mockGoodFixity.computedChecksum = uri;
mockGoodFixity.dsChecksum = uri;
Set<FixityResult> actual = testObj.apply(mockList);
assertEquals(1, actual.size());
assertTrue(actual.contains(mockGoodFixity));
}
}

0 comments on commit e133bf0

Please sign in to comment.