public void assert_failure() { Result<String> test = Result.failure(new IllegalArgumentException("failure")); assertThat(test) .isFailure(ERROR) .hasFailureMessageMatching("failure"); }
/** * Create an {@code Assert} instance for the supplied {@code Result}. * * @param result the result instance to wrap * @return an instance of {@code ResultAssert} */ public static ResultAssert assertThat(Result<?> result) { return new ResultAssert(result); }
/** * Assert that the {@code Result} is a Failure. * * @return this, if the wrapped object is a failure * @throws AssertionError if the wrapped object is a success */ public ResultAssert isFailure() { isNotNull(); if (!actual.isFailure()) { failWithMessage("Expected Failure but was Success with value: <%s>", actual.getValue()); } return this; }
public void evaluate() { PositionTokenEvaluator evaluator = new PositionTokenEvaluator(); Position pos = trade(); EvaluationResult quantity = evaluator.evaluate(pos, FUNCTIONS, "quantity", ImmutableList.of()); assertThat(quantity.getResult()).hasValue(6d); EvaluationResult initialPrice = evaluator.evaluate(pos, FUNCTIONS, "security", ImmutableList.of()); assertThat(initialPrice.getResult()).hasValue(SECURITY); // Check that property name isn't case sensitive EvaluationResult initialPrice2 = evaluator.evaluate(pos, FUNCTIONS, "Security", ImmutableList.of()); assertThat(initialPrice2.getResult()).hasValue(SECURITY); // Unknown property EvaluationResult foo = evaluator.evaluate(pos, FUNCTIONS, "foo", ImmutableList.of()); assertThat(foo.getResult()).isFailure(); }
assertThat(result1.getResult()).isSuccess(); CurrencyParameterSensitivities result1Value = (CurrencyParameterSensitivities) result1.getResult().getValue(); assertThat(result1Value.getSensitivities()).containsAll(expected1.getSensitivities()); assertThat(result2.getResult()).isSuccess(); CurrencyParameterSensitivities result2Value = (CurrencyParameterSensitivities) result2.getResult().getValue(); assertThat(result2Value.getSensitivities()).containsAll(expected2.getSensitivities()); assertThat(result3.getResult()).hasValue(sensitivity2); assertThat(result4.getResult()).isFailure();
@Test(expectedExceptions = AssertionError.class) public void assert_success_getFailure() { Result<String> test = Result.success("success"); assertThat(test).isFailure(); }
/** * Assert that the {@code Result} is a failure with the specified reason. * * @param expected the expected failure reason * @return this, if the wrapped object is a failure with the specified reason * @throws AssertionError if the wrapped object is a success, or does not have the expected reason */ public ResultAssert isFailure(FailureReason expected) { isFailure(); FailureReason actualReason = actual.getFailure().getReason(); if (actualReason != expected) { failWithMessage("Expected Failure with reason: <%s> but was Failure with reason: <%s>", expected, actualReason); } return this; }
/** * Assert that the {@code Result} is a success and contains the specified value. * * @param value the value the {@code Result} is expected to contain * @return this, if the wrapped object is a success and has the specified value * @throws AssertionError if the wrapped object is a failure, or does not have the specified value */ public ResultAssert hasValue(Object value) { isSuccess(); if (!actual.getValue().equals(value)) { failWithMessage("Expected Success with value: <%s> but was: <%s>", value, actual.getValue()); } return this; }
/** * Tests evaluating a bean with a single property. There are 2 different expected behaviours: * * 1) If the token matches the property, the property value is returned and the token is consumed. This is the same * as the normal bean behaviour. * 2) If the token doesn't match the property it is assumed to match something on the property's value. In this * case the property value is returned and no tokens are consumed. */ public void evaluateSingleProperty() { SwapLegAmount amount = SwapLegAmount.builder() .amount(CurrencyAmount.of(Currency.AUD, 7)) .payReceive(PayReceive.PAY) .type(SwapLegType.FIXED) .currency(Currency.AUD) .build(); LegAmounts amounts = LegAmounts.of(amount); BeanTokenEvaluator evaluator = new BeanTokenEvaluator(); EvaluationResult result1 = evaluator.evaluate(amounts, FUNCTIONS, "amounts", ImmutableList.of("foo", "bar")); assertThat(result1.getResult()).hasValue(ImmutableList.of(amount)); assertThat(result1.getRemainingTokens()).isEqualTo(ImmutableList.of("foo", "bar")); EvaluationResult result2 = evaluator.evaluate(amounts, FUNCTIONS, "baz", ImmutableList.of("foo", "bar")); assertThat(result2.getResult()).hasValue(ImmutableList.of(amount)); assertThat(result2.getRemainingTokens()).isEqualTo(ImmutableList.of("baz", "foo", "bar")); }
/** * Create an {@code Assert} instance that enables * assertions on {@code Result} objects. * * @param result the result to create an {@code Assert} for * @return an {@code Assert} instance */ public static ResultAssert assertThat(Result<?> result) { return ResultAssert.assertThat(result); }
public void evaluate() { SecurityTokenEvaluator evaluator = new SecurityTokenEvaluator(); Security sec = security(); EvaluationResult quantity = evaluator.evaluate(sec, FUNCTIONS, "id", ImmutableList.of()); assertThat(quantity.getResult()).hasValue(ID); EvaluationResult initialPrice = evaluator.evaluate(sec, FUNCTIONS, "currency", ImmutableList.of()); assertThat(initialPrice.getResult()).hasValue(USD); // Check that property name isn't case sensitive EvaluationResult initialPrice2 = evaluator.evaluate(sec, FUNCTIONS, "Currency", ImmutableList.of()); assertThat(initialPrice2.getResult()).hasValue(USD); // Unknown property EvaluationResult foo = evaluator.evaluate(sec, FUNCTIONS, "foo", ImmutableList.of()); assertThat(foo.getResult()).isFailure(); }
public void assert_success_combineWith_failure() { Result<String> success = Result.success("Hello"); Result<String> failure = Result.failure(new IllegalArgumentException()); Result<String> test = success.combineWith(failure, FUNCTION_MERGE); assertThat(test).isFailure(ERROR); assertThat(test.getFailure().getItems().size()).isEqualTo(1); }
/** * Assert that the {@code Result} is a failure with the specified message. * * @param regex the regex that the failure message is expected to match * @return this, if the wrapped object is a failure with the specified message * @throws AssertionError if the wrapped object is a success, or does not have the expected message */ public ResultAssert hasFailureMessageMatching(String regex) { isFailure(); String message = actual.getFailure().getMessage(); if (!message.matches(regex)) { failWithMessage("Expected Failure with message matching: <%s> but was Failure with message: <%s>", regex, message); } return this; }
/** * Assert that the {@code Result} is a Success. * * @return this, if the wrapped object is a success * @throws AssertionError if the wrapped object is a failure */ public ResultAssert isSuccess() { isNotNull(); if (!actual.isSuccess()) { Failure failure = actual.getFailure(); failWithMessage("Expected Success but was Failure with reason: <%s> and message: <%s>", failure.getReason(), failure.getMessage()); } return this; }
public void success_flatMap_throwing() { Result<String> success = Result.success("success"); Result<Integer> test = success.flatMap(r -> { throw new IllegalArgumentException("Big bad error"); }); assertEquals(test.isSuccess(), false); assertThat(test) .isFailure(ERROR) .hasFailureMessageMatching("Big bad error"); }
public void evaluate() { TradeTokenEvaluator evaluator = new TradeTokenEvaluator(); Trade trade = trade(); EvaluationResult quantity = evaluator.evaluate(trade, FUNCTIONS, "quantity", ImmutableList.of()); assertThat(quantity.getResult()).hasValue(123d); EvaluationResult initialPrice = evaluator.evaluate(trade, FUNCTIONS, "price", ImmutableList.of()); assertThat(initialPrice.getResult()).hasValue(456d); // Check that property name isn't case sensitive EvaluationResult initialPrice2 = evaluator.evaluate(trade, FUNCTIONS, "price", ImmutableList.of()); assertThat(initialPrice2.getResult()).hasValue(456d); EvaluationResult counterparty = evaluator.evaluate(trade, FUNCTIONS, "counterparty", ImmutableList.of()); assertThat(counterparty.getResult()).hasValue(StandardId.of("cpty", "a")); // Optional property with no value EvaluationResult tradeTime = evaluator.evaluate(trade, FUNCTIONS, "tradeTime", ImmutableList.of()); assertThat(tradeTime.getResult()).isFailure(); // Unknown property EvaluationResult foo = evaluator.evaluate(trade, FUNCTIONS, "foo", ImmutableList.of()); assertThat(foo.getResult()).isFailure(); }
public void flatCombine_iterableWithFailures() { Result<String> success1 = Result.success("success 1"); Result<String> success2 = Result.success("success 2"); Result<String> failure1 = Result.failure(MISSING_DATA, "failure 1"); Result<String> failure2 = Result.failure(ERROR, "failure 2"); Set<Result<String>> results = ImmutableSet.of(success1, success2, failure1, failure2); assertThat(Result.flatCombine(results, Result::success)) .isFailure(FailureReason.MULTIPLE); }