/*
 * 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.au.services.infrastructure.persistence;

import com.scytl.products.ov.au.services.domain.model.adminboard.AdminBoard;
import com.scytl.products.ov.au.services.domain.model.adminboard.AdminBoardRepository;
import com.scytl.products.ov.commons.beans.exceptions.ResourceNotFoundException;
import org.junit.Assert;
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.MockitoAnnotations;
import org.mockito.runners.MockitoJUnitRunner;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.TypedQuery;

import java.util.Arrays;
import java.util.List;

import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.when;


/**
 * Test class of the admin board repository
 */
@RunWith(MockitoJUnitRunner.class)
public class AdminBoardRepositoryTest {

    public static final String TENANT_ID = "100";
    public static final String ELECTION_EVENT_ID = "100";
    public static final String VOTING_CARD_ID = "100";


    @InjectMocks
    private AdminBoardRepository repository = new AdminBoardRepositoryImpl();

    @Mock
    private EntityManager entityManagerMock;

    @Mock
    private TypedQuery<AdminBoard> typedQueryMock;

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

    public void init(){
        MockitoAnnotations.initMocks(this.getClass());
    }


    @Test
    public void findByTenantIdElectionEventId() throws ResourceNotFoundException {

        List<AdminBoard> result = Arrays.asList(new AdminBoard());
        when(entityManagerMock.createQuery(anyString(), eq(AdminBoard.class))).thenReturn(typedQueryMock);
        when(typedQueryMock.getSingleResult()).thenReturn(result.get(0));
        Assert.assertNotNull(repository.findByTenantIdElectionEventId(TENANT_ID, ELECTION_EVENT_ID));

    }


    @Test
    public void findByTenantIdElectionEventIdNotFound() throws ResourceNotFoundException {

        expectedException.expect(ResourceNotFoundException.class);
        when(entityManagerMock.createQuery(anyString(), eq(AdminBoard.class))).thenReturn(typedQueryMock);
        when(typedQueryMock.getSingleResult()).thenThrow(new NoResultException("exception"));
        Assert.assertNotNull(repository.findByTenantIdElectionEventId(TENANT_ID, ELECTION_EVENT_ID));

    }


}
