|
| 1 | +/* |
| 2 | + * Copyright 2024 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.bigquerydatatransfer; |
| 18 | + |
| 19 | +import static com.google.common.truth.Truth.assertThat; |
| 20 | +import static com.google.common.truth.Truth.assertWithMessage; |
| 21 | + |
| 22 | +import com.google.cloud.bigquery.BigQuery; |
| 23 | +import com.google.cloud.bigquery.BigQueryOptions; |
| 24 | +import com.google.cloud.bigquery.DatasetInfo; |
| 25 | +import com.google.cloud.bigquery.Field; |
| 26 | +import com.google.cloud.bigquery.Schema; |
| 27 | +import com.google.cloud.bigquery.StandardSQLTypeName; |
| 28 | +import com.google.cloud.bigquery.StandardTableDefinition; |
| 29 | +import com.google.cloud.bigquery.TableDefinition; |
| 30 | +import com.google.cloud.bigquery.TableId; |
| 31 | +import com.google.cloud.bigquery.TableInfo; |
| 32 | +import com.google.protobuf.Value; |
| 33 | +import java.io.ByteArrayOutputStream; |
| 34 | +import java.io.IOException; |
| 35 | +import java.io.PrintStream; |
| 36 | +import java.util.HashMap; |
| 37 | +import java.util.Map; |
| 38 | +import java.util.UUID; |
| 39 | +import java.util.logging.Level; |
| 40 | +import java.util.logging.Logger; |
| 41 | +import org.junit.After; |
| 42 | +import org.junit.Before; |
| 43 | +import org.junit.BeforeClass; |
| 44 | +import org.junit.Test; |
| 45 | + |
| 46 | +public class CreateAzureBlobStorageTransferIT { |
| 47 | + |
| 48 | + private static final Logger LOG = |
| 49 | + Logger.getLogger(CreateAzureBlobStorageTransferIT.class.getName()); |
| 50 | + private static final String ID = UUID.randomUUID().toString().substring(0, 8); |
| 51 | + private BigQuery bigquery; |
| 52 | + private String name; |
| 53 | + private String displayName; |
| 54 | + private String datasetName; |
| 55 | + private String tableName; |
| 56 | + private ByteArrayOutputStream bout; |
| 57 | + private PrintStream out; |
| 58 | + private PrintStream originalPrintStream; |
| 59 | + |
| 60 | + private static final String PROJECT_ID = requireEnvVar("GOOGLE_CLOUD_PROJECT"); |
| 61 | + private static final String DTS_AZURE_STORAGE_ACCOUNT = |
| 62 | + requireEnvVar("DTS_AZURE_STORAGE_ACCOUNT"); |
| 63 | + private static final String DTS_AZURE_BLOB_CONTAINER = requireEnvVar("DTS_AZURE_BLOB_CONTAINER"); |
| 64 | + private static final String DTS_AZURE_SAS_TOKEN = requireEnvVar("DTS_AZURE_SAS_TOKEN"); |
| 65 | + |
| 66 | + private static String requireEnvVar(String varName) { |
| 67 | + String value = System.getenv(varName); |
| 68 | + assertWithMessage("Environment variable %s is required to perform these tests.", varName) |
| 69 | + .that(value) |
| 70 | + .isNotEmpty(); |
| 71 | + return value; |
| 72 | + } |
| 73 | + |
| 74 | + @BeforeClass |
| 75 | + public static void checkRequirements() { |
| 76 | + requireEnvVar("GOOGLE_CLOUD_PROJECT"); |
| 77 | + requireEnvVar("DTS_AZURE_STORAGE_ACCOUNT"); |
| 78 | + requireEnvVar("DTS_AZURE_BLOB_CONTAINER"); |
| 79 | + requireEnvVar("DTS_AZURE_SAS_TOKEN"); |
| 80 | + } |
| 81 | + |
| 82 | + @Before |
| 83 | + public void setUp() { |
| 84 | + displayName = "MY_TRANSFER_NAME_TEST_" + ID; |
| 85 | + datasetName = "MY_DATASET_NAME_TEST_" + ID; |
| 86 | + tableName = "MY_TABLE_NAME_TEST_" + ID; |
| 87 | + // create a temporary dataset |
| 88 | + bigquery = BigQueryOptions.getDefaultInstance().getService(); |
| 89 | + bigquery.create(DatasetInfo.of(datasetName)); |
| 90 | + // create a temporary table |
| 91 | + Schema schema = |
| 92 | + Schema.of( |
| 93 | + Field.of("name", StandardSQLTypeName.STRING), |
| 94 | + Field.of("post_abbr", StandardSQLTypeName.STRING)); |
| 95 | + TableDefinition tableDefinition = StandardTableDefinition.of(schema); |
| 96 | + TableInfo tableInfo = TableInfo.of(TableId.of(datasetName, tableName), tableDefinition); |
| 97 | + bigquery.create(tableInfo); |
| 98 | + |
| 99 | + bout = new ByteArrayOutputStream(); |
| 100 | + out = new PrintStream(bout); |
| 101 | + originalPrintStream = System.out; |
| 102 | + System.setOut(out); |
| 103 | + } |
| 104 | + |
| 105 | + @After |
| 106 | + public void tearDown() throws IOException { |
| 107 | + // Clean up |
| 108 | + DeleteScheduledQuery.deleteScheduledQuery(name); |
| 109 | + // delete a temporary table |
| 110 | + bigquery.delete(TableId.of(datasetName, tableName)); |
| 111 | + // delete a temporary dataset |
| 112 | + bigquery.delete(datasetName, BigQuery.DatasetDeleteOption.deleteContents()); |
| 113 | + // restores print statements in the original method |
| 114 | + System.out.flush(); |
| 115 | + System.setOut(originalPrintStream); |
| 116 | + LOG.log(Level.INFO, bout.toString()); |
| 117 | + } |
| 118 | + |
| 119 | + @Test |
| 120 | + public void testCreateAzureBlobStorageTransfer() throws IOException { |
| 121 | + Map<String, Value> params = new HashMap<>(); |
| 122 | + params.put( |
| 123 | + "destination_table_name_template", Value.newBuilder().setStringValue(tableName).build()); |
| 124 | + params.put( |
| 125 | + "storage_account", Value.newBuilder().setStringValue(DTS_AZURE_STORAGE_ACCOUNT).build()); |
| 126 | + params.put("container", Value.newBuilder().setStringValue(DTS_AZURE_BLOB_CONTAINER).build()); |
| 127 | + params.put("data_path", Value.newBuilder().setStringValue("*").build()); |
| 128 | + params.put("sas_token", Value.newBuilder().setStringValue(DTS_AZURE_SAS_TOKEN).build()); |
| 129 | + params.put("file_format", Value.newBuilder().setStringValue("CSV").build()); |
| 130 | + params.put("field_delimiter", Value.newBuilder().setStringValue(",").build()); |
| 131 | + params.put("skip_leading_rows", Value.newBuilder().setStringValue("1").build()); |
| 132 | + |
| 133 | + CreateAzureBlobStorageTransfer.createAzureBlobStorageTransfer( |
| 134 | + PROJECT_ID, displayName, datasetName, params); |
| 135 | + String result = bout.toString(); |
| 136 | + name = result.substring(result.indexOf(":") + 1, result.length() - 1); |
| 137 | + assertThat(result).contains("Azure Blob Storage transfer created successfully: "); |
| 138 | + } |
| 139 | +} |
0 commit comments