Skip to content

Commit

Permalink
feat(edc-client):[#616] Add missing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ds-jhartmann committed May 15, 2024
1 parent 2778349 commit 095e237
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ void shouldStopJobAfterDepthIsReached() {

// Assert
WiremockSupport.verifyDiscoveryCalls(1);
WiremockSupport.verifyNegotiationCalls(5);
WiremockSupport.verifyNegotiationCalls(3);

assertThat(jobForJobId.getJob().getState()).isEqualTo(JobState.COMPLETED);
assertThat(jobForJobId.getShells()).hasSize(2);
Expand Down Expand Up @@ -273,7 +273,7 @@ void shouldStartRecursiveProcesses() {
assertThat(jobForJobId.getSubmodels()).hasSize(6);

WiremockSupport.verifyDiscoveryCalls(1);
WiremockSupport.verifyNegotiationCalls(9);
WiremockSupport.verifyNegotiationCalls(6);
}

private void successfulRegistryAndDataRequest(final String globalAssetId, final String idShort, final String bpn,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -292,9 +292,11 @@ public List<CompletableFuture<EndpointDataReference>> getEndpointReferencesForRe
final List<CatalogItem> contractOffers = new ArrayList<>(
catalogFacade.fetchCatalogByFilter(providerWithSuffix, DT_DCAT_TYPE_ID, DT_TAXONOMY_REGISTRY, bpn));

final List<CatalogItem> contractOffersDataCore = catalogFacade.fetchCatalogByFilter(providerWithSuffix,
DT_EDC_TYPE, DT_DATA_CORE_REGISTRY, bpn);
contractOffers.addAll(contractOffersDataCore);
if (contractOffers.isEmpty()) {
final List<CatalogItem> contractOffersDataCore = catalogFacade.fetchCatalogByFilter(providerWithSuffix,
DT_EDC_TYPE, DT_DATA_CORE_REGISTRY, bpn);
contractOffers.addAll(contractOffersDataCore);
}

if (contractOffers.isEmpty()) {
throw new EdcClientException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
package org.eclipse.tractusx.irs.edc.client;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assertions.fail;
import static org.eclipse.tractusx.irs.edc.client.cache.endpointdatareference.EndpointDataReferenceStatus.TokenStatus;
import static org.mockito.ArgumentMatchers.any;
Expand All @@ -45,6 +46,7 @@
import java.time.Duration;
import java.time.Instant;
import java.time.ZoneId;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
Expand Down Expand Up @@ -94,6 +96,10 @@ class EdcSubmodelClientTest extends LocalTestDataConfigurationAware {
private static final String CONNECTOR_ENDPOINT = "https://connector.endpoint.com";
private static final String existingCatenaXId = "urn:uuid:5e1908ed-e176-4f57-9616-1415097d0fdf";
private static final String BPN = "BPNL00000000TEST";
public static final String TAXONOMY_DIGITAL_TWIN_REGISTRY = "https://w3id.org/catenax/taxonomy#DigitalTwinRegistry";
public static final String DCT_TYPE_ID = "'http://purl.org/dc/terms/type'.'@id'";
public static final String EDC_TYPE = "https://w3id.org/edc/v0.0.1/ns/type";
public static final String DATA_CORE_DIGITAL_TWIN_REGISTRY = "data.core.digitalTwinRegistry";

private final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
private final TimeMachine clock = new TimeMachine();
Expand Down Expand Up @@ -462,6 +468,74 @@ void shouldRetrieveEndpointReferenceForAsset2() throws Exception {
assertThat(actual).isEqualTo(expected);
}

@Test
void shouldRetrieveEndpointReferenceForRegistryAssetForOldIdentifier() throws Exception {
// arrange
when(config.getControlplane().getProviderSuffix()).thenReturn(PROVIDER_SUFFIX);

final String agreementId = "agreementId";
when(catalogFacade.fetchCatalogByFilter(any(), eq(DCT_TYPE_ID), eq(TAXONOMY_DIGITAL_TWIN_REGISTRY),
any())).thenReturn(Collections.emptyList());
when(catalogFacade.fetchCatalogByFilter(any(), eq(EDC_TYPE), eq(DATA_CORE_DIGITAL_TWIN_REGISTRY),
any())).thenReturn(List.of(CatalogItem.builder().itemId("asset-id").build()));
when(contractNegotiationService.negotiate(any(), any(),
eq(new EndpointDataReferenceStatus(null, TokenStatus.REQUIRED_NEW)), any())).thenReturn(
NegotiationResponse.builder().contractAgreementId(agreementId).build());
final EndpointDataReference expected = mock(EndpointDataReference.class);
when(endpointDataReferenceCacheService.getEndpointDataReferenceFromStorage(agreementId)).thenReturn(
Optional.ofNullable(expected));

// act
final var result = testee.getEndpointReferencesForRegistryAsset(ENDPOINT_ADDRESS, "bpn");

// assert
assertThat(result).hasSize(1);
final EndpointDataReference actual = result.get(0).get(5, TimeUnit.SECONDS);
assertThat(actual).isEqualTo(expected);
}

@Test
void shouldRetrieveEndpointReferenceForRegistryAssetForNewIdentifier() throws Exception {
// arrange
when(config.getControlplane().getProviderSuffix()).thenReturn(PROVIDER_SUFFIX);

final String agreementId = "agreementId";
when(catalogFacade.fetchCatalogByFilter(any(), eq(DCT_TYPE_ID), eq(TAXONOMY_DIGITAL_TWIN_REGISTRY),
any())).thenReturn(List.of(CatalogItem.builder().itemId("asset-id").build()));
when(contractNegotiationService.negotiate(any(), any(),
eq(new EndpointDataReferenceStatus(null, TokenStatus.REQUIRED_NEW)), any())).thenReturn(
NegotiationResponse.builder().contractAgreementId(agreementId).build());
final EndpointDataReference expected = mock(EndpointDataReference.class);
when(endpointDataReferenceCacheService.getEndpointDataReferenceFromStorage(agreementId)).thenReturn(
Optional.ofNullable(expected));

// act
final var result = testee.getEndpointReferencesForRegistryAsset(ENDPOINT_ADDRESS, "bpn");

// assert
assertThat(result).hasSize(1);
final EndpointDataReference actual = result.get(0).get(5, TimeUnit.SECONDS);
assertThat(actual).isEqualTo(expected);
}

@Test
void shouldFailEndpointReferenceRetrievalForNoRegistryAsset() throws Exception {
// arrange
final String edcUrl = "https://edc.controlplane.org/public";
when(config.getControlplane().getProviderSuffix()).thenReturn(PROVIDER_SUFFIX);

when(catalogFacade.fetchCatalogByFilter(any(), eq(DCT_TYPE_ID), eq(TAXONOMY_DIGITAL_TWIN_REGISTRY),
any())).thenReturn(Collections.emptyList());
when(catalogFacade.fetchCatalogByFilter(any(), eq(EDC_TYPE), eq(DATA_CORE_DIGITAL_TWIN_REGISTRY),
any())).thenReturn(Collections.emptyList());

// act & assert
final String expectedErrorMessage = "No DigitalTwinRegistry contract offers found for endpointAddress '%s' filterKey '%s', filterValue '%s' or filterKey '%s', filterValue '%s'".formatted(
edcUrl, DCT_TYPE_ID, TAXONOMY_DIGITAL_TWIN_REGISTRY, EDC_TYPE, DATA_CORE_DIGITAL_TWIN_REGISTRY);
assertThatThrownBy(() -> testee.getEndpointReferencesForRegistryAsset(edcUrl, "bpn")).isInstanceOf(
EdcClientException.class).hasMessage(expectedErrorMessage);
}

@Test
void shouldUseCachedEndpointReferenceValueWhenTokenIsValid()
throws EdcClientException, ExecutionException, InterruptedException {
Expand Down

0 comments on commit 095e237

Please sign in to comment.