|
| 1 | +package com.baeldung.hibernate.jpacriteriabuilder; |
| 2 | + |
| 3 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 4 | +import static org.hamcrest.collection.IsIterableContainingInAnyOrder.containsInAnyOrder; |
| 5 | +import static org.junit.Assert.assertEquals; |
| 6 | + |
| 7 | +import java.io.IOException; |
| 8 | +import java.util.ArrayList; |
| 9 | +import java.util.List; |
| 10 | +import java.util.stream.Collectors; |
| 11 | + |
| 12 | +import javax.persistence.EntityManager; |
| 13 | + |
| 14 | +import org.hibernate.HibernateException; |
| 15 | +import org.hibernate.Session; |
| 16 | +import org.junit.After; |
| 17 | +import org.junit.Before; |
| 18 | +import org.junit.Test; |
| 19 | + |
| 20 | +import com.baeldung.hibernate.HibernateUtil; |
| 21 | +import com.baeldung.hibernate.entities.Department; |
| 22 | +import com.baeldung.hibernate.entities.DeptEmployee; |
| 23 | +import com.baeldung.hibernate.jpacriteriabuilder.service.EmployeeSearchService; |
| 24 | +import com.baeldung.hibernate.jpacriteriabuilder.service.EmployeeSearchServiceImpl; |
| 25 | + |
| 26 | +public class EmployeeSearchServiceIntegrationTest { |
| 27 | + |
| 28 | + private EntityManager entityManager; |
| 29 | + private EmployeeSearchService searchService; |
| 30 | + private Session session; |
| 31 | + |
| 32 | + @Before |
| 33 | + public final void setup() throws HibernateException, IOException { |
| 34 | + session = HibernateUtil.getSessionFactory() |
| 35 | + .openSession(); |
| 36 | + entityManager = session.getEntityManagerFactory() |
| 37 | + .createEntityManager(); |
| 38 | + searchService = new EmployeeSearchServiceImpl(entityManager); |
| 39 | + |
| 40 | + entityManager.getTransaction() |
| 41 | + .begin(); |
| 42 | + Department department = new Department("Pre Sales"); |
| 43 | + DeptEmployee employee = new DeptEmployee("John Smith", "001", "Manager", department); |
| 44 | + entityManager.persist(department); |
| 45 | + entityManager.persist(employee); |
| 46 | + employee = new DeptEmployee("Ian Evans", "002", "Associate", department); |
| 47 | + entityManager.persist(department); |
| 48 | + entityManager.persist(employee); |
| 49 | + department = new Department("Copporate Sales"); |
| 50 | + employee = new DeptEmployee("Robert Carter", "003", "Manager", department); |
| 51 | + entityManager.persist(department); |
| 52 | + entityManager.persist(employee); |
| 53 | + employee = new DeptEmployee("John Carter", "004", "Senior Manager", department); |
| 54 | + entityManager.persist(employee); |
| 55 | + employee = new DeptEmployee("David Guetta", "009", "Associate", department); |
| 56 | + entityManager.persist(department); |
| 57 | + entityManager.persist(employee); |
| 58 | + department = new Department("Post Sales"); |
| 59 | + employee = new DeptEmployee("Robert Jonas", "005", "Director", department); |
| 60 | + entityManager.persist(department); |
| 61 | + entityManager.persist(employee); |
| 62 | + employee = new DeptEmployee("John Ferros", "006", "Junior Associate", department); |
| 63 | + entityManager.persist(department); |
| 64 | + entityManager.persist(employee); |
| 65 | + department = new Department("Client Support"); |
| 66 | + employee = new DeptEmployee("Robert Mcclements", "007", "Director", department); |
| 67 | + entityManager.persist(department); |
| 68 | + entityManager.persist(employee); |
| 69 | + employee = new DeptEmployee("Peter Parker", "008", "Manager", department); |
| 70 | + entityManager.persist(department); |
| 71 | + entityManager.persist(employee); |
| 72 | + |
| 73 | + } |
| 74 | + |
| 75 | + @After |
| 76 | + public final void teardown() { |
| 77 | + entityManager.getTransaction() |
| 78 | + .rollback(); |
| 79 | + entityManager.close(); |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + public final void givenCriteriaQuery_whenSearchedUsingCriteriaBuilderWithListofAuthors_thenResultIsFilteredByAuthorNames() { |
| 84 | + List<String> designations = new ArrayList<String>() { |
| 85 | + { |
| 86 | + add("Manager"); |
| 87 | + add("Senior Manager"); |
| 88 | + add("Director"); |
| 89 | + } |
| 90 | + }; |
| 91 | + List<DeptEmployee> result = searchService.filterbyDesignationUsingCriteriaBuilder(designations); |
| 92 | + assertEquals("Number of Employees does not match with expected.", 6, result.size()); |
| 93 | + assertThat(result.stream() |
| 94 | + .map(DeptEmployee::getDesignation) |
| 95 | + .distinct() |
| 96 | + .collect(Collectors.toList()), containsInAnyOrder(designations.toArray())); |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + public final void givenCriteriaQuery_whenSearchedUsingExpressionWithListofAuthors_thenResultIsFilteredByAuthorNames() { |
| 101 | + List<String> designations = new ArrayList<String>() { |
| 102 | + { |
| 103 | + add("Manager"); |
| 104 | + add("Senior Manager"); |
| 105 | + add("Director"); |
| 106 | + } |
| 107 | + }; |
| 108 | + List<DeptEmployee> result = searchService.filterbyDesignationUsingExpression(designations); |
| 109 | + assertEquals("Number of Employees does not match with expected.", 6, result.size()); |
| 110 | + assertThat(result.stream() |
| 111 | + .map(DeptEmployee::getDesignation) |
| 112 | + .distinct() |
| 113 | + .collect(Collectors.toList()), containsInAnyOrder(designations.toArray())); |
| 114 | + } |
| 115 | + |
| 116 | + @Test |
| 117 | + public final void givenCriteriaQuery_whenSearchedDepartmentLike_thenResultIsFilteredByDepartment() { |
| 118 | + List<DeptEmployee> result = searchService.searchByDepartmentQuery("Sales"); |
| 119 | + assertEquals("Number of Employees does not match with expected.", 7, result.size()); |
| 120 | + // assertThat(result.stream().map(DeptEmployee::getDesignation).distinct().collect(Collectors.toList()), containsInAnyOrder(designations.toArray())); |
| 121 | + } |
| 122 | +} |
0 commit comments