diff --git a/spi/data-plane/data-plane-spi/src/main/java/org/eclipse/edc/connector/dataplane/spi/pipeline/StreamFailure.java b/spi/data-plane/data-plane-spi/src/main/java/org/eclipse/edc/connector/dataplane/spi/pipeline/StreamFailure.java index ddbe79e24f0..e86a164379c 100644 --- a/spi/data-plane/data-plane-spi/src/main/java/org/eclipse/edc/connector/dataplane/spi/pipeline/StreamFailure.java +++ b/spi/data-plane/data-plane-spi/src/main/java/org/eclipse/edc/connector/dataplane/spi/pipeline/StreamFailure.java @@ -9,7 +9,6 @@ * * Contributors: * Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation - * zub4t - Improving failure detail message * */ @@ -33,11 +32,7 @@ public StreamFailure(List messages, Reason reason) { @Override public String getFailureDetail() { var str = super.getFailureDetail(); - if (str != null && !str.isEmpty()) { - str = reason + ": " + str; - return str; - } - return String.valueOf(reason); + return (str != null && !str.isEmpty()) ? (reason + ": " + str) : (reason + ""); } public Reason getReason() { diff --git a/spi/data-plane/data-plane-spi/src/test/java/org/eclipse/edc/connector/dataplane/spi/pipeline/StreamFailureTest.java b/spi/data-plane/data-plane-spi/src/test/java/org/eclipse/edc/connector/dataplane/spi/pipeline/StreamFailureTest.java new file mode 100644 index 00000000000..ffff19c17be --- /dev/null +++ b/spi/data-plane/data-plane-spi/src/test/java/org/eclipse/edc/connector/dataplane/spi/pipeline/StreamFailureTest.java @@ -0,0 +1,69 @@ +/* + * Copyright (c) 2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) + * + * This program and the accompanying materials are made available under the + * terms of the Apache License, Version 2.0 which is available at + * https://www.apache.org/licenses/LICENSE-2.0 + * + * SPDX-License-Identifier: Apache-2.0 + * + * Contributors: + * Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - Initial implementation + * + */ + +package org.eclipse.edc.connector.dataplane.spi.pipeline; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.util.Collections; +import java.util.List; +import java.util.stream.Stream; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.eclipse.edc.connector.dataplane.spi.pipeline.StreamFailure.Reason.GENERAL_ERROR; +import static org.eclipse.edc.connector.dataplane.spi.pipeline.StreamFailure.Reason.NOT_AUTHORIZED; +import static org.eclipse.edc.connector.dataplane.spi.pipeline.StreamFailure.Reason.NOT_FOUND; + +public class StreamFailureTest { + private StreamFailure streamFailure; + private String failureMessage; + + @BeforeEach + void setup() { + failureMessage = "FAILURE MESSAGE"; + } + + @Test + void verify_with_failureDetail_message_startWith_notFound() { + + streamFailure = new StreamFailure(List.of(failureMessage), NOT_FOUND); + assertThat(streamFailure.getFailureDetail()).startsWith(NOT_FOUND.toString()); + } + + @Test + void verify_with_failureDetail_message_startWith_notAuthorized() { + + streamFailure = new StreamFailure(List.of(failureMessage), NOT_AUTHORIZED); + assertThat(streamFailure.getFailureDetail()).startsWith(NOT_AUTHORIZED.toString()); + } + + @Test + void verify_with_failureDetail_message_startWith_generalError() { + + streamFailure = new StreamFailure(List.of(failureMessage), GENERAL_ERROR); + assertThat(streamFailure.getFailureDetail()).startsWith(GENERAL_ERROR.toString()); + } + + @Test + void verify_with_failureDetail_message_only_contains_reason_when_message_is_empty() { + + assertThat(Stream.of(StreamFailure.Reason.values()) + .allMatch(enumVal -> { + return new StreamFailure(Collections.emptyList(), enumVal) + .getFailureDetail() + .equals(enumVal.toString()); + })).isTrue(); + } +}