package com.huskycode.acceptancetest.jpaquery;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.junit.Assert.assertThat;

import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.transaction.annotation.Transactional;

import com.huskycode.jpaquery.AbstractEntityManagerWired;
import com.huskycode.jpaquery.JPAQueryContext;
import com.huskycode.jpaquery.testmodel.pizza.Address;
import com.huskycode.jpaquery.testmodel.pizza.PizzaOrder;
import com.huskycode.jpaquery.testmodel.pizza.deps.PizzaDeps;
import com.huskycode.jpaquery.types.tree.PersistedResult;

/**
 * @author Varokas Panusuwan
 */
@Transactional
public class JPAQueryIT extends AbstractEntityManagerWired {
    private PizzaDeps pizzaDeps;
    private JPAQueryContext context;
    
    @Before
    public void before() {
    	pizzaDeps = new PizzaDeps();
    }
	
	@Test
    public void testCreateClassWithNoDeps() {
        context = JPAQueryContext.newInstance(entityManager, pizzaDeps.getDeps());
        
        PersistedResult result = context.create(Address.class);
        assertThat(result, is(not(nullValue())));
    }
	
	@Test 
    public void testCreateClassWithDeps() {
        context = JPAQueryContext.newInstance(entityManager, pizzaDeps.getDeps());
        
        PersistedResult result = context.create(PizzaOrder.class);
        assertThat(result, is(not(nullValue())));
    }
}

@@ -41,16 +41,68 @@
 query;%0A%0A
+import static org.hamcrest.CoreMatchers.instanceOf;%0A
 import s
@@ -277,16 +277,122 @@
 tThat;%0A%0A
+import java.util.HashMap;%0Aimport java.util.List;%0Aimport java.util.Map;%0A%0Aimport org.hamcrest.CoreMatchers;%0A
 import o
@@ -682,16 +682,128 @@
 ddress;%0A
+import com.huskycode.jpaquery.testmodel.pizza.Customer;%0Aimport com.huskycode.jpaquery.testmodel.pizza.Employee;%0A
 import c
@@ -1242,204 +1242,404 @@
     
-%7D%0A%09%0A%09@Test%0A    public void testCreateClassWithNoDeps() %7B%0A        context = JPAQueryContext.newInstance(entityManager, pizzaDeps.getDeps());%0A        %0A        PersistedResult result = context.create
+%09context = JPAQueryContext.newInstance(entityManager, pizzaDeps.getDeps());%0A    %7D%0A%09%0A%09@Test%0A    public void testCreateClassWithNoDeps() %7B%0A        PersistedResult result = context.create(Address.class);%0A        %0A        assertThat(result.getPersistedObjects().size(), is(1));%0A        Object persistedObj = result.getPersistedObjects().get(0);%0A        %0A        assertThat(persistedObj, is(instanceOf
 (Add
@@ -1641,32 +1641,34 @@
 f(Address.class)
+))
 ;%0A        assert
@@ -1664,38 +1664,70 @@
      assertThat(
-result
+((Address)persistedObj).getAddressId()
 , is(not(nullVal
@@ -1786,32 +1786,40 @@
 lassWithDeps() %7B
+        
 %0A        context
@@ -1815,213 +1815,1259 @@
     
-context = JPAQueryContext.newInstance(entityManager, pizzaDeps.getDeps());%0A        %0A        PersistedResult result = context.create(PizzaOrder.class);%0A        assertThat(result, is(not(nullValue())));%0A    
+PersistedResult result = context.create(PizzaOrder.class);%0A        %0A        ClassMap resultMap = mapByClass(result.getPersistedObjects());%0A        %0A        Address address = resultMap.getForClass(Address.class);%0A        Employee employee = resultMap.getForClass(Employee.class);%0A        Customer customer = resultMap.getForClass(Customer.class);%0A        PizzaOrder pizzaOrder = resultMap.getForClass(PizzaOrder.class);%0A        %0A        assertThat(employee.getEmployeeAddressId(), is(address.getAddressId()));%0A        assertThat(customer.getCustomerAddressId(), is(address.getAddressId()));%0A        assertThat(pizzaOrder.getCustomerId(), is(customer.getCustomerId()));%0A        assertThat(pizzaOrder.getDeliveredByEmployeeId(), is(employee.getEmployeeId()));%0A        assertThat(pizzaOrder.getTakenByEmployeeId(), is(employee.getEmployeeId()));%0A    %7D%0A%09%0A%09public class ClassMap extends HashMap%3CClass%3C?%3E, Object%3E %7B%0A%09%09private static final long serialVersionUID = 1L;%0A%09%0A%09%09@SuppressWarnings(%22unchecked%22)%0A%09%09public %3CE%3E E getForClass(Class%3CE%3E clazz) %7B%0A%09%09%09return (E) this.get(clazz);%0A%09%09%7D%0A%09%7D%0A%09%0A%09private ClassMap mapByClass(List%3CObject%3E objects) %7B%0A%09%09ClassMap map = new ClassMap();%0A%09%09for(Object obj : objects) %7B%0A%09%09%09map.put(obj.getClass(), obj);%0A%09%09%7D%0A%09%09%0A%09%09return map;%0A%09
 %7D%0A%7D%0A

Improve Integration Test