forked from USACE/cwms-data-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Changed format to include ProjectKind constants
- Loading branch information
1 parent
6803358
commit a61b5ce
Showing
8 changed files
with
296 additions
and
111 deletions.
There are no files selected for viewing
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
65 changes: 65 additions & 0 deletions
65
cwms-data-api/src/main/java/cwms/cda/data/dto/project/LocationsWithProjectKind.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,65 @@ | ||
package cwms.cda.data.dto.project; | ||
|
||
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 com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder; | ||
import cwms.cda.data.dao.project.ProjectKind; | ||
import cwms.cda.data.dto.CwmsId; | ||
import cwms.cda.formatters.Formats; | ||
import cwms.cda.formatters.annotations.FormattableWith; | ||
import cwms.cda.formatters.json.JsonV2; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
@JsonDeserialize(builder = LocationsWithProjectKind.Builder.class) | ||
@JsonInclude(JsonInclude.Include.NON_NULL) | ||
@JsonNaming(PropertyNamingStrategies.KebabCaseStrategy.class) | ||
@FormattableWith(contentType = Formats.JSONV2, aliases = {Formats.JSON}, formatter = JsonV2.class) | ||
public class LocationsWithProjectKind { | ||
ProjectKind kind; | ||
List<CwmsId> locations; | ||
|
||
private LocationsWithProjectKind(Builder builder) { | ||
kind = builder.kind; | ||
locations = builder.locations; | ||
} | ||
|
||
public ProjectKind getKind() { | ||
return kind; | ||
} | ||
|
||
public List<CwmsId> getLocations() { | ||
return locations; | ||
} | ||
|
||
@JsonPOJOBuilder | ||
@JsonNaming(PropertyNamingStrategies.KebabCaseStrategy.class) | ||
public static class Builder { | ||
private ProjectKind kind; | ||
private List<CwmsId> locations = new ArrayList<>(); | ||
|
||
public Builder() { | ||
|
||
} | ||
|
||
public Builder withKind(ProjectKind kind) { | ||
this.kind = kind; | ||
return this; | ||
} | ||
|
||
public Builder withLocations(List<CwmsId> locations) { | ||
if (locations == null) { | ||
this.locations = null; | ||
} else { | ||
this.locations = new ArrayList<>(locations); | ||
} | ||
return this; | ||
} | ||
|
||
public LocationsWithProjectKind build() { | ||
return new LocationsWithProjectKind(this); | ||
} | ||
} | ||
} |
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
51 changes: 51 additions & 0 deletions
51
cwms-data-api/src/test/java/cwms/cda/data/dto/project/LocationsWithProjectKindTest.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,51 @@ | ||
package cwms.cda.data.dto.project; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import cwms.cda.data.dao.project.ProjectKind; | ||
import cwms.cda.data.dto.CwmsId; | ||
import cwms.cda.formatters.json.JsonV2; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import org.junit.jupiter.api.Test; | ||
|
||
|
||
public class LocationsWithProjectKindTest { | ||
|
||
@Test | ||
void test_serialize() throws JsonProcessingException { | ||
LocationsWithProjectKind.Builder builder = new LocationsWithProjectKind.Builder(); | ||
builder.withKind(ProjectKind.EMBANKMENT); | ||
List<CwmsId> locs = new ArrayList<>(); | ||
locs.add(new CwmsId.Builder().withName("Emb1").withOfficeId("SPK").build()); | ||
locs.add(new CwmsId.Builder().withName("Emb2").withOfficeId("SPK").build()); | ||
builder.withLocations(locs); | ||
LocationsWithProjectKind locWithKind = builder.build(); | ||
|
||
ObjectMapper objectMapper = JsonV2.buildObjectMapper(); | ||
String json = objectMapper.writeValueAsString(locWithKind); | ||
assertNotNull(json); | ||
|
||
} | ||
|
||
@Test | ||
void test_deserialize() throws IOException { | ||
|
||
InputStream stream = LocationsWithProjectKind.class.getClassLoader().getResourceAsStream( | ||
"cwms/cda/data/dto/location_with_kind.json"); | ||
assertNotNull(stream); | ||
ObjectMapper objectMapper = JsonV2.buildObjectMapper(); | ||
LocationsWithProjectKind locationsWithKind = objectMapper.readValue(stream, LocationsWithProjectKind.class); | ||
assertNotNull(locationsWithKind); | ||
assertEquals(ProjectKind.EMBANKMENT, locationsWithKind.getKind()); | ||
assertEquals("TestEmbankment1", locationsWithKind.getLocations().get(0).getName()); | ||
assertEquals("TestEmbankment2", locationsWithKind.getLocations().get(1).getName()); | ||
|
||
} | ||
|
||
} |
Oops, something went wrong.