Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DTO and serialization for Time Zones #703

Merged
merged 4 commits into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 37 additions & 27 deletions cwms-data-api/src/main/java/cwms/cda/api/TimeZoneController.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cwms.cda.api;

import static com.codahale.metrics.MetricRegistry.name;
import static cwms.cda.api.Controllers.ACCEPT;
import static cwms.cda.api.Controllers.FORMAT;
import static cwms.cda.api.Controllers.GET_ALL;
import static cwms.cda.api.Controllers.GET_ONE;
Expand All @@ -9,19 +10,25 @@
import static cwms.cda.api.Controllers.SIZE;
import static cwms.cda.api.Controllers.STATUS_200;
import static cwms.cda.api.Controllers.STATUS_501;
import static cwms.cda.api.Controllers.VERSION;
import static cwms.cda.api.Controllers.addDeprecatedContentTypeWarning;
import static cwms.cda.data.dao.JooqDao.getDslContext;

import com.codahale.metrics.Histogram;
import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.Timer;
import cwms.cda.api.errors.CdaError;
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;
import io.javalin.http.Context;
import io.javalin.plugin.openapi.annotations.OpenApi;
import io.javalin.plugin.openapi.annotations.OpenApiParam;
import io.javalin.plugin.openapi.annotations.OpenApiResponse;

import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletResponse;
Expand Down Expand Up @@ -79,41 +86,44 @@ public void getAll(Context ctx) {
try (Timer.Context timeContext = markAndTime(GET_ALL)) {
DSLContext dsl = getDslContext(ctx);
TimeZoneDao dao = new TimeZoneDao(dsl);
String format = ctx.queryParamAsClass(FORMAT, String.class).getOrDefault("json");
String format = ctx.queryParamAsClass(FORMAT, String.class).getOrDefault("");
String header = ctx.header(ACCEPT);

switch (format) {
case "json": {
ctx.contentType(Formats.JSON);
break;
}
case "tab": {
ctx.contentType(Formats.TAB);
break;
}
case "csv": {
ctx.contentType(Formats.CSV);
break;
}
case "xml": {
ctx.contentType(Formats.XML);
break;
ContentType contentType = Formats.parseHeaderAndQueryParm(header, format, TimeZone.class);
String version = contentType.getParameters()
.getOrDefault(VERSION, "");

boolean isLegacyVersion = version.equals("1");

String results;
if (format.isEmpty() && !isLegacyVersion)
{
TimeZones zones = dao.getTimeZones();
results = Formats.format(contentType, zones);
ctx.contentType(contentType.toString());
}
else
{
if (isLegacyVersion)
{
format = Formats.getLegacyTypeFromContentType(contentType);
}
case "wml2": {
ctx.contentType(Formats.WML2);
break;
results = dao.getTimeZones(format);
if (isLegacyVersion)
{
ctx.contentType(contentType.toString());
}
default: {
ctx.status(HttpServletResponse.SC_NOT_IMPLEMENTED)
.json(CdaError.notImplemented());
return;
else
{
ctx.contentType(contentType.getType());
}
}

String results = dao.getTimeZones(format);
addDeprecatedContentTypeWarning(ctx, contentType);

requestResultSize.update(results.length());
ctx.status(HttpServletResponse.SC_OK);
ctx.result(results);
requestResultSize.update(results.length());
} catch (Exception ex) {
logger.log(Level.SEVERE, null, ex);
ctx.status(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
Expand Down
17 changes: 17 additions & 0 deletions cwms-data-api/src/main/java/cwms/cda/data/dao/TimeZoneDao.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
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;
import usace.cwms.db.jooq.codegen.tables.MV_TIME_ZONE;

import java.util.List;
import java.util.stream.Collectors;

public class TimeZoneDao extends JooqDao<String> {

Expand All @@ -12,4 +19,14 @@ public TimeZoneDao(DSLContext dsl) {
public String getTimeZones(String format) {
return CWMS_CAT_PACKAGE.call_RETRIEVE_TIME_ZONES_F(dsl.configuration(), format);
}

public TimeZones getTimeZones()
{
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()));
}
}
57 changes: 57 additions & 0 deletions cwms-data-api/src/main/java/cwms/cda/data/dto/TimeZone.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* 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.time.ZoneId;

@JsonRootName("time-zone")
@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 TimeZone extends CwmsDTO
{
private String timeZone;

public TimeZone()
{
super(null);
}

public TimeZone(String timeZone)
{
this();
this.timeZone = timeZone;
}

public String getTimeZone()
{
return timeZone;
}

@Override
public void validate() throws FieldException
{
try
{
ZoneId zone = ZoneId.of(timeZone);
}
catch (RuntimeException e)
{
throw new FieldException(e.getMessage());
}
}
}
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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ private String getFormatted(ContentType type, List<? extends CwmsDTOBase> dtos,
return outputFormatter.format(dtos);
} else {
String message = String.format("No Format for this content-type and data type : (%s, %s)",
type.toString(), dtos.get(0).getClass().getName());
type.toString(), rootType.getName());
throw new FormattingException(message);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package cwms.cda.api;

import cwms.cda.formatters.Formats;
import io.restassured.filter.log.LogDetail;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.EnumSource;

import javax.servlet.http.HttpServletResponse;

import static cwms.cda.api.Controllers.FORMAT;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.is;

@Tag("integration")
class TimeZoneControllerTestIT extends DataApiTestIT
{
@ParameterizedTest
@EnumSource(GetAllTest.class)
void test_getAll(GetAllTest test)
{
given()
.log().ifValidationFails(LogDetail.ALL,true)
.accept(test._accept)
.when()
.redirects().follow(true)
.redirects().max(3)
.get("/timezones")
.then()
.assertThat()
.log().ifValidationFails(LogDetail.ALL,true)
.statusCode(is(HttpServletResponse.SC_OK))
.contentType(is(test._expectedContentType));
}

@ParameterizedTest
@EnumSource(GetAllLegacyTest.class)
void test_getAll(GetAllLegacyTest test)
{
given()
.log().ifValidationFails(LogDetail.ALL,true)
.queryParam(FORMAT, test._format)
.when()
.redirects().follow(true)
.redirects().max(3)
.get("/timezones")
.then()
.assertThat()
.log().ifValidationFails(LogDetail.ALL,true)
.statusCode(is(HttpServletResponse.SC_OK))
.contentType(is(test._expectedContentType));
}

enum GetAllLegacyTest
{
JSON(Formats.JSON_LEGACY, Formats.JSON),
XML(Formats.XML_LEGACY, Formats.XML),
TAB(Formats.TAB_LEGACY, Formats.TAB),
CSV(Formats.CSV_LEGACY, Formats.CSV),
;

final String _format;
final String _expectedContentType;

GetAllLegacyTest(String format, String expectedContentType)
{
_format = format;
_expectedContentType = expectedContentType;
}
}

enum GetAllTest
{
DEFAULT(Formats.DEFAULT, Formats.JSONV2),
XML(Formats.XML, Formats.XMLV2),
XMLV1(Formats.XMLV1, Formats.XMLV1),
XMLV2(Formats.XMLV2, Formats.XMLV2),
JSON(Formats.JSON, Formats.JSONV2),
JSONV1(Formats.JSONV1, Formats.JSONV1),
JSONV2(Formats.JSONV2, Formats.JSONV2),
;

final String _accept;
final String _expectedContentType;

GetAllTest(String accept, String expectedContentType)
{
_accept = accept;
_expectedContentType = expectedContentType;
}
}
}
Loading
Loading