package entity.test;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import entity.User;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import jpa.controllers.UserJpaController;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
/**
*
* @author atap
* UserTest class implements basic functionalities user entity.
*/
public class UserTest {
private final User s1;
private final User s2;
private static final String PERSISTENCEUNITNAME = "EducationXPU";
private EntityManagerFactory emf;
private EntityManager em;
public UserTest() throws ParseException {
this.s1 = new User("anakin", "anakin@skywalker.net", "24ewRTtr", 'S', 500, new SimpleDateFormat("MM/dd/yy").parse("05/18/05"));
this.s2 = new User("luke", "luke@skywalker.net", "42eeRTew", 'S', 250, new SimpleDateFormat("MM/dd/yy").parse("05/18/05"));
}
@Before
public void setUp() {
emf = Persistence.createEntityManagerFactory(PERSISTENCEUNITNAME);
em = emf.createEntityManager();
}
@Test
public void testAuthenticateInvalidUser() {
UserJpaController service = new UserJpaController(emf);
boolean result = service.authenticateUser("ata@gmail.com", "dddss", s1);
Assert.assertFalse(result);
}
@Test
public void testInsertUsersAndcheckCount() {
em.getTransaction().begin();
em.persist(s1);
em.persist(s2);
em.getTransaction().commit();
List<User> newUsers = em.createNativeQuery("select * from user where email like '%skywalker%' ").getResultList();
Assert.assertEquals(newUsers.size(), 2);
}
@Test
public void testDeleteUsersWithPrimaryKey(){
em.getTransaction().begin();
// primary key
User s1 = em.find(User.class, 11);
em.remove(s1);
em.getTransaction().commit();
}
@Test
public void testDeleteUsersWithName() {
em.getTransaction().begin();
int cnt = em.createNativeQuery("delete from user where email like '%skywalker%' ").executeUpdate();
em.getTransaction().commit();
Assert.assertTrue(cnt > 0);
}
@Test
public void testUserPasswordInvalidLength() {
User user = new User();
user.setPassword("xxaa1");
Assert.fail("User password must be at least 6 characters in length");
}
@After
public void tearDown() {
em.close();
}
}