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();
}
}
Aggregations