5353import org .junit .rules .ExpectedException ;
5454
5555public class ServiceOptionsTest {
56+ private static GoogleCredentials credentials ;
57+ private static GoogleCredentials credentialsWithProjectId ;
58+ private static GoogleCredentials credentialsWithQuotaProject ;
59+
5660 private static final String JSON_KEY =
5761 "{\n "
5862 + " \" private_key_id\" : \" somekeyid\" ,\n "
@@ -80,16 +84,6 @@ public class ServiceOptionsTest {
8084 + " \" client_id\" : \" someclientid.apps.googleusercontent.com\" ,\n "
8185 + " \" type\" : \" service_account\" \n "
8286 + "}" ;
83- private static GoogleCredentials credentials ;
84-
85- static {
86- try {
87- InputStream keyStream = new ByteArrayInputStream (JSON_KEY .getBytes ());
88- credentials = GoogleCredentials .fromStream (keyStream );
89- } catch (IOException e ) {
90- fail ("Couldn't create fake JSON credentials." );
91- }
92- }
9387
9488 private static final String JSON_KEY_PROJECT_ID =
9589 "{\n "
@@ -119,15 +113,51 @@ public class ServiceOptionsTest {
119113 + " \" client_id\" : \" someclientid.apps.googleusercontent.com\" ,\n "
120114 + " \" type\" : \" service_account\" \n "
121115 + "}" ;
122- private static GoogleCredentials credentialsWithProjectId ;
116+
117+ private static final String JSON_KEY_QUOTA_PROJECT_ID =
118+ "{\n "
119+ + " \" private_key_id\" : \" somekeyid\" ,\n "
120+ + " \" private_key\" : \" -----BEGIN PRIVATE KEY-----\\ nMIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggS"
121+ + "kAgEAAoIBAQC+K2hSuFpAdrJI\\ nnCgcDz2M7t7bjdlsadsasad+fvRSW6TjNQZ3p5LLQY1kSZRqBqylRkzteMOyHg"
122+ + "aR\\ n0Pmxh3ILCND5men43j3h4eDbrhQBuxfEMalkG92sL+PNQSETY2tnvXryOvmBRwa/\\ nQP/9dJfIkIDJ9Fw9N4"
123+ + "Bhhhp6mCcRpdQjV38H7JsyJ7lih/oNjECgYAt\\ nknddadwkwewcVxHFhcZJO+XWf6ofLUXpRwiTZakGMn8EE1uVa2"
124+ + "LgczOjwWHGi99MFjxSer5m9\\ n1tCa3/KEGKiS/YL71JvjwX3mb+cewlkcmweBKZHM2JPTk0ZednFSpVZMtycjkbLa"
125+ + "\\ ndYOS8V85AgMBewECggEBAKksaldajfDZDV6nGqbFjMiizAKJolr/M3OQw16K6o3/\\ n0S31xIe3sSlgW0+UbYlF"
126+ + "4U8KifhManD1apVSC3csafaspP4RZUHFhtBywLO9pR5c\\ nr6S5aLp+gPWFyIp1pfXbWGvc5VY/v9x7ya1VEa6rXvL"
127+ + "sKupSeWAW4tMj3eo/64ge\\ nsdaceaLYw52KeBYiT6+vpsnYrEkAHO1fF/LavbLLOFJmFTMxmsNaG0tuiJHgjshB\\ "
128+ + "n82DpMCbXG9YcCgI/DbzuIjsdj2JC1cascSP//3PmefWysucBQe7Jryb6NQtASmnv\\ nCdDw/0jmZTEjpe4S1lxfHp"
129+ + "lAhHFtdgYTvyYtaLZiVVkCgYEA8eVpof2rceecw/I6\\ n5ng1q3Hl2usdWV/4mZMvR0fOemacLLfocX6IYxT1zA1FF"
130+ + "JlbXSRsJMf/Qq39mOR2\\ nSpW+hr4jCoHeRVYLgsbggtrevGmILAlNoqCMpGZ6vDmJpq6ECV9olliDvpPgWOP+\\ nm"
131+ + "YPDreFBGxWvQrADNbRt2dmGsrsCgYEAyUHqB2wvJHFqdmeBsaacewzV8x9WgmeX\\ ngUIi9REwXlGDW0Mz50dxpxcK"
132+ + "CAYn65+7TCnY5O/jmL0VRxU1J2mSWyWTo1C+17L0\\ n3fUqjxL1pkefwecxwecvC+gFFYdJ4CQ/MHHXU81Lwl1iWdF"
133+ + "Cd2UoGddYaOF+KNeM\\ nHC7cmqra+JsCgYEAlUNywzq8nUg7282E+uICfCB0LfwejuymR93CtsFgb7cRd6ak\\ nECR"
134+ + "8FGfCpH8ruWJINllbQfcHVCX47ndLZwqv3oVFKh6pAS/vVI4dpOepP8++7y1u\\ ncoOvtreXCX6XqfrWDtKIvv0vjl"
135+ + "HBhhhp6mCcRpdQjV38H7JsyJ7lih/oNjECgYAt\\ nkndj5uNl5SiuVxHFhcZJO+XWf6ofLUregtevZakGMn8EE1uVa"
136+ + "2AY7eafmoU/nZPT\\ n00YB0TBATdCbn/nBSuKDESkhSg9s2GEKQZG5hBmL5uCMfo09z3SfxZIhJdlerreP\\ nJ7gSi"
137+ + "dI12N+EZxYd4xIJh/HFDgp7RRO87f+WJkofMQKBgGTnClK1VMaCRbJZPriw\\ nEfeFCoOX75MxKwXs6xgrw4W//AYG"
138+ + "GUjDt83lD6AZP6tws7gJ2IwY/qP7+lyhjEqN\\ nHtfPZRGFkGZsdaksdlaksd323423d+15/UvrlRSFPNj1tWQmNKk"
139+ + "XyRDW4IG1Oa2p\\ nrALStNBx5Y9t0/LQnFI4w3aG\\ n-----END PRIVATE KEY-----\\ n\" ,\n "
140+ + " \" project_id\" : \" someprojectid\" ,\n "
141+ + " \" client_email\" : \" someclientid@developer.gserviceaccount.com\" ,\n "
142+ + " \" client_id\" : \" someclientid.apps.googleusercontent.com\" ,\n "
143+ + " \" type\" : \" service_account\" ,\n "
144+ + " \" quota_project_id\" : \" some-quota-project-id\" \n "
145+ + "}" ;
123146
124147 static {
148+ credentials = loadCredentials (JSON_KEY );
149+ credentialsWithProjectId = loadCredentials (JSON_KEY_PROJECT_ID );
150+ credentialsWithQuotaProject = loadCredentials (JSON_KEY_QUOTA_PROJECT_ID );
151+ }
152+
153+ static GoogleCredentials loadCredentials (String credentialFile ) {
125154 try {
126- InputStream keyStream = new ByteArrayInputStream (JSON_KEY_PROJECT_ID .getBytes ());
127- credentialsWithProjectId = GoogleCredentials .fromStream (keyStream );
155+ InputStream keyStream = new ByteArrayInputStream (credentialFile .getBytes ());
156+ return GoogleCredentials .fromStream (keyStream );
128157 } catch (IOException e ) {
129158 fail ("Couldn't create fake JSON credentials." );
130159 }
160+ return null ;
131161 }
132162
133163 private static final ApiClock TEST_CLOCK = new TestClock ();
@@ -138,6 +168,7 @@ public class ServiceOptionsTest {
138168 .setHost ("host" )
139169 .setProjectId ("project-id" )
140170 .setRetrySettings (ServiceOptions .getNoRetrySettings ())
171+ .setQuotaProjectId ("quota-project-id" )
141172 .build ();
142173 private static final TestServiceOptions OPTIONS_NO_CREDENTIALS =
143174 TestServiceOptions .newBuilder ()
@@ -146,6 +177,7 @@ public class ServiceOptionsTest {
146177 .setHost ("host" )
147178 .setProjectId ("project-id" )
148179 .setRetrySettings (ServiceOptions .getNoRetrySettings ())
180+ .setQuotaProjectId ("quota-project-id" )
149181 .build ();
150182 private static final TestServiceOptions DEFAULT_OPTIONS =
151183 TestServiceOptions .newBuilder ().setProjectId ("project-id" ).build ();
@@ -283,6 +315,39 @@ public void testBuilder() {
283315 assertSame (ServiceOptions .getDefaultRetrySettings (), DEFAULT_OPTIONS .getRetrySettings ());
284316 }
285317
318+ @ Test
319+ public void testBuilder_quotaProjectServiceOptionTakesPrecedence () {
320+ TestServiceOptions noCredsWithQuotaProject =
321+ TestServiceOptions .newBuilder ()
322+ .setCredentials (NoCredentials .getInstance ())
323+ .setProjectId ("project-id" )
324+ .setQuotaProjectId ("quota-project-id" )
325+ .build ();
326+ TestServiceOptions quotaProjectCredsWithQuotaProject =
327+ TestServiceOptions .newBuilder ()
328+ .setQuotaProjectId ("quota-project-id" )
329+ .setCredentials (credentialsWithQuotaProject )
330+ .build ();
331+ TestServiceOptions quotaProjectCredsWithQuotaProject2 =
332+ TestServiceOptions .newBuilder ()
333+ .setCredentials (credentialsWithQuotaProject )
334+ .setQuotaProjectId ("quota-project-id" )
335+ .build ();
336+ TestServiceOptions quotaProjectCreds =
337+ TestServiceOptions .newBuilder ().setCredentials (credentialsWithQuotaProject ).build ();
338+ TestServiceOptions none =
339+ TestServiceOptions .newBuilder ()
340+ .setCredentials (NoCredentials .getInstance ())
341+ .setProjectId ("project-id" )
342+ .build ();
343+
344+ assertEquals ("quota-project-id" , noCredsWithQuotaProject .getQuotaProjectId ());
345+ assertEquals ("quota-project-id" , quotaProjectCredsWithQuotaProject .getQuotaProjectId ());
346+ assertEquals ("quota-project-id" , quotaProjectCredsWithQuotaProject2 .getQuotaProjectId ());
347+ assertEquals ("some-quota-project-id" , quotaProjectCreds .getQuotaProjectId ());
348+ assertEquals (null , none .getQuotaProjectId ());
349+ }
350+
286351 @ Test
287352 public void testBuilderNoCredentials () {
288353 assertEquals (NoCredentials .getInstance (), OPTIONS_NO_CREDENTIALS .getCredentials ());
@@ -293,6 +358,7 @@ public void testBuilderNoCredentials() {
293358 assertEquals ("host" , OPTIONS_NO_CREDENTIALS .getHost ());
294359 assertEquals ("project-id" , OPTIONS_NO_CREDENTIALS .getProjectId ());
295360 assertSame (ServiceOptions .getNoRetrySettings (), OPTIONS_NO_CREDENTIALS .getRetrySettings ());
361+ assertEquals ("quota-project-id" , OPTIONS .getQuotaProjectId ());
296362 }
297363
298364 @ Test
@@ -372,7 +438,8 @@ public void testGetServiceAccountProjectId() throws Exception {
372438 Files .write ("{\" project_id\" :\" my-project-id\" }" .getBytes (), credentialsFile );
373439
374440 assertEquals (
375- "my-project-id" , ServiceOptions .getServiceAccountProjectId (credentialsFile .getPath ()));
441+ "my-project-id" ,
442+ ServiceOptions .getValueFromCredentialsFile (credentialsFile .getPath (), "project_id" ));
376443 }
377444
378445 @ Test
@@ -381,14 +448,14 @@ public void testGetServiceAccountProjectId_badJson() throws Exception {
381448 credentialsFile .deleteOnExit ();
382449 Files .write ("asdfghj" .getBytes (), credentialsFile );
383450
384- assertNull (ServiceOptions .getServiceAccountProjectId (credentialsFile .getPath ()));
451+ assertNull (ServiceOptions .getValueFromCredentialsFile (credentialsFile .getPath (), "project_id" ));
385452 }
386453
387454 @ Test
388455 public void testGetServiceAccountProjectId_nonExistentFile () throws Exception {
389456 File credentialsFile = new File ("/doesnotexist" );
390457
391- assertNull (ServiceOptions .getServiceAccountProjectId (credentialsFile .getPath ()));
458+ assertNull (ServiceOptions .getValueFromCredentialsFile (credentialsFile .getPath (), "project_id" ));
392459 }
393460
394461 @ Test
0 commit comments