|
| 1 | +/* |
| 2 | + * Copyright 2021 Google Inc. |
| 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.contactcenterinsights; |
| 18 | + |
| 19 | +import static com.google.common.truth.Truth.assertThat; |
| 20 | +import static junit.framework.TestCase.assertNotNull; |
| 21 | + |
| 22 | +import com.google.cloud.bigquery.BigQuery; |
| 23 | +import com.google.cloud.bigquery.BigQueryException; |
| 24 | +import com.google.cloud.bigquery.BigQueryOptions; |
| 25 | +import com.google.cloud.bigquery.Dataset; |
| 26 | +import com.google.cloud.bigquery.DatasetId; |
| 27 | +import com.google.cloud.bigquery.DatasetInfo; |
| 28 | +import com.google.cloud.bigquery.Schema; |
| 29 | +import com.google.cloud.bigquery.StandardTableDefinition; |
| 30 | +import com.google.cloud.bigquery.Table; |
| 31 | +import com.google.cloud.bigquery.TableDefinition; |
| 32 | +import com.google.cloud.bigquery.TableId; |
| 33 | +import com.google.cloud.bigquery.TableInfo; |
| 34 | +import java.io.ByteArrayOutputStream; |
| 35 | +import java.io.IOException; |
| 36 | +import java.io.PrintStream; |
| 37 | +import java.util.ArrayList; |
| 38 | +import java.util.UUID; |
| 39 | +import org.junit.After; |
| 40 | +import org.junit.Before; |
| 41 | +import org.junit.BeforeClass; |
| 42 | +import org.junit.Test; |
| 43 | +import org.junit.runner.RunWith; |
| 44 | +import org.junit.runners.JUnit4; |
| 45 | + |
| 46 | +@RunWith(JUnit4.class) |
| 47 | +public class ExportToBigqueryIT { |
| 48 | + |
| 49 | + private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); |
| 50 | + private static final String BIGQUERY_PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); |
| 51 | + private static final String GCLOUD_TESTS_PREFIX = "java_samples_tests"; |
| 52 | + private ByteArrayOutputStream bout; |
| 53 | + private PrintStream out; |
| 54 | + private String bigqueryDatasetId; |
| 55 | + private String bigqueryTableId; |
| 56 | + |
| 57 | + private static void requireEnvVar(String varName) { |
| 58 | + assertNotNull(String.format(varName), String.format(varName)); |
| 59 | + } |
| 60 | + |
| 61 | + @BeforeClass |
| 62 | + public static void checkRequirements() { |
| 63 | + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); |
| 64 | + requireEnvVar("GOOGLE_CLOUD_PROJECT"); |
| 65 | + } |
| 66 | + |
| 67 | + @Before |
| 68 | + public void setUp() throws BigQueryException { |
| 69 | + bout = new ByteArrayOutputStream(); |
| 70 | + out = new PrintStream(bout); |
| 71 | + System.setOut(out); |
| 72 | + |
| 73 | + // Generate BigQuery table and dataset IDs. |
| 74 | + bigqueryDatasetId = |
| 75 | + String.format("%s_%s", GCLOUD_TESTS_PREFIX, UUID.randomUUID().toString().replace("-", "_")); |
| 76 | + bigqueryTableId = |
| 77 | + String.format("%s_%s", GCLOUD_TESTS_PREFIX, UUID.randomUUID().toString().replace("-", "_")); |
| 78 | + |
| 79 | + // Create a BigQuery dataset. |
| 80 | + BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService(); |
| 81 | + DatasetInfo datasetInfo = |
| 82 | + DatasetInfo.newBuilder(DatasetId.of(BIGQUERY_PROJECT_ID, bigqueryDatasetId)).build(); |
| 83 | + Dataset dataset = bigquery.create(datasetInfo); |
| 84 | + |
| 85 | + // Create a BigQuery table under the created dataset. |
| 86 | + Schema schema = Schema.of(new ArrayList<>()); |
| 87 | + TableDefinition tableDefinition = StandardTableDefinition.of(schema); |
| 88 | + TableInfo tableInfo = |
| 89 | + TableInfo.newBuilder(TableId.of(bigqueryDatasetId, bigqueryTableId), tableDefinition) |
| 90 | + .build(); |
| 91 | + Table table = bigquery.create(tableInfo); |
| 92 | + } |
| 93 | + |
| 94 | + @After |
| 95 | + public void tearDown() throws BigQueryException { |
| 96 | + // Delete the BigQuery dataset and table. |
| 97 | + BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService(); |
| 98 | + boolean success = |
| 99 | + bigquery.delete( |
| 100 | + DatasetId.of(PROJECT_ID, bigqueryDatasetId), |
| 101 | + BigQuery.DatasetDeleteOption.deleteContents()); |
| 102 | + System.setOut(null); |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + public void testExportToBigquery() throws Exception, IOException { |
| 107 | + ExportToBigquery.exportToBigquery( |
| 108 | + PROJECT_ID, BIGQUERY_PROJECT_ID, bigqueryDatasetId, bigqueryTableId); |
| 109 | + assertThat(bout.toString()).contains("Exported data to BigQuery"); |
| 110 | + } |
| 111 | +} |
0 commit comments