/*
 * Copyright (c) eHealth
 */
package be.fgov.ehealth.technicalconnector.signature.domain;

import be.ehealth.technicalconnector.service.timestamp.TimestampUtil;
import be.ehealth.technicalconnector.utils.ConnectorIOUtils;
import be.fgov.ehealth.technicalconnector.test.tests.utils.LoggingUtils;
import lombok.extern.slf4j.Slf4j;
import org.junit.BeforeClass;
import org.junit.Test;

import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.concurrent.TimeUnit;

import static be.fgov.ehealth.technicalconnector.test.tests.utils.AssertTools.assertSerializable;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;


/**
 * Class to verify the {@link SignatureVerificationResult}
 * 
 * @author EHP
 */
@Slf4j
public class SignatureVerificationResultTest {

    private static final Instant signingTime = Instant.now().minus(5, ChronoUnit.DAYS);

    @BeforeClass
    public static void init() {
        LoggingUtils.bootstrap();
    }

    @Test
    public void isSerializable() throws Exception {
        SignatureVerificationResult expected = new SignatureVerificationResult();
        byte[] tsToken = ConnectorIOUtils.getResourceAsByteArray("/examples/tstoken.base64");

        expected.getTsTokens().add(TimestampUtil.getTimestamp(ConnectorIOUtils.base64Decode(tsToken, false)));
        assertSerializable(expected, true);

    }

    @Test
    public void timestampBeforeSigningTimeWithinTollerance() throws Exception {
        SignatureVerificationResult result = new SignatureVerificationResult();
        result.setSigningTime(signingTime);
        result.getTimestampGenTimes().add(signingTime.plus(3, ChronoUnit.MINUTES));
        Instant verifiedSigningTime = result.getVerifiedSigningTime(5, TimeUnit.MINUTES);
        assertTrue(verifiedSigningTime.equals(signingTime));
    }

    @Test
    public void timestampAfterSigningTimeWithinTollerance() throws Exception {
        SignatureVerificationResult result = new SignatureVerificationResult();
        result.setSigningTime(signingTime);
        result.getTimestampGenTimes().add(signingTime.plus(3, ChronoUnit.MINUTES));
        Instant verifiedSigningTime = result.getVerifiedSigningTime(5, TimeUnit.MINUTES);
        assertTrue(verifiedSigningTime.equals(signingTime));
    }

    @Test
    public void timestampBeforeSigningTimeNotInTollerance() throws Exception {
        SignatureVerificationResult result = new SignatureVerificationResult();
        result.setSigningTime(signingTime);
        result.getTimestampGenTimes().add(signingTime.minus(10, ChronoUnit.MINUTES));
        Instant verifiedSigningTime = result.getVerifiedSigningTime(5, TimeUnit.MINUTES);
        assertFalse(verifiedSigningTime.equals(signingTime));
    }

    @Test
    public void timestampAfterSigningTimeNotInTollerance() throws Exception {
        SignatureVerificationResult result = new SignatureVerificationResult();
        result.setSigningTime(signingTime);
        result.getTimestampGenTimes()
                .add(signingTime.plus(10, ChronoUnit.MINUTES));
        Instant verifiedSigningTime = result.getVerifiedSigningTime(5, TimeUnit.MINUTES);
        assertFalse(verifiedSigningTime.equals(signingTime));
    }




}
