Skip to content

Commit

Permalink
Adding json resource, and TimeZones class
Browse files Browse the repository at this point in the history
Added TimeZones class so we can deserialize to and from a list object like LocationLevels.
  • Loading branch information
RyanM-RMA committed Jun 17, 2024
1 parent 71401ef commit 687a01e
Show file tree
Hide file tree
Showing 5 changed files with 1,921 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.codahale.metrics.Timer;
import cwms.cda.data.dao.TimeZoneDao;
import cwms.cda.data.dto.TimeZone;
import cwms.cda.data.dto.TimeZones;
import cwms.cda.formatters.ContentType;
import cwms.cda.formatters.Formats;
import io.javalin.apibuilder.CrudHandler;
Expand Down Expand Up @@ -97,8 +98,8 @@ public void getAll(Context ctx) {
String results;
if (format.isEmpty() && !isLegacyVersion)
{
List<TimeZone> zones = dao.getTimeZones();
results = Formats.format(contentType, zones, TimeZone.class);
TimeZones zones = dao.getTimeZones();
results = Formats.format(contentType, zones);
ctx.contentType(contentType.toString());
}
else
Expand Down
15 changes: 8 additions & 7 deletions cwms-data-api/src/main/java/cwms/cda/data/dao/TimeZoneDao.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cwms.cda.data.dao;

import cwms.cda.data.dto.TimeZone;
import cwms.cda.data.dto.TimeZones;
import org.jooq.DSLContext;
import org.jooq.Record1;
import usace.cwms.db.jooq.codegen.packages.CWMS_CAT_PACKAGE;
Expand All @@ -19,13 +20,13 @@ public String getTimeZones(String format) {
return CWMS_CAT_PACKAGE.call_RETRIEVE_TIME_ZONES_F(dsl.configuration(), format);
}

public List<TimeZone> getTimeZones()
public TimeZones getTimeZones()
{
return dsl.select(MV_TIME_ZONE.MV_TIME_ZONE.TIME_ZONE_NAME)
.from(MV_TIME_ZONE.MV_TIME_ZONE)
.stream()
.map(Record1::component1)
.map(TimeZone::new)
.collect(Collectors.toList());
return new TimeZones(dsl.select(MV_TIME_ZONE.MV_TIME_ZONE.TIME_ZONE_NAME)
.from(MV_TIME_ZONE.MV_TIME_ZONE)
.stream()
.map(Record1::component1)
.map(TimeZone::new)
.collect(Collectors.toList()));
}
}
51 changes: 51 additions & 0 deletions cwms-data-api/src/main/java/cwms/cda/data/dto/TimeZones.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright (c) 2024. Hydrologic Engineering Center (HEC).
* United States Army Corps of Engineers
* All Rights Reserved. HEC PROPRIETARY/CONFIDENTIAL.
* Source may not be released without written approval from HEC
*/

package cwms.cda.data.dto;

import com.fasterxml.jackson.annotation.JsonRootName;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
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.JsonV2;
import cwms.cda.formatters.xml.XMLv2;

import java.util.List;

@JsonRootName("time-zones")
@FormattableWith(contentType = Formats.JSONV2, formatter = JsonV2.class, aliases = {Formats.DEFAULT, Formats.JSON})
@FormattableWith(contentType = Formats.XMLV2, formatter = XMLv2.class, aliases = {Formats.XML})
@JsonNaming(PropertyNamingStrategies.KebabCaseStrategy.class)
public final class TimeZones extends CwmsDTO
{
private List<TimeZone> timeZones;

@SuppressWarnings("unused") // for JAXB to handle marshalling
private TimeZones()
{
super(null);
}

public TimeZones(List<TimeZone> timeZones)
{
super(null);
this.timeZones = timeZones;
}

public List<TimeZone> getTimeZones()
{
return timeZones;
}

@Override
public void validate() throws FieldException
{
//No validation needed
}
}
49 changes: 49 additions & 0 deletions cwms-data-api/src/test/java/cwms/cda/data/dto/TimeZoneTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,22 @@
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 org.junit.jupiter.api.function.Executable;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.EnumSource;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.time.ZoneId;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import static org.junit.jupiter.api.Assertions.*;

class TimeZoneTest
Expand Down Expand Up @@ -56,6 +68,43 @@ void test_validate_failure()
assertThrows(FieldException.class, zone::validate);
}

@Test
void test_serialize_list()
{
ContentType contentType = new ContentType(Formats.JSONV2);
TimeZones tzs = new TimeZones(ZoneId.getAvailableZoneIds()
.stream()
.map(TimeZone::new)
.collect(Collectors.toList()));
String json = Formats.format(contentType, tzs);
TimeZones receivedTzs = Formats.parseContent(contentType, json, TimeZones.class);

List<TimeZone> expectedZones = tzs.getTimeZones();
List<TimeZone> receivedZones = receivedTzs.getTimeZones();

assertEquals(expectedZones.size(), receivedZones.size());

assertAll(IntStream.range(0, expectedZones.size())
.mapToObj(i -> testZone(expectedZones.get(i), receivedZones.get(i)))
.collect(Collectors.toList()));
}

@Test
void test_from_resource_file() throws Exception
{
InputStream resource = getClass().getClassLoader().getResourceAsStream("cwms/cda/data/dto/TimeZones.json");
assertNotNull(resource);
String json = IOUtils.toString(resource, StandardCharsets.UTF_8);
ContentType contentType = new ContentType(Formats.JSONV2);
TimeZones receivedTzs = Formats.parseContent(contentType, json, TimeZones.class);
assertTrue(!receivedTzs.getTimeZones().isEmpty());
}

private Executable testZone(TimeZone expected, TimeZone received)
{
return () -> assertEquals(expected.getTimeZone(), received.getTimeZone());
}

enum SerializationType
{
JSONV2(Formats.JSONV2),
Expand Down
Loading

0 comments on commit 687a01e

Please sign in to comment.