/*
 * 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.beam.runners.core.construction;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.beam.model.pipeline.v1.SchemaApi;
import org.apache.beam.model.pipeline.v1.SchemaApi.ArrayType;
import org.apache.beam.model.pipeline.v1.SchemaApi.ArrayTypeValue;
import org.apache.beam.model.pipeline.v1.SchemaApi.AtomicType;
import org.apache.beam.model.pipeline.v1.SchemaApi.AtomicTypeValue;
import org.apache.beam.model.pipeline.v1.SchemaApi.FieldValue;
import org.apache.beam.model.pipeline.v1.SchemaApi.LogicalType;
import org.apache.beam.sdk.schemas.Schema;
import org.apache.beam.sdk.schemas.Schema.Field;
import org.apache.beam.sdk.schemas.Schema.FieldType;
import org.apache.beam.sdk.schemas.SchemaTranslation;
import org.apache.beam.sdk.schemas.logicaltypes.FixedBytes;
import org.apache.beam.sdk.schemas.logicaltypes.MicrosInstant;
import org.apache.beam.sdk.values.Row;
import org.apache.beam.vendor.grpc.v1p36p0.com.google.protobuf.ByteString;
import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.base.Charsets;
import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableList;
import org.junit.Test;
import org.junit.experimental.runners.Enclosed;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameter;
import org.junit.runners.Parameterized.Parameters;

/** Tests for {@link SchemaTranslation}. */
@RunWith(Enclosed.class)
public class SchemaTranslationTest {

  /** Tests round-trip proto encodings for {@link Schema}. */
  @RunWith(Parameterized.class)
  public static class ToFromProtoTest {
    @Parameters(name = "{index}: {0}")
    public static Iterable<Schema> data() {
      Map<String, Integer> optionMap = new HashMap<>();
      optionMap.put("string", 42);
      List<String> optionList = new ArrayList<>();
      optionList.add("string");
      Row optionRow =
          Row.withSchema(
                  Schema.builder()
                      .addField("field_one", FieldType.STRING)
                      .addField("field_two", FieldType.INT32)
                      .build())
              .addValue("value")
              .addValue(42)
              .build();

      Schema.Options.Builder optionsBuilder =
          Schema.Options.builder()
              .setOption("field_option_boolean", FieldType.BOOLEAN, true)
              .setOption("field_option_byte", FieldType.BYTE, (byte) 12)
              .setOption("field_option_int16", FieldType.INT16, (short) 12)
              .setOption("field_option_int32", FieldType.INT32, 12)
              .setOption("field_option_int64", FieldType.INT64, 12L)
              .setOption("field_option_string", FieldType.STRING, "foo")
              .setOption("field_option_bytes", FieldType.BYTES, new byte[] {0x42, 0x69, 0x00})
              .setOption("field_option_float", FieldType.FLOAT, (float) 12.0)
              .setOption("field_option_double", FieldType.DOUBLE, 12.0)
              .setOption(
                  "field_option_map", FieldType.map(FieldType.STRING, FieldType.INT32), optionMap)
              .setOption("field_option_array", FieldType.array(FieldType.STRING), optionList)
              .setOption("field_option_row", optionRow)
              .setOption("field_option_value", FieldType.STRING, "other");

      return ImmutableList.<Schema>builder()
          .add(Schema.of(Field.of("string", FieldType.STRING)))
          .add(
              Schema.of(
                  Field.of("boolean", FieldType.BOOLEAN),
                  Field.of("byte", FieldType.BYTE),
                  Field.of("int16", FieldType.INT16),
                  Field.of("int32", FieldType.INT32),
                  Field.of("int64", FieldType.INT64)))
          .add(
              Schema.of(
                  Field.of(
                      "row",
                      FieldType.row(
                          Schema.of(
                              Field.of("foo", FieldType.STRING),
                              Field.of("bar", FieldType.DOUBLE),
                              Field.of("baz", FieldType.BOOLEAN))))))
          .add(
              Schema.of(
                  Field.of(
                      "array(array(int64)))",
                      FieldType.array(FieldType.array(FieldType.INT64.withNullable(true))))))
          .add(
              Schema.of(
                  Field.of(
                      "iter(iter(int64)))",
                      FieldType.iterable(FieldType.iterable(FieldType.INT64.withNullable(true))))))
          .add(
              Schema.of(
                  Field.of("nullable", FieldType.STRING.withNullable(true)),
                  Field.of("non_nullable", FieldType.STRING.withNullable(false))))
          .add(
              Schema.of(
                  Field.of("decimal", FieldType.DECIMAL), Field.of("datetime", FieldType.DATETIME)))
          .add(Schema.of(Field.of("fixed_bytes", FieldType.logicalType(FixedBytes.of(24)))))
          .add(Schema.of(Field.of("micros_instant", FieldType.logicalType(new MicrosInstant()))))
          .add(
              Schema.of(
                      Field.of("field_with_option_atomic", FieldType.STRING)
                          .withOptions(
                              Schema.Options.builder()
                                  .setOption(
                                      "field_option_atomic", FieldType.INT32, Integer.valueOf(42))
                                  .build()))
                  .withOptions(
                      Schema.Options.builder()
                          .setOption("schema_option_atomic", FieldType.BOOLEAN, true)))
          .add(
              Schema.of(
                      Field.of("field_with_option_map", FieldType.STRING)
                          .withOptions(
                              Schema.Options.builder()
                                  .setOption(
                                      "field_option_map",
                                      FieldType.map(FieldType.STRING, FieldType.INT32),
                                      optionMap)))
                  .withOptions(
                      Schema.Options.builder()
                          .setOption(
                              "field_option_map",
                              FieldType.map(FieldType.STRING, FieldType.INT32),
                              optionMap)))
          .add(
              Schema.of(
                      Field.of("field_with_option_array", FieldType.STRING)
                          .withOptions(
                              Schema.Options.builder()
                                  .setOption(
                                      "field_option_array",
                                      FieldType.array(FieldType.STRING),
                                      optionList)
                                  .build()))
                  .withOptions(
                      Schema.Options.builder()
                          .setOption(
                              "field_option_array", FieldType.array(FieldType.STRING), optionList)))
          .add(
              Schema.of(Field.of("field", FieldType.STRING).withOptions(optionsBuilder))
                  .withOptions(optionsBuilder))
          .build();
    }

