package org.code2bytes.samples.lambda;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;

import org.junit.Before;
import org.junit.Test;

public class PersonListTest {

	private List<Person> persons;

	@Before
	public void setUp() throws Exception {
		persons = new ArrayList<>();
		persons.add(new Person("Test Person 1", 20));
		persons.add(new Person("Test Person 2", 21));
		persons.add(new Person("Test Person 3", 22));
		persons.add(new Person("Test Person 4", 23));
	}

	@Test
	public void testArraySize() {
		assertThat("array contains 4 elements", 4, equalTo(4));
	}

	@Test
	public void testSumOfAges() {
		Integer total = persons.parallelStream().map(p -> p.getAgeInYears())
				.reduce((sum, p) -> sum + p).get();
		assertThat("sum should be 86", 86, equalTo(total));
	}

	@Test
	public void testPersonList() {
		persons.parallelStream().map(p -> p.getName())
				.forEach(name -> System.out.println(name));
	}

	@Test
	public void testPredicateTerm() {
		Predicate<Integer> isOlderThan22 = age -> age > 22;
		boolean matchAll = persons.stream().map(p -> p.getAgeInYears())
				.allMatch(isOlderThan22);
		assertFalse("there are persons of age 22 or below", matchAll);
		boolean matchAny = persons.stream().map(p -> p.getAgeInYears())
				.anyMatch(isOlderThan22);
		assertTrue("there is at least one persons of age 23 or older", matchAny);
	}
}

@@ -260,24 +260,93 @@
 .util.List;%0A
+import java.util.Optional;%0Aimport java.util.function.BinaryOperator;%0A
 import java.
@@ -839,17 +839,30 @@
 ments%22, 
-4
+persons.size()
 , equalT
@@ -1054,18 +1054,21 @@
 be 86%22, 
-86
+total
 , equalT
@@ -1069,21 +1069,18 @@
 equalTo(
-total
+86
 ));%0A%09%7D%0A%0A
@@ -1116,24 +1116,118 @@
 nList() %7B%0A%09%09
+BinaryOperator%3CString%3E concat = (str, p) -%3E str = str + %22, %22 + p;%0A%09%09Optional%3CString%3E output = 
 persons.para
@@ -1230,32 +1230,37 @@
 parallelStream()
+%0A%09%09%09%09
 .map(p -%3E p.getN
@@ -1269,53 +1269,178 @@
 e())
-%0A%09%09%09%09.forEach(name -%3E System.out.println(name
+.reduce(concat);%0A%09%09String expected = %22Test Person 1, Test Person 2, Test Person 3, Test Person 4%22;%0A%09%09assertThat(%22names should be concated%22, output.get(), equalTo(expected
 ));%0A
@@ -1874,11 +1874,209 @@
 Any);%0A%09%7D
+%0A%0A%09@Test%0A%09public void testCountPersonsOverTwenty() %7B%0A%09%09long count = persons.stream().filter(p -%3E (p.getAgeInYears() %3E 20))%0A%09%09%09%09.count();%0A%09%09assertThat(%223 persons are over 20%22, count, equalTo(3L));%0A%09%7D
 %0A%7D%0A

Modify the person list lambda test