diff --git a/pom.xml b/pom.xml index 20808609..463b9914 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ org.hisp dhis2-java-client - 2.6.1 + 2.6.2 jar DHIS 2 API client for Java diff --git a/src/main/java/org/hisp/dhis/response/HttpStatus.java b/src/main/java/org/hisp/dhis/response/HttpStatus.java index c934c5fd..4bd4e2f4 100644 --- a/src/main/java/org/hisp/dhis/response/HttpStatus.java +++ b/src/main/java/org/hisp/dhis/response/HttpStatus.java @@ -27,7 +27,6 @@ */ package org.hisp.dhis.response; -import com.fasterxml.jackson.annotation.JsonCreator; import org.hisp.dhis.model.exception.IllegalArgumentFormatException; /** Enumeration of HTTP status codes. */ @@ -183,22 +182,12 @@ public boolean isError() { return is4xxClientError() || is5xxServerError(); } - /** - * Return a string representation of this status code. - * - * @return a string representation of this status code. - */ - @Override - public String toString() { - return String.format("%s %s", value, name()); - } - /** * Return the enum constant of this type with the specified numeric value. * - * @param statusCode the numeric value of the enum to be returned - * @return the enum constant with the specified numeric value - * @throws IllegalArgumentException if this enum has no constant for the specified numeric value + * @param statusCode the numeric value of the enum to be returned. + * @return the enum constant with the specified numeric value. + * @throws IllegalArgumentException if this enum has no constant for the specified numeric value. */ public static HttpStatus valueOf(int statusCode) { HttpStatus status = resolve(statusCode); @@ -212,24 +201,23 @@ public static HttpStatus valueOf(int statusCode) { /** * Return the enum constant of this type with the specified name. * - * @param name the enum name of the enum to be returned - * @return the enum constant with the specified name - * @throws IllegalArgumentException if this enum has no constant for the specified name + * @param name the enum name of the enum to be returned. + * @return the enum constant with the specified name. + * @throws IllegalArgumentException if this enum has no constant for the specified name. */ - @JsonCreator public static HttpStatus fromName(String name) { try { return Enum.valueOf(HttpStatus.class, name); } catch (IllegalArgumentException ex) { - throw new IllegalArgumentFormatException("No matching name for status code: {}", name); + throw new IllegalArgumentFormatException("No matching name for status code: '{}'", name); } } /** * Resolve the given status code to an {@code HttpStatus}, if possible. * - * @param statusCode the HTTP status code (potentially non-standard) - * @return the corresponding {@code HttpStatus}, or {@code null} if not found + * @param statusCode the HTTP status code (potentially non-standard). + * @return the corresponding {@code HttpStatus}, or {@code null} if not found. */ public static HttpStatus resolve(int statusCode) { for (HttpStatus status : values()) { diff --git a/src/main/java/org/hisp/dhis/response/Response.java b/src/main/java/org/hisp/dhis/response/Response.java index 17e7a44c..bd2b641f 100644 --- a/src/main/java/org/hisp/dhis/response/Response.java +++ b/src/main/java/org/hisp/dhis/response/Response.java @@ -31,6 +31,7 @@ import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonPropertyOrder; import lombok.Getter; import lombok.NoArgsConstructor; import lombok.Setter; @@ -39,6 +40,7 @@ @Getter @Setter @NoArgsConstructor +@JsonPropertyOrder({"httpStatusCode", "httpStatus", "headers", "status", "message"}) public class Response extends BaseHttpResponse { /** HTTP {@link Status} enum. */ @JsonProperty protected Status status; diff --git a/src/main/java/org/hisp/dhis/util/JacksonUtils.java b/src/main/java/org/hisp/dhis/util/JacksonUtils.java index 71f93fd1..5b235fad 100644 --- a/src/main/java/org/hisp/dhis/util/JacksonUtils.java +++ b/src/main/java/org/hisp/dhis/util/JacksonUtils.java @@ -45,6 +45,7 @@ import tools.jackson.databind.JsonNode; import tools.jackson.databind.ObjectMapper; import tools.jackson.databind.SerializationFeature; +import tools.jackson.databind.cfg.EnumFeature; import tools.jackson.databind.json.JsonMapper; import tools.jackson.databind.module.SimpleModule; @@ -91,6 +92,8 @@ private static JsonMapper createJsonMapper() { return JsonMapper.builder() .disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES) .disable(SerializationFeature.FAIL_ON_EMPTY_BEANS) + .disable(EnumFeature.WRITE_ENUMS_USING_TO_STRING) + .disable(EnumFeature.READ_ENUMS_USING_TO_STRING) .changeDefaultPropertyInclusion( incl -> incl.withValueInclusion(JsonInclude.Include.NON_NULL)) .changeDefaultPropertyInclusion( diff --git a/src/test/java/org/hisp/dhis/TestFixture.java b/src/test/java/org/hisp/dhis/TestFixture.java index 2643c9b3..dc910f52 100644 --- a/src/test/java/org/hisp/dhis/TestFixture.java +++ b/src/test/java/org/hisp/dhis/TestFixture.java @@ -38,9 +38,9 @@ public final class TestFixture { public static final String DEV_URL = "https://play.im.dhis2.org/dev"; - public static final String V41_URL = "https://play.im.dhis2.org/stable-2-41-8"; + public static final String V41_URL = "https://play.im.dhis2.org/stable-2-41-8-1"; - public static final String V42_URL = "https://play.im.dhis2.org/stable-2-42-4"; + public static final String V42_URL = "https://play.im.dhis2.org/stable-2-42-4-1"; public static final String LOCAL_URL = "http://localhost/dhis"; diff --git a/src/test/java/org/hisp/dhis/response/HttpStatusTest.java b/src/test/java/org/hisp/dhis/response/HttpStatusTest.java index a4521f45..745d6528 100644 --- a/src/test/java/org/hisp/dhis/response/HttpStatusTest.java +++ b/src/test/java/org/hisp/dhis/response/HttpStatusTest.java @@ -69,6 +69,6 @@ void testFromNameNotFound() { assertThrows(IllegalArgumentFormatException.class, () -> HttpStatus.fromName("INVALID")); assertNotNull(exception); - assertEquals("No matching name for status code: INVALID", exception.getMessage()); + assertEquals("No matching name for status code: 'INVALID'", exception.getMessage()); } } diff --git a/src/test/java/org/hisp/dhis/util/JacksonUtilsTest.java b/src/test/java/org/hisp/dhis/util/JacksonUtilsTest.java index 6e0f58c7..3cb15bef 100644 --- a/src/test/java/org/hisp/dhis/util/JacksonUtilsTest.java +++ b/src/test/java/org/hisp/dhis/util/JacksonUtilsTest.java @@ -44,6 +44,9 @@ import org.hisp.dhis.model.Option; import org.hisp.dhis.model.Product; import org.hisp.dhis.model.ValueType; +import org.hisp.dhis.response.HttpStatus; +import org.hisp.dhis.response.Response; +import org.hisp.dhis.response.Status; import org.hisp.dhis.support.TestTags; import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Test; @@ -270,6 +273,31 @@ void testFromJsonToIntegerSet() { assertEquals(expected, list); } + @Test + void testToJsonEum() { + Response response = new Response(Status.OK, HttpStatus.CONFLICT, "Conflict"); + + String expected = + """ + {"httpStatusCode":409,"httpStatus":"CONFLICT","status":"OK","message":"Conflict"}"""; + + assertEquals(expected, JacksonUtils.toJsonString(response)); + } + + @Test + void testFromJsonEnum() { + String json = + """ + {"httpStatusCode":409,"httpStatus":"CONFLICT","status":"OK","message":"Conflict"}"""; + + Response response = JacksonUtils.fromJson(json, Response.class); + + assertNotNull(response); + assertEquals(409, response.getHttpStatusCode()); + assertEquals(Status.OK, response.getStatus()); + assertEquals(HttpStatus.CONFLICT, response.getHttpStatus()); + } + /** * Returns a {@link DataElement}. *