From 095e237b845bd637f3616d90bf961c34408ef1f5 Mon Sep 17 00:00:00 2001 From: jhartmann Date: Wed, 15 May 2024 17:30:36 +0200 Subject: [PATCH] feat(edc-client):[#616] Add missing tests --- .../irs/IrsWireMockIntegrationTest.java | 4 +- .../irs/edc/client/EdcSubmodelClientImpl.java | 8 +- .../irs/edc/client/EdcSubmodelClientTest.java | 74 +++++++++++++++++++ 3 files changed, 81 insertions(+), 5 deletions(-) diff --git a/irs-api/src/test/java/org/eclipse/tractusx/irs/IrsWireMockIntegrationTest.java b/irs-api/src/test/java/org/eclipse/tractusx/irs/IrsWireMockIntegrationTest.java index 0a27cfe1c9..562083bfac 100644 --- a/irs-api/src/test/java/org/eclipse/tractusx/irs/IrsWireMockIntegrationTest.java +++ b/irs-api/src/test/java/org/eclipse/tractusx/irs/IrsWireMockIntegrationTest.java @@ -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); @@ -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, diff --git a/irs-edc-client/src/main/java/org/eclipse/tractusx/irs/edc/client/EdcSubmodelClientImpl.java b/irs-edc-client/src/main/java/org/eclipse/tractusx/irs/edc/client/EdcSubmodelClientImpl.java index 51fdb5b622..86844d3646 100644 --- a/irs-edc-client/src/main/java/org/eclipse/tractusx/irs/edc/client/EdcSubmodelClientImpl.java +++ b/irs-edc-client/src/main/java/org/eclipse/tractusx/irs/edc/client/EdcSubmodelClientImpl.java @@ -292,9 +292,11 @@ public List> getEndpointReferencesForRe final List contractOffers = new ArrayList<>( catalogFacade.fetchCatalogByFilter(providerWithSuffix, DT_DCAT_TYPE_ID, DT_TAXONOMY_REGISTRY, bpn)); - final List contractOffersDataCore = catalogFacade.fetchCatalogByFilter(providerWithSuffix, - DT_EDC_TYPE, DT_DATA_CORE_REGISTRY, bpn); - contractOffers.addAll(contractOffersDataCore); + if (contractOffers.isEmpty()) { + final List contractOffersDataCore = catalogFacade.fetchCatalogByFilter(providerWithSuffix, + DT_EDC_TYPE, DT_DATA_CORE_REGISTRY, bpn); + contractOffers.addAll(contractOffersDataCore); + } if (contractOffers.isEmpty()) { throw new EdcClientException( diff --git a/irs-edc-client/src/test/java/org/eclipse/tractusx/irs/edc/client/EdcSubmodelClientTest.java b/irs-edc-client/src/test/java/org/eclipse/tractusx/irs/edc/client/EdcSubmodelClientTest.java index c726d09d6e..053aca2ef2 100644 --- a/irs-edc-client/src/test/java/org/eclipse/tractusx/irs/edc/client/EdcSubmodelClientTest.java +++ b/irs-edc-client/src/test/java/org/eclipse/tractusx/irs/edc/client/EdcSubmodelClientTest.java @@ -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; @@ -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; @@ -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(); @@ -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 {