1616
1717package com .google .firebase .testing ;
1818
19- import static com .google .common .base .Preconditions .checkArgument ;
2019import 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 ;
2323import com .google .common .collect .ImmutableList ;
2424import com .google .common .io .CharStreams ;
2525import com .google .firebase .FirebaseApp ;
4040import org .apache .http .entity .StringEntity ;
4141import org .apache .http .impl .client .DefaultHttpClient ;
4242import org .apache .http .util .EntityUtils ;
43- import org .json .JSONObject ;
4443
4544public 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