Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/modules/gcloud.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ Start BigQuery Emulator during a test:
[Creating BigQuery Client](../../modules/gcloud/src/test/java/org/testcontainers/gcloud/BigQueryEmulatorContainerTest.java) inside_block:bigQueryClient
<!--/codeinclude-->

The emulator is started with the `test-project` project id by default. Use `withProjectId` to start it with another one:

<!--codeinclude-->
[Starting a BigQuery Emulator container with a custom project id](../../modules/gcloud/src/test/java/org/testcontainers/gcloud/BigQueryEmulatorContainerTest.java) inside_block:emulatorContainerWithProjectId
<!--/codeinclude-->

### Bigtable

Start Bigtable Emulator during a test:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.testcontainers.gcloud;

import org.apache.commons.lang3.StringUtils;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.utility.DockerImageName;

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

private static final int GRPC_PORT = 9060;

private static final String PROJECT_ID = "test-project";
private static final String DEFAULT_PROJECT_ID = "test-project";

private String projectId = DEFAULT_PROJECT_ID;

public BigQueryEmulatorContainer(String image) {
this(DockerImageName.parse(image));
Expand All @@ -27,7 +30,22 @@ public BigQueryEmulatorContainer(DockerImageName dockerImageName) {
super(dockerImageName);
dockerImageName.assertCompatibleWith(DEFAULT_IMAGE_NAME);
addExposedPorts(HTTP_PORT, GRPC_PORT);
withCommand("--project", PROJECT_ID);
withCommand("--project", DEFAULT_PROJECT_ID);
}

/**
* Sets the project id the emulator is started with. Defaults to {@code test-project}.
*
* @param projectId the project id
* @return this container
* @throws IllegalArgumentException if {@code projectId} is null or blank
*/
public BigQueryEmulatorContainer withProjectId(String projectId) {
if (StringUtils.isBlank(projectId)) {
throw new IllegalArgumentException("BigQuery emulator project id cannot be blank");
}
this.projectId = projectId;
return withCommand("--project", projectId);
Comment thread
alimhin marked this conversation as resolved.
}

public String getEmulatorHttpEndpoint() {
Expand All @@ -39,6 +57,6 @@ public Integer getEmulatorGrpcPort() {
}

public String getProjectId() {
return PROJECT_ID;
return this.projectId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,17 @@
import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.NullAndEmptySource;
import org.junit.jupiter.params.provider.ValueSource;
import org.threeten.bp.Duration;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

class BigQueryEmulatorContainerTest {

Expand Down Expand Up @@ -190,6 +194,38 @@ void testGrcpEndpoint() throws Exception {
}
}

@Test
void testCustomProjectId() {
try (
// emulatorContainerWithProjectId {
BigQueryEmulatorContainer container = new BigQueryEmulatorContainer("ghcr.io/goccy/bigquery-emulator:0.6.5")
.withProjectId("my_project")
// }
) {
container.start();

assertThat(container.getProjectId()).isEqualTo("my_project");

BigQuery bigQuery = getBigQuery(container);
bigQuery.create(DatasetInfo.of(DatasetId.of("my_project", "my_dataset")));

assertThat(bigQuery.getDataset(DatasetId.of("my_project", "my_dataset"))).isNotNull();
}
}

@ParameterizedTest
@NullAndEmptySource
@ValueSource(strings = { " " })
void testBlankProjectIdIsRejected(String projectId) {
try (
BigQueryEmulatorContainer container = new BigQueryEmulatorContainer("ghcr.io/goccy/bigquery-emulator:0.6.5")
) {
assertThatThrownBy(() -> container.withProjectId(projectId))
.isInstanceOf(IllegalArgumentException.class)
.hasMessageContaining("project id");
}
}

private BigQuery getBigQuery(BigQueryEmulatorContainer container) {
String url = container.getEmulatorHttpEndpoint();
return BigQueryOptions
Expand Down
Loading