Search in sources :

Example 1 with Item

use of org.jpwh.model.filtering.interceptor.Item in project microservices by pwillhan.

the class AuditLogging method writeAuditLog.

@Test
public void writeAuditLog() throws Throwable {
    UserTransaction tx = TM.getUserTransaction();
    try {
        Long CURRENT_USER_ID;
        {
            tx.begin();
            EntityManager em = JPA.createEntityManager();
            User currentUser = new User("johndoe");
            em.persist(currentUser);
            tx.commit();
            em.close();
            CURRENT_USER_ID = currentUser.getId();
        }
        EntityManagerFactory emf = JPA.getEntityManagerFactory();
        Map<String, String> properties = new HashMap<String, String>();
        properties.put(org.hibernate.jpa.AvailableSettings.SESSION_INTERCEPTOR, AuditLogInterceptor.class.getName());
        EntityManager em = emf.createEntityManager(properties);
        Session session = em.unwrap(Session.class);
        AuditLogInterceptor interceptor = (AuditLogInterceptor) ((SessionImplementor) session).getInterceptor();
        interceptor.setCurrentSession(session);
        interceptor.setCurrentUserId(CURRENT_USER_ID);
        tx.begin();
        em.joinTransaction();
        Item item = new Item("Foo");
        em.persist(item);
        tx.commit();
        em.clear();
        tx.begin();
        em.joinTransaction();
        List<AuditLogRecord> logs = em.createQuery("select lr from AuditLogRecord lr", AuditLogRecord.class).getResultList();
        assertEquals(logs.size(), 1);
        assertEquals(logs.get(0).getMessage(), "insert");
        assertEquals(logs.get(0).getEntityClass(), Item.class);
        assertEquals(logs.get(0).getEntityId(), item.getId());
        assertEquals(logs.get(0).getUserId(), CURRENT_USER_ID);
        em.createQuery("delete AuditLogRecord").executeUpdate();
        tx.commit();
        em.clear();
        tx.begin();
        em.joinTransaction();
        item = em.find(Item.class, item.getId());
        item.setName("Bar");
        tx.commit();
        em.clear();
        tx.begin();
        em.joinTransaction();
        logs = em.createQuery("select lr from AuditLogRecord lr", AuditLogRecord.class).getResultList();
        assertEquals(logs.size(), 1);
        assertEquals(logs.get(0).getMessage(), "update");
        assertEquals(logs.get(0).getEntityClass(), Item.class);
        assertEquals(logs.get(0).getEntityId(), item.getId());
        assertEquals(logs.get(0).getUserId(), CURRENT_USER_ID);
        tx.commit();
        em.close();
    } finally {
        TM.rollback();
    }
}
Also used : UserTransaction(javax.transaction.UserTransaction) User(org.jpwh.model.filtering.interceptor.User) HashMap(java.util.HashMap) Item(org.jpwh.model.filtering.interceptor.Item) AuditLogRecord(org.jpwh.model.filtering.interceptor.AuditLogRecord) EntityManager(javax.persistence.EntityManager) EntityManagerFactory(javax.persistence.EntityManagerFactory) Session(org.hibernate.Session) JPATest(org.jpwh.env.JPATest) Test(org.testng.annotations.Test)

Aggregations

HashMap (java.util.HashMap)1 EntityManager (javax.persistence.EntityManager)1 EntityManagerFactory (javax.persistence.EntityManagerFactory)1 UserTransaction (javax.transaction.UserTransaction)1 Session (org.hibernate.Session)1 JPATest (org.jpwh.env.JPATest)1 AuditLogRecord (org.jpwh.model.filtering.interceptor.AuditLogRecord)1 Item (org.jpwh.model.filtering.interceptor.Item)1 User (org.jpwh.model.filtering.interceptor.User)1 Test (org.testng.annotations.Test)1