Best Testcontainers-java code snippet using org.testcontainers.containers.FirestoreEmulatorContainer
Source:FirestoreTestBase.java
...16import org.springframework.boot.test.mock.mockito.MockBean;17import org.springframework.context.annotation.Bean;18import org.springframework.context.annotation.Primary;19import org.springframework.test.context.ActiveProfiles;20import org.testcontainers.containers.FirestoreEmulatorContainer;21import org.testcontainers.junit.jupiter.Container;22import org.testcontainers.junit.jupiter.Testcontainers;23import org.testcontainers.utility.DockerImageName;24import uk.gov.ons.ctp.common.cloud.FirestoreProvider;25import uk.gov.ons.ctp.common.event.EventPublisher;26import uk.gov.ons.ctp.common.firestore.TestCloudDataStore;27import uk.gov.ons.ctp.common.utility.ParallelTestLocks;28/** Base class for Firestore integration tests using TestContainers. */29@SpringBootTest(webEnvironment = WebEnvironment.NONE)30@ActiveProfiles("test-containers-firestore")31@Testcontainers32@MockBean({EventPublisher.class, PubSubTemplate.class})33@MockBean(name = "caseEventInbound", value = PubSubInboundChannelAdapter.class)34@MockBean(name = "uacEventInbound", value = PubSubInboundChannelAdapter.class)35@MockBean(name = "surveyEventInbound", value = PubSubInboundChannelAdapter.class)36@MockBean(name = "collectionExerciseEventInbound", value = PubSubInboundChannelAdapter.class)37@ResourceLock(value = ParallelTestLocks.SPRING_TEST, mode = READ_WRITE)38@Tag("firestore")39@Slf4j40public abstract class FirestoreTestBase {41 private static final String EMULATOR_IMG =42 "gcr.io/google.com/cloudsdktool/cloud-sdk:366.0.0-emulators";43 /**44 * This value must be kept in sync with spring.cloud.gcp.firestore.project-id value in the45 * application yaml files. It is hard-coded here in order to be picked up statically before the46 * application context gets created.47 */48 private static final String GCP_PROJECT = "rh-testcontainers";49 protected static final String CASE_SCHEMA = GCP_PROJECT + "-case";50 protected static final String UAC_SCHEMA = GCP_PROJECT + "-uac";51 protected static final String COLLEX_SCHEMA = GCP_PROJECT + "-collection_exercise";52 protected static final String SURVEY_SCHEMA = GCP_PROJECT + "-survey";53 @Autowired protected TestCloudDataStore testDataStore;54 private static TestFirestoreProvider provider;55 @Container56 private static final FirestoreEmulatorContainer firestoreEmulator =57 new FirestoreEmulatorContainer(DockerImageName.parse(EMULATOR_IMG));58 /**59 * The dockerised emulator will change its endpoint for each new test class, so therefore60 * firestore must be reconfigured for each test class that runs.61 */62 @BeforeAll63 public static void classSetup() {64 log.info(65 "Initialising test firestoreProvider against emulator: {}",66 firestoreEmulator.getEmulatorEndpoint());67 if (provider == null) {68 provider = new TestFirestoreProvider();69 }70 provider.init(GCP_PROJECT, firestoreEmulator);71 }72 /**73 * Configure any firestore provider in our test environment to use the emulator. This74 * FirestoreProvider bean will be picked up by everywhere using firestore and override the usual75 * implementation.76 */77 @TestConfiguration78 static class FirestoreConfiguration {79 @Bean80 @Primary81 FirestoreProvider firestoreProvider() {82 return provider;83 }84 }85 protected void deleteAllCollections() {86 testDataStore.deleteCollection(SURVEY_SCHEMA);87 testDataStore.deleteCollection(COLLEX_SCHEMA);88 testDataStore.deleteCollection(UAC_SCHEMA);89 testDataStore.deleteCollection(CASE_SCHEMA);90 }91 /** An implementation of FirestoreProvider that uses the emulator. */92 public static class TestFirestoreProvider implements FirestoreProvider {93 private Firestore firestore;94 public TestFirestoreProvider init(String gcpProject, FirestoreEmulatorContainer emulator) {95 firestore =96 FirestoreOptions.newBuilder()97 .setProjectId(gcpProject)98 .setHost(emulator.getEmulatorEndpoint())99 .setCredentials(NoCredentials.getInstance())100 .build()101 .getService();102 return this;103 }104 public Firestore get() {105 return firestore;106 }107 }108}...
Source:AbstractInterationTest.java
2import com.google.api.client.http.HttpMethods;3import java.net.URI;4import java.util.Map;5import org.junit.jupiter.api.extension.ExtendWith;6import org.testcontainers.containers.FirestoreEmulatorContainer;7import org.testcontainers.junit.jupiter.Container;8import org.testcontainers.junit.jupiter.Testcontainers;9import org.testcontainers.utility.DockerImageName;10import uk.org.webcompere.systemstubs.environment.EnvironmentVariables;11import uk.org.webcompere.systemstubs.jupiter.SystemStub;12import uk.org.webcompere.systemstubs.jupiter.SystemStubsExtension;13@ExtendWith(SystemStubsExtension.class)14@Testcontainers15public abstract class AbstractInterationTest {16 @Container17 public final static FirestoreEmulatorContainer FIRESTORE_EMULATOR = new FirestoreEmulatorContainer(18 DockerImageName.parse("gcr.io/google.com/cloudsdktool/cloud-sdk:317.0.0-emulators"));19 public final static String uriString = "https://LOCATION-PROJECT_ID.cloudfunctions.net/clockify-webhook";20 public final static String CLOCKIFY_SIGNATURE = "verySecretSignature";21 @SystemStub22 public final EnvironmentVariables environmentVariables = new EnvironmentVariables() //23 .set("GOOGLE_CLOUD_PROJECT", "test-project") //24 .set("CLOCKIFY_SIGNATURE", CLOCKIFY_SIGNATURE) //25 .set("FIRESTORE_EMULATOR_HOST", FIRESTORE_EMULATOR.getEmulatorEndpoint());26 public final Map<String, String> headerMap = Map.of( //27 "Clockify-Signature", CLOCKIFY_SIGNATURE, //28 "Content-Type", "application/json");29 public final JavaHttpRequest.JavaHttpRequestBuilder requestBuilder = JavaHttpRequest.builder() //30 .uri(URI.create(uriString)).method(HttpMethods.POST).httpHeaders(headerMap);31}...
Source:FirestoreMetricsReproducerApplicationTests.java
...4import org.junit.jupiter.api.Test;5import org.springframework.boot.test.context.SpringBootTest;6import org.springframework.test.context.DynamicPropertyRegistry;7import org.springframework.test.context.DynamicPropertySource;8import org.testcontainers.containers.FirestoreEmulatorContainer;9import org.testcontainers.junit.jupiter.Container;10import org.testcontainers.junit.jupiter.Testcontainers;11import org.testcontainers.utility.DockerImageName;12@SpringBootTest13@Testcontainers14class FirestoreMetricsReproducerApplicationTests {15 @Container16 private static FirestoreEmulatorContainer emulator = new FirestoreEmulatorContainer(17 DockerImageName.parse("google/cloud-sdk:382.0.0-emulators")18 .asCompatibleSubstituteFor("gcr.io/google.com/cloudsdktool/cloud-sdk")19 );20 @DynamicPropertySource21 static void redisProperties(DynamicPropertyRegistry registry) {22 registry.add("spring.cloud.gcp.firestore.emulator.enabled", () -> true);23 registry.add("spring.cloud.gcp.firestore.host-port", emulator::getEmulatorEndpoint);24 }25 @Test26 void contextLoads() {27 Assertions.assertTrue(true, "The app should start successfully");28 }29}...
FirestoreEmulatorContainer
Using AI Code Generation
1import com.google.cloud.firestore.Firestore;2import com.google.cloud.firestore.FirestoreOptions;3import org.junit.jupiter.api.Test;4import org.testcontainers.containers.FirestoreEmulatorContainer;5import org.testcontainers.junit.jupiter.Container;6import org.testcontainers.junit.jupiter.Testcontainers;7public class FirestoreEmulatorContainerTest {8 private static final FirestoreEmulatorContainer firestoreEmulatorContainer = new FirestoreEmulatorContainer();9 public void testFirestoreEmulatorContainer() {10 FirestoreOptions firestoreOptions = FirestoreOptions.getDefaultInstance().toBuilder()11 .setProjectId(firestoreEmulatorContainer.getProjectId())12 .setHost(firestoreEmulatorContainer.getHost())13 .setPort(firestoreEmulatorContainer.getPort())14 .setCredentials(firestoreEmulatorContainer.getEmulatorCredentials())15 .build();16 Firestore firestore = firestoreOptions.getService();17 firestore.listCollections().forEach(collectionReference -> {18 System.out.println(collectionReference.getId());19 });20 }21}
FirestoreEmulatorContainer
Using AI Code Generation
1import com.google.cloud.firestore.Firestore;2import com.google.cloud.firestore.FirestoreOptions;3import org.junit.jupiter.api.Test;4import org.testcontainers.containers.FirestoreEmulatorContainer;5import org.testcontainers.junit.jupiter.Container;6import org.testcontainers.junit.jupiter.Testcontainers;7public class FirestoreEmulatorContainerTest {8 private static FirestoreEmulatorContainer emulator = new FirestoreEmulatorContainer();9 public void test() {10 FirestoreOptions options = FirestoreOptions.newBuilder()11 .setProjectId(emulator.getProjectId())12 .setHost(emulator.getHost())13 .setPort(emulator.getPort())14 .build();15 Firestore firestore = options.getService();16 }17}
FirestoreEmulatorContainer
Using AI Code Generation
1import com.google.cloud.firestore.Firestore;2import com.google.cloud.firestore.FirestoreOptions;3import com.google.cloud.firestore.QueryDocumentSnapshot;4import com.google.firebase.FirebaseApp;5import com.google.firebase.FirebaseOptions;6import com.google.firebase.cloud.FirestoreClient;7import java.io.IOException;8import java.util.List;9import java.util.concurrent.ExecutionException;10import org.junit.ClassRule;11import org.junit.Test;12import org.testcontainers.containers.FirestoreEmulatorContainer;13public class FirestoreEmulatorContainerTest {14 new FirestoreEmulatorContainer("1.11.3");15 public void testFirestoreEmulator() throws IOException, ExecutionException, InterruptedException {16 new FirebaseOptions.Builder()17 .setCredentials(GoogleCredentials.getApplicationDefault())18 .setProjectId("test-project")19 .build();20 FirebaseApp.initializeApp(options);21 FirestoreOptions.newBuilder()22 .setProjectId("test-project")23 .setHost(firestoreEmulator.getEmulatorEndpoint())24 .setCredentials(GoogleCredentials.getApplicationDefault())25 .build();26 Firestore db = firestoreOptions.getService();27 FirestoreClient.getFirestore()28 .collection("users")29 .document("alovelace")30 .set(Collections.singletonMap("first", "Ada"))31 .get();32 FirestoreClient.getFirestore().collection("users").get().get().getDocuments();33 assertEquals(1, documents.size());34 assertEquals("Ada", documents.get(0).getString("first"));35 }36}37import org.testcontainers.containers.FirestoreEmulatorContainer;38 new FirestoreEmulatorContainer("1.11.3");
FirestoreEmulatorContainer
Using AI Code Generation
1import com.google.cloud.firestore.Firestore;2import com.google.cloud.firestore.FirestoreOptions;3import com.google.cloud.firestore.QueryDocumentSnapshot;4import com.google.cloud.firestore.QuerySnapshot;5import org.junit.jupiter.api.Test;6import org.testcontainers.containers.FirestoreEmulatorContainer;7import org.testcontainers.junit.jupiter.Container;8import org.testcontainers.junit.jupiter.Testcontainers;9import java.util.List;10public class FirestoreEmulatorContainerTest {11 public static final FirestoreEmulatorContainer firestoreEmulator = new FirestoreEmulatorContainer();12 public void test() throws Exception {13 FirestoreOptions options = FirestoreOptions.newBuilder()14 .setProjectId(firestoreEmulator.getProjectId())15 .setHost(firestoreEmulator.getEmulatorEndpoint())16 .setCredentials(firestoreEmulator.getCredentials())17 .build();18 Firestore firestore = options.getService();19 firestore.collection("users").add(new User("John", "Doe")).get();20 QuerySnapshot querySnapshot = firestore.collection("users").get().get();21 List<QueryDocumentSnapshot> documents = querySnapshot.getDocuments();22 for (QueryDocumentSnapshot document : documents) {23 System.out.println(document.toObject(User.class));24 }25 }26}27User{firstName='John', lastName='Doe'}
FirestoreEmulatorContainer
Using AI Code Generation
1import org.testcontainers.containers.FirestoreEmulatorContainer;2public class 1 {3 public static void main(String[] args) {4 FirestoreEmulatorContainer container = new FirestoreEmulatorContainer();5 container.start();6 System.out.println("Firestore Emulator is running on port: " + container.getPort());7 }8}
FirestoreEmulatorContainer
Using AI Code Generation
1import com.google.cloud.firestore.Firestore;2import com.google.cloud.firestore.FirestoreOptions;3import com.google.cloud.firestore.Query;4import com.google.cloud.firestore.QueryDocumentSnapshot;5import com.google.cloud.firestore.QuerySnapshot;6import java.util.List;7import org.junit.ClassRule;8import org.junit.Test;9import org.testcontainers.containers.FirestoreEmulatorContainer;10public class FirestoreEmulatorContainerTest {11 private static final String EMULATOR_HOST = "emulator.host";12 new FirestoreEmulatorContainer();13 public void testFirestoreEmulatorContainer() throws Exception {14 System.setProperty(EMULATOR_HOST, firestoreEmulatorContainer.getEmulatorEndpoint());15 FirestoreOptions firestoreOptions = FirestoreOptions.getDefaultInstance().toBuilder()16 .setHost(firestoreEmulatorContainer.getEmulatorEndpoint())17 .setProjectId(firestoreEmulatorContainer.getProjectId())18 .build();19 Firestore firestore = firestoreOptions.getService();20 Query query = firestore.collection("users");21 QuerySnapshot querySnapshot = query.get().get();22 List<QueryDocumentSnapshot> documents = querySnapshot.getDocuments();23 System.out.println("Number of documents: " + documents.size());24 }25}26import com.google.cloud.firestore.Firestore;27import com.google.cloud.firestore.FirestoreOptions;28import com.google.cloud.firestore.Query;29import com.google.cloud.firestore.QueryDocumentSnapshot;30import com.google.cloud.firestore.QuerySnapshot;31import java.util.List;32import org.junit.ClassRule;33import org.junit.Test;34import org.testcontainers.containers.FirestoreEmulatorContainer;35public class FirestoreEmulatorContainerTest {36 private static final String EMULATOR_HOST = "emulator.host";37 new FirestoreEmulatorContainer();38 public void testFirestoreEmulatorContainer() throws Exception {39 System.setProperty(EMULATOR_HOST, firestoreEmulatorContainer.getEmulatorEndpoint());40 FirestoreOptions firestoreOptions = FirestoreOptions.getDefaultInstance().toBuilder()41 .setHost(firestoreEmulatorContainer.getEmulatorEndpoint())42 .setProjectId(firestoreEmulatorContainer.getProjectId())43 .build();44 Firestore firestore = firestoreOptions.getService();45 Query query = firestore.collection("users");
FirestoreEmulatorContainer
Using AI Code Generation
1import com.google.cloud.firestore.Firestore;2import com.google.cloud.firestore.FirestoreOptions;3import com.google.cloud.firestore.QueryDocumentSnapshot;4import com.google.cloud.firestore.QuerySnapshot;5import com.google.cloud.firestore.WriteResult;6import com.google.firebase.cloud.FirestoreClient;7import com.google.firebase.database.annotations.Nullable;8import java.util.List;9import java.util.concurrent.ExecutionException;10import org.junit.jupiter.api.AfterEach;11import org.junit.jupiter.api.BeforeEach;12import org.junit.jupiter.api.Test;13import org.testcontainers.containers.FirestoreEmulatorContainer;14import org.testcontainers.junit.jupiter.Container;15import org.testcontainers.junit.jupiter.Testcontainers;16public class FirestoreEmulatorContainerTest {17 private static final FirestoreEmulatorContainer firestoreEmulatorContainer = new FirestoreEmulatorContainer();18 private Firestore db;19 public void setUp() {20 db = FirestoreOptions.newBuilder()21 .setProjectId(firestoreEmulatorContainer.getProjectId())22 .setHost(firestoreEmulatorContainer.getEmulatorEndpoint())23 .setCredentials(GoogleCredentials.getApplicationDefault())24 .build()25 .getService();26 }27 public void tearDown() {28 db.close();29 }30 public void testFirestoreEmulatorContainer() throws ExecutionException, InterruptedException {31 WriteResult writeResult = db.collection("test").document("test").set("test").get();32 System.out.println("WriteResult: " + writeResult);33 QuerySnapshot querySnapshot = db.collection("test").get().get();34 List<QueryDocumentSnapshot> documents = querySnapshot.getDocuments();35 System.out.println("Documents: " + documents);36 }37}38import com.google.cloud.firestore.Firestore;39import com.google.cloud.firestore.FirestoreOptions;40import com.google.cloud.firestore.QueryDocumentSnapshot;41import com.google.cloud.firestore.QuerySnapshot;42import com.google.cloud.firestore.WriteResult;43import com.google.firebase.database.annotations.Nullable;44import java.util.List;45import java.util.concurrent.ExecutionException;46import org.junit.jupiter.api.AfterEach;47import org.junit.jupiter.api.BeforeEach;48import org.junit.jupiter.api.Test;49import org.testcontainers.containers.FirestoreEmulator;50import org.testcontainers.junit.jupiter.Container;51import org.testcontainers.junit.jupiter.Testcontainers;52public class FirestoreEmulatorContainerTest {
FirestoreEmulatorContainer
Using AI Code Generation
1import static com.google.common.truth.Truth.assertThat;2import static org.junit.jupiter.api.Assertions.assertEquals;3import static org.junit.jupiter.api.Assertions.assertThrows;4import com.google.cloud.firestore.Firestore;5import com.google.cloud.firestore.FirestoreOptions;6import com.google.firebase.FirebaseApp;7import com.google.firebase.FirebaseOptions;8import com.google.firebase.cloud.FirestoreClient;9import com.google.firebase.testing.IntegrationTestUtils;10import com.google.firebase.testing.ServiceAccount;11import java.io.IOException;12import org.junit.jupiter.api.AfterAll;13import org.junit.jupiter.api.BeforeAll;14import org.junit.jupiter.api.Test;15import org.junit.jupiter.api.extension.ExtendWith;16import org.testcontainers.containers.FirestoreEmulatorContainer;17import org.testcontainers.junit.jupiter.Container;18import org.testcontainers.junit.jupiter.Testcontainers;19import org.testcontainers.utility.DockerImageName;20@ExtendWith(Testcontainers.class)21public class FirestoreEmulatorTest {22 private static final String FIRESTORE_EMULATOR_HOST = "FIRESTORE_EMULATOR_HOST";23 new FirestoreEmulatorContainer(DockerImageName.parse("gcr.io/google.com/cloudsdktool/cloud-sdk:latest"));24 private static Firestore firestore;25 public static void setUp() throws IOException {26 ServiceAccount account = IntegrationTestUtils.getServiceAccount();27 FirebaseOptions options = FirebaseOptions.builder()28 .setCredentials(account.getCredentials())29 .setProjectId(account.getProjectId())30 .build();31 FirebaseApp.initializeApp(options);32 firestore = FirestoreClient.getFirestore();33 }34 public static void tearDown() {35 firestore.close();36 }37 public void testFirestoreEmulator() throws InterruptedException {38 String projectId = firestoreEmulator.getProjectId();39 String host = firestoreEmulator.getEmulatorEndpoint();40 String hostWithProjectId = host + "?ns=" + projectId;41 assertThat(firestoreEmulator.isRunning()).isTrue();42 assertThat(System.getenv(FIRESTORE_EMULATOR_HOST)).isEqualTo(host);43 assertThat(firestoreEmulator.isEmulatorEnabled(FirestoreOptions.getDefaultInstance())).isTrue();44 assertThat(firestoreEmulator.isEmulatorEnabled(FirestoreOptions.newBuilder().build())).isTrue
FirestoreEmulatorContainer
Using AI Code Generation
1import com.google.auth.oauth2.GoogleCredentials;2import com.google.cloud.firestore.Firestore;3import com.google.cloud.firestore.FirestoreOptions;4import com.google.common.collect.ImmutableList;5import org.testcontainers.containers.FirestoreEmulatorContainer;6import org.testcontainers.utility.DockerImageName;7import java.io.IOException;8import java.util.List;9public class FirestoreEmulatorContainerTest {10 public static void main(String[] args) throws IOException, InterruptedException {11 FirestoreEmulatorContainer firestoreEmulatorContainer = new FirestoreEmulatorContainer(DockerImageName.parse("gcr.io/google.com/cloudsdktool/cloud-sdk:316.0.0-emulators"));12 firestoreEmulatorContainer.start();13 String host = firestoreEmulatorContainer.getHost();14 int port = firestoreEmulatorContainer.getPort();15 List<String> endpoints = ImmutableList.of(host + ":" + port);16 FirestoreOptions firestoreOptions = FirestoreOptions.newBuilder()17 .setProjectId("test-project")18 .setHost(host + ":" + port)19 .setCredentials(GoogleCredentials.getApplicationDefault())20 .setEmulatorHost(host + ":" + port)21 .build();22 Firestore firestore = firestoreOptions.getService();23 firestore.document("test-collection/test-document");24 firestoreEmulatorContainer.stop();25 }26}
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!