package org.aaron1011.namehistory.core;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import java.io.ByteArrayInputStream;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.Date;
import java.util.UUID;

import static org.junit.Assert.*;

import static org.powermock.api.mockito.PowerMockito.*;

@RunWith(PowerMockRunner.class)
@PrepareForTest({ PlayerNameHistoryFetcher.class })
public class PlayerNameHistoryFetcherTest {

    private final static String API_DATA = "[{'name':'Djinnibone'},{'name':'Djinnibutt','changedToAt':1413217187000},{'name':'Djinnibone','changedToAt':1413217202000}]";

    @Test
    public void testGetPlayerNameHistory() throws Exception {
        //mockStatic(PlayerNameHistoryFetcher.class);
        stub(method(PlayerNameHistoryFetcher.class, "getAPIResponse")).toReturn(new ByteArrayInputStream(API_DATA.getBytes()));
        //doReturn(new ByteArrayInputStream(API_DATA.getBytes())).when(PlayerNameHistoryFetcher.);

        PlayerNameHistory history = PlayerNameHistoryFetcher.getPlayerNameHistory(UUID.randomUUID());
        System.out.println("Got history");
        System.out.println(history);
        /*URL url = mock(URL.class);
        URLConnection urlConnectionMock = mock(URLConnection.class);

        when(url.openConnection()).thenReturn(urlConnectionMock);

        URLConnection openConnection = url.openConnection();

        assertSame(openConnection, urlConnectionMock);*/
        /*mockStatic(URL.class);

        URLConnection urlConnectionMock = PowerMockito.mock(URLConnection.class);

        when(URL.openConnection).thenReturn(urlConnectionMock);
        PowerMockito.when(urlConnectionMock.getInputStream()).thenReturn(new ByteArrayInputStream(API_DATA.getBytes()));

        PlayerNameHistory history = PlayerNameHistoryFetcher.getPlayerNameHistory(UUID.fromString("3e50893f4903402fb3b810158c6ed75b"));
        assertEquals(history.getUuid(), "3e50893f4903402fb3b810158c6ed75b");

        TimestampedName[] names = { new TimestampedName("Djinnibone", null), new TimestampedName("Djinnibutt", new Date(1413217187000L)),
                new TimestampedName("Djinnibone", new Date(1413217202000L)) };

        assertEquals(history.getNames(), names);*/
    }
}

@@ -889,62 +889,8 @@
 n %7B%0A
-        //mockStatic(PlayerNameHistoryFetcher.class);%0A
     
@@ -1016,107 +1016,8 @@
 )));
-%0A        //doReturn(new ByteArrayInputStream(API_DATA.getBytes())).when(PlayerNameHistoryFetcher.);
 %0A%0A  
@@ -1200,1087 +1200,8 @@
 y);%0A
-        /*URL url = mock(URL.class);%0A        URLConnection urlConnectionMock = mock(URLConnection.class);%0A%0A        when(url.openConnection()).thenReturn(urlConnectionMock);%0A%0A        URLConnection openConnection = url.openConnection();%0A%0A        assertSame(openConnection, urlConnectionMock);*/%0A        /*mockStatic(URL.class);%0A%0A        URLConnection urlConnectionMock = PowerMockito.mock(URLConnection.class);%0A%0A        when(URL.openConnection).thenReturn(urlConnectionMock);%0A        PowerMockito.when(urlConnectionMock.getInputStream()).thenReturn(new ByteArrayInputStream(API_DATA.getBytes()));%0A%0A        PlayerNameHistory history = PlayerNameHistoryFetcher.getPlayerNameHistory(UUID.fromString(%223e50893f4903402fb3b810158c6ed75b%22));%0A        assertEquals(history.getUuid(), %223e50893f4903402fb3b810158c6ed75b%22);%0A%0A        TimestampedName%5B%5D names = %7B new TimestampedName(%22Djinnibone%22, null), new TimestampedName(%22Djinnibutt%22, new Date(1413217187000L)),%0A                new TimestampedName(%22Djinnibone%22, new Date(1413217202000L)) %7D;%0A%0A        assertEquals(history.getNames(), names);*/%0A
     

Remove commented out code