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.
pull/140/head
Richard Körber 2023-05-07 09:25:22 +02:00
parent c26d6b1f8a
commit c08c85b95c
No known key found for this signature in database
GPG Key ID: AAB9FD19C78AA3E0
2 changed files with 13 additions and 5 deletions

View File

@ -45,6 +45,7 @@ public class Order extends AcmeJsonResource {
* <p> * <p>
* Possible values are: {@link Status#PENDING}, {@link Status#READY}, * Possible values are: {@link Status#PENDING}, {@link Status#READY},
* {@link Status#PROCESSING}, {@link Status#VALID}, {@link Status#INVALID}. * {@link Status#PROCESSING}, {@link Status#VALID}, {@link Status#INVALID}.
* If the server supports STAR, another possible value is {@link Status#CANCELED}.
*/ */
public Status getStatus() { public Status getStatus() {
return getJSON().get("status").asStatus(); 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. * Gets the STAR extension's {@link Certificate} if it is available.
* *
* @since 2.6 * @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<Certificate> getAutoRenewalCertificate() { public Certificate getAutoRenewalCertificate() {
return getJSON().get("star-certificate") return getJSON().get("star-certificate")
.map(Value::asURL) .optional()
.map(getLogin()::bindCertificate); .map(Value::asURL)
.map(getLogin()::bindCertificate)
.orElseThrow(() -> new IllegalStateException("Order is in an invalid state"));
} }
/** /**

View File

@ -218,7 +218,8 @@ public class OrderTest {
.isEqualTo("2016-01-08T00:00:00Z"); .isEqualTo("2016-01-08T00:00:00Z");
softly.assertThat(order.getCertificate().getLocation()) softly.assertThat(order.getCertificate().getLocation())
.isEqualTo(url("https://example.com/acme/cert/1234")); .isEqualTo(url("https://example.com/acme/cert/1234"));
softly.assertThat(order.getAutoRenewalCertificate()).isEmpty(); softly.assertThatIllegalStateException()
.isThrownBy(order::getAutoRenewalCertificate);
softly.assertThat(order.getFinalizeLocation()).isEqualTo(finalizeUrl); softly.assertThat(order.getFinalizeLocation()).isEqualTo(finalizeUrl);
var auths = order.getAuthorizations(); var auths = order.getAuthorizations();
@ -310,7 +311,7 @@ public class OrderTest {
try (var softly = new AutoCloseableSoftAssertions()) { try (var softly = new AutoCloseableSoftAssertions()) {
softly.assertThatIllegalStateException() softly.assertThatIllegalStateException()
.isThrownBy(order::getCertificate); .isThrownBy(order::getCertificate);
softly.assertThat(order.getAutoRenewalCertificate().orElseThrow().getLocation()) softly.assertThat(order.getAutoRenewalCertificate().getLocation())
.isEqualTo(url("https://example.com/acme/cert/1234")); .isEqualTo(url("https://example.com/acme/cert/1234"));
softly.assertThat(order.isAutoRenewing()).isTrue(); softly.assertThat(order.isAutoRenewing()).isTrue();
softly.assertThat(order.getAutoRenewalStartDate().orElseThrow()) softly.assertThat(order.getAutoRenewalStartDate().orElseThrow())