/*
 * 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 com.dtstack.flink.sql.sink.cassandra;

import com.dtstack.flink.sql.sink.cassandra.table.CassandraTableInfo;
import com.dtstack.flink.sql.table.AbstractTargetTableInfo;
import org.apache.flink.api.common.typeinfo.TypeInformation;
import org.apache.flink.api.java.typeutils.RowTypeInfo;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.DataStreamSink;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.reflect.Whitebox;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

/**
 * @author: chuixue
 * @create: 2020-07-28 09:46
 * @description:
 **/
@RunWith(PowerMockRunner.class)
@PrepareForTest({CassandraSink.class})
public class CassandraSinkTest {

    private CassandraSink cassandraSink = new CassandraSink();

//    @Test
    public void testGenStreamSink() {
        CassandraTableInfo cassandraTableInfo = new CassandraTableInfo();
        Assert.assertEquals(cassandraSink, cassandraSink.genStreamSink(cassandraTableInfo));
    }

//    @Test
    public void testEmitDataStream() {
        DataStream dataStream = mock(DataStream.class);
        String[] fieldNames = new String[]{};
        TypeInformation[] fieldTypes = new TypeInformation[]{TypeInformation.of(Integer.class), TypeInformation.of(String.class), TypeInformation.of(Integer.class)};
        Whitebox.setInternalState(cassandraSink, "address", "address");
        Whitebox.setInternalState(cassandraSink, "database", "database");
        Whitebox.setInternalState(cassandraSink, "tableName", "tableName");
        Whitebox.setInternalState(cassandraSink, "password", "password");
        Whitebox.setInternalState(cassandraSink, "userName", "userName");
        Whitebox.setInternalState(cassandraSink, "maxRequestsPerConnection", 1);
        Whitebox.setInternalState(cassandraSink, "coreConnectionsPerHost", 1);
        Whitebox.setInternalState(cassandraSink, "maxConnectionsPerHost", 1);
        Whitebox.setInternalState(cassandraSink, "maxQueueSize", 1);
        Whitebox.setInternalState(cassandraSink, "readTimeoutMillis", 1);
        Whitebox.setInternalState(cassandraSink, "connectTimeoutMillis", 1);
        Whitebox.setInternalState(cassandraSink, "poolTimeoutMillis", 1);
        Whitebox.setInternalState(cassandraSink, "fieldNames", fieldNames);
        Whitebox.setInternalState(cassandraSink, "fieldTypes", fieldTypes);

        DataStreamSink dataStreamSink = PowerMockito.mock(DataStreamSink.class);
        when(dataStream.addSink(any())).thenReturn(dataStreamSink);
        when(dataStreamSink.setParallelism(anyInt())).thenReturn(dataStreamSink);

        cassandraSink.emitDataStream(dataStream);
    }
}
