/*
 * 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.wayang.spark.operators;

import org.apache.spark.sql.Dataset;
import org.apache.spark.sql.Row;
import org.apache.spark.sql.RowFactory;
import org.apache.spark.sql.types.DataTypes;
import org.apache.spark.sql.types.StructField;
import org.apache.spark.sql.types.StructType;
import org.apache.wayang.spark.channels.DatasetChannel;
import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

class DatasetChannelTest extends SparkOperatorTestBase {

    @Test
    void acceptAndProvideDataset() {
        List<Row> rows = sampleRows();
        Dataset<Row> dataset = createDataset(rows);
        DatasetChannel.Instance instance = createDatasetChannelInstance(false);

        instance.accept(dataset, this.sparkExecutor);

        assertEquals(rows, instance.provideDataset().collectAsList());
    }

    @Test
    void instrumentationCountsRows() {
        List<Row> rows = sampleRows();
        Dataset<Row> dataset = createDataset(rows);
        DatasetChannel.Instance instance = createDatasetChannelInstance(true);

        instance.accept(dataset, this.sparkExecutor);

        assertTrue(instance.getMeasuredCardinality().isPresent());
        assertEquals(rows.size(), instance.getMeasuredCardinality().getAsLong());
    }

    @Test
    void noInstrumentationLeavesCardinalityEmpty() {
        DatasetChannel.Instance instance = createDatasetChannelInstance(false);

        instance.accept(createDataset(sampleRows()), this.sparkExecutor);

        assertFalse(instance.getMeasuredCardinality().isPresent());
    }

    private DatasetChannel.Instance createDatasetChannelInstance(boolean instrumented) {
        DatasetChannel channel = (DatasetChannel) DatasetChannel.UNCACHED_DESCRIPTOR
                .createChannel(null, this.configuration);
        if (instrumented) {
            channel.markForInstrumentation();
        }
        return (DatasetChannel.Instance) channel.createInstance(this.sparkExecutor, null, -1);
    }

    private Dataset<Row> createDataset(List<Row> rows) {
        return this.sparkExecutor.ss.createDataFrame(rows, sampleSchema());
    }

    private StructType sampleSchema() {
        StructField[] fields = new StructField[]{
                DataTypes.createStructField("name", DataTypes.StringType, false),
                DataTypes.createStructField("age", DataTypes.IntegerType, false)
        };
        return new StructType(fields);
    }

    private List<Row> sampleRows() {
        return Arrays.asList(
                RowFactory.create("alice", 30),
                RowFactory.create("bob", 25)
        );
    }
}
