/*
 * Copyright 2020 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
 *
 * 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.example.bigquery;

import static com.google.common.truth.Truth.assertThat;
import static junit.framework.TestCase.assertNotNull;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.UUID;

import com.google.cloud.bigquery.Field;
import com.google.cloud.bigquery.LegacySQLTypeName;
import com.google.cloud.bigquery.Schema;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

public class AddEmptyColumnIT {
  private ByteArrayOutputStream bout;
  private PrintStream out;

  private static final String BIGQUERY_DATASET_NAME = System.getenv("BIGQUERY_DATASET_NAME");

  private static void requireEnvVar(String varName) {
    assertNotNull (
        "Environment variable " + varName + " is required to perform these tests.",
        System.getenv(varName));
  }

  @BeforeClass
  public static void checkRequirements() {
    requireEnvVar("BIGQUERY_DATASET_NAME");
  }

  @Before
  public void setUp() throws Exception {
    bout = new ByteArrayOutputStream();
    out = new PrintStream(bout);
    System.setOut(out);
  }

  @After
  public void tearDown() {
    System.setOut(null);
  }

  @Test
  public void addEmptyColumn() {
    String tableName = "AddEmptyColumnTestTable_" + UUID.randomUUID().toString().replace('-', '_');
    Schema schema =
            Schema.of(
                    Field.of("booleanField", LegacySQLTypeName.BOOLEAN),
                    Field.of("numericField", LegacySQLTypeName.NUMERIC));

    // Create table in dataset for testing
    CreateTable.createTable(BIGQUERY_DATASET_NAME, tableName, schema);

    String randomColumnName = "new_" + UUID.randomUUID().toString().replace('-', '_');
    AddEmptyColumn.addEmptyColumn(randomColumnName, BIGQUERY_DATASET_NAME, tableName);
    assertThat(bout.toString()).contains("Empty column successfully added to table");
  }
}

@@ -730,98 +730,8 @@
 l;%0A%0A
-import java.io.ByteArrayOutputStream;%0Aimport java.io.PrintStream;%0Aimport java.util.UUID;%0A%0A
 impo
@@ -859,16 +859,105 @@
 Schema;%0A
+import java.io.ByteArrayOutputStream;%0Aimport java.io.PrintStream;%0Aimport java.util.UUID;%0A
 import o
@@ -1315,17 +1315,16 @@
 tNotNull
- 
 (%0A      
@@ -1934,20 +1934,16 @@
         
-    
 Schema.o
@@ -1945,24 +1945,16 @@
 ema.of(%0A
-        
         
@@ -2010,24 +2010,16 @@
 OLEAN),%0A
-        
         

fix imports order and run code formatter (#496)