OrderedFlagSource implements FlagSource { @Override public Optional<RawFlag> fetch(FlagId id, FetchVector vector) { return sources.stream() .map(source -> source.fetch(id, vector)) .filter(Optional::isPresent) .map(Optional::get) .findFirst(); } OrderedFlagSource(FlagSource... sources); @Override Optional<RawFlag> fetch(FlagId id, FetchVector vector);  }
@Test public void test() { FlagSource source1 = mock(FlagSource.class); FlagSource source2 = mock(FlagSource.class); OrderedFlagSource orderedSource = new OrderedFlagSource(source1, source2); FlagId id = new FlagId("id"); FetchVector vector = new FetchVector(); when(source1.fetch(any(), any())).thenReturn(Optional.empty()); when(source2.fetch(any(), any())).thenReturn(Optional.empty()); assertFalse(orderedSource.fetch(id, vector).isPresent()); verify(source1, times(1)).fetch(any(), any()); verify(source2, times(1)).fetch(any(), any()); RawFlag rawFlag = mock(RawFlag.class); when(source1.fetch(any(), any())).thenReturn(Optional.empty()); when(source2.fetch(any(), any())).thenReturn(Optional.of(rawFlag)); assertEquals(orderedSource.fetch(id, vector), Optional.of(rawFlag)); verify(source1, times(2)).fetch(any(), any()); verify(source2, times(2)).fetch(any(), any()); when(source1.fetch(any(), any())).thenReturn(Optional.of(rawFlag)); when(source2.fetch(any(), any())).thenReturn(Optional.empty()); assertEquals(orderedSource.fetch(id, vector), Optional.of(rawFlag)); verify(source1, times(3)).fetch(any(), any()); verify(source2, times(2)).fetch(any(), any()); }