Skip to content

Commit b9ffe41

Browse files
authored
Merge pull request GoogleCloudPlatform#377 from GoogleCloudPlatform/firebase-rest
Add examples for direct firebase calls.
2 parents be5bc08 + 90360d1 commit b9ffe41

File tree

2 files changed

+314
-0
lines changed

2 files changed

+314
-0
lines changed

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

+60
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
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,254 @@
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.spy;
21+
import static org.mockito.Mockito.times;
22+
import static org.mockito.Mockito.verify;
23+
24+
import com.google.api.client.http.LowLevelHttpRequest;
25+
import com.google.api.client.http.LowLevelHttpResponse;
26+
import com.google.api.client.testing.http.MockHttpTransport;
27+
import com.google.api.client.testing.http.MockLowLevelHttpRequest;
28+
import com.google.api.client.testing.http.MockLowLevelHttpResponse;
29+
import com.google.appengine.tools.development.testing.LocalAppIdentityServiceTestConfig;
30+
import com.google.appengine.tools.development.testing.LocalServiceTestHelper;
31+
import org.junit.After;
32+
import org.junit.Before;
33+
import org.junit.BeforeClass;
34+
import org.junit.Test;
35+
import org.junit.runner.RunWith;
36+
import org.junit.runners.JUnit4;
37+
import org.mockito.MockitoAnnotations;
38+
39+
import java.io.ByteArrayInputStream;
40+
import java.io.IOException;
41+
42+
/**
43+
* Unit tests for {@link FirebaseChannel}.
44+
*/
45+
@RunWith(JUnit4.class)
46+
public class FirebaseChannelTest {
47+
private static final String FIREBASE_DB_URL = "http://firebase.com/dburl";
48+
private final LocalServiceTestHelper helper =
49+
new LocalServiceTestHelper(new LocalAppIdentityServiceTestConfig());
50+
51+
private static FirebaseChannel firebaseChannel;
52+
53+
@BeforeClass
54+
public static void setUpBeforeClass() {
55+
// Mock out the firebase config
56+
FirebaseChannel.firebaseConfigStream = new ByteArrayInputStream(
57+
String.format("databaseURL: \"%s\"", FIREBASE_DB_URL).getBytes());
58+
59+
firebaseChannel = FirebaseChannel.getInstance();
60+
}
61+
62+
@Before
63+
public void setUp() throws Exception {
64+
MockitoAnnotations.initMocks(this);
65+
helper.setUp();
66+
}
67+
68+
@After
69+
public void tearDown() {
70+
helper.tearDown();
71+
}
72+
73+
@Test
74+
public void sendFirebaseMessage_create() throws Exception {
75+
// Mock out the firebase response. See
76+
// http://g.co/dv/api-client-library/java/google-http-java-client/unit-testing
77+
MockHttpTransport mockHttpTransport = spy(new MockHttpTransport() {
78+
@Override
79+
public LowLevelHttpRequest buildRequest(String method, String url) throws IOException {
80+
return new MockLowLevelHttpRequest() {
81+
@Override
82+
public LowLevelHttpResponse execute() throws IOException {
83+
MockLowLevelHttpResponse response = new MockLowLevelHttpResponse();
84+
response.setStatusCode(200);
85+
return response;
86+
}
87+
};
88+
}
89+
});
90+
FirebaseChannel.getInstance().httpTransport = mockHttpTransport;
91+
92+
firebaseChannel.sendFirebaseMessage("my_key", new Game());
93+
94+
verify(mockHttpTransport, times(1)).buildRequest(
95+
"PATCH", FIREBASE_DB_URL + "/channels/my_key.json");
96+
}
97+
98+
@Test
99+
public void sendFirebaseMessage_delete() throws Exception {
100+
// Mock out the firebase response. See
101+
// http://g.co/dv/api-client-library/java/google-http-java-client/unit-testing
102+
MockHttpTransport mockHttpTransport = spy(new MockHttpTransport() {
103+
@Override
104+
public LowLevelHttpRequest buildRequest(String method, String url) throws IOException {
105+
return new MockLowLevelHttpRequest() {
106+
@Override
107+
public LowLevelHttpResponse execute() throws IOException {
108+
MockLowLevelHttpResponse response = new MockLowLevelHttpResponse();
109+
response.setStatusCode(200);
110+
return response;
111+
}
112+
};
113+
}
114+
});
115+
FirebaseChannel.getInstance().httpTransport = mockHttpTransport;
116+
117+
firebaseChannel.sendFirebaseMessage("my_key", null);
118+
119+
verify(mockHttpTransport, times(1)).buildRequest(
120+
"DELETE", FIREBASE_DB_URL + "/channels/my_key.json");
121+
}
122+
123+
@Test
124+
public void createFirebaseToken() throws Exception {
125+
Game game = new Game();
126+
127+
String jwt = firebaseChannel.createFirebaseToken(game, "userId");
128+
129+
assertThat(jwt).matches("^([\\w+/=-]+\\.){2}[\\w+/=-]+$");
130+
}
131+
132+
@Test
133+
public void firebasePut() throws Exception {
134+
// Mock out the firebase response. See
135+
// http://g.co/dv/api-client-library/java/google-http-java-client/unit-testing
136+
MockHttpTransport mockHttpTransport = spy(new MockHttpTransport() {
137+
@Override
138+
public LowLevelHttpRequest buildRequest(String method, String url) throws IOException {
139+
return new MockLowLevelHttpRequest() {
140+
@Override
141+
public LowLevelHttpResponse execute() throws IOException {
142+
MockLowLevelHttpResponse response = new MockLowLevelHttpResponse();
143+
response.setStatusCode(200);
144+
return response;
145+
}
146+
};
147+
}
148+
});
149+
FirebaseChannel.getInstance().httpTransport = mockHttpTransport;
150+
Game game = new Game();
151+
152+
firebaseChannel.firebasePut(FIREBASE_DB_URL + "/my/path", game);
153+
154+
verify(mockHttpTransport, times(1)).buildRequest("PUT", FIREBASE_DB_URL + "/my/path");
155+
}
156+
157+
@Test
158+
public void firebasePatch() throws Exception {
159+
// Mock out the firebase response. See
160+
// http://g.co/dv/api-client-library/java/google-http-java-client/unit-testing
161+
MockHttpTransport mockHttpTransport = spy(new MockHttpTransport() {
162+
@Override
163+
public LowLevelHttpRequest buildRequest(String method, String url) throws IOException {
164+
return new MockLowLevelHttpRequest() {
165+
@Override
166+
public LowLevelHttpResponse execute() throws IOException {
167+
MockLowLevelHttpResponse response = new MockLowLevelHttpResponse();
168+
response.setStatusCode(200);
169+
return response;
170+
}
171+
};
172+
}
173+
});
174+
FirebaseChannel.getInstance().httpTransport = mockHttpTransport;
175+
Game game = new Game();
176+
177+
firebaseChannel.firebasePatch(FIREBASE_DB_URL + "/my/path", game);
178+
179+
verify(mockHttpTransport, times(1)).buildRequest("PATCH", FIREBASE_DB_URL + "/my/path");
180+
}
181+
182+
@Test
183+
public void firebasePost() throws Exception {
184+
// Mock out the firebase response. See
185+
// http://g.co/dv/api-client-library/java/google-http-java-client/unit-testing
186+
MockHttpTransport mockHttpTransport = spy(new MockHttpTransport() {
187+
@Override
188+
public LowLevelHttpRequest buildRequest(String method, String url) throws IOException {
189+
return new MockLowLevelHttpRequest() {
190+
@Override
191+
public LowLevelHttpResponse execute() throws IOException {
192+
MockLowLevelHttpResponse response = new MockLowLevelHttpResponse();
193+
response.setStatusCode(200);
194+
return response;
195+
}
196+
};
197+
}
198+
});
199+
FirebaseChannel.getInstance().httpTransport = mockHttpTransport;
200+
Game game = new Game();
201+
202+
firebaseChannel.firebasePost(FIREBASE_DB_URL + "/my/path", game);
203+
204+
verify(mockHttpTransport, times(1)).buildRequest("POST", FIREBASE_DB_URL + "/my/path");
205+
}
206+
207+
@Test
208+
public void firebaseGet() throws Exception {
209+
// Mock out the firebase response. See
210+
// http://g.co/dv/api-client-library/java/google-http-java-client/unit-testing
211+
MockHttpTransport mockHttpTransport = spy(new MockHttpTransport() {
212+
@Override
213+
public LowLevelHttpRequest buildRequest(String method, String url) throws IOException {
214+
return new MockLowLevelHttpRequest() {
215+
@Override
216+
public LowLevelHttpResponse execute() throws IOException {
217+
MockLowLevelHttpResponse response = new MockLowLevelHttpResponse();
218+
response.setStatusCode(200);
219+
return response;
220+
}
221+
};
222+
}
223+
});
224+
FirebaseChannel.getInstance().httpTransport = mockHttpTransport;
225+
226+
firebaseChannel.firebaseGet(FIREBASE_DB_URL + "/my/path");
227+
228+
verify(mockHttpTransport, times(1)).buildRequest("GET", FIREBASE_DB_URL + "/my/path");
229+
}
230+
231+
@Test
232+
public void firebaseDelete() 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.firebaseDelete(FIREBASE_DB_URL + "/my/path");
251+
252+
verify(mockHttpTransport, times(1)).buildRequest("DELETE", FIREBASE_DB_URL + "/my/path");
253+
}
254+
}

0 commit comments

Comments
 (0)