From 177264c6490871e6a557f087270dfbd29723e60b Mon Sep 17 00:00:00 2001 From: Jihoon Son Date: Sat, 14 Aug 2021 03:30:21 -0700 Subject: [PATCH] resultFormat name in camel case (#11585) * resultFormat name in camel case * test for letter case --- .../apache/druid/sql/http/ResultFormat.java | 27 +++++-- .../druid/sql/http/ResultFormatTest.java | 70 +++++++++++++++++++ 2 files changed, 91 insertions(+), 6 deletions(-) create mode 100644 sql/src/test/java/org/apache/druid/sql/http/ResultFormatTest.java diff --git a/sql/src/main/java/org/apache/druid/sql/http/ResultFormat.java b/sql/src/main/java/org/apache/druid/sql/http/ResultFormat.java index bb9979cf186..3e77076a614 100644 --- a/sql/src/main/java/org/apache/druid/sql/http/ResultFormat.java +++ b/sql/src/main/java/org/apache/druid/sql/http/ResultFormat.java @@ -20,6 +20,7 @@ package org.apache.druid.sql.http; import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonValue; import com.fasterxml.jackson.databind.ObjectMapper; import org.apache.druid.java.util.common.StringUtils; @@ -32,7 +33,7 @@ import java.util.List; public enum ResultFormat { - ARRAY { + ARRAY("array") { @Override public String contentType() { @@ -46,7 +47,7 @@ public enum ResultFormat } }, - ARRAYLINES { + ARRAYLINES("arrayLines") { @Override public String contentType() { @@ -60,7 +61,7 @@ public enum ResultFormat } }, - CSV { + CSV("csv") { @Override public String contentType() { @@ -74,7 +75,7 @@ public enum ResultFormat } }, - OBJECT { + OBJECT("object") { @Override public String contentType() { @@ -88,7 +89,7 @@ public enum ResultFormat } }, - OBJECTLINES { + OBJECTLINES("objectLines") { @Override public String contentType() { @@ -102,11 +103,25 @@ public enum ResultFormat } }; + private final String name; + + ResultFormat(final String name) + { + this.name = name; + } + public abstract String contentType(); public abstract Writer createFormatter(OutputStream outputStream, ObjectMapper jsonMapper) throws IOException; - interface Writer extends Closeable + @Override + @JsonValue + public String toString() + { + return name; + } + + public interface Writer extends Closeable { /** * Start of the response, called once per writer. diff --git a/sql/src/test/java/org/apache/druid/sql/http/ResultFormatTest.java b/sql/src/test/java/org/apache/druid/sql/http/ResultFormatTest.java new file mode 100644 index 00000000000..9f9f8400ffd --- /dev/null +++ b/sql/src/test/java/org/apache/druid/sql/http/ResultFormatTest.java @@ -0,0 +1,70 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.druid.sql.http; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import junitparams.JUnitParamsRunner; +import junitparams.Parameters; +import org.apache.druid.annotations.UsedByJUnitParamsRunner; +import org.apache.druid.jackson.DefaultObjectMapper; +import org.apache.druid.java.util.common.StringUtils; +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; + +import java.util.EnumSet; + +@RunWith(JUnitParamsRunner.class) +public class ResultFormatTest +{ + + private final ObjectMapper jsonMapper = new DefaultObjectMapper(); + + @Test + @Parameters(source = ResultFormatTypeProvider.class) + public void testSerde(ResultFormat target) throws JsonProcessingException + { + final String json = jsonMapper.writeValueAsString(target); + Assert.assertEquals(StringUtils.format("\"%s\"", target.toString()), json); + Assert.assertEquals(target, jsonMapper.readValue(json, ResultFormat.class)); + } + + @Test + public void testDeserializeWithDifferentCase() throws JsonProcessingException + { + Assert.assertEquals(ResultFormat.OBJECTLINES, jsonMapper.readValue("\"OBJECTLINES\"", ResultFormat.class)); + Assert.assertEquals(ResultFormat.OBJECTLINES, jsonMapper.readValue("\"objectLines\"", ResultFormat.class)); + Assert.assertEquals(ResultFormat.OBJECTLINES, jsonMapper.readValue("\"objectlines\"", ResultFormat.class)); + Assert.assertEquals(ResultFormat.OBJECTLINES, jsonMapper.readValue("\"oBjEcTlInEs\"", ResultFormat.class)); + } + + public static class ResultFormatTypeProvider + { + @UsedByJUnitParamsRunner + public static Object[] provideResultFormats() + { + return EnumSet.allOf(ResultFormat.class) + .stream() + .map(format -> new Object[]{format}) + .toArray(Object[]::new); + } + } +}