-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CTO-67 - Added Location Identifier dto
- Loading branch information
1 parent
5593da0
commit d34c71a
Showing
3 changed files
with
225 additions
and
0 deletions.
There are no files selected for viewing
109 changes: 109 additions & 0 deletions
109
cwms-data-api/src/main/java/cwms/cda/data/dto/LocationIdentifier.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
/* | ||
* 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.annotation.JsonPropertyOrder; | ||
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) | ||
@JsonPropertyOrder({ "officeId", "locationId" }) | ||
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); | ||
} | ||
} | ||
} |
112 changes: 112 additions & 0 deletions
112
cwms-data-api/src/test/java/cwms/cda/data/dto/LocationIdentifierTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
/* | ||
* 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"); | ||
} | ||
|
||
@Test | ||
void createLocationIdentifier_verifyOrdering() { | ||
LocationIdentifier locationIdentifier = new LocationIdentifier.Builder() | ||
.withLocationId("Stream123") | ||
.withOfficeId("Office123") | ||
.build(); | ||
|
||
ContentType contentType = new ContentType(Formats.JSON); | ||
String json = Formats.format(contentType, locationIdentifier); | ||
|
||
// Verify that officeId comes before locationId | ||
int officeIdIndex = json.indexOf("\"office-id\""); | ||
int locationIdIndex = json.indexOf("\"location-id\""); | ||
|
||
assertTrue(officeIdIndex < locationIdIndex, "The officeId field should come before the locationId field in the JSON string"); | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
cwms-data-api/src/test/resources/cwms/cda/data/dto/location_identifier.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"office-id": "Office123", | ||
"location-id": "Stream123" | ||
} |