From c08c85b95c4daa246ded3bce319c3dd85891ead2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Richard=20K=C3=B6rber?= Date: Sun, 7 May 2023 09:25:22 +0200 Subject: [PATCH] Return certificate on getAutoRenewalCertificate Similar to getCertificate(), getAutoRenewalCertificate() would only return an empty optional if the order state is not valid. To keep the API simple, getAutoRenewalCertificate() now always returns a non-null certificate, and throws an exception otherwise. --- .../src/main/java/org/shredzone/acme4j/Order.java | 13 ++++++++++--- .../test/java/org/shredzone/acme4j/OrderTest.java | 5 +++-- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/acme4j-client/src/main/java/org/shredzone/acme4j/Order.java b/acme4j-client/src/main/java/org/shredzone/acme4j/Order.java index f8c8e418..faf66a41 100644 --- a/acme4j-client/src/main/java/org/shredzone/acme4j/Order.java +++ b/acme4j-client/src/main/java/org/shredzone/acme4j/Order.java @@ -45,6 +45,7 @@ public class Order extends AcmeJsonResource { *

* Possible values are: {@link Status#PENDING}, {@link Status#READY}, * {@link Status#PROCESSING}, {@link Status#VALID}, {@link Status#INVALID}. + * If the server supports STAR, another possible value is {@link Status#CANCELED}. */ public Status getStatus() { return getJSON().get("status").asStatus(); @@ -132,11 +133,17 @@ public class Order extends AcmeJsonResource { * Gets the STAR extension's {@link Certificate} if it is available. * * @since 2.6 + * @throws IllegalStateException + * if the order is not ready yet. You must finalize the order first, and wait + * for the status to become {@link Status#VALID}. It is also thrown if the + * order has been {@link Status#CANCELED}. */ - public Optional getAutoRenewalCertificate() { + public Certificate getAutoRenewalCertificate() { return getJSON().get("star-certificate") - .map(Value::asURL) - .map(getLogin()::bindCertificate); + .optional() + .map(Value::asURL) + .map(getLogin()::bindCertificate) + .orElseThrow(() -> new IllegalStateException("Order is in an invalid state")); } /** diff --git a/acme4j-client/src/test/java/org/shredzone/acme4j/OrderTest.java b/acme4j-client/src/test/java/org/shredzone/acme4j/OrderTest.java index 918c71f8..76d8e488 100644 --- a/acme4j-client/src/test/java/org/shredzone/acme4j/OrderTest.java +++ b/acme4j-client/src/test/java/org/shredzone/acme4j/OrderTest.java @@ -218,7 +218,8 @@ public class OrderTest { .isEqualTo("2016-01-08T00:00:00Z"); softly.assertThat(order.getCertificate().getLocation()) .isEqualTo(url("https://example.com/acme/cert/1234")); - softly.assertThat(order.getAutoRenewalCertificate()).isEmpty(); + softly.assertThatIllegalStateException() + .isThrownBy(order::getAutoRenewalCertificate); softly.assertThat(order.getFinalizeLocation()).isEqualTo(finalizeUrl); var auths = order.getAuthorizations(); @@ -310,7 +311,7 @@ public class OrderTest { try (var softly = new AutoCloseableSoftAssertions()) { softly.assertThatIllegalStateException() .isThrownBy(order::getCertificate); - softly.assertThat(order.getAutoRenewalCertificate().orElseThrow().getLocation()) + softly.assertThat(order.getAutoRenewalCertificate().getLocation()) .isEqualTo(url("https://example.com/acme/cert/1234")); softly.assertThat(order.isAutoRenewing()).isTrue(); softly.assertThat(order.getAutoRenewalStartDate().orElseThrow())