Skip to content

Commit

Permalink
feature(impl):[#395] Corrected / improved test according to review se…
Browse files Browse the repository at this point in the history
…ssion
  • Loading branch information
dsmf committed Mar 1, 2024
1 parent cfd2279 commit 18a6239
Showing 1 changed file with 75 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,22 +48,26 @@
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;
import org.eclipse.edc.spi.types.domain.edr.EndpointDataReference;
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;
Expand All @@ -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
Expand All @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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());
Expand All @@ -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());
Expand All @@ -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());
Expand Down Expand Up @@ -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<DigitalTwinRegistryKey> digitalTwinRegistryKeys = decentralDigitalTwinRegistryService.lookupShellIdentifiers(
TEST_BPN);

Expand All @@ -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,

Check notice

Code scanning / CodeQL

Useless parameter Note test

The parameter 'title' is never used.
List<CompletableFuture<EndpointDataReference>> endpointDataReferenceForAssetFutures)
throws RegistryServiceException, EdcRetrieverException {
// Arrange
givenThat(postDiscoveryFinder200());
final List<String> 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<String> 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<DigitalTwinRegistryKey> 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<? extends Arguments> 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<String> 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<DigitalTwinRegistryKey> digitalTwinRegistryKeys = decentralDigitalTwinRegistryService.lookupShellIdentifiers(
Expand All @@ -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)));
}
}
Expand Down

0 comments on commit 18a6239

Please sign in to comment.