resultFormat name in camel case (#11585)

* resultFormat name in camel case

* test for letter case
This commit is contained in:
Jihoon Son 2021-08-14 03:30:21 -07:00 committed by GitHub
parent 776ddf76f4
commit 177264c649
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 91 additions and 6 deletions

View File

@ -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.

View File

@ -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);
}
}
}