From 18a62390697553ac8c07938111a3526a3967fb70 Mon Sep 17 00:00:00 2001 From: Matthias Fischer Date: Fri, 1 Mar 2024 14:49:12 +0100 Subject: [PATCH] feature(impl):[#395] Corrected / improved test according to review session --- ...igitalTwinRegistryServiceWiremockTest.java | 87 ++++++++++++++++--- 1 file changed, 75 insertions(+), 12 deletions(-) diff --git a/irs-registry-client/src/test/java/org/eclipse/tractusx/irs/registryclient/decentral/DecentralDigitalTwinRegistryServiceWiremockTest.java b/irs-registry-client/src/test/java/org/eclipse/tractusx/irs/registryclient/decentral/DecentralDigitalTwinRegistryServiceWiremockTest.java index 019b2c8cba..31418299ee 100644 --- a/irs-registry-client/src/test/java/org/eclipse/tractusx/irs/registryclient/decentral/DecentralDigitalTwinRegistryServiceWiremockTest.java +++ b/irs-registry-client/src/test/java/org/eclipse/tractusx/irs/registryclient/decentral/DecentralDigitalTwinRegistryServiceWiremockTest.java @@ -48,15 +48,18 @@ import static org.eclipse.tractusx.irs.testing.wiremock.DtrWiremockSupport.getShellDescriptor404; import static org.eclipse.tractusx.irs.testing.wiremock.WireMockConfig.restTemplateProxy; import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import java.nio.charset.StandardCharsets; import java.util.Base64; import java.util.Collection; +import java.util.Collections; import java.util.List; import java.util.Map; import java.util.concurrent.CompletableFuture; +import java.util.stream.Stream; import com.github.tomakehurst.wiremock.junit5.WireMockRuntimeInfo; import com.github.tomakehurst.wiremock.junit5.WireMockTest; @@ -64,6 +67,7 @@ import org.eclipse.tractusx.irs.component.Shell; import org.eclipse.tractusx.irs.data.StringMapper; import org.eclipse.tractusx.irs.edc.client.configuration.JsonLdConfiguration; +import org.eclipse.tractusx.irs.edc.client.exceptions.EdcClientException; import org.eclipse.tractusx.irs.edc.client.model.EDRAuthCode; import org.eclipse.tractusx.irs.registryclient.DigitalTwinRegistryKey; import org.eclipse.tractusx.irs.registryclient.discovery.ConnectorEndpointsService; @@ -73,12 +77,18 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtensionContext; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.ArgumentsProvider; +import org.junit.jupiter.params.provider.ArgumentsSource; import org.springframework.web.client.RestTemplate; @WireMockTest class DecentralDigitalTwinRegistryServiceWiremockTest { private static final String PROXY_SERVER_HOST = "127.0.0.1"; - private final EdcEndpointReferenceRetriever edcSubmodelFacadeMock = mock(EdcEndpointReferenceRetriever.class); + private final EdcEndpointReferenceRetriever edcEndpointReferenceRetrieverMock = mock( + EdcEndpointReferenceRetriever.class); private DecentralDigitalTwinRegistryService decentralDigitalTwinRegistryService; @BeforeEach @@ -87,7 +97,8 @@ void setUp(WireMockRuntimeInfo wireMockRuntimeInfo) { final var discoveryFinderClient = new DiscoveryFinderClientImpl(DISCOVERY_FINDER_URL, restTemplate); final var connectorEndpointsService = new ConnectorEndpointsService(discoveryFinderClient); - final var endpointDataForConnectorsService = new EndpointDataForConnectorsService(edcSubmodelFacadeMock); + final var endpointDataForConnectorsService = new EndpointDataForConnectorsService( + edcEndpointReferenceRetrieverMock); final var decentralDigitalTwinRegistryClient = new DecentralDigitalTwinRegistryClient(restTemplate, SHELL_DESCRIPTORS_TEMPLATE, LOOKUP_SHELLS_TEMPLATE); decentralDigitalTwinRegistryService = new DecentralDigitalTwinRegistryService(connectorEndpointsService, @@ -105,7 +116,7 @@ void shouldDiscoverEDCAndRequestRegistry() throws RegistryServiceException, EdcR givenThat(getShellDescriptor200()); final var endpointDataReference = endpointDataReference("assetId"); - when(edcSubmodelFacadeMock.getEndpointReferencesForAsset(any(), any(), any())).thenReturn( + when(edcEndpointReferenceRetrieverMock.getEndpointReferencesForAsset(any(), any(), any())).thenReturn( List.of(CompletableFuture.completedFuture(endpointDataReference))); // Act @@ -154,7 +165,7 @@ void shouldThrowInCaseOfLookupShellsError() throws EdcRetrieverException { givenThat(postEdcDiscovery200()); final var endpointDataReference = endpointDataReference("assetId"); - when(edcSubmodelFacadeMock.getEndpointReferencesForAsset(any(), any(), any())).thenReturn( + when(edcEndpointReferenceRetrieverMock.getEndpointReferencesForAsset(any(), any(), any())).thenReturn( List.of(CompletableFuture.completedFuture(endpointDataReference))); givenThat(getLookupShells404()); @@ -175,7 +186,7 @@ void shouldThrowInCaseOfShellDescriptorsError() throws EdcRetrieverException { givenThat(postEdcDiscovery200()); final var endpointDataReference = endpointDataReference("assetId"); - when(edcSubmodelFacadeMock.getEndpointReferencesForAsset(any(), any(), any())).thenReturn( + when(edcEndpointReferenceRetrieverMock.getEndpointReferencesForAsset(any(), any(), any())).thenReturn( List.of(CompletableFuture.completedFuture(endpointDataReference))); givenThat(getLookupShells200()); @@ -198,7 +209,7 @@ void shouldThrowExceptionOnEmptyShells() throws EdcRetrieverException { givenThat(postEdcDiscovery200()); final var endpointDataReference = endpointDataReference("assetId"); - when(edcSubmodelFacadeMock.getEndpointReferencesForAsset(any(), any(), any())).thenReturn( + when(edcEndpointReferenceRetrieverMock.getEndpointReferencesForAsset(any(), any(), any())).thenReturn( List.of(CompletableFuture.completedFuture(endpointDataReference))); givenThat(getLookupShells200Empty()); @@ -228,10 +239,10 @@ void lookupShellIdentifiers_oneEDC_oneDTR() throws RegistryServiceException, Edc // simulate endpoint data reference final var endpointDataReference = endpointDataReference("assetId"); - when(edcSubmodelFacadeMock.getEndpointReferencesForAsset(any(), any(), any())).thenReturn( + when(edcEndpointReferenceRetrieverMock.getEndpointReferencesForAsset(any(), any(), any())).thenReturn( List.of(CompletableFuture.completedFuture(endpointDataReference))); - // Act & Assert + // Act final Collection digitalTwinRegistryKeys = decentralDigitalTwinRegistryService.lookupShellIdentifiers( TEST_BPN); @@ -244,18 +255,69 @@ void lookupShellIdentifiers_oneEDC_oneDTR() throws RegistryServiceException, Edc verify(exactly(edcUrls.size()), getRequestedFor(urlPathEqualTo(LOOKUP_SHELLS_PATH))); } - @Test - void lookupShellIdentifiers_multipleEDCs_oneDTR() throws RegistryServiceException, EdcRetrieverException { + @ParameterizedTest(name = "{0}") + @ArgumentsSource(NoOrFailedEndpointDataReferenceProvider.class) + void lookupShellIdentifiers_multipleEDCs_oneDTR(String title, + List> endpointDataReferenceForAssetFutures) + throws RegistryServiceException, EdcRetrieverException { // Arrange givenThat(postDiscoveryFinder200()); - final List edcUrls = List.of("https://test.edc1.io", "https://test.edc2.io"); + final String edc1Url = "https://test.edc1.io"; + final String edc2Url = "https://test.edc2.io"; + final List edcUrls = List.of(edc1Url, edc2Url); givenThat(postEdcDiscovery200(TEST_BPN, edcUrls)); givenThat(getLookupShells200()); // simulate endpoint data reference final var endpointDataReference = endpointDataReference("assetId"); - when(edcSubmodelFacadeMock.getEndpointReferencesForAsset(any(), any(), any())).thenReturn( + when(edcEndpointReferenceRetrieverMock.getEndpointReferencesForAsset(eq(edc1Url), any(), any())).thenReturn( List.of(CompletableFuture.completedFuture(endpointDataReference))); + when(edcEndpointReferenceRetrieverMock.getEndpointReferencesForAsset(eq(edc2Url), any(), any())).thenReturn( + endpointDataReferenceForAssetFutures); + + // Act + final Collection digitalTwinRegistryKeys = decentralDigitalTwinRegistryService.lookupShellIdentifiers( + TEST_BPN); + + // Assert + assertThat(digitalTwinRegistryKeys).hasSize(1); + assertThat(digitalTwinRegistryKeys.stream().findFirst().get().shellId()).isEqualTo( + "urn:uuid:21f7ebea-fa8a-410c-a656-bd9082e67dcf"); + verify(exactly(1), postRequestedFor(urlPathEqualTo(DISCOVERY_FINDER_PATH))); + verify(exactly(1), postRequestedFor(urlPathEqualTo(EDC_DISCOVERY_PATH))); + // because just one DTR + verify(exactly(1), getRequestedFor(urlPathEqualTo(LOOKUP_SHELLS_PATH))); + } + + public static class NoOrFailedEndpointDataReferenceProvider implements ArgumentsProvider { + @Override + public Stream provideArguments(final ExtensionContext extensionContext) { + return Stream.of( + // failed future + Arguments.of("given failed future", List.of(CompletableFuture.failedFuture( + new EdcRetrieverException(new EdcClientException(new RuntimeException("test")))))), + // no result + Arguments.of("given no result", Collections.emptyList())); + } + } + + @Test + void lookupShellIdentifiers_multipleEDCs_multipleDTRs() throws RegistryServiceException, EdcRetrieverException { + // Arrange + givenThat(postDiscoveryFinder200()); + final String edc1Url = "https://test.edc1.io"; + final String edc2Url = "https://test.edc2.io"; + final List edcUrls = List.of(edc1Url, edc2Url); + givenThat(postEdcDiscovery200(TEST_BPN, edcUrls)); + givenThat(getLookupShells200()); + + // simulate endpoint data reference + final var endpointDataReference1 = endpointDataReference("dtr1-assetId"); + final var endpointDataReference2 = endpointDataReference("dtr2-assetId"); + when(edcEndpointReferenceRetrieverMock.getEndpointReferencesForAsset(eq(edc1Url), any(), any())).thenReturn( + List.of(CompletableFuture.completedFuture(endpointDataReference1))); + when(edcEndpointReferenceRetrieverMock.getEndpointReferencesForAsset(eq(edc2Url), any(), any())).thenReturn( + List.of(CompletableFuture.completedFuture(endpointDataReference2))); // Act & Assert final Collection digitalTwinRegistryKeys = decentralDigitalTwinRegistryService.lookupShellIdentifiers( @@ -267,6 +329,7 @@ void lookupShellIdentifiers_multipleEDCs_oneDTR() throws RegistryServiceExceptio "urn:uuid:21f7ebea-fa8a-410c-a656-bd9082e67dcf"); verify(exactly(1), postRequestedFor(urlPathEqualTo(DISCOVERY_FINDER_PATH))); verify(exactly(1), postRequestedFor(urlPathEqualTo(EDC_DISCOVERY_PATH))); + // multiple DTR (one per EDC) verify(exactly(edcUrls.size()), getRequestedFor(urlPathEqualTo(LOOKUP_SHELLS_PATH))); } }