Add tests for UrlResourceCache content lengths

This commit is contained in:
Winston Li 2017-08-13 17:23:45 +01:00 committed by Michael Walker
parent 6c71c2cb97
commit 90e3417aec

View file

@ -3,13 +3,16 @@ package uk.ac.ic.wlgitbridge.bridge.resource;
import com.ning.http.client.HttpResponseHeaders; import com.ning.http.client.HttpResponseHeaders;
import org.junit.Test; import org.junit.Test;
import uk.ac.ic.wlgitbridge.bridge.db.noop.NoopDbStore; import uk.ac.ic.wlgitbridge.bridge.db.noop.NoopDbStore;
import uk.ac.ic.wlgitbridge.bridge.util.CastUtil;
import uk.ac.ic.wlgitbridge.git.exception.SizeLimitExceededException; import uk.ac.ic.wlgitbridge.git.exception.SizeLimitExceededException;
import uk.ac.ic.wlgitbridge.io.http.ning.NingHttpClientFacade; import uk.ac.ic.wlgitbridge.io.http.ning.NingHttpClientFacade;
import uk.ac.ic.wlgitbridge.io.http.ning.NingHttpHeaders; import uk.ac.ic.wlgitbridge.io.http.ning.NingHttpHeaders;
import uk.ac.ic.wlgitbridge.util.FunctionT; import uk.ac.ic.wlgitbridge.util.FunctionT;
import java.io.IOException;
import java.util.HashMap; import java.util.HashMap;
import java.util.Optional; import java.util.Optional;
import java.util.concurrent.ExecutionException;
import static org.mockito.Matchers.any; import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
@ -35,21 +38,73 @@ public class UrlResourceCacheTest {
.build(); .build();
} }
@Test private void respondWithContentLength(long cl, long actual)
public void getThrowsSizeLimitWhenContentLengthTooBig() throws Exception { throws ExecutionException {
when(http.get(any(), any())).thenAnswer(invoc -> { when(http.get(any(), any())).thenAnswer(invoc -> {
Object[] args = invoc.getArguments(); Object[] args = invoc.getArguments();
//noinspection unchecked //noinspection unchecked
((FunctionT< ((FunctionT<
HttpResponseHeaders, Boolean, SizeLimitExceededException HttpResponseHeaders, Boolean, SizeLimitExceededException
>) args[1]).apply(withContentLength(2)); >) args[1]).apply(withContentLength(cl));
return new byte[0]; return new byte[CastUtil.assumeInt(actual)];
}); });
}
private void respondWithContentLength(long cl) throws ExecutionException {
respondWithContentLength(cl, cl);
}
private void getWithMaxLength(Optional<Long> max)
throws IOException, SizeLimitExceededException {
cache.get( cache.get(
PROJ, URL, NEW_PATH, PROJ, URL, NEW_PATH, new HashMap<>(), new HashMap<>(), max);
new HashMap<>(), new HashMap<>(),
Optional.of(2L)
);
} }
private void getWithMaxLength(long max)
throws IOException, SizeLimitExceededException {
getWithMaxLength(Optional.of(max));
}
private void getWithoutLimit()
throws IOException, SizeLimitExceededException {
getWithMaxLength(Optional.empty());
}
@Test
public void getDoesNotThrowWhenContentLengthLT() throws Exception {
respondWithContentLength(1);
getWithMaxLength(2);
}
@Test
public void getDoesNotThrowWhenContentLengthEQ() throws Exception {
respondWithContentLength(2);
getWithMaxLength(2);
}
@Test (expected = SizeLimitExceededException.class)
public void getThrowsSizeLimitExceededWhenContentLengthGT()
throws Exception {
respondWithContentLength(3);
getWithMaxLength(2);
}
@Test
public void getWithEmptyContentIsValid() throws Exception {
respondWithContentLength(0);
getWithMaxLength(0);
}
@Test
public void getWithoutLimitDoesNotThrow() throws Exception {
respondWithContentLength(Integer.MAX_VALUE, 0);
getWithoutLimit();
}
@Test (expected = SizeLimitExceededException.class)
public void getThrowsIfActualContentTooBig() throws Exception {
respondWithContentLength(0, 10);
getWithMaxLength(5);
}
} }