diff --git a/src/test/java/part1/exercise/CompletableFutureBasics.java b/src/test/java/part1/exercise/CompletableFutureBasics.java index cfd0861..b2f939d 100755 --- a/src/test/java/part1/exercise/CompletableFutureBasics.java +++ b/src/test/java/part1/exercise/CompletableFutureBasics.java @@ -61,20 +61,20 @@ public void createNonEmpty() throws ExecutionException, InterruptedException { final Person person = new Person("John", "Galt", 33); // TODO Create non empty Optional - final Optional optPerson = null; + final Optional optPerson = Optional.of(person); assertTrue(optPerson.isPresent()); assertEquals(person, optPerson.get()); // TODO Create stream with a single element - final Stream streamPerson = null; + final Stream streamPerson = Stream.of(person); final List persons = streamPerson.collect(toList()); assertThat(persons.size(), is(1)); assertEquals(person, persons.get(0)); // TODO Create completed CompletableFuture - final CompletableFuture futurePerson = null; + final CompletableFuture futurePerson = CompletableFuture.completedFuture(person); assertTrue(futurePerson.isDone()); assertEquals(person, futurePerson.get()); @@ -83,18 +83,19 @@ public void createNonEmpty() throws ExecutionException, InterruptedException { @Test public void createEmpty() throws ExecutionException, InterruptedException { // TODO Create empty Optional - final Optional optPerson = null; + final Optional optPerson = Optional.empty(); assertFalse(optPerson.isPresent()); // TODO Create empty stream - final Stream streamPerson = null; + final Stream streamPerson = Stream.empty(); final List persons = streamPerson.collect(toList()); assertThat(persons.size(), is(0)); // TODO Complete CompletableFuture with NoSuchElementException - final CompletableFuture futurePerson = null; + final CompletableFuture futurePerson = new CompletableFuture<>(); + futurePerson.completeExceptionally(new NoSuchElementException()); // futurePerson.??? assertTrue(futurePerson.isCompletedExceptionally()); @@ -108,27 +109,30 @@ public void forEach() throws ExecutionException, InterruptedException { final Person person = new Person("John", "Galt", 33); // TODO Create non empty Optional - final Optional optPerson = null; + final Optional optPerson = Optional.of(person); final CompletableFuture result1 = new CompletableFuture<>(); // TODO using optPerson.ifPresent complete result1 + optPerson.ifPresent(result1::complete); assertEquals(person, result1.get()); // TODO Create stream with a single element - final Stream streamPerson = null; + final Stream streamPerson = Stream.of(person); final CompletableFuture result2 = new CompletableFuture<>(); // TODO Using streamPerson.forEach complete result2 + streamPerson.forEach(result2::complete); assertEquals(person, result2.get()); // TODO Create completed CompletableFuture - final CompletableFuture futurePerson = null; + final CompletableFuture futurePerson = CompletableFuture.completedFuture(person); final CompletableFuture result3 = new CompletableFuture<>(); // TODO Using futurePerson.thenAccept complete result3 + futurePerson.thenAccept(result3::complete); assertEquals(person, result3.get()); } @@ -137,26 +141,26 @@ public void map() throws ExecutionException, InterruptedException { final Person person = new Person("John", "Galt", 33); // TODO Create non empty Optional - final Optional optPerson = null; + final Optional optPerson = Optional.of(person); // TODO get Optional from optPerson - final Optional optFirstName = null; + final Optional optFirstName = optPerson.map(Person::getFirstName); assertEquals(person.getFirstName(), optFirstName.get()); // TODO Create stream with a single element - final Stream streamPerson = null; + final Stream streamPerson = Stream.of(person); // TODO Get Stream from streamPerson - final Stream streamFirstName = null; + final Stream streamFirstName = streamPerson.map(Person::getFirstName); assertEquals(person.getFirstName(), streamFirstName.collect(toList()).get(0)); // TODO Create completed CompletableFuture - final CompletableFuture futurePerson = null; + final CompletableFuture futurePerson = CompletableFuture.completedFuture(person); // TODO Get CompletableFuture from futurePerson - final CompletableFuture futureFirstName = null; + final CompletableFuture futureFirstName = futurePerson.thenApply(Person::getFirstName); assertEquals(person.getFirstName(), futureFirstName.get()); } @@ -166,29 +170,29 @@ public void flatMap() throws ExecutionException, InterruptedException { final Person person = employeeDb.get(keys.get(0)).thenApply(Employee::getPerson).get(); // TODO Create non empty Optional - final Optional optPerson = null; + final Optional optPerson = Optional.of(person); // TODO Using flatMap and .getFirstName().codePoints().mapToObj(p -> p).findFirst() // TODO get the first letter of first name if any final Optional optFirstCodePointOfFirstName = - null; + optPerson.flatMap(person1 -> person1.getFirstName().codePoints().mapToObj(value -> value).findFirst()); assertEquals(Integer.valueOf(65), optFirstCodePointOfFirstName.get()); // TODO Create stream with a single element - final Stream streamPerson = null; + final Stream streamPerson = Stream.of(person); // TODO Using flatMapToInt and .getFirstName().codePoints() get codepoints stream from streamPerson - final IntStream codePoints = null; + final IntStream codePoints = streamPerson.flatMapToInt(person1 -> person1.getFirstName().codePoints()); final int[] codePointsArray = codePoints.toArray(); assertEquals(person.getFirstName(), new String(codePointsArray, 0, codePointsArray.length)); // TODO Create completed CompletableFuture - final CompletableFuture futurePerson = null; + final CompletableFuture futurePerson = CompletableFuture.completedFuture(person); // TODO Get CompletableFuture from futurePerson using getKeyByPerson and employeeDb - final CompletableFuture futureEmployee = null; + final CompletableFuture futureEmployee = futurePerson.thenCompose(person1 -> employeeDb.get(getKeyByPerson(person1))); assertEquals(person, futureEmployee.thenApply(Employee::getPerson).get()); }