Skip to content

Commit 374c334

Browse files
committed
Add integration tests for Datastore quickstart.
1 parent 04ac9da commit 374c334

File tree

5 files changed

+115
-3
lines changed

5 files changed

+115
-3
lines changed

bigquery/cloud-client/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
<dependency>
5151
<groupId>com.google.truth</groupId>
5252
<artifactId>truth</artifactId>
53-
<version>0.29</version>
53+
<version>0.30</version>
5454
<scope>test</scope>
5555
</dependency>
5656
</dependencies>

datastore/cloud-client/README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Getting Started with Cloud Datastore and the Google Cloud Client libraries
2+
3+
[Google Cloud Datastore][Datastore] is a highly-scalable NoSQL database for your applications.
4+
These sample Java applications demonstrate how to access the Datastore API using
5+
the [Google Cloud Client Library for Java][google-cloud-java].
6+
7+
[Datastore]: https://cloud.google.com/datastore/
8+
[google-cloud-java]: https://github.com/GoogleCloudPlatform/google-cloud-java
9+
10+
## Quickstart
11+
12+
Install [Maven](http://maven.apache.org/).
13+
14+
Build your project with:
15+
16+
mvn clean package -DskipTests
17+
18+
You can then run a given `ClassName` via:
19+
20+
mvn exec:java -Dexec.mainClass=com.example.bigquery.ClassName \
21+
-DpropertyName=propertyValue \
22+
-Dexec.args="any arguments to the app"
23+
24+
### Creating a new entity (using the quickstart sample)
25+
26+
mvn exec:java -Dexec.mainClass=com.example.datastore.QuickstartSample

datastore/cloud-client/pom.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,19 @@
3939
<artifactId>google-cloud-datastore</artifactId>
4040
<version>0.4.0</version>
4141
</dependency>
42+
43+
<!-- Test dependencies -->
44+
<dependency>
45+
<groupId>junit</groupId>
46+
<artifactId>junit</artifactId>
47+
<version>4.12</version>
48+
<scope>test</scope>
49+
</dependency>
50+
<dependency>
51+
<groupId>com.google.truth</groupId>
52+
<artifactId>truth</artifactId>
53+
<version>0.30</version>
54+
<scope>test</scope>
55+
</dependency>
4256
</dependencies>
4357
</project>

datastore/cloud-client/src/main/java/com/example/datastore/QuickstartSample.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ public static void main(String... args) throws Exception {
3737

3838
// Prepares the new entity
3939
Entity task = Entity.builder(taskKey)
40-
.set("description", "Buy milk")
41-
.build();
40+
.set("description", "Buy milk")
41+
.build();
4242

4343
// Saves the entity
4444
datastore.put(task);
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
Copyright 2016, Google, Inc.
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.datastore;
18+
19+
import static com.google.common.truth.Truth.assertThat;
20+
21+
import com.google.cloud.datastore.Datastore;
22+
import com.google.cloud.datastore.DatastoreOptions;
23+
import com.google.cloud.datastore.Key;
24+
import org.junit.After;
25+
import org.junit.Before;
26+
import org.junit.Test;
27+
import org.junit.runner.RunWith;
28+
import org.junit.runners.JUnit4;
29+
30+
import java.io.ByteArrayOutputStream;
31+
import java.io.PrintStream;
32+
33+
/**
34+
* Tests for quickstart sample.
35+
*/
36+
@RunWith(JUnit4.class)
37+
@SuppressWarnings("checkstyle:abbreviationaswordinname")
38+
public class QuickstartSampleIT {
39+
private ByteArrayOutputStream bout;
40+
private PrintStream out;
41+
42+
private static final void deleteTestEntity() {
43+
Datastore datastore = DatastoreOptions.defaultInstance().service();
44+
String kind = "Task";
45+
String name = "sampletask1";
46+
Key taskKey = datastore.newKeyFactory().kind(kind).newKey(name);
47+
datastore.delete(taskKey);
48+
}
49+
50+
@Before
51+
public void setUp() {
52+
deleteTestEntity();
53+
54+
bout = new ByteArrayOutputStream();
55+
out = new PrintStream(bout);
56+
System.setOut(out);
57+
}
58+
59+
@After
60+
public void tearDown() {
61+
System.setOut(null);
62+
deleteTestEntity();
63+
}
64+
65+
@Test
66+
public void testQuickstart() throws Exception {
67+
QuickstartSample.main();
68+
String got = bout.toString();
69+
assertThat(got).contains("Saved sampletask1: Buy milk");
70+
}
71+
}
72+
// [END datastore_quickstart]

0 commit comments

Comments
 (0)