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>
|
||||
<commons-lang.version>2.2</commons-lang.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -4,69 +4,48 @@ import java.util.Date;
|
|||
|
||||
public class Employee implements Comparable<Employee>{
|
||||
|
||||
private String name;
|
||||
private int age;
|
||||
private double salary;
|
||||
private Date joiningDate;
|
||||
private String name;
|
||||
private Date joiningDate;
|
||||
|
||||
public Employee(String name, int age, double salary, Date joiningDate) {
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
this.salary = salary;
|
||||
this.joiningDate = joiningDate;
|
||||
}
|
||||
public Employee(String name, Date joiningDate) {
|
||||
this.name = name;
|
||||
this.joiningDate = joiningDate;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public int getAge() {
|
||||
return age;
|
||||
}
|
||||
public Date getJoiningDate() {
|
||||
return joiningDate;
|
||||
}
|
||||
|
||||
public void setAge(int age) {
|
||||
this.age = age;
|
||||
}
|
||||
public void setJoiningDate(Date joiningDate) {
|
||||
this.joiningDate = joiningDate;
|
||||
}
|
||||
|
||||
public double getSalary() {
|
||||
return salary;
|
||||
}
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
return ((Employee) obj).getName()
|
||||
.equals(getName());
|
||||
}
|
||||
|
||||
public void setSalary(double salary) {
|
||||
this.salary = salary;
|
||||
}
|
||||
@Override
|
||||
public String toString() {
|
||||
return new StringBuffer().append("(")
|
||||
.append(getName())
|
||||
.append(",")
|
||||
.append(getJoiningDate())
|
||||
.append(")")
|
||||
.toString();
|
||||
}
|
||||
|
||||
public Date getJoiningDate() {
|
||||
return joiningDate;
|
||||
}
|
||||
|
||||
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());
|
||||
}
|
||||
@Override
|
||||
public int compareTo(Employee employee) {
|
||||
return getJoiningDate().compareTo(employee.getJoiningDate());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -14,133 +14,132 @@ import org.junit.Test;
|
|||
|
||||
public class EmployeeSortingByDateUnitTest {
|
||||
|
||||
private List<Employee> employees = new ArrayList<>();
|
||||
private List<Employee> employeesSortedByDateAsc = new ArrayList<>();
|
||||
private List<Employee> employeesSortedByDateDesc = new ArrayList<>();
|
||||
private List<Employee> employees = new ArrayList<>();
|
||||
private List<Employee> employeesSortedByDateAsc = new ArrayList<>();
|
||||
private List<Employee> employeesSortedByDateDesc = new ArrayList<>();
|
||||
|
||||
@Before
|
||||
public void initVariables() {
|
||||
@Before
|
||||
public void initVariables() {
|
||||
|
||||
Collections.addAll(employees,
|
||||
new Employee("Earl", 43, 10000, DateUtils.addMonths(new Date(), -2)),
|
||||
new Employee("Frank", 33, 7000, DateUtils.addDays(new Date(), -20)),
|
||||
new Employee("Steve", 26, 6000, DateUtils.addDays(new Date(), -10)),
|
||||
new Employee("Jessica", 23, 4000, DateUtils.addMonths(new Date(), -6)),
|
||||
new Employee("Pearl", 33, 6000, DateUtils.addYears(new Date(), -1)),
|
||||
new Employee("John", 23, 5000, new Date())
|
||||
);
|
||||
Collections.addAll(employees,
|
||||
new Employee("Earl", DateUtils.addMonths(new Date(), -2)),
|
||||
new Employee("Frank", DateUtils.addDays(new Date(), -20)),
|
||||
new Employee("Steve", DateUtils.addDays(new Date(), -10)),
|
||||
new Employee("Jessica", DateUtils.addMonths(new Date(), -6)),
|
||||
new Employee("Pearl", DateUtils.addYears(new Date(), -1)),
|
||||
new Employee("John", new Date())
|
||||
);
|
||||
|
||||
Collections.addAll(employeesSortedByDateDesc,
|
||||
new Employee("John", 23, 5000, new Date()),
|
||||
new Employee("Steve", 26, 6000, DateUtils.addDays(new Date(), -10)),
|
||||
new Employee("Frank", 33, 7000, DateUtils.addDays(new Date(), -20)),
|
||||
new Employee("Earl", 43, 10000, DateUtils.addMonths(new Date(), -2)),
|
||||
new Employee("Jessica", 23, 4000, DateUtils.addMonths(new Date(), -6)),
|
||||
new Employee("Pearl", 33, 6000, DateUtils.addYears(new Date(), -1))
|
||||
);
|
||||
Collections.addAll(employeesSortedByDateDesc,
|
||||
new Employee("John", new Date()),
|
||||
new Employee("Steve", DateUtils.addDays(new Date(), -10)),
|
||||
new Employee("Frank", DateUtils.addDays(new Date(), -20)),
|
||||
new Employee("Earl", DateUtils.addMonths(new Date(), -2)),
|
||||
new Employee("Jessica", DateUtils.addMonths(new Date(), -6)),
|
||||
new Employee("Pearl", DateUtils.addYears(new Date(), -1))
|
||||
);
|
||||
|
||||
Collections.addAll(employeesSortedByDateAsc,
|
||||
new Employee("Pearl", 33, 6000, DateUtils.addYears(new Date(), -1)),
|
||||
new Employee("Jessica", 23, 4000, DateUtils.addMonths(new Date(), -6)),
|
||||
new Employee("Earl", 43, 10000, DateUtils.addMonths(new Date(), -2)),
|
||||
new Employee("Frank", 33, 7000, DateUtils.addDays(new Date(), -20)),
|
||||
new Employee("Steve", 26, 6000, DateUtils.addDays(new Date(), -10)),
|
||||
new Employee("John", 23, 5000, new Date())
|
||||
);
|
||||
}
|
||||
Collections.addAll(employeesSortedByDateAsc,
|
||||
new Employee("Pearl", DateUtils.addYears(new Date(), -1)),
|
||||
new Employee("Jessica", DateUtils.addMonths(new Date(), -6)),
|
||||
new Employee("Earl", DateUtils.addMonths(new Date(), -2)),
|
||||
new Employee("Frank", DateUtils.addDays(new Date(), -20)),
|
||||
new Employee("Steve", DateUtils.addDays(new Date(), -10)),
|
||||
new Employee("John", new Date())
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEmpList_SortEmpList_thenSortedListinNaturalOrder() {
|
||||
Collections.sort(employees);
|
||||
@Test
|
||||
public void givenEmpList_SortEmpList_thenSortedListinNaturalOrder() {
|
||||
Collections.sort(employees);
|
||||
System.out.println(employees);
|
||||
|
||||
assertEquals(employees, employeesSortedByDateAsc);
|
||||
}
|
||||
assertEquals(employees, employeesSortedByDateAsc);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEmpList_SortEmpList_thenCheckSortedList() {
|
||||
@Test
|
||||
public void givenEmpList_SortEmpList_thenCheckSortedList() {
|
||||
|
||||
Collections.sort(employees, new Comparator<Employee>() {
|
||||
public int compare(Employee o1, Employee o2) {
|
||||
return o1.getJoiningDate().compareTo(o2.getJoiningDate());
|
||||
}
|
||||
});
|
||||
Collections.sort(employees, new Comparator<Employee>() {
|
||||
public int compare(Employee o1, Employee o2) {
|
||||
return o1.getJoiningDate().compareTo(o2.getJoiningDate());
|
||||
}
|
||||
});
|
||||
|
||||
assertEquals(employees, employeesSortedByDateAsc);
|
||||
}
|
||||
assertEquals(employees, employeesSortedByDateAsc);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEmpList_SortEmpList_thenCheckSortedListV1() {
|
||||
@Test
|
||||
public void givenEmpList_SortEmpList_thenCheckSortedListV1() {
|
||||
|
||||
Collections.sort(employees, new Comparator<Employee>() {
|
||||
public int compare(Employee emp1, Employee emp2) {
|
||||
if (emp1.getJoiningDate() == null || emp2.getJoiningDate() == null)
|
||||
return 0;
|
||||
return emp1.getJoiningDate().compareTo(emp2.getJoiningDate());
|
||||
}
|
||||
});
|
||||
Collections.sort(employees, new Comparator<Employee>() {
|
||||
public int compare(Employee emp1, Employee emp2) {
|
||||
if (emp1.getJoiningDate() == null || emp2.getJoiningDate() == null)
|
||||
return 0;
|
||||
return emp1.getJoiningDate().compareTo(emp2.getJoiningDate());
|
||||
}
|
||||
});
|
||||
|
||||
assertEquals(employees, employeesSortedByDateAsc);
|
||||
}
|
||||
assertEquals(employees, employeesSortedByDateAsc);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEmpList_SortEmpList_thenSortedListinAscOrder() {
|
||||
Collections.sort(employees, Collections.reverseOrder());
|
||||
@Test
|
||||
public void givenEmpList_SortEmpList_thenSortedListinDescOrder() {
|
||||
Collections.sort(employees, Collections.reverseOrder());
|
||||
|
||||
assertEquals(employees, employeesSortedByDateDesc);
|
||||
}
|
||||
assertEquals(employees, employeesSortedByDateDesc);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEmpList_SortEmpList_thenCheckSortedListAsc() {
|
||||
@Test
|
||||
public void givenEmpList_SortEmpList_thenCheckSortedListAsc() {
|
||||
|
||||
Collections.sort(employees, new Comparator<Employee>() {
|
||||
public int compare(Employee emp1, Employee emp2) {
|
||||
return emp2.getJoiningDate().compareTo(emp1.getJoiningDate());
|
||||
}
|
||||
});
|
||||
Collections.sort(employees, new Comparator<Employee>() {
|
||||
public int compare(Employee emp1, Employee emp2) {
|
||||
return emp2.getJoiningDate().compareTo(emp1.getJoiningDate());
|
||||
}
|
||||
});
|
||||
|
||||
assertEquals(employees, employeesSortedByDateDesc);
|
||||
}
|
||||
assertEquals(employees, employeesSortedByDateDesc);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEmpList_SortEmpList_thenCheckSortedListAscV1() {
|
||||
@Test
|
||||
public void givenEmpList_SortEmpList_thenCheckSortedListDescV1() {
|
||||
|
||||
Collections.sort(employees, new Comparator<Employee>() {
|
||||
public int compare(Employee emp1, Employee emp2) {
|
||||
if (emp1.getJoiningDate() == null || emp2.getJoiningDate() == null)
|
||||
return 0;
|
||||
return emp2.getJoiningDate().compareTo(emp1.getJoiningDate());
|
||||
}
|
||||
});
|
||||
Collections.sort(employees, new Comparator<Employee>() {
|
||||
public int compare(Employee emp1, Employee emp2) {
|
||||
return emp2.getJoiningDate().compareTo(emp1.getJoiningDate());
|
||||
}
|
||||
});
|
||||
|
||||
assertEquals(employees, employeesSortedByDateDesc);
|
||||
}
|
||||
assertEquals(employees, employeesSortedByDateDesc);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEmpList_SortEmpList_thenCheckSortedListDescLambda() {
|
||||
@Test
|
||||
public void givenEmpList_SortEmpList_thenCheckSortedListDescLambda() {
|
||||
|
||||
Collections.sort(employees,
|
||||
(emp1, emp2) -> emp2.getJoiningDate().compareTo(emp1.getJoiningDate()));
|
||||
Collections.sort(employees,
|
||||
(emp1, emp2) -> emp2.getJoiningDate().compareTo(emp1.getJoiningDate()));
|
||||
|
||||
assertEquals(employees, employeesSortedByDateDesc);
|
||||
}
|
||||
assertEquals(employees, employeesSortedByDateDesc);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEmpList_SortEmpList_thenCheckSortedListDescLambdaV1() {
|
||||
@Test
|
||||
public void givenEmpList_SortEmpList_thenCheckSortedListDescLambdaV1() {
|
||||
|
||||
Collections.sort(employees, (emp1, emp2) -> {
|
||||
if (emp1.getJoiningDate() == null || emp2.getJoiningDate() == null)
|
||||
return 0;
|
||||
return emp2.getJoiningDate().compareTo(emp1.getJoiningDate());
|
||||
});
|
||||
Collections.sort(employees, (emp1, emp2) -> {
|
||||
if (emp1.getJoiningDate() == null || emp2.getJoiningDate() == null)
|
||||
return 0;
|
||||
return emp2.getJoiningDate().compareTo(emp1.getJoiningDate());
|
||||
});
|
||||
|
||||
assertEquals(employees, employeesSortedByDateDesc);
|
||||
}
|
||||
assertEquals(employees, employeesSortedByDateDesc);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEmpList_SortEmpList_thenCheckSortedListAscLambda() {
|
||||
Collections.sort(employees,
|
||||
Comparator.comparing(Employee::getJoiningDate));
|
||||
assertEquals(employees, employeesSortedByDateAsc);
|
||||
}
|
||||
@Test
|
||||
public void givenEmpList_SortEmpList_thenCheckSortedListAscLambda() {
|
||||
Collections.sort(employees,
|
||||
Comparator.comparing(Employee::getJoiningDate));
|
||||
assertEquals(employees, employeesSortedByDateAsc);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue