|
| 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.Collectors; |
| 12 | +import java.util.stream.Stream; |
| 13 | + |
| 14 | +import static org.assertj.core.api.Assertions.assertThat; |
| 15 | +import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy; |
| 16 | + |
| 17 | +public class StreamFilterUnitTest { |
| 18 | + |
| 19 | + @Test |
| 20 | + public void givenListOfCustomers_whenFilterByPoints_thenGetTwo() { |
| 21 | + Customer john = new Customer("John P.", 15); |
| 22 | + Customer sarah = new Customer("Sarah M.", 200); |
| 23 | + Customer charles = new Customer("Charles B.", 150); |
| 24 | + Customer mary = new Customer("Mary T.", 1); |
| 25 | + List<Customer> customers = Arrays.asList(john, sarah, charles, mary); |
| 26 | + |
| 27 | + List<Customer> customersWithMoreThan100Points = customers |
| 28 | + .stream() |
| 29 | + .filter(c -> c.getPoints() > 100) |
| 30 | + .collect(Collectors.toList()); |
| 31 | + |
| 32 | + assertThat(customersWithMoreThan100Points).hasSize(2); |
| 33 | + assertThat(customersWithMoreThan100Points).contains(sarah, charles); |
| 34 | + } |
| 35 | + |
| 36 | + @Test |
| 37 | + public void givenListOfCustomers_whenFilterByPointsAndName_thenGetOne() { |
| 38 | + Customer john = new Customer("John P.", 15); |
| 39 | + Customer sarah = new Customer("Sarah M.", 200); |
| 40 | + Customer charles = new Customer("Charles B.", 150); |
| 41 | + Customer mary = new Customer("Mary T.", 1); |
| 42 | + List<Customer> customers = Arrays.asList(john, sarah, charles, mary); |
| 43 | + |
| 44 | + List<Customer> charlesWithMoreThan100Points = customers |
| 45 | + .stream() |
| 46 | + .filter(c -> c.getPoints() > 100 && c |
| 47 | + .getName() |
| 48 | + .startsWith("Charles")) |
| 49 | + .collect(Collectors.toList()); |
| 50 | + |
| 51 | + assertThat(charlesWithMoreThan100Points).hasSize(1); |
| 52 | + assertThat(charlesWithMoreThan100Points).contains(charles); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + public void givenListOfCustomers_whenFilterByMethodReference_thenGetTwo() { |
| 57 | + Customer john = new Customer("John P.", 15); |
| 58 | + Customer sarah = new Customer("Sarah M.", 200); |
| 59 | + Customer charles = new Customer("Charles B.", 150); |
| 60 | + Customer mary = new Customer("Mary T.", 1); |
| 61 | + List<Customer> customers = Arrays.asList(john, sarah, charles, mary); |
| 62 | + |
| 63 | + List<Customer> customersWithMoreThan100Points = customers |
| 64 | + .stream() |
| 65 | + .filter(Customer::hasOverThousandPoints) |
| 66 | + .collect(Collectors.toList()); |
| 67 | + |
| 68 | + assertThat(customersWithMoreThan100Points).hasSize(2); |
| 69 | + assertThat(customersWithMoreThan100Points).contains(sarah, charles); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + public void givenListOfCustomersWithOptional_whenFilterBy100Points_thenGetTwo() { |
| 74 | + Optional<Customer> john = Optional.of(new Customer("John P.", 15)); |
| 75 | + Optional<Customer> sarah = Optional.of(new Customer("Sarah M.", 200)); |
| 76 | + Optional<Customer> mary = Optional.of(new Customer("Mary T.", 300)); |
| 77 | + List<Optional<Customer>> customers = Arrays.asList(john, sarah, Optional.empty(), mary, Optional.empty()); |
| 78 | + |
| 79 | + List<Customer> customersWithMoreThan100Points = customers |
| 80 | + .stream() |
| 81 | + .flatMap(c -> c |
| 82 | + .map(Stream::of) |
| 83 | + .orElseGet(Stream::empty)) |
| 84 | + .filter(Customer::hasOverThousandPoints) |
| 85 | + .collect(Collectors.toList()); |
| 86 | + |
| 87 | + assertThat(customersWithMoreThan100Points).hasSize(2); |
| 88 | + assertThat(customersWithMoreThan100Points).contains(sarah.get(), mary.get()); |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + public void givenListOfCustomers_whenFilterWithCustomHandling_thenThrowException() { |
| 93 | + Customer john = new Customer("John P.", 15, "https://images.unsplash.com/photo-1543320485-d0d5a49c2b2e"); |
| 94 | + Customer sarah = new Customer("Sarah M.", 200); |
| 95 | + Customer charles = new Customer("Charles B.", 150); |
| 96 | + Customer mary = new Customer("Mary T.", 1, "https://images.unsplash.com/photo-1543297057-25167dfc180e"); |
| 97 | + List<Customer> customers = Arrays.asList(john, sarah, charles, mary); |
| 98 | + |
| 99 | + assertThatThrownBy(() -> customers |
| 100 | + .stream() |
| 101 | + .filter(Customer::hasValidProfilePhotoWithoutCheckedException) |
| 102 | + .count()).isInstanceOf(RuntimeException.class); |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + public void givenListOfCustomers_whenFilterWithThrowingFunction_thenThrowException() { |
| 107 | + Customer john = new Customer("John P.", 15, "https://images.unsplash.com/photo-1543320485-d0d5a49c2b2e"); |
| 108 | + Customer sarah = new Customer("Sarah M.", 200); |
| 109 | + Customer charles = new Customer("Charles B.", 150); |
| 110 | + Customer mary = new Customer("Mary T.", 1, "https://images.unsplash.com/photo-1543297057-25167dfc180e"); |
| 111 | + List<Customer> customers = Arrays.asList(john, sarah, charles, mary); |
| 112 | + |
| 113 | + assertThatThrownBy(() -> customers |
| 114 | + .stream() |
| 115 | + .filter((ThrowingPredicate.unchecked(Customer::hasValidProfilePhoto))) |
| 116 | + .collect(Collectors.toList())).isInstanceOf(WrappedException.class); |
| 117 | + } |
| 118 | + |
| 119 | + @Test |
| 120 | + public void givenListOfCustomers_whenFilterWithTryCatch_thenGetTwo() { |
| 121 | + Customer john = new Customer("John P.", 15, "https://images.unsplash.com/photo-1543320485-d0d5a49c2b2e"); |
| 122 | + Customer sarah = new Customer("Sarah M.", 200); |
| 123 | + Customer charles = new Customer("Charles B.", 150); |
| 124 | + Customer mary = new Customer("Mary T.", 1, "https://images.unsplash.com/photo-1543297057-25167dfc180e"); |
| 125 | + List<Customer> customers = Arrays.asList(john, sarah, charles, mary); |
| 126 | + |
| 127 | + List<Customer> customersWithValidProfilePhoto = customers |
| 128 | + .stream() |
| 129 | + .filter(c -> { |
| 130 | + try { |
| 131 | + return c.hasValidProfilePhoto(); |
| 132 | + } catch (IOException e) { |
| 133 | + //handle exception |
| 134 | + } |
| 135 | + return false; |
| 136 | + }) |
| 137 | + .collect(Collectors.toList()); |
| 138 | + |
| 139 | + assertThat(customersWithValidProfilePhoto).hasSize(2); |
| 140 | + assertThat(customersWithValidProfilePhoto).contains(john, mary); |
| 141 | + } |
| 142 | + |
| 143 | + @Test |
| 144 | + public void givenListOfCustomers_whenFilterWithTryCatchAndRuntime_thenThrowException() { |
| 145 | + 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), |
| 146 | + new Customer("Mary T.", 1, "https://images.unsplash.com/photo-1543297057-25167dfc180e")); |
| 147 | + |
| 148 | + assertThatThrownBy(() -> customers |
| 149 | + .stream() |
| 150 | + .filter(c -> { |
| 151 | + try { |
| 152 | + return c.hasValidProfilePhoto(); |
| 153 | + } catch (IOException e) { |
| 154 | + throw new RuntimeException(e); |
| 155 | + } |
| 156 | + }) |
| 157 | + .collect(Collectors.toList())).isInstanceOf(RuntimeException.class); |
| 158 | + } |
| 159 | +} |
0 commit comments