/*
 * Copyright 2016-2017 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package de.vorb.platon.security;

import org.junit.Test;

import java.time.Instant;
import java.util.Base64;

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

public class SignatureComponentsTest {

    private static final String SAMPLE_IDENTIFIER = "comment/1";
    private static final Instant SAMPLE_EXPIRATION_TIME = Instant.parse("2017-10-20T19:06:07.105Z");
    private static final byte[] SAMPLE_SIGNATURE_TOKEN = new byte[]{
            (byte) 240, (byte) 61, (byte) 193, (byte) 104, (byte) 80, (byte) 251, (byte) 37, (byte) 225, (byte) 82,
            (byte) 6, (byte) 173, (byte) 6, (byte) 212, (byte) 8, (byte) 119, (byte) 201, (byte) 3, (byte) 68,
            (byte) 54, (byte) 161, (byte) 4, (byte) 152, (byte) 72, (byte) 26, (byte) 113, (byte) 93, (byte) 91,
            (byte) 216, (byte) 106, (byte) 247, (byte) 2, (byte) 232
    };

    private static final String SAMPLE_SIGNATURE = SAMPLE_IDENTIFIER + '|' + SAMPLE_EXPIRATION_TIME + '|' +
            Base64.getEncoder().encodeToString(SAMPLE_SIGNATURE_TOKEN);

    @Test
    public void fromStringParsesComponents() throws Exception {

        final SignatureComponents signatureComponents = SignatureComponents.fromString(SAMPLE_SIGNATURE);

        assertThat(signatureComponents.getIdentifier()).isEqualTo(SAMPLE_IDENTIFIER);
        assertThat(signatureComponents.getExpirationTime()).isEqualTo(SAMPLE_EXPIRATION_TIME);
        assertThat(signatureComponents.getSignatureToken()).isEqualTo(SAMPLE_SIGNATURE_TOKEN);
    }

    @Test
    public void toStringConcatenatesComponentsUsingPipe() throws Exception {

        final SignatureComponents signatureComponents = SignatureComponents.fromString(SAMPLE_SIGNATURE);

        final String signature = signatureComponents.toString();

        assertThat(signature).isEqualTo(SAMPLE_SIGNATURE);
    }

}

@@ -698,16 +698,64 @@
 nstant;%0A
+import java.time.format.DateTimeParseException;%0A
 import j
@@ -829,16 +829,89 @@
 ertThat;
+%0Aimport static org.assertj.core.api.Assertions.assertThatExceptionOfType;
 %0A%0Apublic
@@ -1720,65 +1720,29 @@
     
-Base64.getEncoder().encodeToString(SAMPLE_SIGNATURE_TOKEN
+encodeSignatureToken(
 );%0A%0A
@@ -2533,10 +2533,848 @@
 %0A    %7D%0A%0A
+    @Test%0A    public void throwsIllegalArgumentExceptionWhenComponentCountInvalid() throws Exception %7B%0A%0A        assertThatExceptionOfType(IllegalArgumentException.class)%0A                .isThrownBy(() -%3E SignatureComponents.fromString(%22a%7Cb%22));%0A%0A        assertThatExceptionOfType(IllegalArgumentException.class)%0A                .isThrownBy(() -%3E SignatureComponents.fromString(%22a%7Cb%7Cc%7Cd%22));%0A    %7D%0A%0A    @Test%0A    public void throwsDateExceptionWhenDateIsNotParsable() throws Exception %7B%0A%0A        assertThatExceptionOfType(DateTimeParseException.class)%0A                .isThrownBy(() -%3E%0A                        SignatureComponents.fromString(SAMPLE_IDENTIFIER + %22%7C1.2.2017%7C%22 + encodeSignatureToken()));%0A    %7D%0A%0A    private static String encodeSignatureToken() %7B%0A        return Base64.getEncoder().encodeToString(SAMPLE_SIGNATURE_TOKEN);%0A    %7D%0A
 %7D%0A

Test exceptions in SignatureComponents