|
| 1 | +/* |
| 2 | + * Copyright 2020 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.example.bigquery; |
| 18 | + |
| 19 | +import static com.google.common.truth.Truth.assertThat; |
| 20 | +import static junit.framework.TestCase.assertNotNull; |
| 21 | + |
| 22 | +import com.google.cloud.bigquery.Schema; |
| 23 | +import java.io.ByteArrayOutputStream; |
| 24 | +import java.io.PrintStream; |
| 25 | +import java.util.UUID; |
| 26 | +import java.util.logging.Level; |
| 27 | +import java.util.logging.Logger; |
| 28 | +import org.junit.After; |
| 29 | +import org.junit.Before; |
| 30 | +import org.junit.BeforeClass; |
| 31 | +import org.junit.Test; |
| 32 | + |
| 33 | +public class UpdateIamPolicyIT { |
| 34 | + |
| 35 | + private final Logger log = Logger.getLogger(this.getClass().getName()); |
| 36 | + private String tableName; |
| 37 | + private ByteArrayOutputStream bout; |
| 38 | + private PrintStream out; |
| 39 | + private PrintStream originalPrintStream; |
| 40 | + |
| 41 | + private static final String BIGQUERY_DATASET_NAME = requireEnvVar("BIGQUERY_DATASET_NAME"); |
| 42 | + |
| 43 | + private static String requireEnvVar(String varName) { |
| 44 | + String value = System.getenv(varName); |
| 45 | + assertNotNull( |
| 46 | + "Environment variable " + varName + " is required to perform these tests.", |
| 47 | + System.getenv(varName)); |
| 48 | + return value; |
| 49 | + } |
| 50 | + |
| 51 | + @BeforeClass |
| 52 | + public static void checkRequirements() { |
| 53 | + requireEnvVar("BIGQUERY_DATASET_NAME"); |
| 54 | + } |
| 55 | + |
| 56 | + @Before |
| 57 | + public void setUp() { |
| 58 | + bout = new ByteArrayOutputStream(); |
| 59 | + out = new PrintStream(bout); |
| 60 | + originalPrintStream = System.out; |
| 61 | + System.setOut(out); |
| 62 | + |
| 63 | + // create a temporary table |
| 64 | + tableName = "UPDATE_POLICY_TABLE_TEST_" + UUID.randomUUID().toString().substring(0, 8); |
| 65 | + CreateTable.createTable(BIGQUERY_DATASET_NAME, tableName, Schema.of()); |
| 66 | + CreateIamPolicy.createIamPolicy(BIGQUERY_DATASET_NAME, tableName); |
| 67 | + } |
| 68 | + |
| 69 | + @After |
| 70 | + public void tearDown() { |
| 71 | + // Clean up |
| 72 | + DeleteTable.deleteTable(BIGQUERY_DATASET_NAME, tableName); |
| 73 | + // restores print statements in the original method |
| 74 | + System.out.flush(); |
| 75 | + System.setOut(originalPrintStream); |
| 76 | + log.log(Level.INFO, bout.toString()); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + public void testUpdateIamPolicy() { |
| 81 | + UpdateIamPolicy.updateIamPolicy(BIGQUERY_DATASET_NAME, tableName); |
| 82 | + assertThat(bout.toString()).contains("Iam policy updated successfully"); |
| 83 | + } |
| 84 | +} |
0 commit comments