ComposedResourceDescriptionProvider implements ResourceDescriptionProvider { @Override public Optional<String> getAsciidocDescription(String fullQualifiedTerraformAddress) { Optional<String> result; for (ResourceDescriptionProvider provider : providers) { result = provider.getAsciidocDescription(fullQualifiedTerraformAddress); if (result.isPresent()) { return result; } } return Optional.empty(); } ComposedResourceDescriptionProvider(Iterable<ResourceDescriptionProvider> providers); @Override Optional<String> getAsciidocDescription(String fullQualifiedTerraformAddress);  }
@Test void getResourceDescriptionShouldReturnEmptyOptionalIfNoProviderReturnsAValue() { ResourceDescriptionProvider p1 = mock(ResourceDescriptionProvider.class); ResourceDescriptionProvider p2 = mock(ResourceDescriptionProvider.class); when(p1.getAsciidocDescription(any())).thenReturn(Optional.empty()); when(p2.getAsciidocDescription(any())).thenReturn(Optional.empty()); List<ResourceDescriptionProvider> providers = Arrays.asList(p1, p2); ResourceDescriptionProvider resourceDescriptionProvider = new ComposedResourceDescriptionProvider(providers); Optional<String> resourceDescription = resourceDescriptionProvider.getAsciidocDescription("some_resource"); assertThat(resourceDescription).isEmpty(); verify(p1, times(1)).getAsciidocDescription("some_resource"); verify(p2, times(1)).getAsciidocDescription("some_resource"); }
@Test void getResourceDescriptionShouldReturnOnFirstResultWithoutInvokingTheFollowinProviders() { ResourceDescriptionProvider p1 = mock(ResourceDescriptionProvider.class); ResourceDescriptionProvider p2 = mock(ResourceDescriptionProvider.class); String testDescription = "some description"; when(p1.getAsciidocDescription(any())).thenReturn(Optional.of(testDescription)); when(p2.getAsciidocDescription(any())).thenReturn(Optional.empty()); List<ResourceDescriptionProvider> providers = Arrays.asList(p1, p2); ResourceDescriptionProvider resourceDescriptionProvider = new ComposedResourceDescriptionProvider(providers); Optional<String> resourceDescription = resourceDescriptionProvider.getAsciidocDescription("some_resource"); assertThat(resourceDescription).isNotEmpty().get().isEqualTo(testDescription); verify(p1, times(1)).getAsciidocDescription("some_resource"); verify(p2, never()).getAsciidocDescription("some_resource"); }