package de.aaschmid.taskwarrior.ssl;

import javax.net.ssl.SSLContext;
import java.security.KeyStore;
import java.security.KeyStore.PasswordProtection;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;

import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

class SslContextFactoryTest {

    @Test
    void getInstance_shouldThrowNullPointerExceptionIfProtocolIsNull() {
        assertThatThrownBy(() -> SslContextFactory.getInstance(null, null, null))
                .isInstanceOf(NullPointerException.class)
                .hasMessage("'protocol' must not be null.");
    }

    @Test
    void getInstance_shouldThrowNullPointerExceptionIfKeyStoreIsNull() {
        assertThatThrownBy(() -> SslContextFactory.getInstance("SSL", null, null))
                .isInstanceOf(NullPointerException.class)
                .hasMessage("'keyStore' must not be null.");
    }

    @Test
    void getInstance_shouldThrowNullPointerExceptionIfKeyStorePasswordIsNull() throws KeyStoreException {
        KeyStore keyStore = KeyStore.Builder.newInstance(KeyStore.getDefaultType(), null, new PasswordProtection("test".toCharArray())).getKeyStore();
        assertThatThrownBy(() -> SslContextFactory.getInstance("SSL", keyStore, null))
                .isInstanceOf(NullPointerException.class)
                .hasMessage("'keyStorePassword' must not be null.");
    }

    @Test
    void getInstance_shouldThrowTaskwarriorSslContextExceptionIfProtocolIsInvalid() throws KeyStoreException {
        KeyStore keyStore = KeyStore.Builder.newInstance(KeyStore.getDefaultType(), null, new PasswordProtection("test".toCharArray())).getKeyStore();
        assertThatThrownBy(() -> SslContextFactory.getInstance("invalid", keyStore, "test"))
                .isInstanceOf(TaskwarriorSslContextException.class)
                .hasMessage("Cannot create SSL context for protocol 'invalid'.")
                .hasCauseInstanceOf(NoSuchAlgorithmException.class);
    }

    @Test
    void getInstance_should() throws KeyStoreException {
        KeyStore keyStore = KeyStore.Builder.newInstance(KeyStore.getDefaultType(), null, new PasswordProtection("test".toCharArray())).getKeyStore();
        SSLContext sslContext = SslContextFactory.getInstance("SSL", keyStore, "test");

        assertThat(sslContext).isNotNull();
    }
}
@@ -1156,128 +1156,37 @@
 e = 
-KeyStore.Builder.newInstance(KeyStore.getDefaultType(), null, new PasswordProtection(%22test%22.toCharArray())).getKeyStore(
+createDefaultKeyStore(%22test1%22
 );%0A 
@@ -1558,128 +1558,37 @@
 e = 
-KeyStore.Builder.newInstance(KeyStore.getDefaultType(), null, new PasswordProtection(%22test%22.toCharArray())).getKeyStore(
+createDefaultKeyStore(%22test2%22
 );%0A 
@@ -2003,128 +2003,37 @@
 e = 
-KeyStore.Builder.newInstance(KeyStore.getDefaultType(), null, new PasswordProtection(%22test%22.toCharArray())).getKeyStore(
+createDefaultKeyStore(%22test3%22
 );%0A 
@@ -2169,10 +2169,251 @@
 );%0A    %7D
+%0A%0A    private static KeyStore createDefaultKeyStore(String password) throws KeyStoreException %7B%0A        return KeyStore.Builder.newInstance(KeyStore.getDefaultType(), null, new PasswordProtection(password.toCharArray())).getKeyStore();%0A    %7D
 %0A%7D

remove redundant code