/*
 * 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 com.google.cloud.bigquery.Schema;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.UUID;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

public class UndeleteTableIT {

  private final Logger log = Logger.getLogger(this.getClass().getName());
  private String tableName;
  private String recoverTableName;
  private ByteArrayOutputStream bout;
  private PrintStream out;
  private PrintStream originalPrintStream;

  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() {
    bout = new ByteArrayOutputStream();
    out = new PrintStream(bout);
    originalPrintStream = System.out;
    System.setOut(out);

    tableName = "UNDELETE_TABLE_TEST_" + UUID.randomUUID().toString().substring(0, 8);
    recoverTableName = "RECOVER_DELETE_TABLE_TEST_" + UUID.randomUUID().toString().substring(0, 8);
    // Create table in dataset for testing
    CreateTable.createTable(BIGQUERY_DATASET_NAME, tableName, Schema.of());
  }

  @After
  public void tearDown() {
    // Clean up
    DeleteTable.deleteTable(BIGQUERY_DATASET_NAME, recoverTableName);
    // restores print statements in the original method
    System.out.flush();
    System.setOut(originalPrintStream);
    log.log(Level.INFO, "\n" + bout.toString());
  }

  @Test
  public void testUndeleteTable() {
    UndeleteTable.undeleteTable(BIGQUERY_DATASET_NAME, tableName, recoverTableName);
    assertThat(bout.toString()).contains("Undelete table recovered successfully.");
  }
}

@@ -759,22 +759,116 @@
 igquery.
-Schema
+Field;%0Aimport com.google.cloud.bigquery.Schema;%0Aimport com.google.cloud.bigquery.StandardSQLTypeName
 ;%0Aimport
@@ -2193,16 +2193,185 @@
 testing%0A
+    Schema schema =%0A        Schema.of(%0A            Field.of(%22stringField%22, StandardSQLTypeName.STRING),%0A            Field.of(%22booleanField%22, StandardSQLTypeName.BOOL));%0A
     Crea
@@ -2428,19 +2428,14 @@
 me, 
-S
+s
 chema
-.of()
 );%0A 

fix undeleteTable sample IT failure (#1912)