From 3b7ba85bb69a515225a4139d33d39d58032c184a Mon Sep 17 00:00:00 2001 From: Bryson Spilman Date: Tue, 18 Jun 2024 15:35:27 -0700 Subject: [PATCH] CTO-67 - Added Location Identifier dto --- .../cwms/cda/data/dto/LocationIdentifier.java | 107 ++++++++++++++++++ .../cda/data/dto/LocationIdentifierTest.java | 95 ++++++++++++++++ .../cda/data/dto/location_identifier.json | 4 + 3 files changed, 206 insertions(+) create mode 100644 cwms-data-api/src/main/java/cwms/cda/data/dto/LocationIdentifier.java create mode 100644 cwms-data-api/src/test/java/cwms/cda/data/dto/LocationIdentifierTest.java create mode 100644 cwms-data-api/src/test/resources/cwms/cda/data/dto/location_identifier.json diff --git a/cwms-data-api/src/main/java/cwms/cda/data/dto/LocationIdentifier.java b/cwms-data-api/src/main/java/cwms/cda/data/dto/LocationIdentifier.java new file mode 100644 index 000000000..a114e5328 --- /dev/null +++ b/cwms-data-api/src/main/java/cwms/cda/data/dto/LocationIdentifier.java @@ -0,0 +1,107 @@ +/* + * MIT License + * + * Copyright (c) 2024 Hydrologic Engineering Center + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package cwms.cda.data.dto; + +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.databind.PropertyNamingStrategies; +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; +import com.fasterxml.jackson.databind.annotation.JsonNaming; +import cwms.cda.api.errors.FieldException; +import cwms.cda.formatters.Formats; +import cwms.cda.formatters.annotations.FormattableWith; +import cwms.cda.formatters.json.JsonV1; + +import java.util.Objects; + +@FormattableWith(contentType = Formats.JSON, formatter = JsonV1.class) +@JsonDeserialize(builder = LocationIdentifier.Builder.class) +@JsonInclude(JsonInclude.Include.NON_NULL) +@JsonNaming(PropertyNamingStrategies.KebabCaseStrategy.class) +public final class LocationIdentifier implements CwmsDTOBase { + + private final String officeId; + private final String locationId; + + public LocationIdentifier(Builder builder) { + this.officeId = builder.officeId; + this.locationId = builder.locationId; + } + + @Override + public void validate() throws FieldException { + if(this.officeId == null || this.officeId.isEmpty()){ + throw new FieldException("The 'officeId' field of a LocationIdentifier cannot be null or empty."); + } + if(this.locationId == null || this.locationId.isEmpty()){ + throw new FieldException("The 'locationId' field of a LocationIdentifier cannot be null or empty."); + } + } + + public String getOfficeId() { + return officeId; + } + + public String getLocationId() { + return locationId; + } + + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + + LocationIdentifier that = (LocationIdentifier) o; + return Objects.equals(getOfficeId(), that.getOfficeId()) + && Objects.equals(getLocationId(), that.getLocationId()); + } + + @Override + public int hashCode() { + return Objects.hash(getOfficeId(), getLocationId()); + } + + public static class Builder { + private String officeId; + private String locationId; + + public Builder withOfficeId(String officeId) { + this.officeId = officeId; + return this; + } + public Builder withLocationId(String locationId) { + this.locationId = locationId; + return this; + } + + public LocationIdentifier build() { + return new LocationIdentifier(this); + } + } +} \ No newline at end of file diff --git a/cwms-data-api/src/test/java/cwms/cda/data/dto/LocationIdentifierTest.java b/cwms-data-api/src/test/java/cwms/cda/data/dto/LocationIdentifierTest.java new file mode 100644 index 000000000..a3b453e77 --- /dev/null +++ b/cwms-data-api/src/test/java/cwms/cda/data/dto/LocationIdentifierTest.java @@ -0,0 +1,95 @@ +/* + * MIT License + * + * Copyright (c) 2024 Hydrologic Engineering Center + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package cwms.cda.data.dto; + +import cwms.cda.api.errors.FieldException; +import cwms.cda.formatters.ContentType; +import cwms.cda.formatters.Formats; +import org.apache.commons.io.IOUtils; +import org.junit.jupiter.api.Test; + +import java.io.InputStream; +import java.nio.charset.StandardCharsets; + +import static org.junit.jupiter.api.Assertions.*; + +final class LocationIdentifierTest { + + @Test + void createLocationIdentifier_allFieldsProvided_success() { + LocationIdentifier item = new LocationIdentifier.Builder() + .withLocationId("Stream123") + .withOfficeId("Office123") + .build(); + assertAll(() -> assertEquals("Stream123", item.getLocationId(), "The location ID does not match the provided value"), + () -> assertEquals("Office123", item.getOfficeId(), "The office ID does not match the provided value")); + } + + @Test + void createLocationIdentifier_missingField_throwsFieldException() { + assertAll( + // When locationId is missing + () -> assertThrows(FieldException.class, () -> { + LocationIdentifier item = new LocationIdentifier.Builder() + .withOfficeId("Office123") + .build(); + item.validate(); + }, "The validate method should have thrown a FieldException because the locationId field is missing"), + + // When officeId is missing + () -> assertThrows(FieldException.class, () -> { + LocationIdentifier item = new LocationIdentifier.Builder() + .withLocationId("Stream123") + .build(); + item.validate(); + }, "The validate method should have thrown a FieldException because the officeId field is missing")); + } + + @Test + void createLocationIdentifier_serialize_roundtrip() { + LocationIdentifier locationIdentifier = new LocationIdentifier.Builder() + .withLocationId("Stream123") + .withOfficeId("Office123") + .build(); + ContentType contentType = new ContentType(Formats.JSON); + String json = Formats.format(contentType, locationIdentifier); + LocationIdentifier deserialized = Formats.parseContent(contentType, json, LocationIdentifier.class); + assertEquals(locationIdentifier, deserialized, "LocationIdentifier deserialized from JSON doesn't equal original"); + } + + @Test + void createLocationIdentifier_deserialize() throws Exception { + LocationIdentifier locationIdentifier = new LocationIdentifier.Builder() + .withLocationId("Stream123") + .withOfficeId("Office123") + .build(); + InputStream resource = this.getClass().getResourceAsStream("/cwms/cda/data/dto/location_identifier.json"); + assertNotNull(resource); + String json = IOUtils.toString(resource, StandardCharsets.UTF_8); + ContentType contentType = new ContentType(Formats.JSON); + LocationIdentifier deserialized = Formats.parseContent(contentType, json, LocationIdentifier.class); + assertEquals(locationIdentifier, deserialized, "LocationIdentifier deserialized from JSON doesn't equal original"); + } +} \ No newline at end of file diff --git a/cwms-data-api/src/test/resources/cwms/cda/data/dto/location_identifier.json b/cwms-data-api/src/test/resources/cwms/cda/data/dto/location_identifier.json new file mode 100644 index 000000000..9e41e4669 --- /dev/null +++ b/cwms-data-api/src/test/resources/cwms/cda/data/dto/location_identifier.json @@ -0,0 +1,4 @@ +{ + "office-id": "Office123", + "location-id": "Stream123" +} \ No newline at end of file