package com.auth0.jwk;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.commons.codec.binary.Base64;
import org.junit.Test;

import java.net.URL;
import java.security.SecureRandom;
import java.util.Map;

import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;

public class JwkTest {

    private static final String RS_256 = "RS256";
    private static final String RSA = "RSA";
    private static final String SIG = "sig";
    private static final String THUMBPRINT = "THUMBPRINT";
    private static final String MODULUS = "vGChUGMTWZNfRsXxd-BtzC4RDYOMqtIhWHol--HNib5SgudWBg6rEcxvR6LWrx57N6vfo68wwT9_FHlZpaK6NXA_dWFW4f3NftfWLL7Bqy90sO4vijM6LMSE6rnl5VB9_Gsynk7_jyTgYWdTwKur0YRec93eha9oCEXmy7Ob1I2dJ8OQmv2GlvA7XZalMxAq4rFnXLzNQ7hCsHrUJP1p7_7SolWm9vTokkmckzSI_mAH2R27Z56DmI7jUkL9fLU-jz-fz4bkNg-mPz4R-kUmM_ld3-xvto79BtxJvOw5qqtLNnRjiDzoqRv-WrBdw5Vj8Pvrg1fwscfVWHlmq-1pFQ";
    private static final String EXPONENT = "AQAB";
    private static final String CERT_CHAIN = "CERT_CHAIN";

    @Test
    public void shouldBuildWithMap() throws Exception {
        final String kid = randomKeyId();
        Map<String, Object> values = publicKeyValues(kid);
        Jwk jwk = Jwk.fromValues(values);

        assertThat(jwk.getId(), equalTo(kid));
        assertThat(jwk.getAlgorithm(), equalTo(RS_256));
        assertThat(jwk.getType(), equalTo(RSA));
        assertThat(jwk.getUsage(), equalTo(SIG));
        assertThat(jwk.getCertificateThumbprint(), equalTo(THUMBPRINT));
        assertThat(jwk.getCertificateChain(), contains(CERT_CHAIN));
    }

    private static String randomKeyId() {
        byte[] bytes = new byte[50];
        new SecureRandom().nextBytes(bytes);
        return Base64.encodeBase64String(bytes);
    }

    private static Map<String, Object> publicKeyValues(String kid) {
        Map<String, Object> values = Maps.newHashMap();
        values.put("alg", RS_256);
        values.put("kty", RSA);
        values.put("use", SIG);
        values.put("x5c", Lists.newArrayList(CERT_CHAIN));
        values.put("x5t", THUMBPRINT);
        values.put("kid", kid);
        values.put("n", MODULUS);
        values.put("e", EXPONENT);
        return values;
    }
}
@@ -160,20 +160,85 @@
 g.junit.
-Test
+Rule;%0Aimport org.junit.Test;%0Aimport org.junit.rules.ExpectedException
 ;%0A%0Aimpor
@@ -351,61 +351,9 @@
 ers.
-contains;%0Aimport static org.hamcrest.Matchers.equalTo
+*
 ;%0Aim
@@ -1116,16 +1116,102 @@
 HAIN%22;%0A%0A
+    @Rule%0A    public ExpectedException expectedException = ExpectedException.none();%0A%0A
     @Tes
@@ -1772,175 +1772,1015 @@
     
-private static String randomKeyId() %7B%0A        byte%5B%5D bytes = new byte%5B50%5D;%0A        new SecureRandom().nextBytes(bytes);%0A        return Base64.encodeBase64String(bytes)
+@Test%0A    public void shouldReturnPublicKey() throws Exception %7B%0A        final String kid = randomKeyId();%0A        Map%3CString, Object%3E values = publicKeyValues(kid);%0A        Jwk jwk = Jwk.fromValues(values);%0A%0A        assertThat(jwk.getPublicKey(), notNullValue());%0A    %7D%0A%0A    @Test%0A    public void shouldReturnNullForNonRSAKey() throws Exception %7B%0A        final String kid = randomKeyId();%0A        Map%3CString, Object%3E values = nonRSAValues(kid);%0A        Jwk jwk = Jwk.fromValues(values);%0A        assertThat(jwk.getPublicKey(), nullValue());%0A    %7D%0A%0A    private static String randomKeyId() %7B%0A        byte%5B%5D bytes = new byte%5B50%5D;%0A        new SecureRandom().nextBytes(bytes);%0A        return Base64.encodeBase64String(bytes);%0A    %7D%0A%0A    private static Map%3CString, Object%3E nonRSAValues(String kid) %7B%0A        Map%3CString, Object%3E values = Maps.newHashMap();%0A        values.put(%22alg%22, %22AES_256%22);%0A        values.put(%22kty%22, %22AES%22);%0A        values.put(%22use%22, SIG);%0A        values.put(%22kid%22, kid);%0A        return values
 ;%0A  

Add more tests