/*
 * 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.concurrent.TimeUnit;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

public class UpdateTableExpirationIT {
  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 updateTableExpiration() {
    String tableName = "update_expiration_table";
    CreateTable.createTable(BIGQUERY_DATASET_NAME, tableName, null);
    Long newExpiration = TimeUnit.MILLISECONDS.convert(1, TimeUnit.DAYS);
    UpdateTableExpiration.updateTableExpiration(BIGQUERY_DATASET_NAME, tableName, newExpiration);
    assertThat(bout.toString()).contains("Table expiration updated successfully");

    // Clean up
    DeleteTable.deleteTable(BIGQUERY_DATASET_NAME, tableName);
  }
}

@@ -726,16 +726,151 @@
 tNull;%0A%0A
+import com.google.cloud.bigquery.Field;%0Aimport com.google.cloud.bigquery.Schema;%0Aimport com.google.cloud.bigquery.StandardSQLTypeName;%0A
 import j
@@ -919,32 +919,55 @@
 io.PrintStream;%0A
+import java.util.UUID;%0A
 import java.util
@@ -1875,45 +1875,312 @@
 ing 
-tableName = %22update_expiration_table%22
+suffix = UUID.randomUUID().toString().replace('-', '_');%0A    String tableName = %22update_expiration_table_%22 + suffix;%0A    Schema schema =%0A            Schema.of(%0A                    Field.of(%22stringField%22, StandardSQLTypeName.STRING),%0A                    Field.of(%22booleanField%22, StandardSQLTypeName.BOOL))
 ;%0A  
@@ -2239,20 +2239,22 @@
 leName, 
-null
+schema
 );%0A    L

Fix flaky test (#397)