BAEL-2197:Code rework

This commit is contained in:
Satyam 2018-10-11 20:55:55 +05:30
parent 7070aae38f
commit ef61b86af3
1 changed files with 45 additions and 66 deletions

View File

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