Skip to content

Commit

Permalink
fix(exception-handling): [eclipse-tractusx#841] add assertions, impro…
Browse files Browse the repository at this point in the history
…ve test readability
  • Loading branch information
dsmf committed Aug 1, 2024
1 parent 2fe49e9 commit 7778cc4
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import static org.eclipse.tractusx.irs.WiremockSupport.randomUUID;
import static org.eclipse.tractusx.irs.component.enums.AspectType.AspectTypesConstants.BATCH;
import static org.eclipse.tractusx.irs.component.enums.AspectType.AspectTypesConstants.SINGLE_LEVEL_BOM_AS_BUILT;
import static org.eclipse.tractusx.irs.testing.wiremock.DiscoveryServiceWiremockSupport.CONTROLPLANE_PUBLIC_URL;
import static org.eclipse.tractusx.irs.testing.wiremock.DiscoveryServiceWiremockSupport.DISCOVERY_FINDER_PATH;
import static org.eclipse.tractusx.irs.testing.wiremock.DiscoveryServiceWiremockSupport.DISCOVERY_FINDER_URL;
import static org.eclipse.tractusx.irs.testing.wiremock.DiscoveryServiceWiremockSupport.EDC_DISCOVERY_PATH;
Expand All @@ -53,7 +54,6 @@
import java.time.Duration;
import java.util.List;
import java.util.Objects;
import java.util.stream.Stream;

import com.github.tomakehurst.wiremock.junit5.WireMockTest;
import org.awaitility.Awaitility;
Expand Down Expand Up @@ -215,12 +215,15 @@ void shouldCreateTombstoneWhenDiscoveryServiceNotAvailable() {
verify(0, postRequestedFor(urlPathEqualTo(EDC_DISCOVERY_PATH)));

assertThat(jobForJobId.getJob().getState()).isEqualTo(JobState.COMPLETED);

assertThat(jobForJobId.getShells()).isEmpty();
assertThat(jobForJobId.getRelationships()).isEmpty();
assertThat(jobForJobId.getTombstones()).hasSize(1);
assertThat(jobForJobId.getTombstones().get(0).getBusinessPartnerNumber()).isEqualTo(
TEST_BPN); // TODO (mfischer) is this correct?
// TODO (mfischer) also check for Endpoint URL

final List<Tombstone> tombstones = jobForJobId.getTombstones();
assertThat(tombstones).hasSize(1);
assertThat(tombstones.get(0).getBusinessPartnerNumber()).isEqualTo(TEST_BPN);
assertThat(tombstones.get(0).getEndpointURL()).describedAs(
"Endpoint URL should be empty because discovery not successful").isEmpty();
}

@Test
Expand All @@ -243,9 +246,15 @@ void shouldCreateTombstoneWhenEdcDiscoveryIsEmpty() {
WiremockSupport.verifyDiscoveryCalls(1);

assertThat(jobForJobId.getJob().getState()).isEqualTo(JobState.COMPLETED);

assertThat(jobForJobId.getShells()).isEmpty();
assertThat(jobForJobId.getRelationships()).isEmpty();
assertThat(jobForJobId.getTombstones()).hasSize(1);

final List<Tombstone> tombstones = jobForJobId.getTombstones();
assertThat(tombstones).hasSize(1);
assertThat(tombstones.get(0).getBusinessPartnerNumber()).isEqualTo(TEST_BPN);
assertThat(tombstones.get(0).getEndpointURL()).describedAs(
"Endpoint URL should be empty because discovery not successful").isEmpty();
}

@Test
Expand Down Expand Up @@ -287,7 +296,7 @@ void shouldStartRecursiveProcesses() {
}

@Test
void shouldCreateDetailedTombstoneForMissmatchPolicy() {
void shouldCreateDetailedTombstoneForMismatchPolicy() {
// Arrange
final String globalAssetId = "urn:uuid:334cce52-1f52-4bc9-9dd1-410bbe497bbc";

Expand All @@ -308,14 +317,22 @@ void shouldCreateDetailedTombstoneForMissmatchPolicy() {
final Jobs jobForJobId = irsService.getJobForJobId(jobHandle.getId(), false);

assertThat(jobForJobId.getJob().getState()).isEqualTo(JobState.COMPLETED);

assertThat(jobForJobId.getShells()).isEmpty();
assertThat(jobForJobId.getRelationships()).isEmpty();
assertThat(jobForJobId.getSubmodels()).isEmpty();

assertThat(jobForJobId.getTombstones()).hasSize(1);

final Tombstone actualTombstone = jobForJobId.getTombstones().get(0);
assertThat(actualTombstone.getProcessingError().getRootCauses()).hasSize(1);
assertThat(actualTombstone.getProcessingError().getRootCauses().get(0)).contains(
"UsagePolicyPermissionException: Policies [default-policy] did not match with policy from BPNL00000000TEST.");
assertThat(actualTombstone.getBusinessPartnerNumber()).isEqualTo(TEST_BPN);
assertThat(actualTombstone.getEndpointURL()).isEqualTo(CONTROLPLANE_PUBLIC_URL);

final List<String> rootCauses = actualTombstone.getProcessingError().getRootCauses();
assertThat(rootCauses).hasSize(1);
assertThat(rootCauses.get(0)).contains(
"UsagePolicyPermissionException: Policies [default-policy] did not match with policy from %s.".formatted(
TEST_BPN));
}

@Test
Expand All @@ -340,13 +357,21 @@ void shouldCreateDetailedTombstoneForEdcErrors() {
final Jobs jobForJobId = irsService.getJobForJobId(jobHandle.getId(), false);

assertThat(jobForJobId.getJob().getState()).isEqualTo(JobState.COMPLETED);

assertThat(jobForJobId.getShells()).isEmpty();
assertThat(jobForJobId.getRelationships()).isEmpty();
assertThat(jobForJobId.getSubmodels()).isEmpty();
assertThat(jobForJobId.getTombstones()).hasSize(1);
final Tombstone actualTombstone = jobForJobId.getTombstones().get(0);
assertThat(actualTombstone.getProcessingError().getRootCauses()).hasSize(1);
assertThat(actualTombstone.getProcessingError().getRootCauses().get(0)).contains("502 Bad Gateway");

final List<Tombstone> tombstones = jobForJobId.getTombstones();
assertThat(tombstones).hasSize(1);

final Tombstone actualTombstone = tombstones.get(0);
assertThat(actualTombstone.getBusinessPartnerNumber()).isEqualTo(TEST_BPN);
assertThat(actualTombstone.getEndpointURL()).isEqualTo(CONTROLPLANE_PUBLIC_URL);

final List<String> rootCauses = actualTombstone.getProcessingError().getRootCauses();
assertThat(rootCauses).hasSize(1);
assertThat(rootCauses.get(0)).contains("502 Bad Gateway");
}

@Test
Expand All @@ -371,18 +396,22 @@ void shouldCreateDetailedTombstoneForDiscoveryErrors() {
final Jobs jobForJobId = irsService.getJobForJobId(jobHandle.getId(), false);

assertThat(jobForJobId.getJob().getState()).isEqualTo(JobState.COMPLETED);

assertThat(jobForJobId.getShells()).isEmpty();
assertThat(jobForJobId.getRelationships()).isEmpty();
assertThat(jobForJobId.getSubmodels()).isEmpty();

assertThat(jobForJobId.getTombstones()).hasSize(1);
final Tombstone actualTombstone = jobForJobId.getTombstones().get(0);
assertThat(actualTombstone.getProcessingError().getRootCauses()).hasSize(1);
assertThat(actualTombstone.getProcessingError().getRootCauses().get(0)).contains(
"No EDC Endpoints could be discovered for BPN '%s'".formatted(TEST_BPN));

assertThat(actualTombstone.getBusinessPartnerNumber()).isEqualTo(TEST_BPN);
assertThat(actualTombstone.getEndpointURL()).describedAs(
"endpoint url empty because it could not be discovered").isEmpty();
"Endpoint url empty because it could not be discovered").isEmpty();

final List<String> rootCauses = actualTombstone.getProcessingError().getRootCauses();
assertThat(rootCauses).hasSize(1);
assertThat(rootCauses.get(0)).contains(
"No EDC Endpoints could be discovered for BPN '%s'".formatted(TEST_BPN));
}

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 @@ -61,6 +61,7 @@
import org.eclipse.tractusx.irs.component.JobParameter;
import org.eclipse.tractusx.irs.component.PartChainIdentificationKey;
import org.eclipse.tractusx.irs.component.Quantity;
import org.eclipse.tractusx.irs.component.Tombstone;
import org.eclipse.tractusx.irs.component.enums.ProcessStep;
import org.eclipse.tractusx.irs.edc.client.EdcSubmodelFacade;
import org.eclipse.tractusx.irs.edc.client.exceptions.EdcClientException;
Expand Down Expand Up @@ -132,12 +133,15 @@ void shouldFillItemContainerWithUpwardRelationshipAndAddChildIdsToProcess()

// then
assertThat(result).isNotNull();

final Quantity quantity = result.getRelationships().get(0).getLinkedItem().getQuantity();
assertThat(quantity.getQuantityNumber()).isEqualTo(20.0);
assertThat(quantity.getMeasurementUnit().getLexicalValue()).isEqualTo("unit:piece");
assertThat(aasTransferProcess.getIdsToProcess()).isNotEmpty();
assertThat(aasTransferProcess.getIdsToProcess().get(0).getGlobalAssetId()).isNotEmpty();
assertThat(aasTransferProcess.getIdsToProcess().get(0).getBpn()).isNotEmpty();

final List<PartChainIdentificationKey> idsToProcess = aasTransferProcess.getIdsToProcess();
assertThat(idsToProcess).isNotEmpty();
assertThat(idsToProcess.get(0).getGlobalAssetId()).isNotEmpty();
assertThat(idsToProcess.get(0).getBpn()).isNotEmpty();
}

@Test
Expand All @@ -164,13 +168,15 @@ void shouldFillItemContainerWithUpwardAsPlannedRelationshipAndAddChildIdsToProce

// then
assertThat(result).isNotNull();

final Quantity quantity = result.getRelationships().get(0).getLinkedItem().getQuantity();
assertThat(quantity.getQuantityNumber()).isEqualTo(20.0);
assertThat(quantity.getMeasurementUnit().getLexicalValue()).isEqualTo("unit:piece");
assertThat(aasTransferProcess.getIdsToProcess()).isNotEmpty();
assertThat(aasTransferProcess.getIdsToProcess().get(0).getGlobalAssetId()).isEqualTo(
"urn:uuid:56319907-28dc-440e-afcc-72d67ad343e7");
assertThat(aasTransferProcess.getIdsToProcess().get(0).getBpn()).isEqualTo("BPNL50096894aNXY");

final List<PartChainIdentificationKey> idsToProcess = aasTransferProcess.getIdsToProcess();
assertThat(idsToProcess).isNotEmpty();
assertThat(idsToProcess.get(0).getGlobalAssetId()).isEqualTo("urn:uuid:56319907-28dc-440e-afcc-72d67ad343e7");
assertThat(idsToProcess.get(0).getBpn()).isEqualTo("BPNL50096894aNXY");
}

