|
28 | 28 | import java.util.List; |
29 | 29 | import java.util.Objects; |
30 | 30 | import java.util.Optional; |
| 31 | +import java.util.function.Predicate; |
31 | 32 | import java.util.stream.Collectors; |
32 | 33 | import java.util.stream.Stream; |
33 | 34 | import org.hypertrace.config.service.v1.ConfigServiceGrpc.ConfigServiceImplBase; |
@@ -178,6 +179,46 @@ public MockGenericConfigService mockGetAll() { |
178 | 179 | return this; |
179 | 180 | } |
180 | 181 |
|
| 182 | + public MockGenericConfigService mockGetAllWithFilter( |
| 183 | + Predicate<ContextSpecificConfig> filterPredicate) { |
| 184 | + |
| 185 | + Mockito.doAnswer( |
| 186 | + invocation -> { |
| 187 | + StreamObserver<GetAllConfigsResponse> responseObserver = |
| 188 | + invocation.getArgument(1, StreamObserver.class); |
| 189 | + GetAllConfigsRequest request = invocation.getArgument(0, GetAllConfigsRequest.class); |
| 190 | + |
| 191 | + List<ContextSpecificConfig> matchingConfigs = |
| 192 | + currentValues |
| 193 | + .row( |
| 194 | + ResourceType.of( |
| 195 | + request.getResourceNamespace(), request.getResourceName())) |
| 196 | + .values() |
| 197 | + .stream() |
| 198 | + .filter( |
| 199 | + config -> { |
| 200 | + if (request.hasFilter()) { |
| 201 | + return filterPredicate.test(config); |
| 202 | + } |
| 203 | + return true; |
| 204 | + }) |
| 205 | + .collect(Collectors.toList()); |
| 206 | + |
| 207 | + GetAllConfigsResponse response = |
| 208 | + GetAllConfigsResponse.newBuilder() |
| 209 | + .addAllContextSpecificConfigs(matchingConfigs) |
| 210 | + .build(); |
| 211 | + |
| 212 | + responseObserver.onNext(response); |
| 213 | + responseObserver.onCompleted(); |
| 214 | + return null; |
| 215 | + }) |
| 216 | + .when(this.mockConfigService) |
| 217 | + .getAllConfigs(ArgumentMatchers.any(), ArgumentMatchers.any()); |
| 218 | + |
| 219 | + return this; |
| 220 | + } |
| 221 | + |
181 | 222 | @SuppressWarnings("unchecked") |
182 | 223 | public MockGenericConfigService mockDelete() { |
183 | 224 | Mockito.doAnswer( |
|
0 commit comments