package retromock.parser;

import com.google.gson.Gson;
import org.junit.Test;
import retrofit.client.Header;
import retrofit.client.Response;
import retrofit.mime.TypedByteArray;
import retromock.test.FileLocator;
import retromock.test.Http200ResponseBean;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.junit.Assert.*;
import static retromock.matchers.IsRegex.matchesRegex;

public class HttpParserTest {

    static final List<Path> HTTP_FILES = FileLocator.findAllInClasspath("http-*-response.txt");
    static final String LOCALHOST = "http://localhost";
    static final Gson GSON = new Gson();

    @Test
    public void testParse200ResponseFromFile() throws Exception {
        Response response = HttpParser.parse(LOCALHOST, getFile("http-200-response.txt"));
        assertNotNull(response);
        assertEquals(LOCALHOST, response.getUrl());
        assertEquals(200, response.getStatus());
        assertEquals("OK", response.getReason());
        Map<String, String> headers = headerMap(response.getHeaders());
        assertEquals("Flat-File", headers.get("X-Powered-By"));
        assertTrue(response.getBody() instanceof TypedByteArray);
        TypedByteArray body = (TypedByteArray) response.getBody();
        assertEquals(headers.get("Content-Type"), body.mimeType());
        assertEquals(headers.get("Content-Length"), String.valueOf(body.length()));
        Http200ResponseBean bodyAsBean = GSON.fromJson(new String(body.getBytes()), Http200ResponseBean.class);
        assertEquals("test", bodyAsBean.title);
        assertEquals("qwerty", bodyAsBean.foot);
    }

    @Test
    public void testParse302ResponseFromFile() throws Exception {
        Response response = HttpParser.parse(LOCALHOST, getFile("http-302-response.txt"));
        assertNotNull(response);
        assertEquals(LOCALHOST, response.getUrl());
        assertEquals(302, response.getStatus());
        assertEquals("Found", response.getReason());
        Map<String, String> headers = headerMap(response.getHeaders());
        assertEquals("http://otherhost/", headers.get("Location"));
        assertEquals("Flat-File", headers.get("X-Powered-By"));
        assertEquals(headers.get("Content-Length"), "0");
    }

    @Test
    public void testParse404ResponseFromFile() throws Exception {
        Response response = HttpParser.parse(LOCALHOST, getFile("http-404-response.txt"));
        assertNotNull(response);
        assertEquals(LOCALHOST, response.getUrl());
        assertEquals(404, response.getStatus());
        assertEquals("Not Found", response.getReason());
        Map<String, String> headers = headerMap(response.getHeaders());
        assertEquals("Flat-File", headers.get("X-Powered-By"));
        assertTrue(response.getBody() instanceof TypedByteArray);
        TypedByteArray body = (TypedByteArray) response.getBody();
        assertEquals(headers.get("Content-Length"), String.valueOf(body.length()));
        String html = new String(body.getBytes());
        assertThat(html.replaceAll("\n", ""), matchesRegex(".+(<\\w+>404 .+</\\w+>.*)+"));
    }

    private Map<String, String> headerMap(List<Header> headers) {
        Map<String, String> headerMap = new HashMap<>();
        for (Header header : headers) {
            headerMap.put(header.getName(), header.getValue());
        }
        return headerMap;
    }

    private static Path getFile(String name) {
        Path fileName = Paths.get(name);
        for (Path path : HTTP_FILES) {
            if (path.endsWith(fileName)) {
                return path;
            }
        }
        return null;
    }
}

@@ -49,16 +49,41 @@
 n.Gson;%0A
+import org.junit.Before;%0A
 import o
@@ -277,16 +277,44 @@
 eBean;%0A%0A
+import java.io.IOException;%0A
 import j
@@ -557,28 +557,23 @@
  %7B%0A%0A    
-static final
+private
  List%3CPa
@@ -590,64 +590,8 @@
 ILES
- = FileLocator.findAllInClasspath(%22http-*-response.txt%22)
 ;%0A  
@@ -686,16 +686,156 @@
 son();%0A%0A
+    @Before%0A    public void setup() throws IOException %7B%0A        HTTP_FILES = FileLocator.findAllInClasspath(%22http-*-response.txt%22);%0A    %7D%0A%0A
     @Tes
@@ -3579,23 +3579,16 @@
 private 
-static 
 Path get

fix HttpParserTest initialization