// Path: src/main/java/wdawson/samples/dropwizard/filters/TLSCertificateAuthorizationFilter.java
// @VisibleForTesting
// static final String X509_CERTIFICATE_ATTRIBUTE = "javax.servlet.request.X509Certificate";


import org.junit.After;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.core.Response;
import java.io.FileInputStream;
import java.security.KeyStore;
import java.security.cert.Certificate;
import java.security.cert.X509Certificate;
import static io.dropwizard.testing.ResourceHelpers.resourceFilePath;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.reset;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyZeroInteractions;
import static org.mockito.Mockito.when;
import static wdawson.samples.dropwizard.filters.TLSCertificateAuthorizationFilter.X509_CERTIFICATE_ATTRIBUTE;

    @BeforeClass
    public static void setupClass() throws Exception {
        homepageCertificateChain = getCertificateChainFromKeyStore("tls/homepage-service-keystore.jks");
        eventCertificateChain = getCertificateChainFromKeyStore("tls/event-service-keystore.jks");
    }

    @After
    public void teardown() {
        reset(servletRequest, requestContext);
    }

    private static X509Certificate[] getCertificateChainFromKeyStore(String keyStoreResource) throws Exception {
        KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
        keyStore.load(new FileInputStream(resourceFilePath(keyStoreResource)), PASSPHRASE);
        Certificate[] certificateChain = keyStore.getCertificateChain("service-tls");

        X509Certificate[] x509CertificateChain = new X509Certificate[certificateChain.length];

        for (int i = 0; i < certificateChain.length; ++i) {
            assertThat(certificateChain[i]).isInstanceOf(X509Certificate.class);
            x509CertificateChain[i] = (X509Certificate) certificateChain[i];
        }
        return x509CertificateChain;
    }

    @Test
    public void filterPassesWhenSubjectMatchesRegex() throws Exception {
        TLSCertificateAuthorizationFilter sut = new TLSCertificateAuthorizationFilter(REG_EX, servletRequest);

// Path: src/main/java/wdawson/samples/dropwizard/filters/TLSCertificateAuthorizationFilter.java
// @VisibleForTesting
// static final String X509_CERTIFICATE_ATTRIBUTE = "javax.servlet.request.X509Certificate";


// Path: src/test/java/wdawson/samples/dropwizard/filters/TLSCertificateAuthorizationFilterTest.java
import org.junit.After;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.core.Response;
import java.io.FileInputStream;
import java.security.KeyStore;
import java.security.cert.Certificate;
import java.security.cert.X509Certificate;
import static io.dropwizard.testing.ResourceHelpers.resourceFilePath;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.reset;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyZeroInteractions;
import static org.mockito.Mockito.when;
import static wdawson.samples.dropwizard.filters.TLSCertificateAuthorizationFilter.X509_CERTIFICATE_ATTRIBUTE;


    @BeforeClass
    public static void setupClass() throws Exception {
        homepageCertificateChain = getCertificateChainFromKeyStore("tls/homepage-service-keystore.jks");
        eventCertificateChain = getCertificateChainFromKeyStore("tls/event-service-keystore.jks");
    }

    @After
    public void teardown() {
        reset(servletRequest, requestContext);
    }

    private static X509Certificate[] getCertificateChainFromKeyStore(String keyStoreResource) throws Exception {
        KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
        keyStore.load(new FileInputStream(resourceFilePath(keyStoreResource)), PASSPHRASE);
        Certificate[] certificateChain = keyStore.getCertificateChain("service-tls");

        X509Certificate[] x509CertificateChain = new X509Certificate[certificateChain.length];

        for (int i = 0; i < certificateChain.length; ++i) {
            assertThat(certificateChain[i]).isInstanceOf(X509Certificate.class);
            x509CertificateChain[i] = (X509Certificate) certificateChain[i];
        }
        return x509CertificateChain;
    }

    @Test
    public void filterPassesWhenSubjectMatchesRegex() throws Exception {
        TLSCertificateAuthorizationFilter sut = new TLSCertificateAuthorizationFilter(REG_EX, servletRequest);

        when(servletRequest.getAttribute(X509_CERTIFICATE_ATTRIBUTE)).thenReturn(homepageCertificateChain);