Skip to content

Commit 1eb7a61

Browse files
committed
Some Trivial Tests (GoogleCloudPlatform#189)
* Some Trivial Tests * Check style isn't my friend today. Several changes due to weird imports in IntelliJ.
1 parent e06ff00 commit 1eb7a61

File tree

4 files changed

+271
-1
lines changed

4 files changed

+271
-1
lines changed

appengine/guestbook-objectify/pom.xml

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<version>1.0-SNAPSHOT</version>
77

88
<groupId>com.example.appengine</groupId>
9-
<artifactId>guestbook</artifactId>
9+
<artifactId>appengine-guestbook-objectify</artifactId>
1010
<properties>
1111
<objectify.version>5.1.5</objectify.version>
1212
<guava.version>18.0</guava.version>
@@ -57,6 +57,18 @@
5757
<!-- [END Objectify_Dependencies] -->
5858

5959
<!-- Test Dependencies -->
60+
<dependency>
61+
<groupId>junit</groupId>
62+
<artifactId>junit</artifactId>
63+
<version>4.12</version>
64+
<scope>test</scope>
65+
</dependency>
66+
<dependency>
67+
<groupId>org.mockito</groupId>
68+
<artifactId>mockito-all</artifactId>
69+
<version>1.10.19</version>
70+
<scope>test</scope>
71+
</dependency>
6072
<dependency>
6173
<groupId>com.google.appengine</groupId>
6274
<artifactId>appengine-testing</artifactId>
@@ -69,6 +81,19 @@
6981
<version>${appengine.sdk.version}</version>
7082
<scope>test</scope>
7183
</dependency>
84+
<dependency>
85+
<groupId>com.google.appengine</groupId>
86+
<artifactId>appengine-tools-sdk</artifactId>
87+
<version>${appengine.sdk.version}</version>
88+
<scope>test</scope>
89+
</dependency>
90+
<dependency>
91+
<groupId>com.google.truth</groupId>
92+
<artifactId>truth</artifactId>
93+
<version>0.28</version>
94+
<scope>test</scope>
95+
</dependency>
96+
7297
</dependencies>
7398

7499
<build>
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* Copyright 2016 Google Inc. All Rights Reserved.
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 http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
package com.example.guestbook;
15+
16+
import static com.example.guestbook.GuestbookTestUtilities.cleanDatastore;
17+
import static org.junit.Assert.assertEquals;
18+
19+
import com.google.appengine.api.datastore.DatastoreService;
20+
import com.google.appengine.api.datastore.DatastoreServiceFactory;
21+
import com.google.appengine.api.datastore.Entity;
22+
import com.google.appengine.api.datastore.KeyFactory;
23+
import com.google.appengine.api.datastore.PreparedQuery;
24+
import com.google.appengine.api.datastore.Query;
25+
import com.google.appengine.tools.development.testing.LocalDatastoreServiceTestConfig;
26+
import com.google.appengine.tools.development.testing.LocalServiceTestHelper;
27+
28+
import com.googlecode.objectify.ObjectifyService;
29+
import com.googlecode.objectify.util.Closeable;
30+
import org.junit.After;
31+
import org.junit.Before;
32+
import org.junit.Test;
33+
import org.junit.runner.RunWith;
34+
import org.junit.runners.JUnit4;
35+
36+
37+
@RunWith(JUnit4.class)
38+
public class GreetingTest {
39+
private static final String TEST_CONTENT = "The world is Blue today";
40+
41+
private final LocalServiceTestHelper helper =
42+
new LocalServiceTestHelper(
43+
// Set no eventual consistency, that way queries return all results.
44+
// https://cloud.google.com/appengine/docs/java/tools/localunittesting#Java_Writing_High_Replication_Datastore_tests
45+
new LocalDatastoreServiceTestConfig()
46+
.setDefaultHighRepJobPolicyUnappliedJobPercentage(0));
47+
48+
private Closeable closeable;
49+
private DatastoreService ds;
50+
51+
@Before
52+
public void setUp() throws Exception {
53+
54+
helper.setUp();
55+
ds = DatastoreServiceFactory.getDatastoreService();
56+
57+
ObjectifyService.register(Guestbook.class);
58+
ObjectifyService.register(Greeting.class);
59+
60+
closeable = ObjectifyService.begin();
61+
62+
cleanDatastore(ds, "default");
63+
}
64+
65+
@After
66+
public void tearDown() {
67+
cleanDatastore(ds, "default");
68+
helper.tearDown();
69+
closeable.close();
70+
}
71+
72+
@Test
73+
public void createSaveObject() throws Exception {
74+
75+
Greeting g = new Greeting("default", TEST_CONTENT);
76+
ObjectifyService.ofy().save().entity(g).now();
77+
78+
Query query = new Query("Greeting")
79+
.setAncestor(new KeyFactory.Builder("Guestbook", "default").getKey());
80+
PreparedQuery pq = ds.prepare(query);
81+
Entity greeting = pq.asSingleEntity(); // Should only be one at this point.
82+
assertEquals(greeting.getProperty("content"), TEST_CONTENT);
83+
}
84+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright 2016 Google Inc. All Rights Reserved.
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 http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
package com.example.guestbook;
15+
16+
import com.google.appengine.api.datastore.DatastoreService;
17+
import com.google.appengine.api.datastore.Entity;
18+
import com.google.appengine.api.datastore.FetchOptions;
19+
import com.google.appengine.api.datastore.Key;
20+
import com.google.appengine.api.datastore.KeyFactory;
21+
import com.google.appengine.api.datastore.PreparedQuery;
22+
import com.google.appengine.api.datastore.Query;
23+
24+
import java.util.ArrayList;
25+
import java.util.List;
26+
27+
public class GuestbookTestUtilities {
28+
29+
public static void cleanDatastore(DatastoreService ds, String book) {
30+
Query query = new Query("Greeting")
31+
.setAncestor(new KeyFactory.Builder("Guestbook", book)
32+
.getKey()).setKeysOnly();
33+
PreparedQuery pq = ds.prepare(query);
34+
List<Entity> entities = pq.asList(FetchOptions.Builder.withDefaults());
35+
ArrayList<Key> keys = new ArrayList<>(entities.size());
36+
37+
for (Entity e : entities) {
38+
keys.add(e.getKey());
39+
}
40+
ds.delete(keys);
41+
}
42+
43+
}
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/*
2+
* Copyright 2015 Google Inc. All Rights Reserved.
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.guestbook;
18+
19+
import static com.example.guestbook.GuestbookTestUtilities.cleanDatastore;
20+
import static org.junit.Assert.assertEquals;
21+
import static org.mockito.Mockito.when;
22+
23+
import com.google.appengine.api.datastore.DatastoreService;
24+
import com.google.appengine.api.datastore.DatastoreServiceFactory;
25+
import com.google.appengine.api.datastore.Entity;
26+
import com.google.appengine.api.datastore.KeyFactory;
27+
import com.google.appengine.api.datastore.PreparedQuery;
28+
import com.google.appengine.api.datastore.Query;
29+
import com.google.appengine.tools.development.testing.LocalDatastoreServiceTestConfig;
30+
import com.google.appengine.tools.development.testing.LocalServiceTestHelper;
31+
32+
import com.googlecode.objectify.ObjectifyService;
33+
import com.googlecode.objectify.util.Closeable;
34+
import org.junit.After;
35+
import org.junit.Before;
36+
import org.junit.Test;
37+
import org.junit.runner.RunWith;
38+
import org.junit.runners.JUnit4;
39+
import org.mockito.Mock;
40+
import org.mockito.MockitoAnnotations;
41+
42+
import java.io.PrintWriter;
43+
import java.io.StringWriter;
44+
45+
import javax.servlet.http.HttpServletRequest;
46+
import javax.servlet.http.HttpServletResponse;
47+
48+
/**
49+
* Unit tests for {@link com.example.guestbook.SignGuestbookServlet}.
50+
*/
51+
@RunWith(JUnit4.class)
52+
public class SignGuestbookServletTest {
53+
private static final String FAKE_URL = "fakey.org/sign";
54+
private static final String FAKE_NAME = "Fake";
55+
56+
private final LocalServiceTestHelper helper =
57+
new LocalServiceTestHelper(
58+
// Set no eventual consistency, that way queries return all results.
59+
// https://cloud.google.com/appengine/docs/java/tools/localunittesting#Java_Writing_High_Replication_Datastore_tests
60+
new LocalDatastoreServiceTestConfig()
61+
.setDefaultHighRepJobPolicyUnappliedJobPercentage(0));
62+
63+
private final String testPhrase = "Noew is the time";
64+
65+
@Mock private HttpServletRequest mockRequest;
66+
67+
@Mock
68+
private HttpServletResponse mockResponse;
69+
70+
private StringWriter stringWriter;
71+
private SignGuestbookServlet servletUnderTest;
72+
private Closeable closeable;
73+
private DatastoreService ds;
74+
75+
@Before
76+
public void setUp() throws Exception {
77+
78+
MockitoAnnotations.initMocks(this);
79+
helper.setUp();
80+
ds = DatastoreServiceFactory.getDatastoreService();
81+
82+
// Set up some fake HTTP requests
83+
when(mockRequest.getRequestURI()).thenReturn(FAKE_URL);
84+
when(mockRequest.getParameter("guestbookName")).thenReturn( "default" );
85+
when(mockRequest.getParameter("content")).thenReturn( testPhrase );
86+
87+
stringWriter = new StringWriter();
88+
when(mockResponse.getWriter()).thenReturn(new PrintWriter(stringWriter));
89+
90+
servletUnderTest = new SignGuestbookServlet();
91+
92+
ObjectifyService.register(Guestbook.class);
93+
ObjectifyService.register(Greeting.class);
94+
95+
closeable = ObjectifyService.begin();
96+
97+
cleanDatastore(ds, "default");
98+
}
99+
100+
@After public void tearDown() {
101+
cleanDatastore(ds, "default");
102+
helper.tearDown();
103+
closeable.close();
104+
}
105+
106+
@Test
107+
public void doPost_userNotLoggedIn() throws Exception {
108+
servletUnderTest.doPost(mockRequest, mockResponse);
109+
110+
Query query = new Query("Greeting")
111+
.setAncestor(new KeyFactory.Builder("Guestbook", "default").getKey());
112+
PreparedQuery pq = ds.prepare(query);
113+
114+
Entity greeting = pq.asSingleEntity(); // Should only be one at this point.
115+
assertEquals(greeting.getProperty("content"), testPhrase);
116+
}
117+
118+
}

0 commit comments

Comments
 (0)