BAEL-5558 (#12222)
* [BAEL-5438] Added Criteria Queries for Employee * [BAEL-5558] Sorting By Date in Java * BAEL-5558: Added and refactored tests name * [BAEL-5558] Refactored the code. * [BAEL-5558] Resolved PMD violation. * [BAEL-5558] Indented with 4 spaces * Refactored the Employee class Co-authored-by: Mayank Agarwal <mayankaggarwal@zeta.tech>
This commit is contained in:
parent
9c40bfe297
commit
26224e4686
|
@ -25,5 +25,4 @@
|
||||||
<properties>
|
<properties>
|
||||||
<commons-lang.version>2.2</commons-lang.version>
|
<commons-lang.version>2.2</commons-lang.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
</project>
|
</project>
|
|
@ -4,69 +4,48 @@ import java.util.Date;
|
||||||
|
|
||||||
public class Employee implements Comparable<Employee>{
|
public class Employee implements Comparable<Employee>{
|
||||||
|
|
||||||
private String name;
|
private String name;
|
||||||
private int age;
|
private Date joiningDate;
|
||||||
private double salary;
|
|
||||||
private Date joiningDate;
|
|
||||||
|
|
||||||
public Employee(String name, int age, double salary, Date joiningDate) {
|
public Employee(String name, Date joiningDate) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.age = age;
|
this.joiningDate = joiningDate;
|
||||||
this.salary = salary;
|
}
|
||||||
this.joiningDate = joiningDate;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getName() {
|
public String getName() {
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setName(String name) {
|
public void setName(String name) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getAge() {
|
public Date getJoiningDate() {
|
||||||
return age;
|
return joiningDate;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setAge(int age) {
|
public void setJoiningDate(Date joiningDate) {
|
||||||
this.age = age;
|
this.joiningDate = joiningDate;
|
||||||
}
|
}
|
||||||
|
|
||||||
public double getSalary() {
|
@Override
|
||||||
return salary;
|
public boolean equals(Object obj) {
|
||||||
}
|
return ((Employee) obj).getName()
|
||||||
|
.equals(getName());
|
||||||
|
}
|
||||||
|
|
||||||
public void setSalary(double salary) {
|
@Override
|
||||||
this.salary = salary;
|
public String toString() {
|
||||||
}
|
return new StringBuffer().append("(")
|
||||||
|
.append(getName())
|
||||||
|
.append(",")
|
||||||
|
.append(getJoiningDate())
|
||||||
|
.append(")")
|
||||||
|
.toString();
|
||||||
|
}
|
||||||
|
|
||||||
public Date getJoiningDate() {
|
@Override
|
||||||
return joiningDate;
|
public int compareTo(Employee employee) {
|
||||||
}
|
return getJoiningDate().compareTo(employee.getJoiningDate());
|
||||||
|
}
|
||||||
public void setJoiningDate(Date joiningDate) {
|
|
||||||
this.joiningDate = joiningDate;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean equals(Object obj) {
|
|
||||||
return ((Employee) obj).getName()
|
|
||||||
.equals(getName());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString() {
|
|
||||||
return new StringBuffer().append("(")
|
|
||||||
.append(getName()).append(",")
|
|
||||||
.append(getAge())
|
|
||||||
.append(",")
|
|
||||||
.append(getSalary()).append(",").append(getJoiningDate())
|
|
||||||
.append(")")
|
|
||||||
.toString();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int compareTo(Employee employee) {
|
|
||||||
return getJoiningDate().compareTo(employee.getJoiningDate());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,133 +14,132 @@ import org.junit.Test;
|
||||||
|
|
||||||
public class EmployeeSortingByDateUnitTest {
|
public class EmployeeSortingByDateUnitTest {
|
||||||
|
|
||||||
private List<Employee> employees = new ArrayList<>();
|
private List<Employee> employees = new ArrayList<>();
|
||||||
private List<Employee> employeesSortedByDateAsc = new ArrayList<>();
|
private List<Employee> employeesSortedByDateAsc = new ArrayList<>();
|
||||||
private List<Employee> employeesSortedByDateDesc = new ArrayList<>();
|
private List<Employee> employeesSortedByDateDesc = new ArrayList<>();
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void initVariables() {
|
public void initVariables() {
|
||||||
|
|
||||||
Collections.addAll(employees,
|
Collections.addAll(employees,
|
||||||
new Employee("Earl", 43, 10000, DateUtils.addMonths(new Date(), -2)),
|
new Employee("Earl", DateUtils.addMonths(new Date(), -2)),
|
||||||
new Employee("Frank", 33, 7000, DateUtils.addDays(new Date(), -20)),
|
new Employee("Frank", DateUtils.addDays(new Date(), -20)),
|
||||||
new Employee("Steve", 26, 6000, DateUtils.addDays(new Date(), -10)),
|
new Employee("Steve", DateUtils.addDays(new Date(), -10)),
|
||||||
new Employee("Jessica", 23, 4000, DateUtils.addMonths(new Date(), -6)),
|
new Employee("Jessica", DateUtils.addMonths(new Date(), -6)),
|
||||||
new Employee("Pearl", 33, 6000, DateUtils.addYears(new Date(), -1)),
|
new Employee("Pearl", DateUtils.addYears(new Date(), -1)),
|
||||||
new Employee("John", 23, 5000, new Date())
|
new Employee("John", new Date())
|
||||||
);
|
);
|
||||||
|
|
||||||
Collections.addAll(employeesSortedByDateDesc,
|
Collections.addAll(employeesSortedByDateDesc,
|
||||||
new Employee("John", 23, 5000, new Date()),
|
new Employee("John", new Date()),
|
||||||
new Employee("Steve", 26, 6000, DateUtils.addDays(new Date(), -10)),
|
new Employee("Steve", DateUtils.addDays(new Date(), -10)),
|
||||||
new Employee("Frank", 33, 7000, DateUtils.addDays(new Date(), -20)),
|
new Employee("Frank", DateUtils.addDays(new Date(), -20)),
|
||||||
new Employee("Earl", 43, 10000, DateUtils.addMonths(new Date(), -2)),
|
new Employee("Earl", DateUtils.addMonths(new Date(), -2)),
|
||||||
new Employee("Jessica", 23, 4000, DateUtils.addMonths(new Date(), -6)),
|
new Employee("Jessica", DateUtils.addMonths(new Date(), -6)),
|
||||||
new Employee("Pearl", 33, 6000, DateUtils.addYears(new Date(), -1))
|
new Employee("Pearl", DateUtils.addYears(new Date(), -1))
|
||||||
);
|
);
|
||||||
|
|
||||||
Collections.addAll(employeesSortedByDateAsc,
|
Collections.addAll(employeesSortedByDateAsc,
|
||||||
new Employee("Pearl", 33, 6000, DateUtils.addYears(new Date(), -1)),
|
new Employee("Pearl", DateUtils.addYears(new Date(), -1)),
|
||||||
new Employee("Jessica", 23, 4000, DateUtils.addMonths(new Date(), -6)),
|
new Employee("Jessica", DateUtils.addMonths(new Date(), -6)),
|
||||||
new Employee("Earl", 43, 10000, DateUtils.addMonths(new Date(), -2)),
|
new Employee("Earl", DateUtils.addMonths(new Date(), -2)),
|
||||||
new Employee("Frank", 33, 7000, DateUtils.addDays(new Date(), -20)),
|
new Employee("Frank", DateUtils.addDays(new Date(), -20)),
|
||||||
new Employee("Steve", 26, 6000, DateUtils.addDays(new Date(), -10)),
|
new Employee("Steve", DateUtils.addDays(new Date(), -10)),
|
||||||
new Employee("John", 23, 5000, new Date())
|
new Employee("John", new Date())
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenEmpList_SortEmpList_thenSortedListinNaturalOrder() {
|
public void givenEmpList_SortEmpList_thenSortedListinNaturalOrder() {
|
||||||
Collections.sort(employees);
|
Collections.sort(employees);
|
||||||
|
System.out.println(employees);
|
||||||
|
|
||||||
assertEquals(employees, employeesSortedByDateAsc);
|
assertEquals(employees, employeesSortedByDateAsc);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenEmpList_SortEmpList_thenCheckSortedList() {
|
public void givenEmpList_SortEmpList_thenCheckSortedList() {
|
||||||
|
|
||||||
Collections.sort(employees, new Comparator<Employee>() {
|
Collections.sort(employees, new Comparator<Employee>() {
|
||||||
public int compare(Employee o1, Employee o2) {
|
public int compare(Employee o1, Employee o2) {
|
||||||
return o1.getJoiningDate().compareTo(o2.getJoiningDate());
|
return o1.getJoiningDate().compareTo(o2.getJoiningDate());
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
assertEquals(employees, employeesSortedByDateAsc);
|
assertEquals(employees, employeesSortedByDateAsc);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenEmpList_SortEmpList_thenCheckSortedListV1() {
|
public void givenEmpList_SortEmpList_thenCheckSortedListV1() {
|
||||||
|
|
||||||
Collections.sort(employees, new Comparator<Employee>() {
|
Collections.sort(employees, new Comparator<Employee>() {
|
||||||
public int compare(Employee emp1, Employee emp2) {
|
public int compare(Employee emp1, Employee emp2) {
|
||||||
if (emp1.getJoiningDate() == null || emp2.getJoiningDate() == null)
|
if (emp1.getJoiningDate() == null || emp2.getJoiningDate() == null)
|
||||||
return 0;
|
return 0;
|
||||||
return emp1.getJoiningDate().compareTo(emp2.getJoiningDate());
|
return emp1.getJoiningDate().compareTo(emp2.getJoiningDate());
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
assertEquals(employees, employeesSortedByDateAsc);
|
assertEquals(employees, employeesSortedByDateAsc);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenEmpList_SortEmpList_thenSortedListinAscOrder() {
|
public void givenEmpList_SortEmpList_thenSortedListinDescOrder() {
|
||||||
Collections.sort(employees, Collections.reverseOrder());
|
Collections.sort(employees, Collections.reverseOrder());
|
||||||
|
|
||||||
assertEquals(employees, employeesSortedByDateDesc);
|
assertEquals(employees, employeesSortedByDateDesc);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenEmpList_SortEmpList_thenCheckSortedListAsc() {
|
public void givenEmpList_SortEmpList_thenCheckSortedListAsc() {
|
||||||
|
|
||||||
Collections.sort(employees, new Comparator<Employee>() {
|
Collections.sort(employees, new Comparator<Employee>() {
|
||||||
public int compare(Employee emp1, Employee emp2) {
|
public int compare(Employee emp1, Employee emp2) {
|
||||||
return emp2.getJoiningDate().compareTo(emp1.getJoiningDate());
|
return emp2.getJoiningDate().compareTo(emp1.getJoiningDate());
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
assertEquals(employees, employeesSortedByDateDesc);
|
assertEquals(employees, employeesSortedByDateDesc);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenEmpList_SortEmpList_thenCheckSortedListAscV1() {
|
public void givenEmpList_SortEmpList_thenCheckSortedListDescV1() {
|
||||||
|
|
||||||
Collections.sort(employees, new Comparator<Employee>() {
|
Collections.sort(employees, new Comparator<Employee>() {
|
||||||
public int compare(Employee emp1, Employee emp2) {
|
public int compare(Employee emp1, Employee emp2) {
|
||||||
if (emp1.getJoiningDate() == null || emp2.getJoiningDate() == null)
|
return emp2.getJoiningDate().compareTo(emp1.getJoiningDate());
|
||||||
return 0;
|
}
|
||||||
return emp2.getJoiningDate().compareTo(emp1.getJoiningDate());
|
});
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
assertEquals(employees, employeesSortedByDateDesc);
|
assertEquals(employees, employeesSortedByDateDesc);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenEmpList_SortEmpList_thenCheckSortedListDescLambda() {
|
public void givenEmpList_SortEmpList_thenCheckSortedListDescLambda() {
|
||||||
|
|
||||||
Collections.sort(employees,
|
Collections.sort(employees,
|
||||||
(emp1, emp2) -> emp2.getJoiningDate().compareTo(emp1.getJoiningDate()));
|
(emp1, emp2) -> emp2.getJoiningDate().compareTo(emp1.getJoiningDate()));
|
||||||
|
|
||||||
assertEquals(employees, employeesSortedByDateDesc);
|
assertEquals(employees, employeesSortedByDateDesc);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenEmpList_SortEmpList_thenCheckSortedListDescLambdaV1() {
|
public void givenEmpList_SortEmpList_thenCheckSortedListDescLambdaV1() {
|
||||||
|
|
||||||
Collections.sort(employees, (emp1, emp2) -> {
|
Collections.sort(employees, (emp1, emp2) -> {
|
||||||
if (emp1.getJoiningDate() == null || emp2.getJoiningDate() == null)
|
if (emp1.getJoiningDate() == null || emp2.getJoiningDate() == null)
|
||||||
return 0;
|
return 0;
|
||||||
return emp2.getJoiningDate().compareTo(emp1.getJoiningDate());
|
return emp2.getJoiningDate().compareTo(emp1.getJoiningDate());
|
||||||
});
|
});
|
||||||
|
|
||||||
assertEquals(employees, employeesSortedByDateDesc);
|
assertEquals(employees, employeesSortedByDateDesc);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenEmpList_SortEmpList_thenCheckSortedListAscLambda() {
|
public void givenEmpList_SortEmpList_thenCheckSortedListAscLambda() {
|
||||||
Collections.sort(employees,
|
Collections.sort(employees,
|
||||||
Comparator.comparing(Employee::getJoiningDate));
|
Comparator.comparing(Employee::getJoiningDate));
|
||||||
assertEquals(employees, employeesSortedByDateAsc);
|
assertEquals(employees, employeesSortedByDateAsc);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue