package com.novoda.downloadmanager;

import java.io.IOException;

import org.junit.Before;
import org.junit.Test;

import static com.google.common.truth.Truth.assertThat;
import static com.novoda.downloadmanager.NetworkResponseFixtures.aNetworkResponse;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;

public class NetworkFileSizeRequesterTest {

    private static final int FILE_BYTES = 1000;
    private static final HttpClient.NetworkResponse UNSUCCESSFUL_RESPONSE = aNetworkResponse().withSuccessful(false).build();
    private static final HttpClient.NetworkResponse SUCCESSFUL_RESPONSE = aNetworkResponse().withHeader("1000").withSuccessful(true).withBodyContentLength(FILE_BYTES).build();
    private static final String ANY_RAW_URL = "http://example.com";

    private final HttpClient httpClient = mock(HttpClient.class);
    private final NetworkRequestCreator requestCreator = new NetworkRequestCreator();

    private NetworkFileSizeRequester fileSizeRequester;

    @Before
    public void setUp() {
        fileSizeRequester = new NetworkFileSizeRequester(httpClient, requestCreator);
    }

    @Test
    public void returnsUnknownSize_whenHttpClientErrors() throws IOException {
        given(httpClient.execute(requestCreator.createFileSizeBodyRequest(ANY_RAW_URL))).willThrow(IOException.class);

        FileSize fileSize = fileSizeRequester.requestFileSize(ANY_RAW_URL);

        assertThat(fileSize).isEqualTo(FileSizeCreator.unknownFileSize());
    }

    @Test
    public void returnsUnknownSize_whenResponseIsUnsuccessful() throws IOException {
        given(httpClient.execute(requestCreator.createFileSizeHeadRequest(ANY_RAW_URL))).willReturn(UNSUCCESSFUL_RESPONSE);
        given(httpClient.execute(requestCreator.createFileSizeBodyRequest(ANY_RAW_URL))).willReturn(UNSUCCESSFUL_RESPONSE);

        FileSize fileSize = fileSizeRequester.requestFileSize(ANY_RAW_URL);

        assertThat(fileSize).isEqualTo(FileSizeCreator.unknownFileSize());
    }

    @Test
    public void returnsFileSize_whenResponseSuccessful() throws IOException {
        given(httpClient.execute(requestCreator.createFileSizeBodyRequest(ANY_RAW_URL))).willReturn(SUCCESSFUL_RESPONSE);

        FileSize fileSize = fileSizeRequester.requestFileSize(ANY_RAW_URL);

        assertThat(fileSize).isEqualTo(FileSizeCreator.createFromTotalSize(FILE_BYTES));
    }
}

@@ -381,56 +381,8 @@
  %7B%0A%0A
-    private static final int FILE_BYTES = 1000;%0A
     
@@ -640,42 +640,8 @@
 ue).
-withBodyContentLength(FILE_BYTES).
 buil
@@ -712,16 +712,64 @@
 le.com%22;
+%0A    private static final int FILE_BYTES = 1000;
 %0A%0A    pr
@@ -1246,36 +1246,36 @@
 r.createFileSize
-Body
+Head
 Request(ANY_RAW_
@@ -2116,36 +2116,36 @@
 r.createFileSize
-Body
+Head
 Request(ANY_RAW_

Revert "Fix failing tests"