Skip to content

Commit 30a0e5b

Browse files
authored
Load integration test credentials from well-known files (firebase#84)
* Load integration test credentials from well-known files * Updated documentation * Updated documentation * Updated error messages
1 parent 5090d39 commit 30a0e5b

3 files changed

Lines changed: 39 additions & 29 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@ target/
66
.project
77
.checkstyle
88
release.properties
9+
integration_cert.json
10+
integration_apikey.txt

CONTRIBUTING.md

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -130,21 +130,20 @@ Integration tests are also written using Junit4. They coexist with the unit test
130130
subdirectory. Integration tests follow the naming convention `*IT.java` (e.g. `DataTestIT.java`),
131131
which enables the Maven Surefire and Failsafe plugins to differentiate between the two types of
132132
tests. Integration tests are executed against a real life Firebase project, and therefore
133-
requires an Internet connection. Create a new project in the
134-
[Firebase console](https://console.firebase.google.com/) if you do not already have one. Use a
135-
separate, dedicated project for integration tests since the test suite makes a large number of
136-
writes to the Firebase realtime database. Download the service account private key from the
137-
"Settings > Service Accounts" page of the project. Also obtain the web API key of the project
138-
from the "Settings > General" page. Now run the following command to invoke the integration
139-
test suite:
133+
requires an Internet connection.
134+
135+
Create a new project in the [Firebase console](https://console.firebase.google.com/) if you do
136+
not already have one. Use a separate, dedicated project for integration tests since the test suite
137+
makes a large number of writes to the Firebase realtime database. Download the service account
138+
private key from the "Settings > Service Accounts" page of the project, and save it as
139+
`integration_cert.json` at the root of the codebase. Also obtain the web API key of the project
140+
from the "Settings > General" page, and save it as `integration_apikey.txt` at the root of the
141+
codebase. Now run the following command to invoke the integration test suite:
140142

141143
```
142-
mvn verify -Dfirebase.it.certificate=path/to/serviceAccount.json -Dfirebase.it.apikey=API-Key
144+
mvn verify
143145
```
144146

145-
Make sure to specify the correct path to your downloaded service account key file as the
146-
`firebase.it.certificate` system property.
147-
148147
The above command invokes both unit and integration test suites. To execute only the integration
149148
tests, specify the `-DskipUTs` flag.
150149

src/test/java/com/google/firebase/testing/IntegrationTestUtils.java

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616

1717
package com.google.firebase.testing;
1818

19-
import static com.google.common.base.Preconditions.checkArgument;
2019
import static com.google.common.base.Preconditions.checkNotNull;
2120

22-
import com.google.common.base.Strings;
21+
import com.google.api.client.googleapis.util.Utils;
22+
import com.google.api.client.json.GenericJson;
2323
import com.google.common.collect.ImmutableList;
2424
import com.google.common.io.CharStreams;
2525
import com.google.firebase.FirebaseApp;
@@ -40,26 +40,28 @@
4040
import org.apache.http.entity.StringEntity;
4141
import org.apache.http.impl.client.DefaultHttpClient;
4242
import org.apache.http.util.EntityUtils;
43-
import org.json.JSONObject;
4443

4544
public class IntegrationTestUtils {
4645

47-
private static JSONObject IT_SERVICE_ACCOUNT;
46+
private static final String IT_SERVICE_ACCOUNT_PATH = "integration_cert.json";
47+
private static final String IT_API_KEY_PATH = "integration_apikey.txt";
48+
49+
private static GenericJson serviceAccount;
50+
private static String apiKey;
4851
private static FirebaseApp masterApp;
4952

50-
private static synchronized JSONObject ensureServiceAccount() {
51-
if (IT_SERVICE_ACCOUNT == null) {
52-
String certificatePath = System.getProperty("firebase.it.certificate");
53-
checkArgument(!Strings.isNullOrEmpty(certificatePath),
54-
"Service account certificate path not set. Set the "
55-
+ "firebase.it.certificate system property and try again.");
56-
try (InputStreamReader reader = new InputStreamReader(new FileInputStream(certificatePath))) {
57-
IT_SERVICE_ACCOUNT = new JSONObject(CharStreams.toString(reader));
53+
private static synchronized GenericJson ensureServiceAccount() {
54+
if (serviceAccount == null) {
55+
try (InputStream stream = new FileInputStream(IT_SERVICE_ACCOUNT_PATH)) {
56+
serviceAccount = Utils.getDefaultJsonFactory().fromInputStream(stream, GenericJson.class);
5857
} catch (IOException e) {
59-
throw new RuntimeException("Failed to read service account certificate", e);
58+
String msg = String.format("Failed to read service account certificate from %s. "
59+
+ "Integration tests require a service account credential obtained from a Firebase "
60+
+ "project. See CONTRIBUTING.md for more details.", IT_SERVICE_ACCOUNT_PATH);
61+
throw new RuntimeException(msg, e);
6062
}
6163
}
62-
return IT_SERVICE_ACCOUNT;
64+
return serviceAccount;
6365
}
6466

6567
public static InputStream getServiceAccountCertificate() {
@@ -78,10 +80,17 @@ public static String getStorageBucket() {
7880
return getProjectId() + ".appspot.com";
7981
}
8082

81-
public static String getApiKey() {
82-
String apiKey = System.getProperty("firebase.it.apikey");
83-
checkArgument(!Strings.isNullOrEmpty(apiKey), "API key not specified. Set the "
84-
+ "firebase.it.apikey system property and try again.");
83+
public static synchronized String getApiKey() {
84+
if (apiKey == null) {
85+
try (InputStream stream = new FileInputStream(IT_API_KEY_PATH)) {
86+
apiKey = CharStreams.toString(new InputStreamReader(stream)).trim();
87+
} catch (IOException e) {
88+
String msg = String.format("Failed to read API key from %s. "
89+
+ "Integration tests require an API key obtained from a Firebase "
90+
+ "project. See CONTRIBUTING.md for more details.", IT_API_KEY_PATH);
91+
throw new RuntimeException(msg, e);
92+
}
93+
}
8594
return apiKey;
8695
}
8796

0 commit comments

Comments
 (0)