package dst.ass1.jpa.tests;

import static org.junit.Assert.assertTrue;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import dst.ass1.jpa.ORMService;
import dst.ass1.jpa.model.IParticipant;
import dst.ass1.jpa.util.Constants;

/**
 * Tests the constraint for IParticipant bank data.
 */
public class Ass1_1_2_01Test {

    @Rule
    public ORMService orm = new ORMService();

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

    @Test
    public void testParticipantAccountNoBankCodeConstraint() {
        IParticipant p1 = orm.getModelFactory().createParticipant();
        IParticipant p2 = orm.getModelFactory().createParticipant();

        p1.setEmail("p1@example.com");
        p2.setEmail("p2@example.com");

        new UniqueConstraintTester<>(p1, p2, e -> {
            e.setAccountNo("uniqueVal1");
            e.setBankCode("uniqueVal2");
        }).run(orm.getEntityManager());
    }

    @Test
    public void testParticipantAccountNoBankCodeConstraintJdbc() {
        assertTrue(orm.getDatabaseGateway().isIndex(Constants.T_PARTICIPANT, Constants.M_PARTICIPANT_ACCOUNT, false));
        assertTrue(orm.getDatabaseGateway().isIndex(Constants.T_PARTICIPANT, Constants.M_PARTICIPANT_BANK_CODE, false));
        assertTrue(orm.getDatabaseGateway().isComposedIndex(Constants.T_PARTICIPANT, Constants.M_PARTICIPANT_ACCOUNT, Constants.M_PARTICIPANT_BANK_CODE));

        assertTrue(orm.getDatabaseGateway().isNullable(Constants.T_PARTICIPANT, Constants.M_PARTICIPANT_ACCOUNT));
        assertTrue(orm.getDatabaseGateway().isNullable(Constants.T_PARTICIPANT, Constants.M_PARTICIPANT_BANK_CODE));
    }

}
