package uk.ac.ic.wlgitbridge.bridge.resource;

import com.ning.http.client.HttpResponseHeaders;
import org.junit.Test;
import uk.ac.ic.wlgitbridge.bridge.db.noop.NoopDbStore;
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.NingHttpHeaders;
import uk.ac.ic.wlgitbridge.util.FunctionT;

import java.util.HashMap;
import java.util.Optional;

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

public class UrlResourceCacheTest {

    private static String PROJ = "proj";

    private static String URL = "http://localhost/file.jpg";

    private static String NEW_PATH = "file1.jpg";

    private final NingHttpClientFacade http = mock(NingHttpClientFacade.class);

    private final UrlResourceCache cache
            = new UrlResourceCache(new NoopDbStore(), http);

    private static HttpResponseHeaders withContentLength(long cl) {
        return NingHttpHeaders
                .builder()
                .addHeader("Content-Length", String.valueOf(cl))
                .build();
    }

    @Test
    public void getThrowsSizeLimitWhenContentLengthTooBig() throws Exception {
        when(http.get(any(), any())).thenAnswer(invoc -> {
            Object[] args = invoc.getArguments();
            //noinspection unchecked
            ((FunctionT<
                    HttpResponseHeaders, Boolean, SizeLimitExceededException
            >) args[1]).apply(withContentLength(2));
            return new byte[0];
        });

        cache.get(
                PROJ, URL, NEW_PATH,
                new HashMap<>(), new HashMap<>(),
                Optional.of(2L)
        );
    }
}

@@ -168,16 +168,66 @@
 bStore;%0A
+import uk.ac.ic.wlgitbridge.bridge.util.CastUtil;%0A
 import u
@@ -454,16 +454,44 @@
 tionT;%0A%0A
+import java.io.IOException;%0A
 import j
@@ -534,16 +534,64 @@
 ptional;
+%0Aimport java.util.concurrent.ExecutionException;
 %0A%0Aimport
@@ -1313,81 +1313,96 @@
     
-@Test%0A    public void getThrowsSizeLimitWhenContentLengthTooBig() throws 
+private void respondWithContentLength(long cl, long actual)%0A            throws Execution
 Exce
@@ -1705,17 +1705,18 @@
 tLength(
-2
+cl
 ));%0A    
@@ -1743,24 +1743,294 @@
 yte%5B
-0%5D;%0A        %7D);%0A
+CastUtil.assumeInt(actual)%5D;%0A        %7D);%0A    %7D%0A%0A    private void respondWithContentLength(long cl) throws ExecutionException %7B%0A        respondWithContentLength(cl, cl);%0A    %7D%0A%0A    private void getWithMaxLength(Optional%3CLong%3E max)%0A        throws IOException, SizeLimitExceededException %7B
 %0A   
@@ -2085,99 +2085,1396 @@
 ATH,
+ new HashMap%3C%3E(), new HashMap%3C%3E(), max);
 %0A    
+%7D%0A%0A
     
-        new HashMap%3C%3E(), new HashMap%3C%3E(),%0A        
+private void getWithMaxLength(long max)%0A            throws IOException, SizeLimitExceededException %7B%0A        getWithMaxLength(Optional.of(max));%0A    %7D%0A%0A    private void getWithoutLimit()%0A            throws IOException, SizeLimitExceededException %7B%0A        getWithMaxLength(Optional.empty());%0A    %7D%0A%0A    @Test%0A    public void getDoesNotThrowWhenContentLengthLT() throws Exception %7B%0A        respondWithContentLength(1);%0A        getWithMaxLength(2);%0A    %7D%0A%0A    @Test%0A    public void getDoesNotThrowWhenContentLengthEQ() throws Exception %7B%0A        respondWithContentLength(2);%0A        getWithMaxLength(2);%0A    %7D%0A%0A    @Test (expected = SizeLimitExceededException.class)%0A    public void getThrowsSizeLimitExceededWhenContentLengthGT()%0A            throws Exception %7B%0A        respondWithContentLength(3);%0A        getWithMaxLength(2);%0A    %7D%0A%0A    @Test%0A    public void getWithEmptyContentIsValid() throws Exception %7B%0A        respondWithContentLength(0);%0A        getWithMaxLength(0);%0A    %7D%0A%0A    @Test%0A    public void getWithoutLimitDoesNotThrow() throws Exception %7B%0A        respondWithContentLength(Integer.MAX_VALUE, 0);%0A        getWithoutLimit();%0A
     
+%7D%0A%0A
     
-Optional.of(2L)%0A        
+@Test (expected = SizeLimitExceededException.class)%0A    public void getThrowsIfActualContentTooBig() throws Exception %7B%0A        respondWithContentLength(0, 10);%0A        getWithMaxLength(5
 );%0A 
@@ -3478,10 +3478,11 @@
 ;%0A    %7D%0A
+%0A
 %7D%0A

Add tests for UrlResourceCache content lengths