-
-
Save fabriciofx/618eca37fc7db53b424e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package foo.model.repository; | |
| import javax.persistence.EntityManager; | |
| import javax.persistence.EntityManagerFactory; | |
| import javax.persistence.Persistence; | |
| import org.junit.AfterClass; | |
| import org.junit.BeforeClass; | |
| public abstract class AbstractRepositoryTest { | |
| protected static EntityManagerFactory emFactory; | |
| protected static EntityManager em; | |
| @BeforeClass | |
| public static void setUp0() { | |
| emFactory = Persistence.createEntityManagerFactory("default-test"); | |
| em = emFactory.createEntityManager(); | |
| em.getTransaction().begin(); | |
| } | |
| @AfterClass | |
| public static void tearDown0() { | |
| em.getTransaction().commit(); | |
| em.close(); | |
| emFactory.close(); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?xml version="1.0" encoding="utf-8" ?> | |
| <persistence xmlns="http://java.sun.com/xml/ns/persistence" | |
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
| xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd" | |
| version="2.0"> | |
| <persistence-unit name="default-test" transaction-type="RESOURCE_LOCAL"> | |
| <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider> | |
| <class>foo.model.entity.Plan</class> | |
| <properties> | |
| <property name="javax.persistence.jdbc.url" value="jdbc:hsqldb:mem:foo-test" /> | |
| <property name="javax.persistence.jdbc.user" value="sa" /> | |
| <property name="javax.persistence.jdbc.password" value="" /> | |
| <property name="javax.persistence.schema-generation.database.action" value="drop-and-create" /> | |
| <property name="hibernate.show_sql" value="true" /> | |
| </properties> | |
| </persistence-unit> | |
| </persistence> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @Dependent | |
| public class PlanRepository { | |
| private final EntityManager em; | |
| @Inject | |
| public PlanRepository(EntityManager em) { | |
| this.em = em; | |
| } | |
| public List<Plan> findActive() { | |
| CriteriaBuilder builder = em.getCriteriaBuilder(); | |
| CriteriaQuery<Plan> c = builder.createQuery(Plan.class); | |
| Root<Plan> root = c.from(Plan.class); | |
| c.where(builder.equal(root.get(Plan_.active), true)); | |
| c.orderBy(builder.asc(root.get(Plan_.name))); | |
| return em.createQuery(c).getResultList(); | |
| } | |
| public List<Plan> findAll(String qs, Page page) { | |
| CriteriaBuilder builder = em.getCriteriaBuilder(); | |
| CriteriaQuery<Long> cx = builder.createQuery(Long.class); | |
| Root<Plan> rootx = cx.from(Plan.class); | |
| cx.select(builder.count(rootx)); | |
| cx.where(builder.like(builder.upper(rootx.get(Plan_.name)), "%" + nullToEmpty(qs).toUpperCase() + "%")); | |
| Long count = em.createQuery(cx).getSingleResult(); | |
| CriteriaQuery<Plan> c = builder.createQuery(Plan.class); | |
| Root<Plan> root = c.from(Plan.class); | |
| c.orderBy(builder.desc(root.get(Plan_.active)), builder.asc(root.get(Plan_.name))); | |
| c.where(builder.like(builder.upper(root.get(Plan_.name)), "%" + nullToEmpty(qs).toUpperCase() + "%")); | |
| TypedQuery<Plan> query = em.createQuery(c); | |
| query.setFirstResult(page.getFirstResult()).setMaxResults(page.getLimit()); | |
| return PaginatedList.newList(page, count.intValue(), query.getResultList()); | |
| } | |
| public Plan findByPK(Long id) { | |
| return em.find(Plan.class, id); | |
| } | |
| public Plan store(Plan entity) { | |
| entity = em.merge(entity); | |
| em.flush(); | |
| return entity; | |
| } | |
| public void delete(Long id) { | |
| Plan entity = em.getReference(Plan.class, id); | |
| em.remove(entity); | |
| em.flush(); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package foo.model.repository; | |
| public class PlanRepositoryTest | |
| extends AbstractRepositoryTest { | |
| private PlanRepository repository; | |
| private Plan plan0; | |
| private Plan plan1; | |
| private Plan plan2; | |
| private Plan plan3; | |
| @Before | |
| public void setUp() { | |
| repository = new PlanRepository(em); | |
| plan0 = repository.store(new Plan(null, true, TEN, ZERO, 0, "Rate 0")); | |
| plan1 = repository.store(new Plan(null, true, TEN, TEN, 0, "Rate 1")); | |
| plan2 = repository.store(new Plan(null, true, TEN, TEN, 0, "Rate 2")); | |
| plan3 = repository.store(new Plan(null, false, TEN, ZERO, 0, "Rate 3")); | |
| } | |
| @After | |
| public void tearDown() { | |
| repository.delete(plan0.getId()); | |
| repository.delete(plan1.getId()); | |
| repository.delete(plan2.getId()); | |
| repository.delete(plan3.getId()); | |
| em.flush(); | |
| } | |
| @Test | |
| public void testFind() { | |
| assertThat(plan0, is(repository.findByPK(plan0.getId()))); | |
| assertThat(plan1, is(repository.findByPK(plan1.getId()))); | |
| assertThat(plan2, is(repository.findByPK(plan2.getId()))); | |
| assertThat(plan3, is(repository.findByPK(plan3.getId()))); | |
| } | |
| @Test | |
| public void testFindActie() { | |
| List<Plan> results = repository.findActive(); | |
| List<Plan> expected = asList(plan0, plan1, plan2); | |
| assertThat(results, is(expected)); | |
| } | |
| @Test | |
| public void testFindWithPaging() { | |
| List<Plan> results = repository.findAll(null, Page.of(1, 2)); | |
| List<Plan> expected = asList(plan0, plan1); | |
| assertThat(results, is(expected)); | |
| results = repository.findAll(null, Page.of(2, 2)); | |
| expected = asList(plan2, plan3); | |
| assertThat(results, is(expected)); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment