|
| 1 | +package com.baeldung.stream.filter; |
| 2 | + |
| 3 | +import org.junit.jupiter.api.Test; |
| 4 | +import pl.touk.throwing.ThrowingPredicate; |
| 5 | +import pl.touk.throwing.exception.WrappedException; |
| 6 | + |
| 7 | +import java.io.IOException; |
| 8 | +import java.util.Arrays; |
| 9 | +import java.util.List; |
| 10 | +import java.util.Optional; |
| 11 | +import java.util.stream.Stream; |
| 12 | + |
| 13 | +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; |
| 14 | +import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy; |
| 15 | + |
| 16 | +public class StreamFilterUnitTest { |
| 17 | + |
| 18 | + @Test |
| 19 | + public void givenListOfCustomers_whenFilterByLambda_thenGetTwo() { |
| 20 | + List<Customer> customers = Arrays.asList(new Customer("John P.", 15), new Customer("Sarah M.", 200), new Customer("Charles B.", 150), new Customer("Mary T.", 1)); |
| 21 | + |
| 22 | + long customersWithMoreThan100Points = customers |
| 23 | + .stream() |
| 24 | + .filter(c -> c.getPoints() > 100) |
| 25 | + .count(); |
| 26 | + |
| 27 | + assertThat(customersWithMoreThan100Points).isEqualTo(2); |
| 28 | + } |
| 29 | + |
| 30 | + @Test |
| 31 | + public void givenListOfCustomers_whenFilterByMethodReference_thenGetTwo() { |
| 32 | + List<Customer> customers = Arrays.asList(new Customer("John P.", 15), new Customer("Sarah M.", 200), new Customer("Charles B.", 150), new Customer("Mary T.", 1)); |
| 33 | + |
| 34 | + long customersWithMoreThan100Points = customers |
| 35 | + .stream() |
| 36 | + .filter(Customer::hasOverThousandPoints) |
| 37 | + .count(); |
| 38 | + |
| 39 | + assertThat(customersWithMoreThan100Points).isEqualTo(2); |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + public void givenListOfCustomersWithOptional_whenFilterBy100Points_thenGetTwo() { |
| 44 | + List<Optional<Customer>> customers = Arrays.asList(Optional.of(new Customer("John P.", 15)), Optional.of(new Customer("Sarah M.", 200)), Optional.empty(), Optional.of(new Customer("Mary T.", 300)), Optional.empty()); |
| 45 | + |
| 46 | + long customersWithMoreThan100Points = customers |
| 47 | + .stream() |
| 48 | + .flatMap(c -> c |
| 49 | + .map(Stream::of) |
| 50 | + .orElseGet(Stream::empty)) |
| 51 | + .filter(Customer::hasOverThousandPoints) |
| 52 | + .count(); |
| 53 | + |
| 54 | + assertThat(customersWithMoreThan100Points).isEqualTo(2); |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + public void givenListOfCustomers_whenFilterWithCustomHandling_thenThrowException() { |
| 59 | + List<Customer> customers = Arrays.asList(new Customer("John P.", 15, "https://images.unsplash.com/photo-1543320485-d0d5a49c2b2e"), new Customer("Sarah M.", 200), new Customer("Charles B.", 150), |
| 60 | + new Customer("Mary T.", 1, "https://images.unsplash.com/photo-1543297057-25167dfc180e")); |
| 61 | + |
| 62 | + assertThatThrownBy(() -> customers |
| 63 | + .stream() |
| 64 | + .filter(Customer::hasValidProfilePhotoWithoutCheckedException) |
| 65 | + .count()).isInstanceOf(RuntimeException.class); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + public void givenListOfCustomers_whenFilterWithThrowingFunction_thenThrowException() { |
| 70 | + List<Customer> customers = Arrays.asList(new Customer("John P.", 15, "https://images.unsplash.com/photo-1543320485-d0d5a49c2b2e"), new Customer("Sarah M.", 200), new Customer("Charles B.", 150), |
| 71 | + new Customer("Mary T.", 1, "https://images.unsplash.com/photo-1543297057-25167dfc180e")); |
| 72 | + |
| 73 | + assertThatThrownBy(() -> customers |
| 74 | + .stream() |
| 75 | + .filter((ThrowingPredicate.unchecked(Customer::hasValidProfilePhoto))) |
| 76 | + .count()).isInstanceOf(WrappedException.class); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + public void givenListOfCustomers_whenFilterWithTryCatch_thenGetTwo() { |
| 81 | + List<Customer> customers = Arrays.asList(new Customer("John P.", 15, "https://images.unsplash.com/photo-1543320485-d0d5a49c2b2e"), new Customer("Sarah M.", 200), new Customer("Charles B.", 150), |
| 82 | + new Customer("Mary T.", 1, "https://images.unsplash.com/photo-1543297057-25167dfc180e")); |
| 83 | + |
| 84 | + long customersWithValidProfilePhoto = customers |
| 85 | + .stream() |
| 86 | + .filter(c -> { |
| 87 | + try { |
| 88 | + return c.hasValidProfilePhoto(); |
| 89 | + } catch (IOException e) { |
| 90 | + //handle exception |
| 91 | + } |
| 92 | + return false; |
| 93 | + }) |
| 94 | + .count(); |
| 95 | + |
| 96 | + assertThat(customersWithValidProfilePhoto).isEqualTo(2); |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + public void givenListOfCustomers_whenFilterWithTryCatchAndRuntime_thenThrowException() { |
| 101 | + List<Customer> customers = Arrays.asList(new Customer("John P.", 15, "https://images.unsplash.com/photo-1543320485-d0d5a49c2b2e"), new Customer("Sarah M.", 200), new Customer("Charles B.", 150), |
| 102 | + new Customer("Mary T.", 1, "https://images.unsplash.com/photo-1543297057-25167dfc180e")); |
| 103 | + |
| 104 | + assertThatThrownBy(() -> customers |
| 105 | + .stream() |
| 106 | + .filter(c -> { |
| 107 | + try { |
| 108 | + return c.hasValidProfilePhoto(); |
| 109 | + } catch (IOException e) { |
| 110 | + throw new RuntimeException(e); |
| 111 | + } |
| 112 | + }) |
| 113 | + .count()).isInstanceOf(RuntimeException.class); |
| 114 | + } |
| 115 | +} |
0 commit comments