package cz.senslog.connector.fetch.azure.auth;

import cz.senslog.connector.tools.json.BasicJson;
import org.junit.jupiter.api.Test;

import java.time.LocalDateTime;

import static org.junit.jupiter.api.Assertions.*;

class AzureAuthorizationInfoTest {

    @Test
    void isAuthorized_ValidExpires_True(){

        AzureAuthorizationInfo authInfo = new AzureAuthorizationInfo();
        authInfo.setAccessToken("#12345");
        authInfo.setExpires(LocalDateTime.now().plusMinutes(1));

        assertTrue(authInfo.isAuthorized());
    }

    @Test
    void isAuthorized_InvalidExpires_False() {

        AzureAuthorizationInfo authInfo = new AzureAuthorizationInfo();
        authInfo.setAccessToken("#12345");
        authInfo.setExpires(LocalDateTime.now().minusMinutes(1));

        assertFalse(authInfo.isAuthorized());
    }

    @Test
    void isAuthorized_MissingExpires_False() {

        AzureAuthorizationInfo authInfo = new AzureAuthorizationInfo();
        authInfo.setAccessToken("#12345");

        assertFalse(authInfo.isAuthorized());
    }

    @Test
    void isAuthorized_MissingToken_False() {

        AzureAuthorizationInfo authInfo = new AzureAuthorizationInfo();
        authInfo.setExpires(LocalDateTime.now().plusMinutes(1));

        assertFalse(authInfo.isAuthorized());
    }

    @Test
    void isAuthorized_MissingAll_False() {

        AzureAuthorizationInfo authInfo = new AzureAuthorizationInfo();

        assertFalse(authInfo.isAuthorized());
    }

    @Test
    void getAccessTokenAndExpires_True() {

        AzureAuthorizationInfo authInfo = new AzureAuthorizationInfo();
        authInfo.setAccessToken("#12345");
        authInfo.setExpires(LocalDateTime.MIN);

        assertEquals("#12345", authInfo.getAccessToken());
        assertEquals(LocalDateTime.MIN, authInfo.getExpires());
    }

    @Test
    void toString_True() {

        AzureAuthorizationInfo authInfo = new AzureAuthorizationInfo();
        authInfo.setAccessToken("#12345");
        authInfo.setExpires(LocalDateTime.MIN);

        AzureAuthorizationInfo parsedInfo = BasicJson.jsonToObject(authInfo.toString(), AzureAuthorizationInfo.class);

        assertEquals("#12345", parsedInfo.getAccessToken());
        assertEquals(LocalDateTime.MIN, parsedInfo.getExpires());


    }
}