From 5f87f695d36956e76088d1adaba56b7cc371be14 Mon Sep 17 00:00:00 2001 From: Mike Neilson Date: Mon, 23 Dec 2024 19:03:10 +0000 Subject: [PATCH] Add and update tests for updated contenttype processing. --- .../java/cwms/cda/formatters/Formats.java | 16 +++++---- .../java/cwms/cda/formatters/FormatsTest.java | 35 +++++++++++++++++-- 2 files changed, 42 insertions(+), 9 deletions(-) diff --git a/cwms-data-api/src/main/java/cwms/cda/formatters/Formats.java b/cwms-data-api/src/main/java/cwms/cda/formatters/Formats.java index ac460b60e..2f924dd79 100644 --- a/cwms-data-api/src/main/java/cwms/cda/formatters/Formats.java +++ b/cwms-data-api/src/main/java/cwms/cda/formatters/Formats.java @@ -116,7 +116,7 @@ public static String getLegacyTypeFromContentType(ContentType contentType) private String getFormatted(ContentType type, CwmsDTOBase toFormat) throws FormattingException { Objects.requireNonNull(toFormat, "Object to be formatted should not be null"); formatters.keySet().forEach(k -> logger.fine(k::toString)); - OutputFormatter outputFormatter = getOutputFormatter(type, toFormat.getClass()); + OutputFormatter outputFormatter = getOutputFormatterInternal(type, toFormat.getClass()); if (outputFormatter != null) { return outputFormatter.format(toFormat); @@ -134,7 +134,7 @@ private String getFormatted(ContentType type, List dtos, logger.finest(() -> key.toString()); } - OutputFormatter outputFormatter = getOutputFormatter(type, rootType); + OutputFormatter outputFormatter = getOutputFormatterInternal(type, rootType); if (outputFormatter != null) { return outputFormatter.format(dtos); @@ -147,7 +147,7 @@ private String getFormatted(ContentType type, List dtos, private T parseContentFromType(ContentType type, String content, Class rootType) throws FormattingException { - OutputFormatter outputFormatter = getOutputFormatter(type, rootType); + OutputFormatter outputFormatter = getOutputFormatterInternal(type, rootType); if (outputFormatter != null) { T retval = outputFormatter.parseContent(content, rootType); retval.validate(); @@ -161,7 +161,7 @@ private T parseContentFromType(ContentType type, String private T parseContentFromType(ContentType type, InputStream content, Class rootType) throws FormattingException { - OutputFormatter outputFormatter = getOutputFormatter(type, rootType); + OutputFormatter outputFormatter = getOutputFormatterInternal(type, rootType); if (outputFormatter != null) { T retval = outputFormatter.parseContent(content, rootType); retval.validate(); @@ -175,7 +175,7 @@ private T parseContentFromType(ContentType type, InputSt private List parseContentListFromType(ContentType type, String content, Class rootType) throws FormattingException { - OutputFormatter outputFormatter = getOutputFormatter(type, rootType); + OutputFormatter outputFormatter = getOutputFormatterInternal(type, rootType); if (outputFormatter != null) { List retval = outputFormatter.parseContentList(content, rootType); if (retval == null) { @@ -192,7 +192,7 @@ private List parseContentListFromType(ContentType typ } } - private OutputFormatter getOutputFormatter(ContentType type, + private OutputFormatter getOutputFormatterInternal(ContentType type, Class klass) { OutputFormatter outputFormatter = null; Map, OutputFormatter> contentFormatters = formatters.get(type); @@ -219,6 +219,10 @@ private OutputFormatter getOutputFormatter(ContentType type, return outputFormatter; } + public static OutputFormatter getOutputFormatter(ContentType ct, Class klass) { + return formats.getOutputFormatterInternal(ct, klass); + } + public static String format(ContentType type, CwmsDTOBase toFormat) throws FormattingException { return formats.getFormatted(type, toFormat); } diff --git a/cwms-data-api/src/test/java/cwms/cda/formatters/FormatsTest.java b/cwms-data-api/src/test/java/cwms/cda/formatters/FormatsTest.java index ea6feb1f4..61698b1b1 100644 --- a/cwms-data-api/src/test/java/cwms/cda/formatters/FormatsTest.java +++ b/cwms-data-api/src/test/java/cwms/cda/formatters/FormatsTest.java @@ -16,6 +16,11 @@ import cwms.cda.data.dto.basinconnectivity.Basin; import cwms.cda.data.dto.project.LockRevokerRights; import cwms.cda.data.dto.project.Project; +import cwms.cda.formatters.json.JsonV2; +import cwms.cda.formatters.xml.XMLv2; +import cwms.cda.formatters.xml.XMLv2Office; +import io.swagger.v3.oas.annotations.Parameter; + import java.util.Map; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; @@ -183,7 +188,7 @@ void testParseHeaderFromFirefox() { //The following header comes from firefox ContentType contentType = Formats.parseHeader(FIREFOX_HEADER, Catalog.class); assertNotNull(contentType); - assertEquals(Formats.DEFAULT, contentType.toString()); + assertEquals(Formats.XML, contentType.toString()); } @EnumSource(ParseHeaderClassAliasTest.class) @@ -270,7 +275,6 @@ enum ParseQueryOrParamTest { final String query; final Class dto; final String type; - ParseQueryOrParamTest(String header, String query, Class dto, String type) { @@ -279,7 +283,32 @@ enum ParseQueryOrParamTest { this.dto = dto; this.type = type; } - } + + + @ParameterizedTest + @EnumSource(ContentTypeFormatterSource.class) + void test_formatter_retrieval(ContentTypeFormatterSource test) { + ContentType ct = Formats.parseHeader(test.contentType, test.dto); + OutputFormatter formatterActual = Formats.getOutputFormatter(ct, test.dto); + assertNotNull(formatterActual, "No formatters available for given Content-Type and DTO."); + assertEquals(test.formatter, formatterActual.getClass(), "Expected Formatter was not returned."); + } + + public enum ContentTypeFormatterSource { + OFFICE_DEFAULT("*/*", Office.class, JsonV2.class), + OFFICE_FIREFOX_DEFAULT("text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", Office.class, XMLv2Office.class) + + ; + final String contentType; + final Class dto; + final Class formatter; + + ContentTypeFormatterSource(String contentType, Class dto, Class formatter) { + this.contentType = contentType; + this.dto = dto; + this.formatter = formatter; + } + } } \ No newline at end of file