|
42 | 42 | import static org.junit.Assert.assertTrue; |
43 | 43 | import static org.junit.Assert.fail; |
44 | 44 |
|
| 45 | +import com.google.api.client.json.GenericJson; |
45 | 46 | import com.google.api.client.json.JsonFactory; |
46 | 47 | import com.google.api.client.json.gson.GsonFactory; |
47 | 48 | import com.google.api.client.json.webtoken.JsonWebSignature; |
@@ -831,6 +832,48 @@ public void onFailure(Throwable exception) { |
831 | 832 | assertTrue("Should have run onSuccess() callback", success.get()); |
832 | 833 | } |
833 | 834 |
|
| 835 | + @Test |
| 836 | + public void fromJSON_noUniverseDomain() throws IOException { |
| 837 | + GenericJson json = |
| 838 | + writeServiceAccountJson( |
| 839 | + SA_CLIENT_ID, |
| 840 | + SA_CLIENT_EMAIL, |
| 841 | + SA_PRIVATE_KEY_PKCS8, |
| 842 | + "test-project-id", |
| 843 | + SA_PRIVATE_KEY_ID, |
| 844 | + QUOTA_PROJECT, |
| 845 | + null); |
| 846 | + ServiceAccountJwtAccessCredentials credentials = |
| 847 | + ServiceAccountJwtAccessCredentials.fromJson(json, URI.create("default-aud")); |
| 848 | + assertEquals(SA_CLIENT_ID, credentials.getClientId()); |
| 849 | + assertEquals(SA_CLIENT_EMAIL, credentials.getClientEmail()); |
| 850 | + assertEquals( |
| 851 | + OAuth2Utils.privateKeyFromPkcs8(SA_PRIVATE_KEY_PKCS8), credentials.getPrivateKey()); |
| 852 | + assertEquals(QUOTA_PROJECT, credentials.getQuotaProjectId()); |
| 853 | + assertEquals(GOOGLE_DEFAULT_UNIVERSE, credentials.getUniverseDomain()); |
| 854 | + } |
| 855 | + |
| 856 | + @Test |
| 857 | + public void fromJSON_UniverseDomain() throws IOException { |
| 858 | + GenericJson json = |
| 859 | + writeServiceAccountJson( |
| 860 | + SA_CLIENT_ID, |
| 861 | + SA_CLIENT_EMAIL, |
| 862 | + SA_PRIVATE_KEY_PKCS8, |
| 863 | + "test-project-id", |
| 864 | + SA_PRIVATE_KEY_ID, |
| 865 | + QUOTA_PROJECT, |
| 866 | + "example.com"); |
| 867 | + ServiceAccountJwtAccessCredentials credentials = |
| 868 | + ServiceAccountJwtAccessCredentials.fromJson(json, URI.create("default-aud")); |
| 869 | + assertEquals(SA_CLIENT_ID, credentials.getClientId()); |
| 870 | + assertEquals(SA_CLIENT_EMAIL, credentials.getClientEmail()); |
| 871 | + assertEquals( |
| 872 | + OAuth2Utils.privateKeyFromPkcs8(SA_PRIVATE_KEY_PKCS8), credentials.getPrivateKey()); |
| 873 | + assertEquals(QUOTA_PROJECT, credentials.getQuotaProjectId()); |
| 874 | + assertEquals("example.com", credentials.getUniverseDomain()); |
| 875 | + } |
| 876 | + |
834 | 877 | @Test |
835 | 878 | public void getUniverseDomain_defaultUniverse() throws IOException { |
836 | 879 | PrivateKey privateKey = OAuth2Utils.privateKeyFromPkcs8(SA_PRIVATE_KEY_PKCS8); |
@@ -879,4 +922,37 @@ private static void testFromStreamException(InputStream stream, String expectedM |
879 | 922 | assertTrue(expected.getMessage().contains(expectedMessageContent)); |
880 | 923 | } |
881 | 924 | } |
| 925 | + private GenericJson writeServiceAccountJson( |
| 926 | + String clientId, |
| 927 | + String clientEmail, |
| 928 | + String privateKeyPkcs8, |
| 929 | + String privateKeyId, |
| 930 | + String projectId, |
| 931 | + String quotaProjectId, |
| 932 | + String universeDomain) { |
| 933 | + GenericJson json = new GenericJson(); |
| 934 | + if (clientId != null) { |
| 935 | + json.put("client_id", clientId); |
| 936 | + } |
| 937 | + if (clientEmail != null) { |
| 938 | + json.put("client_email", clientEmail); |
| 939 | + } |
| 940 | + if (privateKeyPkcs8 != null) { |
| 941 | + json.put("private_key", privateKeyPkcs8); |
| 942 | + } |
| 943 | + if (privateKeyId != null) { |
| 944 | + json.put("private_key_id", privateKeyId); |
| 945 | + } |
| 946 | + if (projectId != null) { |
| 947 | + json.put("project_id", projectId); |
| 948 | + } |
| 949 | + if (quotaProjectId != null) { |
| 950 | + json.put("quota_project_id", quotaProjectId); |
| 951 | + } |
| 952 | + if (universeDomain != null) { |
| 953 | + json.put("universe_domain", universeDomain); |
| 954 | + } |
| 955 | + json.put("type", GoogleCredentials.SERVICE_ACCOUNT_FILE_TYPE); |
| 956 | + return json; |
| 957 | + } |
882 | 958 | } |
0 commit comments