/*
 * Copyright 2018 Scytl Secure Electronic Voting SA
 *
 * All rights reserved
 *
 * See our extended copyright notice in *file 'Copyright.txt' which is part of this source code package
 */
package com.scytl.products.ov.ei.services.domain.model.rule;

import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.when;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.slf4j.Logger;

import com.scytl.cryptolib.api.exceptions.GeneralCryptoLibException;
import com.scytl.products.ov.commons.beans.domain.model.vote.Vote;
import com.scytl.products.ov.commons.beans.exceptions.ResourceNotFoundException;
import com.scytl.products.ov.commons.beans.validation.ValidationErrorType;
import com.scytl.products.ov.commons.validation.ValidationError;
import com.scytl.products.ov.ei.services.domain.model.content.ElectionInformationContent;
import com.scytl.products.ov.ei.services.domain.model.content.ElectionInformationContentRepository;

/**
 *
 */
@RunWith(MockitoJUnitRunner.class)
public class VoteCertificateChainValidationRuleTest {

    private String json =
        "{\"electionEventId\":\"1d942d2f7ee24150af6ab979d3c43c59\",\"electionRootCA\":\"-----BEGIN CERTIFICATE-----\\nMIIDwDCCAqigAwIBAgIVAKNLm2cx4bRtj2SrSRUyECgR9aiWMA0GCSqGSIb3DQEB\\nCwUAMIGGMUAwPgYDVQQDDDdFbGVjdGlvbiBFdmVudCBSb290IENBIDFkOTQyZDJm\\nN2VlMjQxNTBhZjZhYjk3OWQzYzQzYzU5MRYwFAYDVQQLDA1PbmxpbmUgVm90aW5n\\nMRIwEAYDVQQKDAlTd2lzc1Bvc3QxCTAHBgNVBAcMADELMAkGA1UEBhMCQ0gwHhcN\\nMTUxMjIyMTQwNzAxWhcNMTYxMjEwMTAwMDAwWjCBhjFAMD4GA1UEAww3RWxlY3Rp\\nb24gRXZlbnQgUm9vdCBDQSAxZDk0MmQyZjdlZTI0MTUwYWY2YWI5NzlkM2M0M2M1\\nOTEWMBQGA1UECwwNT25saW5lIFZvdGlu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NH8jxZS+2/V0ms4Ku4Sa3t2dGnhRTpY+s\\n6g+HstsgZmMNsvFmYvETK1vr++X6Dek7Sjxl8i5CjUJ9oDmA\\n-----END CERTIFICATE-----\\n\",\"electionInformationParams\":{\"numVotesPerVotingCard\":\"1\",\"numVotesPerAuthToken\":\"1\"},\"signature\":null}";

    private String cert =
        "-----BEGIN CERTIFICATE-----\r\nMIIDoTCCAomgAwIBAgIVAMsl9BPSnbNr/aHfOEtYSN9QyBiHMA0GCSqGSIb3DQEB\r\nCwUAMH4xODA2BgNVBAMML0NyZWRlbnRpYWxzIENBIDFkOTQyZDJmN2VlMjQxNTBh\r\nZjZhYjk3OWQzYzQzYzU5MRYwFAYDVQQLDA1PbmxpbmUgVm90aW5nMRIwEAYDVQQK\r\nDAlTd2lzc1Bvc3QxCTAHBgNVBAcMADELMAkGA1UEBhMCQ0gwHhcNMTUxMjIyMTQw\r\nNzM0WhcNMTYxMjEwMTAwMDAwWjB0MS4wLAYDVQQDDCVTaWduIDUwMWM5NDc4YTg2\r\nNTQxYmI5OGYwZDk3YjljZTYzNmIyMRYwFAYDVQQLDA1PbmxpbmUgVm90aW5nMRIw\r\nEAYDVQQKDAlTd2lzc1Bvc3QxCTAHBgNVBAcMADELMAkGA1UEBhMCQ0gwggEiMA0G\r\nCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQCr2jXbPMBAvq01l38iqyRHC8IGihGe\r\npvsIGU5yK5KeNXv3RqyQkTt1NJjBOwItAiFXVpZmFpEPlPqATYznPUC91bMV7vax\r\n+/+ZdSFAyVoj3DYbyWNvIGW9Abja5Oz/fRG3bOC1Ok0cmJb32a9uR7NnH2M8ik2/\r\n7J4xxvjs1L9o02+U8o9JO1Idw0blz3mQj0DYaEadTPXKDUiA0fQ6tv87A728OSnN\r\ndh9mgcy62+PW/S6kX9PnlYQoJ0nguSCHotmOSmG/OVlsa6V6pg7dvf5asmNE8+mu\r\nkD3fSyVH+HQVQIRv/xF2wDEQf/vvLEcs7Ba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mFIMLqhidzZtPAJ9+qjYIpL0nvVym7/juCxJ8yxuzYVLvx2oAGTKu6BYyEcM\r\n3OKF4mKx6A/8lW4gIISUjxhy47MyEVfqhBLiVvk0WsqVeIYF\r\n-----END CERTIFICATE-----\r\n";

