package ch.makery.address; import ch.makery.address.model.Person; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import java.util.*; /** * Created by Geo on 3/3/2017. */ @RunWith(value = Parameterized.class) public class StreamTest { public Map<String, List<Person>> people; @Before
public void init () { people = new HashMap<>(); people.put("Bucharest", new ArrayList<Person>()); people.put("Pitesti", new ArrayList<Person>()); people.put("Timisoara", new ArrayList<Person>()); people.put("Iasi", new ArrayList<Person>()); people.put("Cluj", new ArrayList<Person>()); people.get("Bucharest").add(new Person("Geo", "T", "Bucharest", 51081, 1969, 2, 21)); people.get("Bucharest").add(new Person("Kate", "M", "Bucharest", 97124, 1999, 2, 21)); people.get("Bucharest").add(new Person("Alex", "W", "Bucharest", 51081, 1999, 2, 21)); people.get("Bucharest").add(new Person("Ana", "T", "Bucharest", 51081, 2009, 2, 21)); people.get("Pitesti").add(new Person("Lena", "A", "Pitesti", 70503, 2009, 2, 21)); people.get("Timisoara").add(new Person("Catalin", "F", "Timisoara", 51081, 2000, 2, 21)); people.get("Iasi").add(new Person("Petri", "T", "Iasi", 51081, 1976, 2, 21)); people.get("Cluj").add(new Person("Sahar", "A", "Cluj", 97124, 1977, 2, 21)); } @Parameterized.Parameter(value=0) public int age; @Parameterized.Parameter(value=1) public String city; @Parameterized.Parameter(value=2) public List<Integer> postalCodes; @Parameterized.Parameter(value=3) public String expected; @Parameterized.Parameters(name = "{index}: testFilterPersons({0},{1},{2}) => {3}") public static Collection<Object[]> data() { return Arrays.asList(new Object[][]{ {18, "Bucharest", Arrays.asList(51081), "Geo T\nAlex W\n"}, {18, "Cluj", Arrays.asList(51081, 97124), "Sahar A\n"} }); } @Test
public void testFilterPersons() { assert(filterPersons(age,city,postalCodes).equals(expected)); } private String filterPersons(int age, String city, List<Integer> postalCodes) { // why void?
return (people.get(city)).stream()
.filter( person -> postalCodes.contains(person.getPostalCode()) && person.getAge() >= age) .map ( person -> person.getFirstName() + " " + person.getLastName()) .reduce ("", (a,b) -> a+b+"\n"); } }
Niciun comentariu:
Trimiteți un comentariu