Skip to content

Commit 2538fb8

Browse files
authored
[gcloud] Allow configuring the BigQuery emulator project id (#12123)
Reject a blank BigQuery emulator project id
1 parent a3cb022 commit 2538fb8

3 files changed

Lines changed: 63 additions & 3 deletions

File tree

‎docs/modules/gcloud.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ Start BigQuery Emulator during a test:
3030
[Creating BigQuery Client](../../modules/gcloud/src/test/java/org/testcontainers/gcloud/BigQueryEmulatorContainerTest.java) inside_block:bigQueryClient
3131
<!--/codeinclude-->
3232

33+
The emulator is started with the `test-project` project id by default. Use `withProjectId` to start it with another one:
34+
35+
<!--codeinclude-->
36+
[Starting a BigQuery Emulator container with a custom project id](../../modules/gcloud/src/test/java/org/testcontainers/gcloud/BigQueryEmulatorContainerTest.java) inside_block:emulatorContainerWithProjectId
37+
<!--/codeinclude-->
38+
3339
### Bigtable
3440

3541
Start Bigtable Emulator during a test:

‎modules/gcloud/src/main/java/org/testcontainers/gcloud/BigQueryEmulatorContainer.java‎

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.testcontainers.gcloud;
22

3+
import org.apache.commons.lang3.StringUtils;
34
import org.testcontainers.containers.GenericContainer;
45
import org.testcontainers.utility.DockerImageName;
56

@@ -17,7 +18,9 @@ public class BigQueryEmulatorContainer extends GenericContainer<BigQueryEmulator
1718

1819
private static final int GRPC_PORT = 9060;
1920

20-
private static final String PROJECT_ID = "test-project";
21+
private static final String DEFAULT_PROJECT_ID = "test-project";
22+
23+
private String projectId = DEFAULT_PROJECT_ID;
2124

2225
public BigQueryEmulatorContainer(String image) {
2326
this(DockerImageName.parse(image));
@@ -27,7 +30,22 @@ public BigQueryEmulatorContainer(DockerImageName dockerImageName) {
2730
super(dockerImageName);
2831
dockerImageName.assertCompatibleWith(DEFAULT_IMAGE_NAME);
2932
addExposedPorts(HTTP_PORT, GRPC_PORT);
30-
withCommand("--project", PROJECT_ID);
33+
withCommand("--project", DEFAULT_PROJECT_ID);
34+
}
35+
36+
/**
37+
* Sets the project id the emulator is started with. Defaults to {@code test-project}.
38+
*
39+
* @param projectId the project id
40+
* @return this container
41+
* @throws IllegalArgumentException if {@code projectId} is null or blank
42+
*/
43+
public BigQueryEmulatorContainer withProjectId(String projectId) {
44+
if (StringUtils.isBlank(projectId)) {
45+
throw new IllegalArgumentException("BigQuery emulator project id cannot be blank");
46+
}
47+
this.projectId = projectId;
48+
return withCommand("--project", projectId);
3149
}
3250

3351
public String getEmulatorHttpEndpoint() {
@@ -39,6 +57,6 @@ public Integer getEmulatorGrpcPort() {
3957
}
4058

4159
public String getProjectId() {
42-
return PROJECT_ID;
60+
return this.projectId;
4361
}
4462
}

‎modules/gcloud/src/test/java/org/testcontainers/gcloud/BigQueryEmulatorContainerTest.java‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,17 @@
3333
import org.json.JSONArray;
3434
import org.json.JSONObject;
3535
import org.junit.jupiter.api.Test;
36+
import org.junit.jupiter.params.ParameterizedTest;
37+
import org.junit.jupiter.params.provider.NullAndEmptySource;
38+
import org.junit.jupiter.params.provider.ValueSource;
3639
import org.threeten.bp.Duration;
3740

3841
import java.math.BigDecimal;
3942
import java.util.List;
4043
import java.util.stream.Collectors;
4144

4245
import static org.assertj.core.api.Assertions.assertThat;
46+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
4347

4448
class BigQueryEmulatorContainerTest {
4549

@@ -190,6 +194,38 @@ void testGrcpEndpoint() throws Exception {
190194
}
191195
}
192196

197+
@Test
198+
void testCustomProjectId() {
199+
try (
200+
// emulatorContainerWithProjectId {
201+
BigQueryEmulatorContainer container = new BigQueryEmulatorContainer("ghcr.io/goccy/bigquery-emulator:0.6.5")
202+
.withProjectId("my_project")
203+
// }
204+
) {
205+
container.start();
206+
207+
assertThat(container.getProjectId()).isEqualTo("my_project");
208+
209+
BigQuery bigQuery = getBigQuery(container);
210+
bigQuery.create(DatasetInfo.of(DatasetId.of("my_project", "my_dataset")));
211+
212+
assertThat(bigQuery.getDataset(DatasetId.of("my_project", "my_dataset"))).isNotNull();
213+
}
214+
}
215+
216+
@ParameterizedTest
217+
@NullAndEmptySource
218+
@ValueSource(strings = { " " })
219+
void testBlankProjectIdIsRejected(String projectId) {
220+
try (
221+
BigQueryEmulatorContainer container = new BigQueryEmulatorContainer("ghcr.io/goccy/bigquery-emulator:0.6.5")
222+
) {
223+
assertThatThrownBy(() -> container.withProjectId(projectId))
224+
.isInstanceOf(IllegalArgumentException.class)
225+
.hasMessageContaining("project id");
226+
}
227+
}
228+
193229
private BigQuery getBigQuery(BigQueryEmulatorContainer container) {
194230
String url = container.getEmulatorHttpEndpoint();
195231
return BigQueryOptions

0 commit comments

Comments
 (0)