/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package entity.service.test;
import entity.Courses;
import entity.Enrollment;
import entity.User;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;
/**
*
* @author atap
* UserServiceTest1 class implements user entity's interaction with other entities;
*/
public class UserServiceTest1 {
private static final String PERSISTENCEUNITNAME = "EducationXPU";
private EntityManagerFactory emf;
private EntityManager em;
private int primaryId;
private User tempUser;
public UserServiceTest1() {
}
@Before
public void setUp() {
emf = Persistence.createEntityManagerFactory(PERSISTENCEUNITNAME);
em = emf.createEntityManager();
}
@After
public void tearDown() {
if (em != null) {
removeTestData();
em.close();
}
if (emf != null) {
emf.close();
}
}
@Test
public void testUserEnrollment() throws ParseException {
tempUser = new User("anakin", "anakin@skywalker.net", "24ewRTtr", 'S', 500, new SimpleDateFormat("MM/dd/yy").parse("05/18/05"));
List<Enrollment> enrollList = new ArrayList<Enrollment>();
// create courses and add to enroll list of temp user;
Courses tempCourse = new Courses("testClass with john lennon", 'A', "How to play guitar without pick", new SimpleDateFormat("MM/dd/yy").parse("05/18/05"));
tempCourse.setCreaterUserId(tempUser);
Enrollment tempEnroll = new Enrollment();
tempEnroll.setCourseId(tempCourse);
tempEnroll.setSysdate(new SimpleDateFormat("MM/dd/yy").parse("01/10/02"));
tempEnroll.setUserId(tempUser);
// add to list of user's enrollment list
enrollList.add(tempEnroll);
tempUser.setEnrollmentList(enrollList);
em.getTransaction().begin();
em.persist(tempUser);
em.getTransaction().commit();
}
// tempUser's primaryId will be needed to delete user and it's associative entities; enrollment and courses entities.
private void removeTestData() {
primaryId = Integer.parseInt(emf.getPersistenceUnitUtil().getIdentifier(tempUser).toString());
em.getTransaction().begin();
User user = em.find(User.class, primaryId);
if (user != null) {
em.remove(user);
}
em.getTransaction().commit();
}
}