Skip to content
This repository was archived by the owner on Feb 24, 2026. It is now read-only.

Commit 3280d7a

Browse files
docs(sample): add IT for WriteToDefaultStream (#1158)
Fixes #1156
1 parent fc4a5c0 commit 3280d7a

File tree

1 file changed

+96
-0
lines changed

1 file changed

+96
-0
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
* Copyright 2021 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.bigquerystorage;
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.BigQuery.DatasetDeleteOption;
24+
import com.google.cloud.bigquery.BigQueryOptions;
25+
import com.google.cloud.bigquery.DatasetId;
26+
import com.google.cloud.bigquery.DatasetInfo;
27+
import com.google.cloud.bigquery.Field;
28+
import com.google.cloud.bigquery.Schema;
29+
import com.google.cloud.bigquery.StandardSQLTypeName;
30+
import com.google.cloud.bigquery.StandardTableDefinition;
31+
import com.google.cloud.bigquery.TableId;
32+
import com.google.cloud.bigquery.TableInfo;
33+
import java.io.ByteArrayOutputStream;
34+
import java.io.PrintStream;
35+
import java.util.UUID;
36+
import org.junit.After;
37+
import org.junit.Before;
38+
import org.junit.BeforeClass;
39+
import org.junit.Test;
40+
import org.junit.runner.RunWith;
41+
import org.junit.runners.JUnit4;
42+
43+
@RunWith(JUnit4.class)
44+
public class WriteToDefaultStreamIT {
45+
46+
private static final String GOOGLE_CLOUD_PROJECT = System.getenv("GOOGLE_CLOUD_PROJECT");
47+
48+
private ByteArrayOutputStream bout;
49+
private PrintStream out;
50+
private BigQuery bigquery;
51+
private String datasetName;
52+
private String tableName;
53+
54+
private static void requireEnvVar(String varName) {
55+
assertNotNull(
56+
"Environment variable " + varName + " is required to perform these tests.",
57+
System.getenv(varName));
58+
}
59+
60+
@BeforeClass
61+
public static void checkRequirements() {
62+
requireEnvVar("GOOGLE_CLOUD_PROJECT");
63+
}
64+
65+
@Before
66+
public void setUp() {
67+
bout = new ByteArrayOutputStream();
68+
out = new PrintStream(bout);
69+
System.setOut(out);
70+
71+
bigquery = BigQueryOptions.getDefaultInstance().getService();
72+
73+
// Create a new dataset and table for each test.
74+
datasetName = "WRITE_STREAM_TEST" + UUID.randomUUID().toString().substring(0, 8);
75+
tableName = "DEFAULT_STREAM_TEST" + UUID.randomUUID().toString().substring(0, 8);
76+
Schema schema = Schema.of(Field.of("col1", StandardSQLTypeName.STRING));
77+
bigquery.create(DatasetInfo.newBuilder(datasetName).build());
78+
TableInfo tableInfo =
79+
TableInfo.newBuilder(TableId.of(datasetName, tableName), StandardTableDefinition.of(schema))
80+
.build();
81+
bigquery.create(tableInfo);
82+
}
83+
84+
@After
85+
public void tearDown() {
86+
bigquery.delete(
87+
DatasetId.of(GOOGLE_CLOUD_PROJECT, datasetName), DatasetDeleteOption.deleteContents());
88+
System.setOut(null);
89+
}
90+
91+
@Test
92+
public void testWriteToDefaultStream() throws Exception {
93+
WriteToDefaultStream.writeToDefaultStream(GOOGLE_CLOUD_PROJECT, datasetName, tableName);
94+
assertThat(bout.toString()).contains("Appended records successfully.");
95+
}
96+
}

0 commit comments

Comments
 (0)