BAEL-2197:Code rework
This commit is contained in:
parent
7070aae38f
commit
ef61b86af3
|
@ -1,114 +1,93 @@
|
|||
package findItems;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class FindItemsBasedOnValues {
|
||||
|
||||
static SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("dd-MM-yyyy");
|
||||
List<Employee> EmplList = new ArrayList<Employee>();
|
||||
List<Department> deptList = new ArrayList<Department>();
|
||||
|
||||
public static void main(String[] args) throws ParseException {
|
||||
FindItemsBasedOnValues findItems = new FindItemsBasedOnValues();
|
||||
findItems.givenListOfCompanies_thenListOfClientsIsFilteredCorrectly();
|
||||
findItems.givenDepartmentList_thenEmployeeListIsFilteredCorrectly();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenListOfCompanies_thenListOfClientsIsFilteredCorrectly() throws ParseException {
|
||||
List<Client> expectedList = new ArrayList<Client>();
|
||||
Client expectedClient = new Client(1001, DATE_FORMAT.parse("01-02-2018"));
|
||||
expectedList.add(expectedClient);
|
||||
public void givenDepartmentList_thenEmployeeListIsFilteredCorrectly() {
|
||||
Integer expectedId = 1002;
|
||||
|
||||
List<Company> listOfCompanies = new ArrayList<Company>();
|
||||
List<Client> listOfClients = new ArrayList<Client>();
|
||||
populate(listOfCompanies, listOfClients);
|
||||
populate(EmplList, deptList);
|
||||
|
||||
List<Client> filteredList = listOfClients.stream()
|
||||
.filter(client -> listOfCompanies.stream()
|
||||
.anyMatch(company -> company.getType()
|
||||
.equals("eMart") && company.getProjectId()
|
||||
.equals(client.getProjectId()) && company.getStart()
|
||||
.after(client.getDate()) && company.getEnd()
|
||||
.before(client.getDate())))
|
||||
List<Employee> filteredList = EmplList.stream()
|
||||
.filter(empl -> deptList.stream()
|
||||
.anyMatch(dept -> dept.getDepartment()
|
||||
.equals("sales") && empl.getEmployeeId()
|
||||
.equals(dept.getEmployeeId())))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
assertEquals(expectedClient.getProjectId(), filteredList.get(0)
|
||||
.getProjectId());
|
||||
assertEquals(expectedClient.getDate(), filteredList.get(0)
|
||||
.getDate());
|
||||
assertEquals(expectedId, filteredList.get(0)
|
||||
.getEmployeeId());
|
||||
}
|
||||
|
||||
private void populate(List<Company> companyList, List<Client> clientList) throws ParseException {
|
||||
Company company1 = new Company(1001, DATE_FORMAT.parse("01-03-2018"), DATE_FORMAT.parse("01-01-2018"), "eMart");
|
||||
Company company2 = new Company(1002, DATE_FORMAT.parse("01-02-2018"), DATE_FORMAT.parse("01-04-2018"), "commerce");
|
||||
Company company3 = new Company(1003, DATE_FORMAT.parse("01-06-2018"), DATE_FORMAT.parse("01-02-2018"), "eMart");
|
||||
Company company4 = new Company(1004, DATE_FORMAT.parse("01-03-2018"), DATE_FORMAT.parse("01-06-2018"), "blog");
|
||||
private void populate(List<Employee> EmplList, List<Department> deptList) {
|
||||
Employee employee1 = new Employee(1001, "empl1");
|
||||
Employee employee2 = new Employee(1002, "empl2");
|
||||
Employee employee3 = new Employee(1003, "empl3");
|
||||
|
||||
Collections.addAll(companyList, company1, company2, company3, company4);
|
||||
Collections.addAll(EmplList, employee1, employee2, employee3);
|
||||
|
||||
Client client1 = new Client(1001, DATE_FORMAT.parse("01-02-2018"));
|
||||
Client client2 = new Client(1003, DATE_FORMAT.parse("01-04-2018"));
|
||||
Client client3 = new Client(1005, DATE_FORMAT.parse("01-07-2018"));
|
||||
Client client4 = new Client(1007, DATE_FORMAT.parse("01-08-2018"));
|
||||
Department department1 = new Department(1002, "sales");
|
||||
Department department2 = new Department(1003, "marketing");
|
||||
Department department3 = new Department(1004, "sales");
|
||||
|
||||
Collections.addAll(clientList, client1, client2, client3, client4);
|
||||
Collections.addAll(deptList, department1, department2, department3);
|
||||
}
|
||||
}
|
||||
|
||||
class Company {
|
||||
Long projectId;
|
||||
Date start;
|
||||
Date end;
|
||||
String type;
|
||||
class Employee {
|
||||
Integer employeeId;
|
||||
String employeeName;
|
||||
|
||||
public Company(long projectId, Date start, Date end, String type) {
|
||||
public Employee(Integer employeeId, String employeeName) {
|
||||
super();
|
||||
this.projectId = projectId;
|
||||
this.start = start;
|
||||
this.end = end;
|
||||
this.type = type;
|
||||
this.employeeId = employeeId;
|
||||
this.employeeName = employeeName;
|
||||
}
|
||||
|
||||
public Long getProjectId() {
|
||||
return projectId;
|
||||
public Integer getEmployeeId() {
|
||||
return employeeId;
|
||||
}
|
||||
|
||||
public Date getStart() {
|
||||
return start;
|
||||
}
|
||||
|
||||
public Date getEnd() {
|
||||
return end;
|
||||
}
|
||||
|
||||
public String getType() {
|
||||
return type;
|
||||
public String getEmployeeName() {
|
||||
return employeeName;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class Client {
|
||||
Long projectId;
|
||||
Date date;
|
||||
class Department {
|
||||
Integer employeeId;
|
||||
String department;
|
||||
|
||||
public Client(long projectId, Date date) {
|
||||
public Department(Integer employeeId, String department) {
|
||||
super();
|
||||
this.projectId = projectId;
|
||||
this.date = date;
|
||||
this.employeeId = employeeId;
|
||||
this.department = department;
|
||||
}
|
||||
|
||||
public Long getProjectId() {
|
||||
return projectId;
|
||||
public Integer getEmployeeId() {
|
||||
return employeeId;
|
||||
}
|
||||
|
||||
public Date getDate() {
|
||||
return date;
|
||||
public String getDepartment() {
|
||||
return department;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue