Bael-2227 1 (#5707)

* BAEL-2159: Mini Article on "Separate double into integer and decimal parts"

* BAEL-2227: Refactor: Replace designation with title.
This commit is contained in:
Akash Pandey 2018-11-17 21:26:22 +05:30 committed by maibin
parent c7daccbe6e
commit 331988e407
4 changed files with 24 additions and 25 deletions

View File

@ -10,7 +10,7 @@ public class DeptEmployee {
private String employeeNumber; private String employeeNumber;
private String designation; private String title;
private String name; private String name;
@ -23,11 +23,11 @@ public class DeptEmployee {
this.department = department; this.department = department;
} }
public DeptEmployee(String name, String employeeNumber, String designation, Department department) { public DeptEmployee(String name, String employeeNumber, String title, Department department) {
super(); super();
this.name = name; this.name = name;
this.employeeNumber = employeeNumber; this.employeeNumber = employeeNumber;
this.designation = designation; this.title = title;
this.department = department; this.department = department;
} }
@ -63,11 +63,11 @@ public class DeptEmployee {
this.department = department; this.department = department;
} }
public String getDesignation() { public String getTitle() {
return designation; return title;
} }
public void setDesignation(String designation) { public void setTitle(String title) {
this.designation = designation; this.title = title;
} }
} }

View File

@ -6,9 +6,9 @@ import com.baeldung.hibernate.entities.DeptEmployee;
public interface EmployeeSearchService { public interface EmployeeSearchService {
List<DeptEmployee> filterbyDesignationUsingCriteriaBuilder(List<String> designaitons); List<DeptEmployee> filterbyTitleUsingCriteriaBuilder(List<String> titles);
List<DeptEmployee> filterbyDesignationUsingExpression(List<String> aurhors); List<DeptEmployee> filterbyTitleUsingExpression(List<String> titles);
List<DeptEmployee> searchByDepartmentQuery(String query); List<DeptEmployee> searchByDepartmentQuery(String query);

View File

@ -25,12 +25,12 @@ public class EmployeeSearchServiceImpl implements EmployeeSearchService {
} }
@Override @Override
public List<DeptEmployee> filterbyDesignationUsingCriteriaBuilder(List<String> designations) { public List<DeptEmployee> filterbyTitleUsingCriteriaBuilder(List<String> titles) {
CriteriaQuery<DeptEmployee> criteriaQuery = createCriteriaQuery(DeptEmployee.class); CriteriaQuery<DeptEmployee> criteriaQuery = createCriteriaQuery(DeptEmployee.class);
Root<DeptEmployee> root = criteriaQuery.from(DeptEmployee.class); Root<DeptEmployee> root = criteriaQuery.from(DeptEmployee.class);
In<String> inClause = criteriaBuilder.in(root.get("designation")); In<String> inClause = criteriaBuilder.in(root.get("title"));
for (String designaiton : designations) { for (String title : titles) {
inClause.value(designaiton); inClause.value(title);
} }
criteriaQuery.select(root) criteriaQuery.select(root)
.where(inClause); .where(inClause);
@ -39,12 +39,12 @@ public class EmployeeSearchServiceImpl implements EmployeeSearchService {
} }
@Override @Override
public List<DeptEmployee> filterbyDesignationUsingExpression(List<String> designations) { public List<DeptEmployee> filterbyTitleUsingExpression(List<String> titles) {
CriteriaQuery<DeptEmployee> criteriaQuery = createCriteriaQuery(DeptEmployee.class); CriteriaQuery<DeptEmployee> criteriaQuery = createCriteriaQuery(DeptEmployee.class);
Root<DeptEmployee> root = criteriaQuery.from(DeptEmployee.class); Root<DeptEmployee> root = criteriaQuery.from(DeptEmployee.class);
criteriaQuery.select(root) criteriaQuery.select(root)
.where(root.get("designation") .where(root.get("title")
.in(designations)); .in(titles));
TypedQuery<DeptEmployee> query = entityManager.createQuery(criteriaQuery); TypedQuery<DeptEmployee> query = entityManager.createQuery(criteriaQuery);
return query.getResultList(); return query.getResultList();
} }

View File

@ -81,42 +81,41 @@ public class EmployeeSearchServiceIntegrationTest {
@Test @Test
public final void givenCriteriaQuery_whenSearchedUsingCriteriaBuilderWithListofAuthors_thenResultIsFilteredByAuthorNames() { public final void givenCriteriaQuery_whenSearchedUsingCriteriaBuilderWithListofAuthors_thenResultIsFilteredByAuthorNames() {
List<String> designations = new ArrayList<String>() { List<String> titles = new ArrayList<String>() {
{ {
add("Manager"); add("Manager");
add("Senior Manager"); add("Senior Manager");
add("Director"); add("Director");
} }
}; };
List<DeptEmployee> result = searchService.filterbyDesignationUsingCriteriaBuilder(designations); List<DeptEmployee> result = searchService.filterbyTitleUsingCriteriaBuilder(titles);
assertEquals("Number of Employees does not match with expected.", 6, result.size()); assertEquals("Number of Employees does not match with expected.", 6, result.size());
assertThat(result.stream() assertThat(result.stream()
.map(DeptEmployee::getDesignation) .map(DeptEmployee::getTitle)
.distinct() .distinct()
.collect(Collectors.toList()), containsInAnyOrder(designations.toArray())); .collect(Collectors.toList()), containsInAnyOrder(titles.toArray()));
} }
@Test @Test
public final void givenCriteriaQuery_whenSearchedUsingExpressionWithListofAuthors_thenResultIsFilteredByAuthorNames() { public final void givenCriteriaQuery_whenSearchedUsingExpressionWithListofAuthors_thenResultIsFilteredByAuthorNames() {
List<String> designations = new ArrayList<String>() { List<String> titles = new ArrayList<String>() {
{ {
add("Manager"); add("Manager");
add("Senior Manager"); add("Senior Manager");
add("Director"); add("Director");
} }
}; };
List<DeptEmployee> result = searchService.filterbyDesignationUsingExpression(designations); List<DeptEmployee> result = searchService.filterbyTitleUsingExpression(titles);
assertEquals("Number of Employees does not match with expected.", 6, result.size()); assertEquals("Number of Employees does not match with expected.", 6, result.size());
assertThat(result.stream() assertThat(result.stream()
.map(DeptEmployee::getDesignation) .map(DeptEmployee::getTitle)
.distinct() .distinct()
.collect(Collectors.toList()), containsInAnyOrder(designations.toArray())); .collect(Collectors.toList()), containsInAnyOrder(titles.toArray()));
} }
@Test @Test
public final void givenCriteriaQuery_whenSearchedDepartmentLike_thenResultIsFilteredByDepartment() { public final void givenCriteriaQuery_whenSearchedDepartmentLike_thenResultIsFilteredByDepartment() {
List<DeptEmployee> result = searchService.searchByDepartmentQuery("Sales"); List<DeptEmployee> result = searchService.searchByDepartmentQuery("Sales");
assertEquals("Number of Employees does not match with expected.", 7, result.size()); assertEquals("Number of Employees does not match with expected.", 7, result.size());
// assertThat(result.stream().map(DeptEmployee::getDesignation).distinct().collect(Collectors.toList()), containsInAnyOrder(designations.toArray()));
} }
} }