Use JSON parser for Status

pull/55/head
Richard Körber 2017-04-18 01:08:11 +02:00
parent 698d25fd14
commit e52a900001
7 changed files with 25 additions and 22 deletions

View File

@ -223,7 +223,7 @@ public class Authorization extends AcmeResource {
* JSON data * JSON data
*/ */
protected void unmarshalAuthorization(JSON json) { protected void unmarshalAuthorization(JSON json) {
this.status = Status.parse(json.get("status").asString(), Status.PENDING); this.status = json.get("status").asStatusOrElse(Status.PENDING);
String jsonExpires = json.get("expires").asString(); String jsonExpires = json.get("expires").asString();
if (jsonExpires != null) { if (jsonExpires != null) {

View File

@ -38,18 +38,4 @@ public enum Status {
.orElse(Status.UNKNOWN); .orElse(Status.UNKNOWN);
} }
/**
* Parses the string and returns a corresponding Status object.
*
* @param str
* String to parse
* @param def
* Default Status if str is {@code null}
* @return {@link Status} matching the string, or {@link Status#UNKNOWN} if there was
* no match, or {@code def} if the str was {@code null}
*/
public static Status parse(String str, Status def) {
return str != null ? parse(str) : def;
}
} }

View File

@ -99,7 +99,7 @@ public class Challenge extends AcmeResource {
* Returns the current status of the challenge. * Returns the current status of the challenge.
*/ */
public Status getStatus() { public Status getStatus() {
return Status.parse(data.get(KEY_STATUS).asString(), Status.PENDING); return data.get(KEY_STATUS).asStatusOrElse(Status.PENDING);
} }
/** /**

View File

@ -41,6 +41,7 @@ import java.util.stream.StreamSupport;
import org.jose4j.json.JsonUtil; import org.jose4j.json.JsonUtil;
import org.jose4j.lang.JoseException; import org.jose4j.lang.JoseException;
import org.shredzone.acme4j.Status;
import org.shredzone.acme4j.exception.AcmeProtocolException; import org.shredzone.acme4j.exception.AcmeProtocolException;
/** /**
@ -392,6 +393,21 @@ public final class JSON implements Serializable {
} }
} }
/**
* Returns the parsed status.
*
* @param def
* Default status if value is not present or {@code null}
* @return {@link Status}
*/
public Status asStatusOrElse(Status def) {
if (val == null) {
return def;
}
return Status.parse(val.toString());
}
@Override @Override
public boolean equals(Object obj) { public boolean equals(Object obj) {
if (obj == null || !(obj instanceof Value)) { if (obj == null || !(obj instanceof Value)) {

View File

@ -35,9 +35,6 @@ public class StatusTest {
// unknown status returns UNKNOWN // unknown status returns UNKNOWN
assertThat(Status.parse("foo"), is(Status.UNKNOWN)); assertThat(Status.parse("foo"), is(Status.UNKNOWN));
// null returns default value
assertThat(Status.parse(null, Status.PROCESSING), is(Status.PROCESSING));
} }
} }

View File

@ -36,6 +36,7 @@ import java.util.NoSuchElementException;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import org.junit.Test; import org.junit.Test;
import org.shredzone.acme4j.Status;
import org.shredzone.acme4j.exception.AcmeProtocolException; import org.shredzone.acme4j.exception.AcmeProtocolException;
/** /**
@ -84,7 +85,8 @@ public class JSONTest {
JSON json = TestUtils.getJsonAsObject("json"); JSON json = TestUtils.getJsonAsObject("json");
assertThat(json.keySet(), containsInAnyOrder( assertThat(json.keySet(), containsInAnyOrder(
"text", "number", "boolean", "uri", "url", "date", "array", "collect")); "text", "number", "boolean", "uri", "url", "date", "array",
"collect", "status"));
assertThat(json.contains("text"), is(true)); assertThat(json.contains("text"), is(true));
assertThat(json.contains("music"), is(false)); assertThat(json.contains("music"), is(false));
assertThat(json.get("text"), is(notNullValue())); assertThat(json.get("text"), is(notNullValue()));
@ -178,6 +180,7 @@ public class JSONTest {
assertThat(json.get("uri").asURI(), is(URI.create("mailto:foo@example.com"))); assertThat(json.get("uri").asURI(), is(URI.create("mailto:foo@example.com")));
assertThat(json.get("url").asURL(), is(new URL("http://example.com"))); assertThat(json.get("url").asURL(), is(new URL("http://example.com")));
assertThat(json.get("date").asInstant(), is(date)); assertThat(json.get("date").asInstant(), is(date));
assertThat(json.get("status").asStatusOrElse(Status.INVALID), is(Status.VALID));
JSON.Array array = json.get("array").asArray(); JSON.Array array = json.get("array").asArray();
assertThat(array.get(0).asString(), is("foo")); assertThat(array.get(0).asString(), is("foo"));
@ -204,8 +207,8 @@ public class JSONTest {
assertThat(json.get("none").asURI(), is(nullValue())); assertThat(json.get("none").asURI(), is(nullValue()));
assertThat(json.get("none").asURL(), is(nullValue())); assertThat(json.get("none").asURL(), is(nullValue()));
assertThat(json.get("none").asInstant(), is(nullValue())); assertThat(json.get("none").asInstant(), is(nullValue()));
assertThat(json.get("none").asArray(), is(nullValue()));
assertThat(json.get("none").asObject(), is(nullValue())); assertThat(json.get("none").asObject(), is(nullValue()));
assertThat(json.get("none").asStatusOrElse(Status.INVALID), is(Status.INVALID));
try { try {
json.get("none").asInt(); json.get("none").asInt();

View File

@ -44,7 +44,8 @@ json = \
"url": "http://example.com",\ "url": "http://example.com",\
"date": "2016-01-08T00:00:00Z",\ "date": "2016-01-08T00:00:00Z",\
"array": ["foo", 987, [1, 2, 3], {"test": "ok"}],\ "array": ["foo", 987, [1, 2, 3], {"test": "ok"}],\
"collect": ["foo", "bar", "barfoo"]\ "collect": ["foo", "bar", "barfoo"],\
"status": "VALID"\
} }
newRegistration = \ newRegistration = \