    @InjectMocks
    private VoteCertificateChainValidationRule voteCertChainValidationRule = new VoteCertificateChainValidationRule();

    @Mock
    private ElectionInformationContentRepository electionInformationContentRepositoryMock;

    @Mock
    private Logger LOG;

    @Test
    public void validateOK() throws ResourceNotFoundException, GeneralCryptoLibException {
        ElectionInformationContent electionInformationContent = new ElectionInformationContent();
        electionInformationContent.setId(100);
        electionInformationContent.setTenantId("100");
        electionInformationContent.setElectionEventId("100");
        electionInformationContent.setJson(json);
        when(electionInformationContentRepositoryMock.findByTenantIdElectionEventId(anyString(), anyString()))
            .thenReturn(electionInformationContent);

        Vote vote = new Vote();
        vote.setTenantId("100");
        vote.setElectionEventId("100");
        vote.setCertificate(cert);

        ValidationError result = voteCertChainValidationRule.execute(vote);

        assertTrue(result.getValidationErrorType().equals(ValidationErrorType.SUCCESS));
    }

    @Test
    public void validateCanNotBeParsedCert() throws ResourceNotFoundException, GeneralCryptoLibException {
        ElectionInformationContent electionInformationContent = new ElectionInformationContent();
        electionInformationContent.setId(100);
        electionInformationContent.setTenantId("100");
        electionInformationContent.setElectionEventId("100");
        electionInformationContent.setJson(json);
        when(electionInformationContentRepositoryMock.findByTenantIdElectionEventId(anyString(), anyString()))
            .thenReturn(electionInformationContent);

        Vote vote = new Vote();
        vote.setTenantId("100");
        vote.setElectionEventId("100");
        vote.setCertificate(certInvalidFormat);

        ValidationError result = voteCertChainValidationRule.execute(vote);

        assertTrue(result.getValidationErrorType().equals(ValidationErrorType.FAILED));
    }

    @Test
    public void validateInvalidCert() throws ResourceNotFoundException, GeneralCryptoLibException {
        ElectionInformationContent electionInformationContent = new ElectionInformationContent();
        electionInformationContent.setId(100);
        electionInformationContent.setTenantId("100");
        electionInformationContent.setElectionEventId("100");
        electionInformationContent.setJson(json);
        when(electionInformationContentRepositoryMock.findByTenantIdElectionEventId(anyString(), anyString()))
            .thenReturn(electionInformationContent);

        Vote vote = new Vote();
        vote.setTenantId("100");
        vote.setElectionEventId("100");
        vote.setCertificate(certInvalid);

        ValidationError result = voteCertChainValidationRule.execute(vote);

        assertTrue(result.getValidationErrorType().equals(ValidationErrorType.FAILED));
    }

}
