-
Notifications
You must be signed in to change notification settings - Fork 565
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
8797: Extend Either/EitherAssert capabilities r=npepinpe a=npepinpe ## Description This PR extends `Either` by adding a new API, `Either#getOrElse(R)`. This allows to extract the right value of the `Either` or returning a fallback. I did not add any tests as the implementation is incredibly simple, and I can't foresee ever getting more complex, but do challenge this. It also extends the related `EitherAssert` by adding a new adding new `left` and `right` extraction capabilities. So you can now assert something like: ```java EitherAssert.assertThat(either).left().isEqualTo(1); EitherAssert.assertThat(instantEither) .right() .asInstanceOf(InstanceOfAssertFactories.INSTANT) .isBetween(today, tomorrow); ``` Note that calling `EitherAssert#right()` will, under the hood, still call `EitherAssert#isRight()`. This PR is related to #5525 and is extracted from the bigger spike in #8491. You can review how it's used there, specifically in the `JobBatchCollectorTest`. As such, this is marked for backporting, since we'll backport the complete fix for #5525. ## Related issues related to #5525 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
219 additions
and
163 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
103 changes: 103 additions & 0 deletions
103
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,103 @@ | ||
/* | ||
* 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 { | ||
@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("assertions") | ||
void shouldFailOnNull( | ||
@SuppressWarnings("unused") final String testName, | ||
final Consumer<EitherAssert<Object, Object>> assertion) { | ||
assertThatCode(() -> assertion.accept(eitherAssert)).isInstanceOf(AssertionError.class); | ||
} | ||
|
||
private static Stream<Arguments> assertions() { | ||
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); | ||
} | ||
} | ||
} |
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.