diff --git a/src/main/java/org/hisp/dhis/response/data/DataResponse.java b/src/main/java/org/hisp/dhis/response/data/DataResponse.java index 84242af1..97d18671 100644 --- a/src/main/java/org/hisp/dhis/response/data/DataResponse.java +++ b/src/main/java/org/hisp/dhis/response/data/DataResponse.java @@ -43,6 +43,8 @@ @Setter @NoArgsConstructor public class DataResponse extends Response { + private static final String KEY_ID = "id"; + @JsonProperty protected Object data; /** @@ -63,6 +65,16 @@ public String toString() { return newToStringBuilder(this, super.toString()).append("data", data).toString(); } + /** + * Creates a {@link DataResponse} indicating a HTTP 201 Created status. + * + * @param id the identifier of the created object. + * @return a {@link DataResponse} indicating a HTTP 201 Created status. + */ + public static DataResponse created(String id) { + return new DataResponse(Status.OK, HttpStatus.CREATED, "Object created", getIdMap(id)); + } + /** * Creates a map with a single entry where the key is "id" and the value is the provided value. * @@ -70,6 +82,6 @@ public String toString() { * @return a map containing the "id" entry. */ public static Map getIdMap(String id) { - return Map.of("id", id); + return Map.of(KEY_ID, id); } } diff --git a/src/test/java/org/hisp/dhis/response/data/DataResponseTest.java b/src/test/java/org/hisp/dhis/response/data/DataResponseTest.java new file mode 100644 index 00000000..fe2dd646 --- /dev/null +++ b/src/test/java/org/hisp/dhis/response/data/DataResponseTest.java @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2004-2026, University of Oslo + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * Neither the name of the HISP project nor the names of its contributors may + * be used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package org.hisp.dhis.response.data; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.util.Map; +import org.hisp.dhis.response.HttpStatus; +import org.hisp.dhis.response.Status; +import org.hisp.dhis.support.TestTags; +import org.junit.jupiter.api.Tag; +import org.junit.jupiter.api.Test; + +@Tag(TestTags.UNIT) +class DataResponseTest { + @Test + void testCreated() { + DataResponse created = DataResponse.created("yL45yoxi6ew"); + + assertEquals(Status.OK, created.getStatus()); + assertEquals(HttpStatus.CREATED, created.getHttpStatus()); + assertNotNull(created.getData()); + assertTrue(created.getData() instanceof Map); + assertEquals("yL45yoxi6ew", ((Map) created.getData()).get("id")); + } +}