From 46149c89451ef0912dda6a4fe3ec73c3884c6651 Mon Sep 17 00:00:00 2001 From: David Greven Date: Mon, 13 Dec 2021 06:45:05 +0100 Subject: [PATCH] Add Unexpected type (#29) Signed-off-by: David Greven Signed-off-by: Sebastian Becker --- .../io/carbynestack/common/Unexpected.java | 85 +++++++++++++++++++ .../carbynestack/common/UnexpectedTest.java | 43 ++++++++++ 2 files changed, 128 insertions(+) create mode 100644 common-types/src/main/java/io/carbynestack/common/Unexpected.java create mode 100644 common-types/src/test/java/io/carbynestack/common/UnexpectedTest.java diff --git a/common-types/src/main/java/io/carbynestack/common/Unexpected.java b/common-types/src/main/java/io/carbynestack/common/Unexpected.java new file mode 100644 index 0000000..f5215e5 --- /dev/null +++ b/common-types/src/main/java/io/carbynestack/common/Unexpected.java @@ -0,0 +1,85 @@ +/* + * Copyright (c) 2021 - for information on the respective copyright owner + * see the NOTICE file and/or the repository https://github.com/carbynestack/common. + * + * SPDX-License-Identifier: Apache-2.0 + */ +package io.carbynestack.common; + +import java.util.Optional; + +import static java.util.Objects.requireNonNull; +import static java.util.Optional.ofNullable; + +/** + * Represents an unexpected {@link Throwable} based failure reason. + * + * @version JDK 8 + * @see CsFailureReason + * @since 0.2.0 + */ +public final class Unexpected implements CsFailureReason { + /** + * The failure reason. + * + * @since 0.2.0 + */ + private final Throwable throwable; + + /** + * Creates an {@code Unexpected} instance from a {@code Throwable}. + * + * @param throwable the failure reason + * @since 0.2.0 + */ + public Unexpected(Throwable throwable) { + this.throwable = requireNonNull(throwable); + } + + /** + * {@inheritDoc} + * + * @return the description synopsis + * @see #description() + * @since 0.2.0 + */ + @Override + public String synopsis() { + return "An unknown exception has occurred."; + } + + /** + * {@inheritDoc} + * + * @return the full description + * @see #synopsis() + * @since 0.2.0 + */ + @Override + public String description() { + return throwable.getLocalizedMessage(); + } + + /** + * {@inheritDoc} + * + * @return the stack trace as an {@link Optional} + * @since 0.2.0 + */ + @Override + public Optional stackTrace() { + return ofNullable(throwable.getStackTrace()); + } + + /** + * {@inheritDoc} + * + * @return {@code true} if the request should be triggered, + * otherwise {@code false}. + * @since 0.2.0 + */ + @Override + public boolean reportIssue() { + return true; + } +} diff --git a/common-types/src/test/java/io/carbynestack/common/UnexpectedTest.java b/common-types/src/test/java/io/carbynestack/common/UnexpectedTest.java new file mode 100644 index 0000000..2b45e82 --- /dev/null +++ b/common-types/src/test/java/io/carbynestack/common/UnexpectedTest.java @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2021 - for information on the respective copyright owner + * see the NOTICE file and/or the repository https://github.com/carbynestack/common. + * + * SPDX-License-Identifier: Apache-2.0 + */ +package io.carbynestack.common; + +import org.junit.jupiter.api.Test; + +import java.io.IOException; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +class UnexpectedTest { + private final Unexpected unknown = new Unexpected(new IOException("test")); + + @Test + void constructor() { + assertThatThrownBy(() -> new Unexpected(null)).isExactlyInstanceOf(NullPointerException.class); + } + + @Test + void synopsis() { + assertThat(unknown.synopsis()).isEqualTo("An unknown exception has occurred."); + } + + @Test + void description() { + assertThat(unknown.description()).isEqualTo("test"); + } + + @Test + void stackTrace() { + assertThat(unknown.stackTrace()).isNotEmpty(); + } + + @Test + void reportIssue() { + assertThat(unknown.reportIssue()).isTrue(); + } +}