-
Notifications
You must be signed in to change notification settings - Fork 571
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
8814: [Backport stable/1.3] Extend Either/EitherAssert capabilities r=npepinpe a=npepinpe ## Description This PR backports #8797 to stable/1.3, which is necessary to in the end backport the fix for #5525. There were some conflicts which I corrected with the last commit (one extra dependency pulled). ## Related issues backports #8797 Co-authored-by: Nicolas Pepin-Perreault <nicolas.pepin-perreault@camunda.com> Co-authored-by: Nicolas Pepin-Perreault <43373+npepinpe@users.noreply.github.com>
- Loading branch information
Showing
7 changed files
with
223 additions
and
158 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
test-util/src/test/java/io/camunda/zeebe/test/util/asserts/EitherAssertTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
/* | ||
* Copyright Camunda Services GmbH and/or licensed to Camunda Services GmbH under | ||
* one or more contributor license agreements. See the NOTICE file distributed | ||
* with this work for additional information regarding copyright ownership. | ||
* Licensed under the Zeebe Community License 1.1. You may not use this file | ||
* except in compliance with the Zeebe Community License 1.1. | ||
*/ | ||
package io.camunda.zeebe.test.util.asserts; | ||
|
||
import static org.assertj.core.api.Assertions.assertThatCode; | ||
|
||
import io.camunda.zeebe.util.Either; | ||
import java.util.function.Consumer; | ||
import java.util.stream.Stream; | ||
import org.junit.jupiter.api.Nested; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.parallel.Execution; | ||
import org.junit.jupiter.api.parallel.ExecutionMode; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.Arguments; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
|
||
@Execution(ExecutionMode.CONCURRENT) | ||
final class EitherAssertTest { | ||
|
||
/** | ||
* The test cases must be specified as static methods, but pre-Java 17, it is not allowed to have | ||
* static methods in inner classes. | ||
*/ | ||
private static Stream<Arguments> failOnNullCases() { | ||
return Stream.of( | ||
of("isLeft", EitherAssert::isLeft), | ||
of("isRight", EitherAssert::isRight), | ||
of("left", EitherAssert::left), | ||
of("right", EitherAssert::right)); | ||
} | ||
|
||
/** Convenience method to infer the type of the consumer in the test cases above. */ | ||
private static Arguments of( | ||
final String testName, final Consumer<EitherAssert<Object, Object>> assertion) { | ||
return Arguments.of(testName, assertion); | ||
} | ||
|
||
@Nested | ||
final class RightTest { | ||
private final Either<Object, Object> actual = Either.right("right"); | ||
private final EitherAssert<Object, Object> assertion = EitherAssert.assertThat(actual); | ||
|
||
@Test | ||
void shouldFailIsLeftOnRight() { | ||
assertThatCode(assertion::isLeft).isInstanceOf(AssertionError.class); | ||
} | ||
|
||
@Test | ||
void shouldFailLeftOnRight() { | ||
assertThatCode(assertion::left).isInstanceOf(AssertionError.class); | ||
} | ||
|
||
@Test | ||
void isRightOnRight() { | ||
assertion.isRight(); | ||
} | ||
|
||
@Test | ||
void shouldExtractRightOnRight() { | ||
assertion.right().isEqualTo("right"); | ||
} | ||
} | ||
|
||
@Nested | ||
final class LeftTest { | ||
private final Either<Object, Object> actual = Either.left("left"); | ||
private final EitherAssert<Object, Object> assertion = EitherAssert.assertThat(actual); | ||
|
||
@Test | ||
void shouldFailIsRightOnLeft() { | ||
assertThatCode(assertion::isRight).isInstanceOf(AssertionError.class); | ||
} | ||
|
||
@Test | ||
void shouldFailRightOnLeft() { | ||
assertThatCode(assertion::right).isInstanceOf(AssertionError.class); | ||
} | ||
|
||
@Test | ||
void isLeftOnLeft() { | ||
assertion.isLeft(); | ||
} | ||
|
||
@Test | ||
void shouldExtractLeftOnLeft() { | ||
assertion.left().isEqualTo("left"); | ||
} | ||
} | ||
|
||
@Nested | ||
final class FailOnNullTest { | ||
private final EitherAssert<Object, Object> eitherAssert = EitherAssert.assertThat(null); | ||
|
||
@ParameterizedTest(name = "{0}") | ||
@MethodSource("io.camunda.zeebe.test.util.asserts.EitherAssertTest#failOnNullCases") | ||
void shouldFailOnNull( | ||
@SuppressWarnings("unused") final String testName, | ||
final Consumer<EitherAssert<Object, Object>> assertion) { | ||
assertThatCode(() -> assertion.accept(eitherAssert)).isInstanceOf(AssertionError.class); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.