@ParameterizedTest
Expand All @@ -197,12 +203,15 @@ void shouldFillItemContainerWithSupportedRelationshipAndAddChildIdsToProcess(fin
// then
assertThat(result).isNotNull();
assertThat(result.getRelationships()).hasSize(1);

final Quantity quantity = result.getRelationships().get(0).getLinkedItem().getQuantity();
assertThat(quantity.getQuantityNumber()).isEqualTo(20.0);
assertThat(quantity.getMeasurementUnit().getLexicalValue()).isEqualTo("unit:piece");
assertThat(aasTransferProcess.getIdsToProcess()).isNotEmpty();
assertThat(aasTransferProcess.getIdsToProcess().get(0).getGlobalAssetId()).isNotEmpty();
assertThat(aasTransferProcess.getIdsToProcess().get(0).getBpn()).isNotEmpty();

final List<PartChainIdentificationKey> idsToProcess = aasTransferProcess.getIdsToProcess();
assertThat(idsToProcess).isNotEmpty();
assertThat(idsToProcess.get(0).getGlobalAssetId()).isNotEmpty();
assertThat(idsToProcess.get(0).getBpn()).isNotEmpty();
}

private static Stream<Arguments> relationshipParameters() {
Expand Down Expand Up @@ -246,12 +255,15 @@ void shouldFillItemContainerWithPotentialFutureMinorVersions(final String relati
// then
assertThat(result).isNotNull();
assertThat(result.getRelationships()).hasSize(1);

final Quantity quantity = result.getRelationships().get(0).getLinkedItem().getQuantity();
assertThat(quantity.getQuantityNumber()).isEqualTo(20.0);
assertThat(quantity.getMeasurementUnit().getLexicalValue()).isEqualTo("unit:piece");
assertThat(aasTransferProcess.getIdsToProcess()).isNotEmpty();
assertThat(aasTransferProcess.getIdsToProcess().get(0).getGlobalAssetId()).isNotEmpty();
assertThat(aasTransferProcess.getIdsToProcess().get(0).getBpn()).isNotEmpty();

final List<PartChainIdentificationKey> idsToProcess = aasTransferProcess.getIdsToProcess();
assertThat(idsToProcess).isNotEmpty();
assertThat(idsToProcess.get(0).getGlobalAssetId()).isNotEmpty();
assertThat(idsToProcess.get(0).getBpn()).isNotEmpty();
}

public static Stream<Arguments> relationshipParametersFutureVersions() {
Expand Down Expand Up @@ -294,12 +306,15 @@ void shouldFillItemContainerWithPreviousVersions(final String relationshipFile,
// then
assertThat(result).isNotNull();
assertThat(result.getRelationships()).hasSize(1);

final Quantity quantity = result.getRelationships().get(0).getLinkedItem().getQuantity();
assertThat(quantity.getQuantityNumber()).isEqualTo(2.5);
assertThat(quantity.getMeasurementUnit().getLexicalValue()).isEqualTo("unit:litre");
assertThat(aasTransferProcess.getIdsToProcess()).isNotEmpty();
assertThat(aasTransferProcess.getIdsToProcess().get(0).getGlobalAssetId()).isNotEmpty();
assertThat(aasTransferProcess.getIdsToProcess().get(0).getBpn()).isNotEmpty();

final List<PartChainIdentificationKey> idsToProcess = aasTransferProcess.getIdsToProcess();
assertThat(idsToProcess).isNotEmpty();
assertThat(idsToProcess.get(0).getGlobalAssetId()).isNotEmpty();
assertThat(idsToProcess.get(0).getBpn()).isNotEmpty();
}

public static Stream<Arguments> relationshipParametersPreviousVersions() {
Expand All @@ -322,10 +337,15 @@ void shouldPutTombstoneForMissingBpn() {

// then
assertThat(result).isNotNull();
assertThat(result.getTombstones()).hasSize(1);
assertThat(result.getTombstones().get(0).getCatenaXId()).isEqualTo("testId");
assertThat(result.getTombstones().get(0).getProcessingError().getProcessStep()).isEqualTo(
ProcessStep.SUBMODEL_REQUEST);

final List<Tombstone> tombstones = result.getTombstones();
assertThat(tombstones).hasSize(1);

assertThat(tombstones.get(0).getEndpointURL()).isEqualTo("address");
assertThat(tombstones.get(0).getBusinessPartnerNumber()).isNull();

assertThat(tombstones.get(0).getCatenaXId()).isEqualTo("testId");
assertThat(tombstones.get(0).getProcessingError().getProcessStep()).isEqualTo(ProcessStep.SUBMODEL_REQUEST);
}

@Test
Expand All @@ -349,12 +369,14 @@ void shouldCatchRestClientExceptionAndPutTombstone() throws EdcClientException {
// then
assertThat(result).isNotNull();
assertThat(result.getTombstones()).hasSize(1);
assertThat(result.getTombstones().get(0).getBusinessPartnerNumber()).isEqualTo(
partChainIdentificationKey.getBpn()); // TODO (mfischer) is this correct?
assertThat(result.getTombstones().get(0).getEndpointURL()).isEqualTo("address");
assertThat(result.getTombstones().get(0).getCatenaXId()).isEqualTo("itemId");
assertThat(result.getTombstones().get(0).getProcessingError().getProcessStep()).isEqualTo(
ProcessStep.SUBMODEL_REQUEST);

final Tombstone tombstone = result.getTombstones().get(0);

assertThat(tombstone.getBusinessPartnerNumber()).isEqualTo(partChainIdentificationKey.getBpn());
assertThat(tombstone.getEndpointURL()).isEqualTo("address");

assertThat(tombstone.getCatenaXId()).isEqualTo("itemId");
assertThat(tombstone.getProcessingError().getProcessStep()).isEqualTo(ProcessStep.SUBMODEL_REQUEST);
}

@Test
Expand All @@ -377,9 +399,11 @@ void shouldCatchJsonParseExceptionAndPutTombstone() throws EdcClientException {
// then
assertThat(result).isNotNull();
assertThat(result.getTombstones()).hasSize(1);

assertThat(result.getTombstones().get(0).getBusinessPartnerNumber()).isEqualTo(
partChainIdentificationKey.getBpn()); // TODO (mfischer) is this correct?
partChainIdentificationKey.getBpn());
assertThat(result.getTombstones().get(0).getEndpointURL()).isEqualTo("address");

assertThat(result.getTombstones().get(0).getCatenaXId()).isEqualTo("itemId");
assertThat(result.getTombstones().get(0).getProcessingError().getProcessStep()).isEqualTo(
ProcessStep.SUBMODEL_REQUEST);
Expand All @@ -405,14 +429,18 @@ void shouldCatchUsagePolicyExceptionAndPutTombstone() throws EdcClientException

// then
assertThat(result).isNotNull();
assertThat(result.getTombstones()).hasSize(1);
assertThat(result.getTombstones().get(0).getBusinessPartnerNumber()).isEqualTo(

final List<Tombstone> tombstones = result.getTombstones();
assertThat(tombstones).hasSize(1);

final Tombstone tombstone = tombstones.get(0);
assertThat(tombstone.getBusinessPartnerNumber()).isEqualTo(
businessPartnerNumber); // TODO (mfischer) is this correct or should it be the bpn from partChainIdentificationKey?
assertThat(result.getTombstones().get(0).getEndpointURL()).isEqualTo("address");
assertThat(result.getTombstones().get(0).getCatenaXId()).isEqualTo("itemId");
assertThat(result.getTombstones().get(0).getBusinessPartnerNumber()).isEqualTo(businessPartnerNumber);
assertThat(result.getTombstones().get(0).getProcessingError().getProcessStep()).isEqualTo(
ProcessStep.USAGE_POLICY_VALIDATION);
assertThat(tombstone.getEndpointURL()).isEqualTo("address");

assertThat(tombstone.getCatenaXId()).isEqualTo("itemId");
assertThat(tombstone.getBusinessPartnerNumber()).isEqualTo(businessPartnerNumber);
assertThat(tombstone.getProcessingError().getProcessStep()).isEqualTo(ProcessStep.USAGE_POLICY_VALIDATION);
}

private static PartChainIdentificationKey createKey() {
Expand Down

0 comments on commit 7778cc4

Please sign in to comment.