22
22
import static org .junit .Assert .assertNotEquals ;
23
23
import static org .junit .Assert .assertNull ;
24
24
import static org .junit .Assert .assertSame ;
25
+ import static org .junit .Assert .assertThrows ;
25
26
import static org .junit .Assert .assertTrue ;
26
27
import static org .junit .Assert .fail ;
27
28
@@ -55,6 +56,7 @@ public class ServiceOptionsTest {
55
56
private static GoogleCredentials credentials ;
56
57
private static GoogleCredentials credentialsWithProjectId ;
57
58
private static GoogleCredentials credentialsWithQuotaProject ;
59
+ private static GoogleCredentials credentialsNotInGDU ;
58
60
59
61
private static final String JSON_KEY =
60
62
"{\n "
@@ -81,7 +83,8 @@ public class ServiceOptionsTest {
81
83
+ "XyRDW4IG1Oa2p\\ nrALStNBx5Y9t0/LQnFI4w3aG\\ n-----END PRIVATE KEY-----\\ n\" ,\n "
82
84
+
" \" client_email\" : \" [email protected] \" ,\n "
83
85
+ " \" client_id\" : \" someclientid.apps.googleusercontent.com\" ,\n "
84
- + " \" type\" : \" service_account\" \n "
86
+ + " \" type\" : \" service_account\" ,\n "
87
+ + " \" universe_domain\" : \" googleapis.com\" \n "
85
88
+ "}" ;
86
89
87
90
private static final String JSON_KEY_PROJECT_ID =
@@ -110,7 +113,8 @@ public class ServiceOptionsTest {
110
113
+ " \" project_id\" : \" someprojectid\" ,\n "
111
114
+
" \" client_email\" : \" [email protected] \" ,\n "
112
115
+ " \" client_id\" : \" someclientid.apps.googleusercontent.com\" ,\n "
113
- + " \" type\" : \" service_account\" \n "
116
+ + " \" type\" : \" service_account\" ,\n "
117
+ + " \" universe_domain\" : \" googleapis.com\" \n "
114
118
+ "}" ;
115
119
116
120
private static final String JSON_KEY_QUOTA_PROJECT_ID =
@@ -140,13 +144,45 @@ public class ServiceOptionsTest {
140
144
+
" \" client_email\" : \" [email protected] \" ,\n "
141
145
+ " \" client_id\" : \" someclientid.apps.googleusercontent.com\" ,\n "
142
146
+ " \" type\" : \" service_account\" ,\n "
143
- + " \" quota_project_id\" : \" some-quota-project-id\" \n "
147
+ + " \" quota_project_id\" : \" some-quota-project-id\" ,\n "
148
+ + " \" universe_domain\" : \" googleapis.com\" \n "
149
+ + "}" ;
150
+
151
+ // Key added by copying the keys above and adding in the universe domain field
152
+ private static final String JSON_KEY_NON_GDU =
153
+ "{\n "
154
+ + " \" private_key_id\" : \" somekeyid\" ,\n "
155
+ + " \" private_key\" : \" -----BEGIN PRIVATE KEY-----\\ nMIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggS"
156
+ + "kAgEAAoIBAQC+K2hSuFpAdrJI\\ nnCgcDz2M7t7bjdlsadsasad+fvRSW6TjNQZ3p5LLQY1kSZRqBqylRkzteMOyHg"
157
+ + "aR\\ n0Pmxh3ILCND5men43j3h4eDbrhQBuxfEMalkG92sL+PNQSETY2tnvXryOvmBRwa/\\ nQP/9dJfIkIDJ9Fw9N4"
158
+ + "Bhhhp6mCcRpdQjV38H7JsyJ7lih/oNjECgYAt\\ nknddadwkwewcVxHFhcZJO+XWf6ofLUXpRwiTZakGMn8EE1uVa2"
159
+ + "LgczOjwWHGi99MFjxSer5m9\\ n1tCa3/KEGKiS/YL71JvjwX3mb+cewlkcmweBKZHM2JPTk0ZednFSpVZMtycjkbLa"
160
+ + "\\ ndYOS8V85AgMBewECggEBAKksaldajfDZDV6nGqbFjMiizAKJolr/M3OQw16K6o3/\\ n0S31xIe3sSlgW0+UbYlF"
161
+ + "4U8KifhManD1apVSC3csafaspP4RZUHFhtBywLO9pR5c\\ nr6S5aLp+gPWFyIp1pfXbWGvc5VY/v9x7ya1VEa6rXvL"
162
+ + "sKupSeWAW4tMj3eo/64ge\\ nsdaceaLYw52KeBYiT6+vpsnYrEkAHO1fF/LavbLLOFJmFTMxmsNaG0tuiJHgjshB\\ "
163
+ + "n82DpMCbXG9YcCgI/DbzuIjsdj2JC1cascSP//3PmefWysucBQe7Jryb6NQtASmnv\\ nCdDw/0jmZTEjpe4S1lxfHp"
164
+ + "lAhHFtdgYTvyYtaLZiVVkCgYEA8eVpof2rceecw/I6\\ n5ng1q3Hl2usdWV/4mZMvR0fOemacLLfocX6IYxT1zA1FF"
165
+ + "JlbXSRsJMf/Qq39mOR2\\ nSpW+hr4jCoHeRVYLgsbggtrevGmILAlNoqCMpGZ6vDmJpq6ECV9olliDvpPgWOP+\\ nm"
166
+ + "YPDreFBGxWvQrADNbRt2dmGsrsCgYEAyUHqB2wvJHFqdmeBsaacewzV8x9WgmeX\\ ngUIi9REwXlGDW0Mz50dxpxcK"
167
+ + "CAYn65+7TCnY5O/jmL0VRxU1J2mSWyWTo1C+17L0\\ n3fUqjxL1pkefwecxwecvC+gFFYdJ4CQ/MHHXU81Lwl1iWdF"
168
+ + "Cd2UoGddYaOF+KNeM\\ nHC7cmqra+JsCgYEAlUNywzq8nUg7282E+uICfCB0LfwejuymR93CtsFgb7cRd6ak\\ nECR"
169
+ + "8FGfCpH8ruWJINllbQfcHVCX47ndLZwqv3oVFKh6pAS/vVI4dpOepP8++7y1u\\ ncoOvtreXCX6XqfrWDtKIvv0vjl"
170
+ + "HBhhhp6mCcRpdQjV38H7JsyJ7lih/oNjECgYAt\\ nkndj5uNl5SiuVxHFhcZJO+XWf6ofLUregtevZakGMn8EE1uVa"
171
+ + "2AY7eafmoU/nZPT\\ n00YB0TBATdCbn/nBSuKDESkhSg9s2GEKQZG5hBmL5uCMfo09z3SfxZIhJdlerreP\\ nJ7gSi"
172
+ + "dI12N+EZxYd4xIJh/HFDgp7RRO87f+WJkofMQKBgGTnClK1VMaCRbJZPriw\\ nEfeFCoOX75MxKwXs6xgrw4W//AYG"
173
+ + "GUjDt83lD6AZP6tws7gJ2IwY/qP7+lyhjEqN\\ nHtfPZRGFkGZsdaksdlaksd323423d+15/UvrlRSFPNj1tWQmNKk"
174
+ + "XyRDW4IG1Oa2p\\ nrALStNBx5Y9t0/LQnFI4w3aG\\ n-----END PRIVATE KEY-----\\ n\" ,\n "
175
+ +
" \" client_email\" : \" [email protected] \" ,\n "
176
+ + " \" client_id\" : \" someclientid.apps.googleusercontent.com\" ,\n "
177
+ + " \" type\" : \" service_account\" ,\n "
178
+ + " \" universe_domain\" : \" random.com\" \n "
144
179
+ "}" ;
145
180
146
181
static {
147
182
credentials = loadCredentials (JSON_KEY );
148
183
credentialsWithProjectId = loadCredentials (JSON_KEY_PROJECT_ID );
149
184
credentialsWithQuotaProject = loadCredentials (JSON_KEY_QUOTA_PROJECT_ID );
185
+ credentialsNotInGDU = loadCredentials (JSON_KEY_NON_GDU );
150
186
}
151
187
152
188
static GoogleCredentials loadCredentials (String credentialFile ) {
@@ -471,6 +507,129 @@ public void testResponseHeaderDoesNotContainMetaDataFlavor() throws Exception {
471
507
assertThat (ServiceOptions .headerContainsMetadataFlavor (httpResponse )).isFalse ();
472
508
}
473
509
510
+ @ Test
511
+ public void testGetResolvedEndpoint_noUniverseDomain () {
512
+ TestServiceOptions options = TestServiceOptions .newBuilder ().setProjectId ("project-id" ).build ();
513
+ assertThat (options .getResolvedHost ("service" )).isEqualTo ("https://service.googleapis.com" );
514
+ }
515
+
516
+ @ Test
517
+ public void testGetResolvedEndpoint_emptyUniverseDomain () {
518
+ TestServiceOptions options =
519
+ TestServiceOptions .newBuilder ().setUniverseDomain ("" ).setProjectId ("project-id" ).build ();
520
+ IllegalArgumentException exception =
521
+ assertThrows (IllegalArgumentException .class , () -> options .getResolvedHost ("service" ));
522
+ assertThat (exception .getMessage ()).isEqualTo ("The universe domain cannot be empty" );
523
+ }
524
+
525
+ @ Test
526
+ public void testGetResolvedEndpoint_customUniverseDomain () {
527
+ TestServiceOptions options =
528
+ TestServiceOptions .newBuilder ()
529
+ .setUniverseDomain ("test.com" )
530
+ .setProjectId ("project-id" )
531
+ .build ();
532
+ assertThat (options .getResolvedHost ("service" )).isEqualTo ("https://service.test.com" );
533
+ }
534
+
535
+ @ Test
536
+ public void testGetResolvedEndpoint_customUniverseDomain_customHost () {
537
+ TestServiceOptions options =
538
+ TestServiceOptions .newBuilder ()
539
+ .setUniverseDomain ("test.com" )
540
+ .setHost ("https://service.random.com/" )
541
+ .setProjectId ("project-id" )
542
+ .build ();
543
+ assertThat (options .getResolvedHost ("service" )).isEqualTo ("https://service.random.com/" );
544
+ }
545
+
546
+ @ Test
547
+ public void testGetResolvedApiaryHost_noUniverseDomain () {
548
+ TestServiceOptions options = TestServiceOptions .newBuilder ().setProjectId ("project-id" ).build ();
549
+ assertThat (options .getResolvedApiaryHost ("service" ))
550
+ .isEqualTo ("https://service.googleapis.com/" );
551
+ }
552
+
553
+ @ Test
554
+ public void testGetResolvedApiaryHost_customUniverseDomain_noHost () {
555
+ TestServiceOptions options =
556
+ TestServiceOptions .newBuilder ()
557
+ .setUniverseDomain ("test.com" )
558
+ .setHost (null )
559
+ .setProjectId ("project-id" )
560
+ .build ();
561
+ assertThat (options .getResolvedApiaryHost ("service" )).isEqualTo ("https://service.test.com/" );
562
+ }
563
+
564
+ @ Test
565
+ public void testGetResolvedApiaryHost_customUniverseDomain_customHost () {
566
+ TestServiceOptions options =
567
+ TestServiceOptions .newBuilder ()
568
+ .setUniverseDomain ("test.com" )
569
+ .setHost ("https://service.random.com" )
570
+ .setProjectId ("project-id" )
571
+ .build ();
572
+ assertThat (options .getResolvedApiaryHost ("service" )).isEqualTo ("https://service.test.com/" );
573
+ }
574
+
575
+ // No User Configuration = GDU, Default Credentials = GDU
576
+ @ Test
577
+ public void testIsValidUniverseDomain_noUserUniverseDomainConfig_defaultCredentials ()
578
+ throws IOException {
579
+ TestServiceOptions options =
580
+ TestServiceOptions .newBuilder ()
581
+ .setProjectId ("project-id" )
582
+ .setHost ("https://test.random.com" )
583
+ .setCredentials (credentials )
584
+ .build ();
585
+ assertThat (options .hasValidUniverseDomain ()).isTrue ();
586
+ }
587
+
588
+ // No User Configuration = GDU, non Default Credentials = random.com
589
+ // non-GDU Credentials could be any domain, the tests use random.com
590
+ @ Test
591
+ public void testIsValidUniverseDomain_noUserUniverseDomainConfig_nonGDUCredentials ()
592
+ throws IOException {
593
+ TestServiceOptions options =
594
+ TestServiceOptions .newBuilder ()
595
+ .setProjectId ("project-id" )
596
+ .setHost ("https://test.random.com" )
597
+ .setCredentials (credentialsNotInGDU )
598
+ .build ();
599
+ assertThat (options .hasValidUniverseDomain ()).isFalse ();
600
+ }
601
+
602
+ // User Configuration = random.com, Default Credentials = GDU
603
+ // User Credentials could be set to any domain, the tests use random.com
604
+ @ Test
605
+ public void testIsValidUniverseDomain_userUniverseDomainConfig_defaultCredentials ()
606
+ throws IOException {
607
+ TestServiceOptions options =
608
+ TestServiceOptions .newBuilder ()
609
+ .setProjectId ("project-id" )
610
+ .setHost ("https://test.random.com" )
611
+ .setUniverseDomain ("random.com" )
612
+ .setCredentials (credentials )
613
+ .build ();
614
+ assertThat (options .hasValidUniverseDomain ()).isFalse ();
615
+ }
616
+
617
+ // User Configuration = random.com, non Default Credentials = random.com
618
+ // User Credentials and non GDU Credentials could be set to any domain,
619
+ // the tests use random.com
620
+ @ Test
621
+ public void testIsValidUniverseDomain_userUniverseDomainConfig_nonGDUCredentials ()
622
+ throws IOException {
623
+ TestServiceOptions options =
624
+ TestServiceOptions .newBuilder ()
625
+ .setProjectId ("project-id" )
626
+ .setHost ("https://test.random.com" )
627
+ .setUniverseDomain ("random.com" )
628
+ .setCredentials (credentialsNotInGDU )
629
+ .build ();
630
+ assertThat (options .hasValidUniverseDomain ()).isTrue ();
631
+ }
632
+
474
633
private HttpResponse createHttpResponseWithHeader (final Multimap <String , String > headers )
475
634
throws Exception {
476
635
HttpTransport mockHttpTransport =
0 commit comments