/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mockupcode.slack.rtm.api;

import com.mockupcode.slack.rtm.api.json.connection.SlackInfo;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
//import org.easymock.EasyMock;
import static org.hamcrest.core.Is.*;
import org.junit.After;
import org.junit.AfterClass;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
//import org.powermock.api.easymock.PowerMock;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

/**
 *
 * @author Jirawong Wongdokpuang <greannetwork@gmail.com>
 */
@RunWith(PowerMockRunner.class)
@PowerMockIgnore({"javax.net.ssl.*", "javax.net.*"})
@PrepareForTest({SlackAuthen.class, HttpClient.class, GetMethod.class})
public class SlackAuthenTest {

    public SlackAuthenTest() {
    }

    @BeforeClass
    public static void setUpClass() {
    }

    @AfterClass
    public static void tearDownClass() {
    }

    @Before
    public void setUp() {
    }

    @After
    public void tearDown() {
    }


    @Test
    public void testTokenAuthenWithInvalidToken() throws Exception {
        InputStream inputStream = new ByteArrayInputStream("{\"ok\":false}".getBytes());
        
        HttpClient httpClient = PowerMockito.mock(HttpClient.class);
        GetMethod getMethod = PowerMockito.mock(GetMethod.class);
        
        PowerMockito.whenNew(HttpClient.class).withNoArguments().thenReturn(httpClient);
        PowerMockito.whenNew(GetMethod.class).withAnyArguments().thenReturn(getMethod);
        
        Mockito.when(httpClient.executeMethod(getMethod)).thenReturn(HttpStatus.SC_OK);
        Mockito.when(getMethod.getResponseBodyAsStream()).thenReturn(inputStream);
        
        SlackInfo tokenAuthen = new SlackAuthen().tokenAuthen("token",null,0);
        assertThat(tokenAuthen.isOk(), is(false));
    }

    @Test
    public void testTokenAuthenSuccess() throws Exception {
        InputStream inputStream = new ByteArrayInputStream("{\"ok\":true}".getBytes());
        
        HttpClient httpClient = PowerMockito.mock(HttpClient.class);
        GetMethod getMethod = PowerMockito.mock(GetMethod.class);
        
        PowerMockito.whenNew(HttpClient.class).withNoArguments().thenReturn(httpClient);
        PowerMockito.whenNew(GetMethod.class).withAnyArguments().thenReturn(getMethod);
        
        Mockito.when(httpClient.executeMethod(getMethod)).thenReturn(HttpStatus.SC_OK);
        Mockito.when(getMethod.getResponseBodyAsStream()).thenReturn(inputStream);
        
        SlackInfo tokenAuthen = new SlackAuthen().tokenAuthen("token",null,0);
        assertThat(tokenAuthen.isOk(), is(true));
    }
}

@@ -503,40 +503,8 @@
 od;%0A
-//import org.easymock.EasyMock;%0A
 impo
@@ -533,24 +533,67 @@
 .core.Is.*;%0A
+import static org.hamcrest.core.IsEqual.*;%0A
 import org.j
@@ -857,55 +857,8 @@
 to;%0A
-//import org.powermock.api.easymock.PowerMock;%0A
 impo
@@ -1554,17 +1554,16 @@
 %0A    %7D%0A%0A
-%0A
     @Tes
@@ -1718,33 +1718,25 @@
 etBytes());%0A
-        
 %0A
+
         Http
@@ -1854,33 +1854,25 @@
 hod.class);%0A
-        
 %0A
+
         Powe
@@ -2032,33 +2032,25 @@
 getMethod);%0A
-        
 %0A
+
         Mock
@@ -2204,32 +2204,24 @@
 putStream);%0A
-        
 %0A        Sla
@@ -2271,37 +2271,39 @@
 nAuthen(%22token%22,
+ 
 null,
+ 
 0);%0A        asse
@@ -2506,25 +2506,17 @@
 tes());%0A
-        
 %0A
+
         
@@ -2642,25 +2642,17 @@
 class);%0A
-        
 %0A
+
         
@@ -2820,25 +2820,17 @@
 ethod);%0A
-        
 %0A
+
         
@@ -2992,24 +2992,25 @@
 tream);%0A
+%0A
         
 %0A       
@@ -3001,16 +3001,493 @@
         
+SlackInfo tokenAuthen = new SlackAuthen().tokenAuthen(%22token%22, null, 0);%0A        assertThat(tokenAuthen.isOk(), is(true));%0A    %7D%0A%0A    @Test%0A    public void testTokenAuthenNotHttpOK() throws Exception %7B%0A        HttpClient httpClient = PowerMockito.mock(HttpClient.class);%0A%0A        PowerMockito.whenNew(HttpClient.class).withNoArguments().thenReturn(httpClient);%0A        Mockito.when(httpClient.executeMethod(Mockito.any(GetMethod.class))).thenReturn(HttpStatus.SC_BAD_GATEWAY);%0A
 %0A       
@@ -3549,21 +3549,23 @@
 %22token%22,
+ 
 null,
+ 
 0);%0A    
@@ -3594,32 +3594,130 @@
 then.isOk(), is(
-true
+false));%0A        assertThat(tokenAuthen.getError(), equalTo(%22http_status_%22 + HttpStatus.SC_BAD_GATEWAY
 ));%0A    %7D%0A%7D%0A

add test when authen response not http_ok