/*
 * Copyright 2022 Google LLC
 *
 * 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
 *
 *       https://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.google.cloud.spark.bigquery.v2.context;

import static org.junit.Assert.assertThrows;
import static org.mockito.Mockito.*;

import com.google.api.gax.retrying.RetrySettings;
import com.google.cloud.bigquery.Field;
import com.google.cloud.bigquery.LegacySQLTypeName;
import com.google.cloud.bigquery.Schema;
import com.google.cloud.bigquery.StandardTableDefinition;
import com.google.cloud.bigquery.TableId;
import com.google.cloud.bigquery.TableInfo;
import com.google.cloud.bigquery.connector.common.BigQueryClient;
import com.google.cloud.bigquery.connector.common.BigQueryClientFactory;
import org.apache.spark.sql.SaveMode;
import org.apache.spark.sql.types.DataTypes;
import org.apache.spark.sql.types.Metadata;
import org.apache.spark.sql.types.StructField;
import org.apache.spark.sql.types.StructType;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;

@RunWith(MockitoJUnitRunner.class)
public class BigQueryDirectDataSourceWriterContextTest {

  @Mock BigQueryClient bigQueryClient;

  @Mock BigQueryClientFactory bigQueryClientFactory;

  @Mock RetrySettings bigqueryDataWriterHelperRetrySettings;

  TableId destinationTableId = TableId.of("dataset", "table");
  String writeUUID = "00000000-0000-0000-0000-000000000000";
  StructType sparkSchema =
      new StructType(
          new StructField[] {
            StructField.apply("foo", DataTypes.StringType, true, Metadata.empty())
          });

  TableInfo destinationTable =
      TableInfo.newBuilder(
              destinationTableId,
              StandardTableDefinition.newBuilder()
                  .setSchema(
                      Schema.of(
                          Field.newBuilder("foo", LegacySQLTypeName.STRING)
                              .setMode(Field.Mode.NULLABLE)
                              .build()))
                  .build())
          .build();
  TableInfo tempTable =
      TableInfo.newBuilder(
              TableId.of("dataset", "temp_table"),
              StandardTableDefinition.newBuilder()
                  .setSchema(
                      Schema.of(
                          Field.newBuilder("foo", LegacySQLTypeName.STRING)
                              .setMode(Field.Mode.NULLABLE)
                              .build()))
                  .build())
          .build();

  @Test
  public void testDeleteOnAbort_saveModeAppend() {
    when(bigQueryClient.tableExists(any())).thenReturn(true);
    when(bigQueryClient.getTable(any())).thenReturn(destinationTable);
    when(bigQueryClient.createTablePathForBigQueryStorage(any())).thenReturn("");
    BigQueryDirectDataSourceWriterContext ctx =
        createBigQueryDirectDataSourceWriterContext(SaveMode.Append);
    ctx.abort(null);
    verify(bigQueryClient, times(0)).deleteTable(any());
  }

  @Test
  public void testDeleteOnAbort_saveModeErrorIfExists() {
    when(bigQueryClient.tableExists(any())).thenReturn(true);
    when(bigQueryClient.getTable(any())).thenReturn(destinationTable);
    assertThrows(
        IllegalArgumentException.class,
        () -> {
          BigQueryDirectDataSourceWriterContext ctx =
              createBigQueryDirectDataSourceWriterContext(SaveMode.ErrorIfExists);
        });
  }

  @Test
  public void testDeleteOnAbort_saveModeIgnore() {
    when(bigQueryClient.tableExists(any())).thenReturn(true);
    when(bigQueryClient.getTable(any())).thenReturn(destinationTable);
    when(bigQueryClient.createTablePathForBigQueryStorage(any())).thenReturn("");
    BigQueryDirectDataSourceWriterContext ctx =
        createBigQueryDirectDataSourceWriterContext(SaveMode.Ignore);
    ctx.abort(null);
    verify(bigQueryClient, times(0)).deleteTable(any());
  }

  @Test
  public void testDeleteOnAbort_saveModeOverwrite() {
    when(bigQueryClient.tableExists(any())).thenReturn(true);
    when(bigQueryClient.getTable(any())).thenReturn(destinationTable);
    when(bigQueryClient.createTempTable(any(), any())).thenReturn(tempTable);
    when(bigQueryClient.createTablePathForBigQueryStorage(any())).thenReturn("");
    BigQueryDirectDataSourceWriterContext ctx =
        createBigQueryDirectDataSourceWriterContext(SaveMode.Overwrite);
    ctx.abort(null);
    verify(bigQueryClient, times(1)).deleteTable(any());
  }

  @Test
  public void testDeleteOnAbort_newTable() {
    when(bigQueryClient.tableExists(destinationTableId)).thenReturn(false);
    when(bigQueryClient.createTable(any(), any())).thenReturn(destinationTable);
    when(bigQueryClient.createTablePathForBigQueryStorage(any())).thenReturn("");
    BigQueryDirectDataSourceWriterContext ctx =
        createBigQueryDirectDataSourceWriterContext(SaveMode.Append);
    ctx.abort(null);
    verify(bigQueryClient, times(1)).deleteTable(any());
  }

  private BigQueryDirectDataSourceWriterContext createBigQueryDirectDataSourceWriterContext(
      SaveMode saveMode) {
    return new BigQueryDirectDataSourceWriterContext(
        bigQueryClient,
        bigQueryClientFactory,
        destinationTableId,
        writeUUID,
        saveMode,
        sparkSchema,
        bigqueryDataWriterHelperRetrySettings);
  }
}

@@ -1192,16 +1192,56 @@
 actory;%0A
+import com.google.common.base.Optional;%0A
 import o
@@ -5793,16 +5793,43 @@
 Settings
+,%0A        Optional.absent()
 );%0A  %7D%0A%7D

Fix failing unit test due to constructor parameter mismatch (#565)