package com.clouway.oauth2.jws;

import org.junit.Test;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Collections;

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

/**
 * @author Miroslav Genov (miroslav.genov@clouway.com)
 */
public class ReadPemFilesTest {
  private static final byte[] EXPECTED_BYTES = {48, -126, 4, -68, 2, 1, 0, 48, 13, 6, 9, 42, -122, 72, -122, -9, 13, 1, 1, 1, 5, 0, 4, -126, 4, -90, 48, -126, 4, -94, 2, 1, 0, 2, -126, 1, 1, 0, -120, -127, -83, 85, 5, 94, 82, -7, -40, -119, -39, -90, -31, -31, -123, 71, 1, 102, -5, -82, 83, 61, -90, 114, -75, 25, 86, 78, 96, 78, -79, -123, -30, 117, -114, -54, 49, -121, 77, -27, 80, 120, -16, -16, 123, 65, 71, 101, 18, 32, 5, -122, -43, -81, -28, 0, 7, 115, 126, 120, 43, 20, 91, -99, -92, -29, -71, 69, 77, 114, 16, 89, -13, -39, 99, 112, -53, -72, 35, -33, 27, -75, 122, -70, -83, -58, 103, 45, 58, 76, -122, 3, 104, 95, 1, 60, -57, 81, -12, -83, 74, 95, 32, -56, -59, -54, 127, 113, -108, 69, -18, 38, -7, -113, -22, 107, 89, -56, -48, 41, 70, -84, -107, -101, 21, -45, 61, -125, 63, 88, -115, 23, -9, -84, 2, 108, 97, -108, 117, -19, -47, -78, 5, 18, 29, -108, -57, -118, 23, -12, 46, 116, -2, -68, 14, -1, -10, -3, -5, 44, 58, 26, -56, -49, -108, 89, -105, 106, 100, -94, -11, -104, -119, -42, 113, 121, 16, 101, -11, 116, -67, -99, -86, 20, 100, -49, 7, -66, -86, 50, 81, 49, 59, 7, 40, -17, 12, 20, 125, -88, 25, 18, -104, 37, -55, 1, 70, -4, -37, -14, -28, 64, 82, -31, -71, -8, 103, 28, 36, -126, -15, -65, -92, 29, 106, 65, -122, 35, 42, -78, 85, 103, 71, 77, 38, -122, 36, 7, 9, 110, -49, 54, -46, -33, 111, -85, -96, 77, -109, 98, 113, -16, 72, 104, -115, 67, 2, 3, 1, 0, 1, 2, -126, 1, 0, 19, 5, -25, 78, -38, -31, -85, 52, -112, 61, -99, -84, 72, -4, 49, -93, -108, -119, 114, 119, 119, -72, 54, -78, -13, -109, -27, 88, 36, -55, -78, -87, -110, -58, -119, 48, -102, 56, 93, -35, -96, 26, 74, -111, 104, -53, -49, 20, 65, -77, 94, -79, -52, -93, -89, 101, 109, -92, 101, -78, 127, 102, 101, -30, 17, 49, 127, 7, 46, 100, -65, -20, -103, 51, -23, 76, -75, -57, 34, -112, -101, -2, -19, 108, -120, 125, 6, -79, -31, -42, -6, 50, 13, 96, 119, -2, -83, 84, 57, -127, -48, -60, 115, 0, 39, -72, 49, -116, 117, 122, 77, 86, -9, 30, 92, 47, -69, -94, 101, -121, -21, 6, 112, 14, -35, -70, -61, 110, -117, 39, 125, 127, 104, 124, 47, -114, -71, 72, 109, 10, -72, 60, 4, -76, -128, 8, 122, 8, 117, 32, -55, -29, -81, -16, -55, 2, 71, -102, -43, -123, 47, 67, 111, 77, -44, -23, 1, 81, 115, -65, 66, 48, -56, 33, -105, -35, 58, 11, -38, -22, 70, 48, -26, 33, 125, -52, 18, 31, -56, -119, 111, -31, 39, 57, -77, -14, 67, -51, 47, 84, 98, 81, -66, 78, 35, 110, 28, 120, 59, 86, 107, 96, 51, -68, 102, -91, -64, 52, -76, 74, -5, 82, -15, 32, 83, -10, 50, 52, -122, -46, -91, -16, -47, -104, -22, -119, 39, 126, 74, 41, -61, -94, -18, 86, 54, 118, -124, -25, 96, 87, 124, -10, -24, 50, 118, 97, 2, -127, -127, 0, -37, 82, 80, 39, -87, -44, -117, -82, -36, -34, -92, -90, 77, 60, -108, 42, 54, 0, -74, 1, 53, 21, -66, -105, 4, -21, -116, -34, -29, -53, 124, -79, 94, 101, 86, -57, 92, 120, -71, 101, -51, 92, 67, -46, 24, -31, -22, 120, 42, -54, 112, -58, 38, 60, -23, -8, 1, 28, 75, -110, -48, -100, 38, 34, -99, 86, -12, -57, -56, -120, 102, 80, -71, 116, -125, -61, 63, -50, 102, 33, -10, 0, 68, 81, 71, 23, 109, 3, 15, 65, -24, 48, -105, -36, -58, -89, -98, 38, -58, -108, -37, -125, -28, -121, -74, 60, 87, -16, 66, 106, -50, 3, 51, -64, 114, 58, -73, -127, -109, 102, -65, 85, -106, -116, 88, 15, 87, -13, 2, -127, -127, 0, -97, 85, -38, -121, 5, -26, 67, 57, 42, 7, 60, 118, 125, 2, -109, 44, 28, 6, 9, -102, -97, -12, 48, 28, -17, -67, 7, 90, -30, 86, 94, -122, 98, 125, 29, -25, -21, -28, -99, -19, -109, -121, 56, -60, 127, -123, 56, 48, 4, -87, -91, -121, -96, 87, -66, 99, -34, 87, -71, 30, -67, -58, 65, -94, 6, 34, 7, -106, 19, 10, -41, 20, -128, -44, 105, -89, -7, 101, -113, 78, 56, 79, 112, -38, -122, 89, 119, -34, 78, 115, 25, 53, 104, 25, -126, -22, 59, 123, 93, 31, 28, -43, -112, 79, 100, 100, -107, -24, 9, 124, 30, 125, -36, 78, 10, -13, 118, 62, -92, 111, -101, -31, -89, 98, 28, -60, 25, 113, 2, -127, -128, 43, -120, -10, 82, 57, 58, -16, -109, 64, 82, -115, -102, 89, 64, -118, 36, -86, -35, -113, 67, 68, 23, 81, -30, -58, 94, 63, 115, 75, 2, 108, 121, 4, -60, -42, -23, -15, -24, 32, -35, 29, 91, -112, 54, -41, 43, -103, 16, 75, 102, 45, -26, -105, 86, -122, 22, -19, -103, -127, 66, -34, 87, -16, -107, -12, 103, 123, 101, 96, -33, -6, 119, 81, -88, -40, 50, -36, -86, -118, -55, 120, -4, 106, -109, 59, -86, 97, -64, 54, -70, 29, 109, -17, -25, 42, 35, -63, -105, -18, -39, 67, -115, -75, -62, 94, -95, 49, -49, -122, 79, -113, 31, 65, -103, -108, -45, 36, -13, -6, -97, 109, -95, 126, -91, -45, 33, 16, -109, 2, -127, -128, 11, -113, 124, 26, 125, -40, 22, 26, -120, -83, 27, -70, -15, 25, 5, -70, -106, -111, -19, -110, 117, 118, -113, 35, 26, 85, -39, -71, -61, 96, 70, -35, 115, 57, -26, -72, -108, 9, -13, 87, 112, 115, -56, 47, -95, -100, -11, -43, -82, 16, -20, -4, -87, 42, -108, -102, -60, 96, 19, 3, 98, -5, -122, 121, -115, -2, 92, 20, -116, 111, 37, -23, -53, -21, 29, 123, 93, -127, -5, -30, -51, -3, 29, -112, 13, 93, 98, 112, 19, -69, 16, 34, 42, -38, 103, 0, 61, -82, -106, -76, 18, 48, -128, -106, -14, 14, -58, 61, -98, -86, -115, -65, -50, 3, 47, -110, 56, -117, 20, -3, 48, 15, 48, 72, -45, 19, 123, 81, 2, -127, -128, 119, -37, -45, -120, 83, -55, -56, -90, 126, 10, 64, 56, -123, -108, -41, 96, 84, 79, -123, -40, -114, -33, -29, 51, 109, 51, -13, 64, 120, -86, 6, -19, 97, -104, -24, 35, -74, -80, 105, -118, 42, -12, -5, -125, -108, 37, -25, -7, -101, -50, -54, 48, -109, 54, -117, 54, -68, -47, -47, 20, 13, 19, 46, 107, 34, -64, -121, 87, -126, 39, 32, 118, 39, 32, -4, 110, -59, 61, -89, 88, -3, -9, -57, 90, -27, -78, -114, -23, 7, -100, 83, 127, -102, -76, 121, -17, -70, -90, -51, -71, -51, 105, -127, 124, 70, 37, 51, -67, -99, 60, 80, -94, 123, 94, -125, 116, -96, 72, -52, -26, -125, -73, -60, -95, -107, -71, 66, -10};

  @Test
  public void happyPath() throws IOException {
    Pem pem = new Pem();
    Pem.Block block = pem.parse(new FileInputStream("oauth2-server/src/test/java/com/clouway/oauth2/jws/testdata/secret.pem"));
    assertThat(block, is(equalTo(new Pem.Block("PRIVATE KEY", Collections.<String, String>emptyMap(), EXPECTED_BYTES))));
  }

}