diff --git a/src/test/java/lambda/part1/exercise/Lambdas01Exercise.java b/src/test/java/lambda/part1/exercise/Lambdas01Exercise.java index 14cfc0d..fd39fe9 100644 --- a/src/test/java/lambda/part1/exercise/Lambdas01Exercise.java +++ b/src/test/java/lambda/part1/exercise/Lambdas01Exercise.java @@ -42,4 +42,4 @@ void findFirstWithAge30() { assertEquals(person, new Person("name 1", "lastName 2", 30)); } -} +} \ No newline at end of file diff --git a/src/test/java/lambda/part1/exercise/Lambdas02Exercise.java b/src/test/java/lambda/part1/exercise/Lambdas02Exercise.java index 8f47d34..83dbcd2 100644 --- a/src/test/java/lambda/part1/exercise/Lambdas02Exercise.java +++ b/src/test/java/lambda/part1/exercise/Lambdas02Exercise.java @@ -1,13 +1,17 @@ package lambda.part1.exercise; +import com.google.common.base.Optional; +import com.google.common.collect.FluentIterable; import com.google.common.collect.ImmutableList; import data.Person; import org.junit.jupiter.api.Test; +import java.util.Arrays; import java.util.List; -import static org.junit.jupiter.api.Assertions.assertArrayEquals; -import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.is; +import static org.junit.jupiter.api.Assertions.fail; class Lambdas02Exercise { @Test @@ -18,13 +22,21 @@ void sortPersonsByAge() { new Person("name 2", "lastName 1", 30) }; - // TODO use Arrays.sort + Arrays.sort(persons, (o1, o2) -> { + if (o1.getAge() > o2.getAge()) + return 1; + else if (o1.getAge() < o2.getAge()) + return -1; + else + return 0; + }); + - assertArrayEquals(persons, new Person[]{ + assertThat(persons, is(new Person[]{ new Person("name 3", "lastName 3", 20), new Person("name 2", "lastName 1", 30), new Person("name 1", "lastName 2", 40), - }); + })); } @Test @@ -35,10 +47,14 @@ void findFirstWithAge30() { new Person("name 2", "lastName 1", 30) ); - Person person = null; + final Optional personOptional = + FluentIterable.from(persons) + .firstMatch(p -> p.getAge() == 30); - // TODO use FluentIterable - assertEquals(person, new Person("name 1", "lastName 2", 30)); + if (personOptional.isPresent()) + assertThat(personOptional.get(), is(new Person("name 1", "lastName 2", 30))); + else + fail(""); } -} +} \ No newline at end of file