getCertificate() is not Optional

getCertificate() would only return Optional.empty() if it was invoked
before the order was finalized. In order to keep the API simple, that
state will now throw an IllegalStateException, and getCertificate()
directly returns a non-null Certificate now.
pull/140/head
Richard Körber 2023-05-06 17:11:36 +02:00
parent 1907545e5d
commit 5bbf1b5966
No known key found for this signature in database
GPG Key ID: AAB9FD19C78AA3E0
6 changed files with 17 additions and 18 deletions

View File

@ -114,12 +114,17 @@ public class Order extends AcmeJsonResource {
}
/**
* Gets the {@link Certificate} if it is available.
* Gets the {@link Certificate}.
*
* @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}.
*/
public Optional<Certificate> getCertificate() {
public Certificate getCertificate() {
return getJSON().get("certificate")
.map(Value::asURL)
.map(getLogin()::bindCertificate);
.map(Value::asURL)
.map(getLogin()::bindCertificate)
.orElseThrow(() -> new IllegalStateException("Order is not completed"));
}
/**

View File

@ -79,7 +79,7 @@ public class OrderTest {
.isEqualTo("2016-01-01T00:00:00Z");
softly.assertThat(order.getNotAfter().orElseThrow())
.isEqualTo("2016-01-08T00:00:00Z");
softly.assertThat(order.getCertificate().orElseThrow().getLocation())
softly.assertThat(order.getCertificate().getLocation())
.isEqualTo(url("https://example.com/acme/cert/1234"));
softly.assertThat(order.getFinalizeLocation()).isEqualTo(finalizeUrl);
@ -141,13 +141,13 @@ public class OrderTest {
try (var softly = new AutoCloseableSoftAssertions()) {
// Lazy loading
softly.assertThat(requestWasSent).isFalse();
softly.assertThat(order.getCertificate().orElseThrow().getLocation())
softly.assertThat(order.getCertificate().getLocation())
.isEqualTo(url("https://example.com/acme/cert/1234"));
softly.assertThat(requestWasSent).isTrue();
// Subsequent queries do not trigger another load
requestWasSent.set(false);
softly.assertThat(order.getCertificate().orElseThrow().getLocation())
softly.assertThat(order.getCertificate().getLocation())
.isEqualTo(url("https://example.com/acme/cert/1234"));
softly.assertThat(order.getStatus()).isEqualTo(Status.PENDING);
softly.assertThat(order.getExpires().orElseThrow()).isEqualTo("2015-03-01T14:09:00Z");
@ -210,7 +210,7 @@ public class OrderTest {
.isEqualTo("2016-01-01T00:00:00Z");
softly.assertThat(order.getNotAfter().orElseThrow())
.isEqualTo("2016-01-08T00:00:00Z");
softly.assertThat(order.getCertificate().orElseThrow().getLocation())
softly.assertThat(order.getCertificate().getLocation())
.isEqualTo(url("https://example.com/acme/cert/1234"));
softly.assertThat(order.getAutoRenewalCertificate()).isEmpty();
softly.assertThat(order.getFinalizeLocation()).isEqualTo(finalizeUrl);

View File

@ -142,7 +142,7 @@ public class ClientTest {
}
// Get the certificate
Certificate certificate = order.getCertificate().orElseThrow();
Certificate certificate = order.getCertificate();
LOG.info("Success! The certificate for domains {} has been generated!", domains);
LOG.info("Certificate URL: {}", certificate.getLocation());

View File

@ -23,7 +23,6 @@ import java.security.KeyPair;
import org.junit.jupiter.api.Test;
import org.shredzone.acme4j.AccountBuilder;
import org.shredzone.acme4j.Authorization;
import org.shredzone.acme4j.Certificate;
import org.shredzone.acme4j.Order;
import org.shredzone.acme4j.Session;
import org.shredzone.acme4j.Status;
@ -93,9 +92,7 @@ public class OrderHttpIT {
.conditionEvaluationListener(cond -> updateOrder(order))
.untilAsserted(() -> assertThat(order.getStatus()).isNotIn(Status.PENDING, Status.PROCESSING));
var cert = order.getCertificate()
.map(Certificate::getCertificate)
.orElseThrow();
var cert = order.getCertificate().getCertificate();
assertThat(cert.getNotAfter()).isNotNull();
assertThat(cert.getNotBefore()).isNotNull();
assertThat(cert.getSubjectX500Principal().getName()).contains("CN=" + TEST_DOMAIN);

View File

@ -195,7 +195,7 @@ public class OrderIT extends PebbleITBase {
assertThat(order.getStatus()).isEqualTo(Status.VALID);
var certificate = order.getCertificate().orElseThrow();
var certificate = order.getCertificate();
var cert = certificate.getCertificate();
assertThat(cert).isNotNull();
assertThat(cert.getNotBefore().toInstant()).isEqualTo(notBefore);

View File

@ -25,7 +25,6 @@ import java.time.temporal.ChronoUnit;
import org.bouncycastle.asn1.x509.GeneralName;
import org.junit.jupiter.api.Test;
import org.shredzone.acme4j.AccountBuilder;
import org.shredzone.acme4j.Certificate;
import org.shredzone.acme4j.Session;
import org.shredzone.acme4j.Status;
import org.shredzone.acme4j.challenge.Dns01Challenge;
@ -112,9 +111,7 @@ public class OrderWildcardIT extends PebbleITBase {
order.getStatus()).isNotIn(Status.PENDING, Status.PROCESSING));
var cert = order.getCertificate()
.map(Certificate::getCertificate)
.orElseThrow();
var cert = order.getCertificate().getCertificate();
assertThat(cert).isNotNull();
assertThat(cert.getNotAfter()).isNotEqualTo(notBefore);
assertThat(cert.getNotBefore()).isNotEqualTo(notAfter);