/*
 * Licensed 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 com.facebook.presto.decoder.json;

import com.facebook.presto.decoder.DecoderColumnHandle;
import com.facebook.presto.decoder.DecoderTestColumnHandle;
import com.facebook.presto.decoder.FieldValueProvider;
import com.facebook.presto.decoder.RowDecoder;
import com.facebook.presto.spi.PrestoException;
import com.facebook.presto.spi.type.Type;
import com.google.common.collect.ImmutableSet;
import io.airlift.json.ObjectMapperProvider;

import java.util.Map;
import java.util.Optional;

import static java.lang.String.format;
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.Collections.emptyMap;
import static java.util.Objects.requireNonNull;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.util.Preconditions.checkArgument;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertTrue;

public class JsonFieldDecoderTester
{
    private static final JsonRowDecoderFactory DECODER_FACTORY = new JsonRowDecoderFactory(new ObjectMapperProvider().get());

    private Optional<String> dataFormat;
    private Optional<String> formatHint;

    public JsonFieldDecoderTester()
    {
        this(Optional.empty(), Optional.empty());
    }

    public JsonFieldDecoderTester(String dataFormat)
    {
        this(Optional.of(dataFormat), Optional.empty());
    }

    public JsonFieldDecoderTester(String dataFormat, String formatHint)
    {
        this(Optional.of(dataFormat), Optional.of(formatHint));
    }

    private JsonFieldDecoderTester(Optional<String> dataFormat, Optional<String> formatHint)
    {
        this.dataFormat = requireNonNull(dataFormat, "dataFormat is null");
        this.formatHint = requireNonNull(formatHint, "formatHint is null");
    }

    public void assertDecodedAs(String jsonValue, Type type, long expectedValue)
    {
        checkArgument(type.getJavaType() == long.class, "Wrong (not long based) presto type '%s'", type);
        FieldValueProvider decodedValue = decode(Optional.of(jsonValue), type);
        assertFalse(decodedValue.isNull(), format("expected non null when decoding %s as %s", jsonValue, type));
        assertEquals(decodedValue.getLong(), expectedValue);
    }

    public void assertDecodedAsNull(String jsonValue, Type type)
    {
        FieldValueProvider decodedValue = decode(Optional.of(jsonValue), type);
        assertTrue(decodedValue.isNull(), format("expected null when decoding %s as %s", jsonValue, type));
    }

    public void assertMissingDecodedAsNull(Type type)
    {
        FieldValueProvider decodedValue = decode(Optional.empty(), type);
        assertTrue(decodedValue.isNull(), format("expected null when decoding missing field as %s", type));
    }

    public void assertInvalidInput(String jsonValue, Type type, String exceptionRegex)
    {
        assertThatThrownBy(() -> decode(Optional.of(jsonValue), type).getLong())
                .isInstanceOf(PrestoException.class)
                .hasMessageMatching(exceptionRegex);
    }

    private FieldValueProvider decode(Optional<String> jsonValue, Type type)
    {
        String jsonField = "value";
        String json = jsonValue.map(value -> format("{\"%s\":%s}", jsonField, value)).orElse("{}");
        DecoderTestColumnHandle columnHandle = new DecoderTestColumnHandle(
                0,
                "some_column",
                type,
                jsonField,
                dataFormat.orElse(null),
                formatHint.orElse(null),
                false,
                false,
                false);

        RowDecoder rowDecoder = DECODER_FACTORY.create(emptyMap(), ImmutableSet.of(columnHandle));
        Map<DecoderColumnHandle, FieldValueProvider> decodedRow = rowDecoder.decodeRow(json.getBytes(UTF_8), null)
                .orElseThrow(AssertionError::new);
        assertTrue(decodedRow.containsKey(columnHandle), format("column '%s' not found in decoded row", columnHandle.getName()));
        return decodedRow.get(columnHandle);
    }
}

@@ -997,16 +997,47 @@
 rovider;
+%0Aimport io.airlift.slice.Slice;
 %0A%0Aimport
@@ -2858,32 +2858,1418 @@
 dValue);%0A    %7D%0A%0A
+    public void assertDecodedAs(String jsonValue, Type type, double expectedValue)%0A    %7B%0A        checkArgument(type.getJavaType() == double.class, %22Wrong (not double based) presto type '%25s'%22, type);%0A        FieldValueProvider decodedValue = decode(Optional.of(jsonValue), type);%0A        assertFalse(decodedValue.isNull(), format(%22expected non null when decoding %25s as %25s%22, jsonValue, type));%0A        assertEquals(decodedValue.getDouble(), expectedValue);%0A    %7D%0A%0A    public void assertDecodedAs(String jsonValue, Type type, Slice expectedValue)%0A    %7B%0A        checkArgument(type.getJavaType() == Slice.class, %22Wrong (not Slice based) presto type '%25s'%22, type);%0A        FieldValueProvider decodedValue = decode(Optional.of(jsonValue), type);%0A        assertFalse(decodedValue.isNull(), format(%22expected non null when decoding %25s as %25s%22, jsonValue, type));%0A        assertEquals(decodedValue.getSlice(), expectedValue);%0A    %7D%0A%0A    public void assertDecodedAs(String jsonValue, Type type, boolean expectedValue)%0A    %7B%0A        checkArgument(type.getJavaType() == boolean.class, %22Wrong (not boolean based) presto type '%25s'%22, type);%0A        FieldValueProvider decodedValue = decode(Optional.of(jsonValue), type);%0A        assertFalse(decodedValue.isNull(), format(%22expected non null when decoding %25s as %25s%22, jsonValue, type));%0A        assertEquals(decodedValue.getBoolean(), expectedValue);%0A    %7D%0A%0A
     public void 

Add support for all container types to JsonFieldDecoderTester