Skip to content

Commit

Permalink
CTO-67 - Added Location Identifier dto
Browse files Browse the repository at this point in the history
  • Loading branch information
rma-bryson committed Jun 18, 2024
1 parent 5593da0 commit 3b7ba85
Show file tree
Hide file tree
Showing 3 changed files with 206 additions and 0 deletions.
107 changes: 107 additions & 0 deletions cwms-data-api/src/main/java/cwms/cda/data/dto/LocationIdentifier.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
Original file line number Diff line number Diff line change
@@ -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");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"office-id": "Office123",
"location-id": "Stream123"
}

0 comments on commit 3b7ba85

Please sign in to comment.