package com.vaessl.app.connection;

import static com.vaessl.app.Mockdata.*;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.util.List;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import com.vaessl.app.exception.EmptyCredentialsException;

@ExtendWith(MockitoExtension.class)
class ConnectionServiceTest {

    @Mock
    private ConnectionProvider mockProvider;
    @Mock
    private ConnectionRepository mockRepo;

    private ConnectionService connectionService;

    @BeforeEach
    void setup() {
        when(mockProvider.getServiceType()).thenReturn(MOCK_SERVICE_TYPE);
        connectionService = new ConnectionService(List.of(mockProvider), mockRepo);
    }

    @Test
    void login_ShouldAbort_WhenCheckCredentialsThrowsException() {
        ConnectionRequest request =
                new ConnectionRequest(MOCK_URL, MOCK_SERVICE_TYPE, null, null, false);

        doThrow(new EmptyCredentialsException(List.of("username"))).when(mockProvider)
                .checkCredentials(request);

        assertThrows(EmptyCredentialsException.class, () -> connectionService.login(request));

        verify(mockProvider, never()).authenticate(any());
    }
}