    @Parameter(0)
    public Schema schema;

    @Test
    public void toAndFromProto() throws Exception {
      SchemaApi.Schema schemaProto = SchemaTranslation.schemaToProto(schema, true);

      Schema decodedSchema = SchemaTranslation.schemaFromProto(schemaProto);
      assertThat(decodedSchema, equalTo(schema));
    }
  }

  /** Tests round-trip proto encodings for {@link Schema}. */
  @RunWith(Parameterized.class)
  public static class FromProtoToProtoTest {
    @Parameters(name = "{index}: {0}")
    public static Iterable<SchemaApi.Schema> data() {
      SchemaApi.Schema.Builder builder = SchemaApi.Schema.newBuilder();
      // A go 'int'
      builder.addFields(
          SchemaApi.Field.newBuilder()
              .setName("goInt")
              .setDescription("An int from go")
              .setType(
                  SchemaApi.FieldType.newBuilder()
                      .setLogicalType(
                          SchemaApi.LogicalType.newBuilder()
                              .setUrn("gosdk:int")
                              .setRepresentation(
                                  SchemaApi.FieldType.newBuilder()
                                      .setAtomicType(SchemaApi.AtomicType.INT64))
                              .build()))
              .setId(0)
              .setEncodingPosition(0)
              .build());
      // A pickled python object
      builder.addFields(
          SchemaApi.Field.newBuilder()
              .setName("pythonObject")
              .setType(
                  SchemaApi.FieldType.newBuilder()
                      .setLogicalType(
                          SchemaApi.LogicalType.newBuilder()
                              .setUrn("pythonsdk:value")
                              .setPayload(
                                  ByteString.copyFrom(
                                      "some payload describing a python type", Charsets.UTF_8))
                              .setRepresentation(
                                  SchemaApi.FieldType.newBuilder()
                                      .setAtomicType(SchemaApi.AtomicType.BYTES))
                              .build()))
              .setId(1)
              .setEncodingPosition(1)
              .build());
      // An enum logical type that Java doesn't know about
      builder.addFields(
          SchemaApi.Field.newBuilder()
              .setName("enum")
              .setType(
                  SchemaApi.FieldType.newBuilder()
                      .setLogicalType(
                          LogicalType.newBuilder()
                              .setUrn("strange_enum")
                              .setArgumentType(
                                  SchemaApi.FieldType.newBuilder()
                                      .setArrayType(
                                          ArrayType.newBuilder()
                                              .setElementType(
                                                  SchemaApi.FieldType.newBuilder()
                                                      .setAtomicType(AtomicType.STRING))))
                              .setArgument(
                                  FieldValue.newBuilder()
                                      .setArrayValue(
                                          ArrayTypeValue.newBuilder()
                                              .addElement(
                                                  FieldValue.newBuilder()
                                                      .setAtomicValue(
                                                          AtomicTypeValue.newBuilder()
                                                              .setString("FOO")
                                                              .build())
                                                      .build())
                                              .addElement(
                                                  FieldValue.newBuilder()
                                                      .setAtomicValue(
                                                          AtomicTypeValue.newBuilder()
                                                              .setString("BAR")
                                                              .build())
                                                      .build())
                                              .build())
                                      .build())
                              .setRepresentation(
                                  SchemaApi.FieldType.newBuilder().setAtomicType(AtomicType.BYTE))
                              .build()))
              .setId(2)
              .setEncodingPosition(2)
              .build());
      SchemaApi.Schema unknownLogicalTypeSchema = builder.build();

      return ImmutableList.<SchemaApi.Schema>builder().add(unknownLogicalTypeSchema).build();
    }

    @Parameter(0)
    public SchemaApi.Schema schemaProto;

    @Test
    public void fromProtoAndToProto() throws Exception {
      Schema decodedSchema = SchemaTranslation.schemaFromProto(schemaProto);

      SchemaApi.Schema reencodedSchemaProto = SchemaTranslation.schemaToProto(decodedSchema, true);

      assertThat(reencodedSchemaProto, equalTo(schemaProto));
    }
  }
}
