}
@Test
public void insertAndFetchUser() {
UserManager userMan = new UserManager();
userMan.setEntityManager(em);
// Test getting same item back using email!
String johnEmail = "aa@a.com";
FireUser user1 = TestUtil.createRandomUser();
user1.setEmail(johnEmail);
List<FireUser> us = userMan.getAll();
for (FireUser u : us) System.out.println("User: " + u.getEmail());
em.getTransaction().begin();
userMan.create(user1);
em.getTransaction().commit();
// Lets find per email
FireUser user = userMan.findUser(johnEmail);
assertNotNull(user);
List<FireUser> users = userMan.findUsersByFirstName(user1.getFname());
assertTrue(users.contains(user1));
users = userMan.findUsersByLastName(user1.getLname());
assertTrue(users.contains(user1));
FireUser user2 = userMan.read(user1.getId());
assertNotNull(user2);
assertTrue(user1.equals(user2));
// Add 10 users to ensure we have enought
// users for following tests
em.getTransaction().begin();
for(int i = 0; i < 10; i++) {
userMan.create(new FireUser());
}
em.getTransaction().commit();
// Get all
List<FireUser> all = userMan.getAll();
assertTrue(all.size() >= 10);
// Get range tests
all = userMan.getRange(0, 5);
assertTrue(all.size() == 5);
all = userMan.getRange(5, 5);
assertTrue(all.size() == 5);
// Test update
String name = user1.getFname();
user2.setFname("Another");
em.getTransaction().begin();
userMan.update(user2);
em.getTransaction().commit();
user2 = userMan.read(user1.getId());
assertFalse(name.equals(user2.getFname()));
// Test remove
em.getTransaction().begin();
userMan.delete(user2);
em.getTransaction().commit();
user2 = userMan.read(user1.getId());
assertNull(user2);
}