Skip to content

Commit 5707ae1

Browse files
author
Jerjou Cheng
committed
Add examples for direct firebase calls.
1 parent be5bc08 commit 5707ae1

File tree

2 files changed

+340
-0
lines changed

2 files changed

+340
-0
lines changed

appengine/firebase-tictactoe/src/main/java/com/example/appengine/firetactoe/FirebaseChannel.java

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.example.appengine.firetactoe;
1818

1919
import com.google.api.client.extensions.appengine.http.UrlFetchTransport;
20+
import com.google.api.client.auth.oauth2.Credential;
2021
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
2122
import com.google.api.client.http.ByteArrayContent;
2223
import com.google.api.client.http.GenericUrl;
@@ -172,4 +173,63 @@ public String createFirebaseToken(Game game, String userId) {
172173
AppIdentityService.SigningResult result = appIdentity.signForApp(toSign.getBytes());
173174
return String.format("%s.%s", toSign, base64.encode(result.getSignature()));
174175
}
176+
177+
// The following methods are to illustrate making various calls to Firebase from App Engine
178+
// Standard
179+
180+
public HttpResponse firebasePut(String path, Object object) throws IOException {
181+
// Make requests auth'ed using Application Default Credentials
182+
Credential credential = GoogleCredential.getApplicationDefault().createScoped(FIREBASE_SCOPES);
183+
HttpRequestFactory requestFactory = httpTransport.createRequestFactory(credential);
184+
185+
String json = new Gson().toJson(object);
186+
GenericUrl url = new GenericUrl(path);
187+
188+
return requestFactory.buildPutRequest(
189+
url, new ByteArrayContent("application/json", json.getBytes())).execute();
190+
}
191+
192+
public HttpResponse firebasePatch(String path, Object object) throws IOException {
193+
// Make requests auth'ed using Application Default Credentials
194+
Credential credential = GoogleCredential.getApplicationDefault().createScoped(FIREBASE_SCOPES);
195+
HttpRequestFactory requestFactory = httpTransport.createRequestFactory(credential);
196+
197+
String json = new Gson().toJson(object);
198+
GenericUrl url = new GenericUrl(path);
199+
200+
return requestFactory.buildPatchRequest(
201+
url, new ByteArrayContent("application/json", json.getBytes())).execute();
202+
}
203+
204+
public HttpResponse firebasePost(String path, Object object) throws IOException {
205+
// Make requests auth'ed using Application Default Credentials
206+
Credential credential = GoogleCredential.getApplicationDefault().createScoped(FIREBASE_SCOPES);
207+
HttpRequestFactory requestFactory = httpTransport.createRequestFactory(credential);
208+
209+
String json = new Gson().toJson(object);
210+
GenericUrl url = new GenericUrl(path);
211+
212+
return requestFactory.buildPostRequest(
213+
url, new ByteArrayContent("application/json", json.getBytes())).execute();
214+
}
215+
216+
public HttpResponse firebaseGet(String path) throws IOException {
217+
// Make requests auth'ed using Application Default Credentials
218+
Credential credential = GoogleCredential.getApplicationDefault().createScoped(FIREBASE_SCOPES);
219+
HttpRequestFactory requestFactory = httpTransport.createRequestFactory(credential);
220+
221+
GenericUrl url = new GenericUrl(path);
222+
223+
return requestFactory.buildGetRequest(url).execute();
224+
}
225+
226+
public HttpResponse firebaseDelete(String path) throws IOException {
227+
// Make requests auth'ed using Application Default Credentials
228+
Credential credential = GoogleCredential.getApplicationDefault().createScoped(FIREBASE_SCOPES);
229+
HttpRequestFactory requestFactory = httpTransport.createRequestFactory(credential);
230+
231+
GenericUrl url = new GenericUrl(path);
232+
233+
return requestFactory.buildDeleteRequest(url).execute();
234+
}
175235
}
Lines changed: 280 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,280 @@
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
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.appengine.firetactoe;
18+
19+
import static com.google.common.truth.Truth.assertThat;
20+
import static org.mockito.Mockito.anyString;
21+
import static org.mockito.Mockito.eq;
22+
import static org.mockito.Mockito.spy;
23+
import static org.mockito.Mockito.times;
24+
import static org.mockito.Mockito.verify;
25+
import static org.mockito.Mockito.when;
26+
27+
import com.google.api.client.http.LowLevelHttpRequest;
28+
import com.google.api.client.http.LowLevelHttpResponse;
29+
import com.google.api.client.testing.http.MockHttpTransport;
30+
import com.google.api.client.testing.http.MockLowLevelHttpRequest;
31+
import com.google.api.client.testing.http.MockLowLevelHttpResponse;
32+
import com.google.appengine.tools.development.testing.LocalDatastoreServiceTestConfig;
33+
import com.google.appengine.tools.development.testing.LocalServiceTestHelper;
34+
import com.google.appengine.tools.development.testing.LocalURLFetchServiceTestConfig;
35+
import com.google.appengine.tools.development.testing.LocalUserServiceTestConfig;
36+
import com.google.common.collect.ImmutableMap;
37+
import com.googlecode.objectify.Objectify;
38+
import com.googlecode.objectify.ObjectifyFactory;
39+
import com.googlecode.objectify.ObjectifyService;
40+
import com.googlecode.objectify.util.Closeable;
41+
import org.junit.After;
42+
import org.junit.Before;
43+
import org.junit.BeforeClass;
44+
import org.junit.Test;
45+
import org.junit.runner.RunWith;
46+
import org.junit.runners.JUnit4;
47+
import org.mockito.Matchers;
48+
import org.mockito.Mock;
49+
import org.mockito.MockitoAnnotations;
50+
51+
import java.io.ByteArrayInputStream;
52+
import java.io.IOException;
53+
import java.lang.StringBuffer;
54+
import java.util.HashMap;
55+
import javax.servlet.RequestDispatcher;
56+
import javax.servlet.http.HttpServletRequest;
57+
import javax.servlet.http.HttpServletResponse;
58+
import com.google.appengine.tools.development.testing.LocalAppIdentityServiceTestConfig;
59+
60+
/**
61+
* Unit tests for {@link FirebaseChannel}.
62+
*/
63+
@RunWith(JUnit4.class)
64+
public class FirebaseChannelTest {
65+
private static final String FIREBASE_DB_URL = "http://firebase.com/dburl";
66+
private final LocalServiceTestHelper helper =
67+
new LocalServiceTestHelper(new LocalAppIdentityServiceTestConfig());
68+
69+
private FirebaseChannel firebaseChannel;
70+
71+
@BeforeClass
72+
public static void setUpBeforeClass() {
73+
// Reset the Factory so that all translators work properly.
74+
ObjectifyService.setFactory(new ObjectifyFactory());
75+
ObjectifyService.register(Game.class);
76+
// Mock out the firebase config
77+
FirebaseChannel.firebaseConfigStream = new ByteArrayInputStream(
78+
String.format("databaseURL: \"%s\"", FIREBASE_DB_URL).getBytes());
79+
}
80+
81+
@Before
82+
public void setUp() throws Exception {
83+
MockitoAnnotations.initMocks(this);
84+
helper.setUp();
85+
firebaseChannel = FirebaseChannel.getInstance();
86+
}
87+
88+
@After
89+
public void tearDown() {
90+
helper.tearDown();
91+
}
92+
93+
@Test
94+
public void sendFirebaseMessage_create() throws Exception {
95+
// Mock out the firebase response. See
96+
// http://g.co/dv/api-client-library/java/google-http-java-client/unit-testing
97+
MockHttpTransport mockHttpTransport = spy(new MockHttpTransport() {
98+
@Override
99+
public LowLevelHttpRequest buildRequest(String method, String url) throws IOException {
100+
return new MockLowLevelHttpRequest() {
101+
@Override
102+
public LowLevelHttpResponse execute() throws IOException {
103+
MockLowLevelHttpResponse response = new MockLowLevelHttpResponse();
104+
response.setStatusCode(200);
105+
return response;
106+
}
107+
};
108+
}
109+
});
110+
FirebaseChannel.getInstance().httpTransport = mockHttpTransport;
111+
112+
firebaseChannel.sendFirebaseMessage("my_key", new Game("userX", "userO", "board", true));
113+
114+
verify(mockHttpTransport, times(1)).buildRequest(
115+
"PATCH", FIREBASE_DB_URL + "/channels/my_key.json");
116+
}
117+
118+
@Test
119+
public void sendFirebaseMessage_delete() throws Exception {
120+
// Mock out the firebase response. See
121+
// http://g.co/dv/api-client-library/java/google-http-java-client/unit-testing
122+
MockHttpTransport mockHttpTransport = spy(new MockHttpTransport() {
123+
@Override
124+
public LowLevelHttpRequest buildRequest(String method, String url) throws IOException {
125+
return new MockLowLevelHttpRequest() {
126+
@Override
127+
public LowLevelHttpResponse execute() throws IOException {
128+
MockLowLevelHttpResponse response = new MockLowLevelHttpResponse();
129+
response.setStatusCode(200);
130+
return response;
131+
}
132+
};
133+
}
134+
});
135+
FirebaseChannel.getInstance().httpTransport = mockHttpTransport;
136+
137+
firebaseChannel.sendFirebaseMessage("my_key", null);
138+
139+
verify(mockHttpTransport, times(1)).buildRequest(
140+
"DELETE", FIREBASE_DB_URL + "/channels/my_key.json");
141+
}
142+
143+
@Test
144+
public void createFirebaseToken() throws Exception {
145+
Game game = new Game();
146+
game.setId("myid");
147+
148+
String jwt = firebaseChannel.createFirebaseToken(game, "userId");
149+
150+
assertThat(jwt).matches("^([\\w+/=-]+\\.){2}[\\w+/=-]+$");
151+
}
152+
153+
@Test
154+
public void firebasePut() throws Exception {
155+
// Mock out the firebase response. See
156+
// http://g.co/dv/api-client-library/java/google-http-java-client/unit-testing
157+
MockHttpTransport mockHttpTransport = spy(new MockHttpTransport() {
158+
@Override
159+
public LowLevelHttpRequest buildRequest(String method, String url) throws IOException {
160+
return new MockLowLevelHttpRequest() {
161+
@Override
162+
public LowLevelHttpResponse execute() throws IOException {
163+
MockLowLevelHttpResponse response = new MockLowLevelHttpResponse();
164+
response.setStatusCode(200);
165+
return response;
166+
}
167+
};
168+
}
169+
});
170+
FirebaseChannel.getInstance().httpTransport = mockHttpTransport;
171+
Game game = new Game("userX", "userO", "board", true);
172+
game.setId("myid");
173+
174+
firebaseChannel.firebasePut(FIREBASE_DB_URL + "/my/path", game);
175+
176+
verify(mockHttpTransport, times(1)).buildRequest("PUT", FIREBASE_DB_URL + "/my/path");
177+
}
178+
179+
@Test
180+
public void firebasePatch() throws Exception {
181+
// Mock out the firebase response. See
182+
// http://g.co/dv/api-client-library/java/google-http-java-client/unit-testing
183+
MockHttpTransport mockHttpTransport = spy(new MockHttpTransport() {
184+
@Override
185+
public LowLevelHttpRequest buildRequest(String method, String url) throws IOException {
186+
return new MockLowLevelHttpRequest() {
187+
@Override
188+
public LowLevelHttpResponse execute() throws IOException {
189+
MockLowLevelHttpResponse response = new MockLowLevelHttpResponse();
190+
response.setStatusCode(200);
191+
return response;
192+
}
193+
};
194+
}
195+
});
196+
FirebaseChannel.getInstance().httpTransport = mockHttpTransport;
197+
Game game = new Game("userX", "userO", "board", true);
198+
game.setId("myid");
199+
200+
firebaseChannel.firebasePatch(FIREBASE_DB_URL + "/my/path", game);
201+
202+
verify(mockHttpTransport, times(1)).buildRequest("PATCH", FIREBASE_DB_URL + "/my/path");
203+
}
204+
205+
@Test
206+
public void firebasePost() throws Exception {
207+
// Mock out the firebase response. See
208+
// http://g.co/dv/api-client-library/java/google-http-java-client/unit-testing
209+
MockHttpTransport mockHttpTransport = spy(new MockHttpTransport() {
210+
@Override
211+
public LowLevelHttpRequest buildRequest(String method, String url) throws IOException {
212+
return new MockLowLevelHttpRequest() {
213+
@Override
214+
public LowLevelHttpResponse execute() throws IOException {
215+
MockLowLevelHttpResponse response = new MockLowLevelHttpResponse();
216+
response.setStatusCode(200);
217+
return response;
218+
}
219+
};
220+
}
221+
});
222+
FirebaseChannel.getInstance().httpTransport = mockHttpTransport;
223+
Game game = new Game("userX", "userO", "board", true);
224+
game.setId("myid");
225+
226+
firebaseChannel.firebasePost(FIREBASE_DB_URL + "/my/path", game);
227+
228+
verify(mockHttpTransport, times(1)).buildRequest("POST", FIREBASE_DB_URL + "/my/path");
229+
}
230+
231+
@Test
232+
public void firebaseGet() throws Exception {
233+
// Mock out the firebase response. See
234+
// http://g.co/dv/api-client-library/java/google-http-java-client/unit-testing
235+
MockHttpTransport mockHttpTransport = spy(new MockHttpTransport() {
236+
@Override
237+
public LowLevelHttpRequest buildRequest(String method, String url) throws IOException {
238+
return new MockLowLevelHttpRequest() {
239+
@Override
240+
public LowLevelHttpResponse execute() throws IOException {
241+
MockLowLevelHttpResponse response = new MockLowLevelHttpResponse();
242+
response.setStatusCode(200);
243+
return response;
244+
}
245+
};
246+
}
247+
});
248+
FirebaseChannel.getInstance().httpTransport = mockHttpTransport;
249+
250+
firebaseChannel.firebaseGet(FIREBASE_DB_URL + "/my/path");
251+
252+
verify(mockHttpTransport, times(1)).buildRequest("GET", FIREBASE_DB_URL + "/my/path");
253+
}
254+
255+
@Test
256+
public void firebaseDelete() throws Exception {
257+
// Mock out the firebase response. See
258+
// http://g.co/dv/api-client-library/java/google-http-java-client/unit-testing
259+
MockHttpTransport mockHttpTransport = spy(new MockHttpTransport() {
260+
@Override
261+
public LowLevelHttpRequest buildRequest(String method, String url) throws IOException {
262+
return new MockLowLevelHttpRequest() {
263+
@Override
264+
public LowLevelHttpResponse execute() throws IOException {
265+
MockLowLevelHttpResponse response = new MockLowLevelHttpResponse();
266+
response.setStatusCode(200);
267+
return response;
268+
}
269+
};
270+
}
271+
});
272+
FirebaseChannel.getInstance().httpTransport = mockHttpTransport;
273+
Game game = new Game("userX", "userO", "board", true);
274+
game.setId("myid");
275+
276+
firebaseChannel.firebaseDelete(FIREBASE_DB_URL + "/my/path");
277+
278+
verify(mockHttpTransport, times(1)).buildRequest("DELETE", FIREBASE_DB_URL + "/my/path");
279+
}
280+
}

0 commit comments

Comments
 (0)