/*
 * 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.service.ballotbox;

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

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

import com.scytl.products.ov.commons.beans.exceptions.EntryPersistenceException;
import com.scytl.products.ov.commons.beans.exceptions.ResourceNotFoundException;
import com.scytl.products.ov.ei.services.domain.model.ballotbox.BallotBoxInformation;
import com.scytl.products.ov.ei.services.domain.model.ballotbox.BallotBoxInformationRepository;
import com.scytl.products.ov.ei.services.domain.model.ballotbox.BallotBoxStatus;

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

    @Mock
    private BallotBoxInformationRepository ballotBoxInformationRepository;

    @Rule
    public ExpectedException expectedException = ExpectedException.none();

    @InjectMocks
    private BallotBoxInformationService ballotBoxInformationService = new BallotBoxInformationService();

    @Test
    public void isBallotBoxForTest() throws ResourceNotFoundException {
        String tenantId = "100";
        String electionEventId = "18aeec427aac47219d656421b864a649";
        String ballotBoxId = "08c7224118ce4daeaac8725f104765a0";
        String json =
            "{\r\n  \"id\" : \"7271439c37af4c4ca6ff28ab1dd4465d\",\r\n  \"startDate\" : \"2016-03-20T23:00Z\",\r\n  \"endDate\" : \"2016-12-20T23:00Z\",\r\n  \"encryptionParameters\" : {\r\n    \"g\" : \"2\",\r\n    \"p\" : \"16370518994319586760319791526293535327576438646782139419846004180837103527129035954742043590609421369665944746587885814920851694546456891767644945459124422553763416586515339978014154452159687109161090635367600349264934924141746082060353483306855352192358732451955232000593777554431798981574529854314651092086488426390776811367125009551346089319315111509277347117467107914073639456805159094562593954195960531136052208019343392906816001017488051366518122404819967204601427304267380238263913892658950281593755894747339126531018026798982785331079065126375455293409065540731646939808640273393855256230820509217411510058759\",\r\n    \"q\" : \"8185259497159793380159895763146767663788219323391069709923002090418551763564517977371021795304710684832972373293942907460425847273228445883822472729562211276881708293257669989007077226079843554580545317683800174632467462070873041030176741653427676096179366225977616000296888777215899490787264927157325546043244213195388405683562504775673044659657555754638673558733553957036819728402579547281296977097980265568026104009671696453408000508744025683259061202409983602300713652133690119131956946329475140796877947373669563265509013399491392665539532563187727646704532770365823469904320136696927628115410254608705755029379\"\r\n  },\r\n  \"electoralAuthorityId\" : \"487e364e8d0d47fd94f57aac14abdd48\",\r\n  \"eeid\" : \"18aeec427aac47219d656421b864a649\",\r\n  \"bid\" : \"08c7224118ce4daeaac8725f104765a0\",\r\n  \"ballotBoxCert\" : \"-----BEGIN CERTIFICATE-----\\r\\nMIIDozCCAougAwIBAgIVAMIXJTOdeJBJf0YMGVg2FzEr4GjgMA0GCSqGSIb3DQEB\\r\\nCwUAMHsxNTAzBgNVBAMMLFNlcnZpY2VzIENBIDE4YWVlYzQyN2FhYzQ3MjE5ZDY1\\r\\nNjQyMWI4NjRhNjQ5MRYwFAYDVQQLDA1PbmxpbmUgVm90aW5nMRIwEAYDVQQKDAlT\\r\\nd2lzc1Bvc3QxCTAHBgNVBAcMADELMAkGA1UEBhMCQ0gwHhcNMTYwMzIxMTEyMTU0\\r\\nWhcNMTcxMjIwMjMwMDAwWjB5MTMwMQYDVQQDDCpCYWxsb3RCb3ggNzI3MTQzOWMz\\r\\nN2FmNGM0Y2E2ZmYyOGFiMWRkNDQ2NWQxFjAUBgNVBAsMDU9ubGluZSBWb3Rpbmcx\\r\\nEjAQBgNVBAoMCVN3aXNzUG9zdDEJMAcGA1UEBwwAMQswCQYDVQQGEwJDSDCCASIw\\r\\nDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAK37vy3N2Gz2QYOQ+SfEkp9aLZiM\\r\\nnJIsogN0SD0fXc5qZuzf1TYRj2F+waU708xYkDzhIuifmNMPGLX35xN1pe0CIOfs\\r\\nrTwLj9xmvTqtP7ZIefII5kldgIsEsQU6qemQX7YAl2IZs/RQIdVlK82QnWfN1Rj4\\r\\nHEinKYd675WefGdI3wKHsRMraAvYxnMMP3lXe3690fa96UqhTHOvnATShki6Uzdj\\r\\ndVKIF9SQ6GRKp8R9uHUxPTZ5y1XbUA/TJ14o6RtST2/CJTj/Zfma4Qgodw9CSNYp\\r\\nEc/8/nG4fTcT/eURqojs/h/vpFwHXlHlzDdOvadN4Hg3bzeaiiGvK2BWM4ECAwEA\\r\\nAaMgMB4wDgYDVR0PAQH/BAQDAgbAMAwGA1UdEwEB/wQCMAAwDQYJKoZIhvcNAQEL\\r\\nBQADggEBANMqAM6ihvsXgM8p0Wne3A1MypDtuSXCGfZLub6rMOJQxDLDHxiz8cY7\\r\\nqrGiaHqKqIQ6NydPCRaMGhToV8EnylO1/Lx1KD+vM2EOTG3sNB+Ho87AydY6wJ+n\\r\\np1GD7bs0rVeOMkHgY8XPPbhaYrrThTbWYjeGEl2qS0Pctztc76KCLH9qhZfxM6qt\\r\\nmtZTG76mans84Op5fE8GbdtLtG5UoleltHG9MIPbyOmeQmoWg/p0s0J/OxkFcKP8\\r\\nRhyxcqlKri1TafUNjY5XlmVJyLYnGwtkJi7G6pQweTk1A8sGJYWmUrQaLQuJvt72\\r\\nUeC4zZaWL7AFIrvLurCJjAB31lV/Vyo=\\r\\n-----END CERTIFICATE-----\\r\\n\",\r\n  \"test\" : true\r\n}";

        BallotBoxInformation bbi = new BallotBoxInformation();
        bbi.setTenantId(tenantId);
        bbi.setElectionEventId(electionEventId);
        bbi.setBallotBoxId(ballotBoxId);
        bbi.setJson(json);
        when(ballotBoxInformationRepository.findByTenantIdElectionEventIdBallotBoxId(tenantId, electionEventId,
            ballotBoxId)).thenReturn(bbi);

        assertTrue(ballotBoxInformationService.isBallotBoxForTest(tenantId, electionEventId, ballotBoxId));
    }

    @Test
    public void isNotABallotBoxForTest() throws ResourceNotFoundException {
        String tenantId = "100";
        String electionEventId = "18aeec427aac47219d656421b864a649";
        String ballotBoxId = "08c7224118ce4daeaac8725f104765a0";
        String json =
            "{\r\n  \"id\" : \"7271439c37af4c4ca6ff28ab1dd4465d\",\r\n  \"startDate\" : \"2016-03-20T23:00Z\",\r\n  \"endDate\" : \"2016-12-20T23:00Z\",\r\n  \"encryptionParameters\" : {\r\n    \"g\" : \"2\",\r\n    \"p\" : \"16370518994319586760319791526293535327576438646782139419846004180837103527129035954742043590609421369665944746587885814920851694546456891767644945459124422553763416586515339978014154452159687109161090635367600349264934924141746082060353483306855352192358732451955232000593777554431798981574529854314651092086488426390776811367125009551346089319315111509277347117467107914073639456805159094562593954195960531136052208019343392906816001017488051366518122404819967204601427304267380238263913892658950281593755894747339126531018026798982785331079065126375455293409065540731646939808640273393855256230820509217411510058759\",\r\n    \"q\" : \"8185259497159793380159895763146767663788219323391069709923002090418551763564517977371021795304710684832972373293942907460425847273228445883822472729562211276881708293257669989007077226079843554580545317683800174632467462070873041030176741653427676096179366225977616000296888777215899490787264927157325546043244213195388405683562504775673044659657555754638673558733553957036819728402579547281296977097980265568026104009671696453408000508744025683259061202409983602300713652133690119131956946329475140796877947373669563265509013399491392665539532563187727646704532770365823469904320136696927628115410254608705755029379\"\r\n  },\r\n  \"electoralAuthorityId\" : \"487e364e8d0d47fd94f57aac14abdd48\",\r\n  \"eeid\" : \"18aeec427aac47219d656421b864a649\",\r\n  \"bid\" : \"08c7224118ce4daeaac8725f104765a0\",\r\n  \"ballotBoxCert\" : \"-----BEGIN CERTIFICATE-----\\r\\nMIIDozCCAougAwIBAgIVAMIXJTOdeJBJf0YMGVg2FzEr4GjgMA0GCSqGSIb3DQEB\\r\\nCwUAMHsxNTAzBgNVBAMMLFNlcnZpY2VzIENBIDE4YWVlYzQyN2FhYzQ3MjE5ZDY1\\r\\nNjQyMWI4NjRhNjQ5MRYwFAYDVQQLDA1PbmxpbmUgVm90aW5nMRIwEAYD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\nRhyxcqlKri1TafUNjY5XlmVJyLYnGwtkJi7G6pQweTk1A8sGJYWmUrQaLQuJvt72\\r\\nUeC4zZaWL7AFIrvLurCJjAB31lV/Vyo=\\r\\n-----END CERTIFICATE-----\\r\\n\",\r\n  \"test\" : false\r\n}";

        BallotBoxInformation bbi = new BallotBoxInformation();
        bbi.setTenantId(tenantId);
        bbi.setElectionEventId(electionEventId);
        bbi.setBallotBoxId(ballotBoxId);
        bbi.setJson(json);
        when(ballotBoxInformationRepository.findByTenantIdElectionEventIdBallotBoxId(tenantId, electionEventId,
            ballotBoxId)).thenReturn(bbi);

        assertFalse(ballotBoxInformationService.isBallotBoxForTest(tenantId, electionEventId, ballotBoxId));
    }

    @Test
    public void blockBallotBox() throws ResourceNotFoundException, EntryPersistenceException {

        BallotBoxInformation ballotBoxInformation = fillBallotBoxInformation();
        BallotBoxInformation ballotBoxInformationBlocked = fillBallotBoxInformation();
        ballotBoxInformationBlocked.setStatus(BallotBoxStatus.BLOCKED);
        when(ballotBoxInformationRepository.findByTenantIdElectionEventIdBallotBoxId(anyString(), anyString(),
            anyString())).thenReturn(ballotBoxInformation);
        when(ballotBoxInformationRepository.update(ballotBoxInformation)).thenReturn(ballotBoxInformationBlocked);

        BallotBoxInformation result =
            ballotBoxInformationService.changeBallotBoxStatus("1", "1", "1", BallotBoxStatus.BLOCKED);
        assertTrue(BallotBoxStatus.BLOCKED.equals(result.getStatus()));
    }

    @Test
    public void blockNonExistingBallotBox() throws ResourceNotFoundException, EntryPersistenceException {

        expectedException.expect(ResourceNotFoundException.class);
        when(ballotBoxInformationRepository.findByTenantIdElectionEventIdBallotBoxId(anyString(), anyString(),
            anyString())).thenThrow(new ResourceNotFoundException("exception"));
        ballotBoxInformationService.changeBallotBoxStatus("1", "1", "1", BallotBoxStatus.BLOCKED);

    }

    @Test
    public void unblockBallotBox() throws ResourceNotFoundException , EntryPersistenceException{

        BallotBoxInformation ballotBoxInformation = fillBallotBoxInformation();
        BallotBoxInformation ballotBoxInformationUnblocked = fillBallotBoxInformation();
        ballotBoxInformationUnblocked.setStatus(BallotBoxStatus.NONE);
        when(ballotBoxInformationRepository.findByTenantIdElectionEventIdBallotBoxId(anyString(), anyString(),
                anyString())).thenReturn(ballotBoxInformation);
        when(ballotBoxInformationRepository.update(ballotBoxInformation)).thenReturn(ballotBoxInformationUnblocked);

        BallotBoxInformation result = ballotBoxInformationService.changeBallotBoxStatus("1", "1", "1", BallotBoxStatus.BLOCKED);
        assertFalse(BallotBoxStatus.BLOCKED.equals(result.getStatus()));
    }

    @Test
    public void unblockNonExistingBallotBox() throws ResourceNotFoundException, EntryPersistenceException {

        expectedException.expect(ResourceNotFoundException.class);
        when(ballotBoxInformationRepository.findByTenantIdElectionEventIdBallotBoxId(anyString(), anyString(),
            anyString())).thenThrow(new ResourceNotFoundException("exception"));
        ballotBoxInformationService.changeBallotBoxStatus("1", "1", "1", BallotBoxStatus.BLOCKED);

    }

    private BallotBoxInformation fillBallotBoxInformation() {
        String tenantId = "100";
        String electionEventId = "18aeec427aac47219d656421b864a649";
        String ballotBoxId = "08c7224118ce4daeaac8725f104765a0";
        String json =
            "{\r\n  \"id\" : \"7271439c37af4c4ca6ff28ab1dd4465d\",\r\n  \"startDate\" : \"2016-03-20T23:00Z\",\r\n  \"endDate\" : \"2016-12-20T23:00Z\",\r\n  \"encryptionParameters\" : {\r\n    \"g\" : \"2\",\r\n    \"p\" : \"16370518994319586760319791526293535327576438646782139419846004180837103527129035954742043590609421369665944746587885814920851694546456891767644945459124422553763416586515339978014154452159687109161090635367600349264934924141746082060353483306855352192358732451955232000593777554431798981574529854314651092086488426390776811367125009551346089319315111509277347117467107914073639456805159094562593954195960531136052208019343392906816001017488051366518122404819967204601427304267380238263913892658950281593755894747339126531018026798982785331079065126375455293409065540731646939808640273393855256230820509217411510058759\",\r\n    \"q\" : \"8185259497159793380159895763146767663788219323391069709923002090418551763564517977371021795304710684832972373293942907460425847273228445883822472729562211276881708293257669989007077226079843554580545317683800174632467462070873041030176741653427676096179366225977616000296888777215899490787264927157325546043244213195388405683562504775673044659657555754638673558733553957036819728402579547281296977097980265568026104009671696453408000508744025683259061202409983602300713652133690119131956946329475140796877947373669563265509013399491392665539532563187727646704532770365823469904320136696927628115410254608705755029379\"\r\n  },\r\n  \"electoralAuthorityId\" : \"487e364e8d0d47fd94f57aac14abdd48\",\r\n  \"eeid\" : \"18aeec427aac47219d656421b864a649\",\r\n  \"bid\" : \"08c7224118ce4daeaac8725f104765a0\",\r\n  \"ballotBoxCert\" : \"-----BEGIN CERTIFICATE-----\\r\\nMIIDozCCAougAwIBAgIVAMIXJTOdeJBJf0YMGVg2FzEr4GjgMA0GCSqGSIb3DQEB\\r\\nCwUAMHsxNTAzBgNVBAMMLFNlcnZpY2VzIENBIDE4YWVlYzQyN2FhYzQ3MjE5ZDY1\\r\\nNjQyMWI4NjRhNjQ5MRYwFAYDVQQLDA1PbmxpbmUgVm90aW5nMRIwEAYDVQQKDAlT\\r\\nd2lzc1Bvc3QxCTAHBgNVBAcMADELMAkGA1UEBhMCQ0gwHhcNMTYwMzIxMTEyMTU0\\r\\nWhcNMTcxMjIwMjMwMDAwWjB5MTMwMQYDVQQDDCpCYWxsb3RCb3ggNzI3MTQzOWMz\\r\\nN2FmNGM0Y2E2ZmYyOGFiMWRkNDQ2NWQxFjAUBgNVBAsMDU9ubGluZSBWb3Rpbmcx\\r\\nEjAQBgNVBAoMCVN3aXNzUG9zdDEJMAcGA1UEBwwAMQswCQYDVQQGEwJDSDCCASIw\\r\\nDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAK37vy3N2Gz2QYOQ+SfEkp9aLZiM\\r\\nnJIsogN0SD0fXc5qZuzf1TYRj2F+waU708xYkDzhIuifmNMPGLX35xN1pe0CIOfs\\r\\nrTwLj9xmvTqtP7ZIefII5kldgIsEsQU6qemQX7YAl2IZs/RQIdVlK82QnWfN1Rj4\\r\\nHEinKYd675WefGdI3wKHsRMraAvYxnMMP3lXe3690fa96UqhTHOvnATShki6Uzdj\\r\\ndVKIF9SQ6GRKp8R9uHUxPTZ5y1XbUA/TJ14o6RtST2/CJTj/Zfma4Qgodw9CSNYp\\r\\nEc/8/nG4fTcT/eURqojs/h/vpFwHXlHlzDdOvadN4Hg3bzeaiiGvK2BWM4ECAwEA\\r\\nAaMgMB4wDgYDVR0PAQH/BAQDAgbAMAwGA1UdEwEB/wQCMAAwDQYJKoZIhvcNAQEL\\r\\nBQADggEBANMqAM6ihvsXgM8p0Wne3A1MypDtuSXCGfZLub6rMOJQxDLDHxiz8cY7\\r\\nqrGiaHqKqIQ6NydPCRaMGhToV8EnylO1/Lx1KD+vM2EOTG3sNB+Ho87AydY6wJ+n\\r\\np1GD7bs0rVeOMkHgY8XPPbhaYrrThTbWYjeGEl2qS0Pctztc76KCLH9qhZfxM6qt\\r\\nmtZTG76mans84Op5fE8GbdtLtG5UoleltHG9MIPbyOmeQmoWg/p0s0J/OxkFcKP8\\r\\nRhyxcqlKri1TafUNjY5XlmVJyLYnGwtkJi7G6pQweTk1A8sGJYWmUrQaLQuJvt72\\r\\nUeC4zZaWL7AFIrvLurCJjAB31lV/Vyo=\\r\\n-----END CERTIFICATE-----\\r\\n\",\r\n  \"test\" : false\r\n}";

        BallotBoxInformation bbi = new BallotBoxInformation();
        bbi.setTenantId(tenantId);
        bbi.setElectionEventId(electionEventId);
        bbi.setBallotBoxId(ballotBoxId);
        bbi.setJson(json);
        return bbi;
    }

}
