Skip to content

Commit 7efb716

Browse files
igorbernstein2kolea2
authored andcommitted
chore: copy the emulator back into this repo. (#41)
* chore: copy the emulator back into this repo. It was accidentally moved into its own repo https://github.com/googleapis/java-bigtable-emulator. This PR copies it back. Git history is short enough that a subtree merge would be over kill. * fix up readme * fix deps * fix deps 2 * fix deps
1 parent 4091ebb commit 7efb716

File tree

9 files changed

+843
-5
lines changed

9 files changed

+843
-5
lines changed
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
# Google Cloud Java Emulator for Bigtable
2+
3+
A Java wrapper for the [Cloud Bigtable][cloud-bigtable] emulator. This
4+
wrapper bundles the native Bigtable emulator and exposes a simple Java
5+
interface to ease writing tests. Please note that this wrapper is under
6+
heavy development and APIs may change in the future.
7+
8+
## Quickstart
9+
10+
If you are using Maven, add this to your pom.xml file
11+
```xml
12+
<dependencyManagement>
13+
<dependencies>
14+
<dependency>
15+
<groupId>com.google.cloud</groupId>
16+
<artifactId>google-cloud-bom</artifactId>
17+
<!-- Replace with the latest version -->
18+
<version>0.116.0-alpha</version>
19+
<type>pom</type>
20+
<scope>import</scope>
21+
</dependency>
22+
</dependencies>
23+
</dependencyManagement>
24+
25+
<dependencies>
26+
<dependency>
27+
<groupId>com.google.cloud</groupId>
28+
<artifactId>google-cloud-bigtable</artifactId>
29+
</dependency>
30+
31+
<dependency>
32+
<groupId>com.google.cloud</groupId>
33+
<artifactId>google-cloud-bigtable-emulator</artifactId>
34+
<scope>test</scope>
35+
</dependency>
36+
37+
<dependency>
38+
<groupId>junit</groupId>
39+
<artifactId>junit</artifactId>
40+
<version>4.12</version>
41+
<scope>test</scope>
42+
</dependency>
43+
</dependencies>
44+
```
45+
46+
47+
## Getting Started
48+
49+
Here is a code snippet showing a simple JUnit test. Add the following imports
50+
at the top of your file:
51+
52+
```java
53+
import com.google.api.core.ApiFuture;
54+
import com.google.api.gax.core.NoCredentialsProvider;
55+
import com.google.api.gax.grpc.GrpcTransportChannel;
56+
import com.google.api.gax.rpc.FixedTransportChannelProvider;
57+
import com.google.cloud.bigtable.admin.v2.BigtableTableAdminClient;
58+
import com.google.cloud.bigtable.admin.v2.BigtableTableAdminSettings;
59+
import com.google.cloud.bigtable.admin.v2.models.CreateTableRequest;
60+
import com.google.cloud.bigtable.data.v2.BigtableDataClient;
61+
import com.google.cloud.bigtable.data.v2.BigtableDataSettings;
62+
import com.google.cloud.bigtable.data.v2.models.Row;
63+
import com.google.cloud.bigtable.data.v2.models.RowMutation;
64+
import com.google.cloud.bigtable.emulator.v2.BigtableEmulatorRule;
65+
import java.io.IOException;
66+
import java.util.concurrent.ExecutionException;
67+
import org.junit.Assert;
68+
import org.junit.Before;
69+
import org.junit.Rule;
70+
import org.junit.Test;
71+
import org.junit.runner.RunWith;
72+
import org.junit.runners.JUnit4;
73+
```
74+
75+
Then, to make a query to Bigtable, use the following code:
76+
```java
77+
@RunWith(JUnit4.class)
78+
public class ExampleTest {
79+
// Initialize the emulator Rule
80+
@Rule
81+
public final BigtableEmulatorRule bigtableEmulator = BigtableEmulatorRule.create();
82+
83+
// Clients that will be connected to the emulator
84+
private BigtableTableAdminClient tableAdminClient;
85+
private BigtableDataClient dataClient;
86+
87+
@Before
88+
public void setUp() throws IOException {
89+
// Initialize the clients to connect to the emulator
90+
BigtableTableAdminSettings.Builder tableAdminSettings = BigtableTableAdminSettings.newBuilderForEmulator(bigtableEmulator.getPort());
91+
tableAdminClient = BigtableTableAdminClient.create(tableAdminSettings.build());
92+
93+
BigtableDataSettings.Builder dataSettings = BigtableDataSettings.newBuilderForEmulator(bigtableEmulator.getPort());
94+
dataClient = BigtableDataClient.create(dataSettings.build());
95+
96+
// Create a test table that can be used in tests
97+
tableAdminClient.createTable(
98+
CreateTableRequest.of("fake-table")
99+
.addFamily("cf")
100+
);
101+
}
102+
103+
@Test
104+
public void testWriteRead() throws ExecutionException, InterruptedException {
105+
ApiFuture<Void> mutateFuture = dataClient.mutateRowAsync(
106+
RowMutation.create("fake-table", "fake-key")
107+
.setCell("cf", "col", "value")
108+
);
109+
110+
mutateFuture.get();
111+
112+
ApiFuture<Row> rowFuture = dataClient.readRowAsync("fake-table", "fake-key");
113+
114+
Assert.assertEquals("value", rowFuture.get().getCells().get(0).getValue().toStringUtf8());
115+
}
116+
}
117+
```
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<artifactId>google-cloud-bigtable-emulator</artifactId>
8+
<version>0.114.1-alpha-SNAPSHOT</version><!-- {x-version-update:google-cloud-bigtable-emulator:current} -->
9+
<name>Google Cloud Java - Bigtable Emulator</name>
10+
<url>https://github.com/googleapis/java-bigtable</url>
11+
<description>
12+
A Java wrapper for the Cloud Bigtable emulator.
13+
</description>
14+
<parent>
15+
<groupId>com.google.cloud</groupId>
16+
<artifactId>google-cloud-bigtable-parent</artifactId>
17+
<version>1.5.1-SNAPSHOT</version><!-- {x-version-update:google-cloud-bigtable:current} -->
18+
</parent>
19+
<scm>
20+
<connection>scm:git:[email protected]:googleapis/java-bigtable.git</connection>
21+
<developerConnection>scm:git:[email protected]:googleapis/java-bigtable.git</developerConnection>
22+
<url>https://github.com/googleapis/java-bigtable</url>
23+
<tag>HEAD</tag>
24+
</scm>
25+
26+
<developers>
27+
<developer>
28+
<id>igorberstein</id>
29+
<name>Igor Bernstein</name>
30+
<email>[email protected]</email>
31+
<organization>Google</organization>
32+
<roles>
33+
<role>Developer</role>
34+
</roles>
35+
</developer>
36+
</developers>
37+
38+
<build>
39+
<plugins>
40+
<plugin>
41+
<!-- https://github.com/googleapis/java-gcloud-maven-plugin -->
42+
<groupId>com.google.cloud</groupId>
43+
<artifactId>google-cloud-gcloud-maven-plugin</artifactId>
44+
<version>0.1.0</version>
45+
46+
<executions>
47+
<execution>
48+
<id>gen-sources</id>
49+
<phase>generate-resources</phase>
50+
<goals>
51+
<goal>download</goal>
52+
</goals>
53+
<configuration>
54+
<componentNames>
55+
<componentName>bigtable-darwin-x86</componentName>
56+
<componentName>bigtable-darwin-x86_64</componentName>
57+
<componentName>bigtable-linux-x86</componentName>
58+
<componentName>bigtable-linux-x86_64</componentName>
59+
<componentName>bigtable-windows-x86</componentName>
60+
<componentName>bigtable-windows-x86_64</componentName>
61+
</componentNames>
62+
</configuration>
63+
</execution>
64+
</executions>
65+
</plugin>
66+
67+
<plugin>
68+
<groupId>org.apache.maven.plugins</groupId>
69+
<artifactId>maven-dependency-plugin</artifactId>
70+
<configuration>
71+
<!-- grpc-netty-shaded is used at test runtime -->
72+
<usedDependencies>io.grpc:grpc-netty-shaded</usedDependencies>
73+
</configuration>
74+
</plugin>
75+
</plugins>
76+
</build>
77+
78+
<dependencies>
79+
<!-- Compile deps in alphabetical order -->
80+
<dependency>
81+
<groupId>com.google.api</groupId>
82+
<artifactId>api-common</artifactId>
83+
</dependency>
84+
<dependency>
85+
<groupId>io.grpc</groupId>
86+
<artifactId>grpc-api</artifactId>
87+
<!-- gRPC deps are provided by the client -->
88+
<scope>provided</scope>
89+
</dependency>
90+
91+
<dependency>
92+
<groupId>junit</groupId>
93+
<artifactId>junit</artifactId>
94+
<!-- NOTE: this is intentionally in compile scope since it provides a JUnit Rule -->
95+
<scope>compile</scope>
96+
<optional>true</optional>
97+
</dependency>
98+
99+
<!-- Test deps, in alphabetical order -->
100+
<dependency>
101+
<groupId>com.google.api.grpc</groupId>
102+
<artifactId>grpc-google-cloud-bigtable-v2</artifactId>
103+
<scope>test</scope>
104+
</dependency>
105+
106+
<dependency>
107+
<groupId>com.google.api.grpc</groupId>
108+
<artifactId>grpc-google-cloud-bigtable-admin-v2</artifactId>
109+
<scope>test</scope>
110+
</dependency>
111+
112+
<dependency>
113+
<groupId>com.google.api.grpc</groupId>
114+
<artifactId>proto-google-cloud-bigtable-v2</artifactId>
115+
<scope>test</scope>
116+
</dependency>
117+
118+
<dependency>
119+
<groupId>com.google.api.grpc</groupId>
120+
<artifactId>proto-google-cloud-bigtable-admin-v2</artifactId>
121+
<scope>test</scope>
122+
</dependency>
123+
124+
<dependency>
125+
<groupId>com.google.protobuf</groupId>
126+
<artifactId>protobuf-java</artifactId>
127+
<scope>test</scope>
128+
</dependency>
129+
130+
<dependency>
131+
<groupId>com.google.truth</groupId>
132+
<artifactId>truth</artifactId>
133+
<scope>test</scope>
134+
</dependency>
135+
136+
<dependency>
137+
<groupId>io.grpc</groupId>
138+
<artifactId>grpc-netty-shaded</artifactId>
139+
<scope>test</scope>
140+
</dependency>
141+
</dependencies>
142+
</project>
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
* Copyright 2018 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+
* https://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+
package com.google.cloud.bigtable.emulator.v2;
17+
18+
import com.google.api.core.BetaApi;
19+
import io.grpc.ManagedChannel;
20+
import org.junit.rules.ExternalResource;
21+
22+
/**
23+
* The BigtableEmulatorRule manages the lifecycle of the Bigtable {@link Emulator}. Before the start
24+
* of a test, the emulator will be started on a random port and will be shutdown after the test
25+
* finishes.
26+
*
27+
* <p>Example usage: <code>{@code
28+
* {@literal @RunWith(JUnit4.class)}
29+
* public class MyTest {
30+
* {@literal @Rule}
31+
* public final BigtableEmulatorRule bigtableEmulator = BigtableEmulatorRule.create();
32+
*
33+
* {@literal @Test}
34+
* public void testUsingEmulator() {
35+
* ManagedChannel adminChannel = bigtableEmulator.getAdminChannel();
36+
* // Do something with channel
37+
* }
38+
* }
39+
* }</code>
40+
*/
41+
@BetaApi("Surface for Bigtable emulator is not yet stable")
42+
public class BigtableEmulatorRule extends ExternalResource {
43+
private Emulator emulator;
44+
45+
public static BigtableEmulatorRule create() {
46+
return new BigtableEmulatorRule();
47+
}
48+
49+
private BigtableEmulatorRule() {}
50+
51+
/** Initializes the Bigtable emulator before a test runs. */
52+
@Override
53+
protected void before() throws Throwable {
54+
emulator = Emulator.createBundled();
55+
emulator.start();
56+
}
57+
58+
/** Stops the Bigtable emulator after a test finishes. */
59+
@Override
60+
protected void after() {
61+
emulator.stop();
62+
emulator = null;
63+
}
64+
65+
/**
66+
* Gets a {@link ManagedChannel} connected to the Emulator. The channel is configured for data
67+
* operations.
68+
*/
69+
public ManagedChannel getDataChannel() {
70+
return emulator.getDataChannel();
71+
}
72+
73+
/**
74+
* Gets a {@link ManagedChannel} connected to the Emulator. This channel should be used for admin
75+
* operations.
76+
*/
77+
public ManagedChannel getAdminChannel() {
78+
return emulator.getAdminChannel();
79+
}
80+
81+
/**
82+
* Gets the port of the emulator, allowing the caller to create their own {@link ManagedChannel}.
83+
*/
84+
public int getPort() {
85+
return emulator.getPort();
86+
}
87+
}

0 commit comments

Comments
 (0)