Created
August 5, 2011 14:34
-
-
Save criminy/1127665 to your computer and use it in GitHub Desktop.
Proof of concept Mockito Mock of the terrastoreclient.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Bean | |
public Map<String, DocumentPackage> mockDpBucket() { | |
return new HashMap<String, DocumentPackage>(); | |
} | |
class WildcardExtractingHamcrestMatcher extends ArgumentMatcher<String> { | |
String regex; | |
public WildcardExtractingHamcrestMatcher(String reg) { | |
this.regex = reg; | |
} | |
@Override | |
public boolean matches(Object item) { | |
return item.toString().matches(regex); | |
} | |
} | |
@Bean | |
public TerrastoreClient terrastoreClient(final ApplicationContext ctx) | |
{ | |
TerrastoreClient client = mock(TerrastoreClient.class); | |
{ | |
BucketsOperation op = mock(BucketsOperation.class); | |
when(op.list()).thenReturn(Collections.singleton("packages")); | |
when(client.buckets()).thenReturn(op); | |
} | |
{ | |
BucketOperation packagesOp = mock(BucketOperation.class); | |
{ | |
when(packagesOp.key(anyString())).thenAnswer(new Answer<KeyOperation>() { | |
@Override | |
public KeyOperation answer(InvocationOnMock invocation) | |
throws Throwable { | |
final String key = (String) invocation.getArguments()[0]; | |
KeyOperation keyOp = mock(KeyOperation.class); | |
doAnswer(new Answer<Object>() { | |
@Override | |
public Object answer(InvocationOnMock invocation) | |
throws Throwable { | |
DocumentPackage o = (DocumentPackage) invocation.getArguments()[0]; | |
ctx.getBean("mockDpBucket",Map.class).put(key,o); | |
return null; | |
} | |
}).when(keyOp).put(anyObject()); | |
return keyOp; | |
} | |
}); | |
doAnswer(new Answer<Object> () { | |
@Override | |
public Object answer(InvocationOnMock invocation) | |
throws Throwable { | |
Logger log = Logger.getLogger("us.gaje.mock.terrastore"); | |
log.debug("clearing"); | |
ctx.getBean("mockDpBucket",Map.class).clear(); | |
return null; | |
} | |
}).when(packagesOp).clear(); | |
when(packagesOp.predicate(argThat(new WildcardExtractingHamcrestMatcher(".*")))).thenAnswer( | |
new Answer<PredicateOperation>() { | |
public PredicateOperation answer(InvocationOnMock invocation) throws Throwable { | |
PredicateOperation pred = mock(PredicateOperation.class); | |
when(pred.get(DocumentPackage.class)).thenReturn( | |
ctx.getBean("mockDpBucket",Map.class)); | |
return pred; | |
}; | |
}); | |
} | |
{ | |
ValuesOperation valOp = mock(ValuesOperation.class); | |
when(valOp.get(DocumentPackage.class)).thenReturn(ctx.getBean("mockDpBucket",Map.class)); | |
when(packagesOp.values()).thenReturn(valOp); | |
} | |
when(client.bucket("packages")).thenReturn(packagesOp); | |
} | |
return client; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment