package be.iminds.jfed.gts_highlevel.controller;

import be.iminds.ilabt.jfed.lowlevel.HttpCallDetails;
import be.iminds.ilabt.jfed.lowlevel.api.RestApi;
import be.iminds.ilabt.jfed.lowlevel.connection.ConnectionConfig;
import be.iminds.ilabt.jfed.lowlevel.connection.JFedConnection;
import be.iminds.jfed.gts_highlevel.GtsService;
import be.iminds.jfed.gts_highlevel.MockHttpCallDetails;
import be.iminds.jfed.gts_highlevel.model.GtsCredentials;
import org.apache.http.StatusLine;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.testng.Assert;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import java.util.Date;

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

/**
 * User: twalcari
 * Date: 9/23/2015
 * Time: 11:45
 */
public class LoginControllerTest {
    private static final String TOKEN = "49a44a30-0679-402f-b1f6-155019d6cdce";

    @Mock
    private GtsConnectionProvider gtsConnectionProvider;
    @Mock
    private GtsService gtsService;


    private LoginController loginController;


    @BeforeMethod
    public void setUp() throws Exception {
        MockitoAnnotations.initMocks(this);

        when(gtsConnectionProvider.getConnection()).thenReturn(null);

        loginController = new LoginController(gtsService, gtsConnectionProvider);
    }

    @Test
    public void testLogin() throws Exception {
        when(gtsService.login(any())).thenReturn(new RestApi.RestReply<String>(new MockHttpCallDetails(), TOKEN));

        GtsCredentials credential = loginController.login();
        Assert.assertEquals(credential.getToken(), TOKEN);

    }


    @Test(expectedExceptions = GtsException.class)
    public void testLoginWithError() throws Exception {


        HttpCallDetails error401HttpCall = new HttpCallDetails() {
            @Override
            public String getServerUrl() {
                return null;
            }

            @Override
            public String getRequestHttpRequestLine() {
                return null;
            }

            @Override
            public String getRequestHttpHeaders() {
                return null;
            }

            @Override
            public String getRequestHttpContent() {
                return null;
            }

            @Override
            public int getResultHttpStatusCode() {
                return 401;
            }

            @Override
            public String getResultHttpStatusReasonPhrase() {
                return null;
            }

            @Override
            public String getResultHttpStatusLine() {
                return "HTTP/1.1 401 Unauthorized";
            }

            @Override
            public StatusLine getResultHttpStatusLineObject() {
                return null;
            }

            @Override
            public String getResultHttpHeaders() {
                return "Content-Length: 0\n" +
                        "Date: Wed, 23 Sep 2015 14:40:09 GMT\n" +
                        "WWW-Authenticate: Basic realm=\"GTS user relam\"\n" +
                        "Server: Jetty(7.6.8.v20121106)";
            }

            @Override
            public String getResultHttpContent() {
                return "";
            }

            @Override
            public Date getStartTime() {
                return null;
            }

            @Override
            public Date getStopTime() {
                return null;
            }

            @Override
            public Throwable getException() {
                return null;
            }

            @Override
            public JFedConnection getLastConnection() {
                return null;
            }

            @Override
            public ConnectionConfig getConnectionConfig() {
                return null;
            }
        };

        when(gtsService.login(any())).thenReturn(new RestApi.RestReply<String>(error401HttpCall, ""));



        loginController.login();
    }
}