package dst.ass1.jpa.tests;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;

import javax.persistence.metamodel.ManagedType;
import javax.persistence.metamodel.Type;

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

import dst.ass1.jpa.ORMService;
import dst.ass1.jpa.model.IAddress;

/**
 * Tests if IAddress is implemented correctly.
 */
public class Ass1_1_1_05Test {

    @Rule
    public ORMService orm = new ORMService();

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

    @Test
    public void addressEntityCannotBePersisted() {
        IAddress address = orm.getModelFactory().createAddress();
        assertNotNull(address);

        address.setCity("city1");
        address.setStreet("street1");
        address.setZipCode("zip1");

        expectedException.expect(IllegalArgumentException.class);
        orm.getEntityManager().persist(address);
    }

    @Test
    public void addressIsEmbeddableType() {
        IAddress address = orm.getModelFactory().createAddress();
        assertNotNull(address);

        ManagedType<?> type = orm.getEntityManager().getMetamodel().managedType(address.getClass());
        assertNotNull(type);
        assertThat(type.getPersistenceType(), is(Type.PersistenceType.EMBEDDABLE));
    }

    @Test
    public void addressHasNoTable() throws Exception {
        assertFalse(orm.getDatabaseGateway().isTable("ADDRESS"));
    }

}
