Search in sources :

Example 1 with Category

use of org.jpwh.model.customsql.Category in project microservices by pwillhan.

the class CustomSQL method readRestrictedCollection.

// The "ACTIVE = 'true'" SQL restriction doesn't work on Oracle, they
// still don't have a boolean datatype...
@Test(groups = { "H2", "POSTGRESQL" })
public void readRestrictedCollection() throws Exception {
    CustomSQLTestData testData = create();
    Long CATEGORY_ID = testData.categories.getFirstId();
    Long ITEM_ID = testData.items.getFirstId();
    UserTransaction tx = TM.getUserTransaction();
    try {
        tx.begin();
        EntityManager em = JPA.createEntityManager();
        {
            Category category = em.find(Category.class, CATEGORY_ID);
            assertEquals(loadEventListener.getLoadCount(Category.class), 1);
            Set<Item> items = category.getItems();
            assertEquals(items.size(), 1);
            assertEquals(items.iterator().next().getId(), ITEM_ID);
        }
        em.clear();
        loadEventListener.reset();
        tx.commit();
        em.close();
    } finally {
        TM.rollback();
    }
}
Also used : UserTransaction(javax.transaction.UserTransaction) EntityManager(javax.persistence.EntityManager) Category(org.jpwh.model.customsql.Category) Set(java.util.Set) JPATest(org.jpwh.env.JPATest) Test(org.testng.annotations.Test)

Example 2 with Category

use of org.jpwh.model.customsql.Category in project microservices by pwillhan.

the class CustomSQL method create.

public CustomSQLTestData create() throws Exception {
    UserTransaction tx = TM.getUserTransaction();
    tx.begin();
    EntityManager em = JPA.createEntityManager();
    CustomSQLTestData testData = new CustomSQLTestData();
    testData.categories = new TestData(new Long[1]);
    testData.items = new TestData(new Long[2]);
    testData.bids = new TestData(new Long[3]);
    testData.users = new TestData(new Long[2]);
    User johndoe = new User("johndoe");
    em.persist(johndoe);
    testData.users.identifiers[0] = johndoe.getId();
    User janeroe = new User("janeroe");
    em.persist(janeroe);
    testData.users.identifiers[1] = janeroe.getId();
    Category category = new Category();
    category.setName("Foo");
    em.persist(category);
    testData.categories.identifiers[0] = category.getId();
    Item item = new Item();
    item.setName("Some item");
    item.setCategory(category);
    item.setSeller(johndoe);
    item.setAuctionEnd(CalendarUtil.TOMORROW.getTime());
    item.getImages().add(new Image("foo.jpg", 640, 480));
    item.getImages().add(new Image("bar.jpg", 800, 600));
    item.getImages().add(new Image("baz.jpg", 640, 480));
    em.persist(item);
    testData.items.identifiers[0] = item.getId();
    for (int i = 1; i <= 3; i++) {
        Bid bid = new Bid();
        bid.setAmount(new BigDecimal(10 + i));
        bid.setItem(item);
        bid.setBidder(janeroe);
        em.persist(bid);
        testData.bids.identifiers[i - 1] = bid.getId();
    }
    Item otherItem = new Item(category, "Inactive item", CalendarUtil.TOMORROW.getTime(), johndoe);
    otherItem.setActive(false);
    em.persist(otherItem);
    tx.commit();
    em.close();
    return testData;
}
Also used : UserTransaction(javax.transaction.UserTransaction) Item(org.jpwh.model.customsql.Item) EntityManager(javax.persistence.EntityManager) User(org.jpwh.model.customsql.User) Category(org.jpwh.model.customsql.Category) TestData(org.jpwh.shared.util.TestData) Image(org.jpwh.model.customsql.Image) Bid(org.jpwh.model.customsql.Bid) BigDecimal(java.math.BigDecimal)

Aggregations

EntityManager (javax.persistence.EntityManager)2 UserTransaction (javax.transaction.UserTransaction)2 Category (org.jpwh.model.customsql.Category)2 BigDecimal (java.math.BigDecimal)1 Set (java.util.Set)1 JPATest (org.jpwh.env.JPATest)1 Bid (org.jpwh.model.customsql.Bid)1 Image (org.jpwh.model.customsql.Image)1 Item (org.jpwh.model.customsql.Item)1 User (org.jpwh.model.customsql.User)1 TestData (org.jpwh.shared.util.TestData)1 Test (org.testng.annotations.Test)1