|
19 | 19 | import static junit.framework.TestCase.assertFalse; |
20 | 20 | import static org.junit.Assert.assertEquals; |
21 | 21 | import static org.junit.Assert.assertNotEquals; |
| 22 | +import static org.junit.Assert.assertNull; |
22 | 23 | import static org.junit.Assert.assertSame; |
23 | 24 | import static org.junit.Assert.assertTrue; |
24 | 25 | import static org.junit.Assert.fail; |
|
28 | 29 | import com.google.auth.oauth2.GoogleCredentials; |
29 | 30 | import com.google.cloud.spi.ServiceRpcFactory; |
30 | 31 | import java.io.ByteArrayInputStream; |
| 32 | +import java.io.File; |
31 | 33 | import java.io.IOException; |
32 | 34 | import java.io.InputStream; |
| 35 | +import java.nio.charset.Charset; |
33 | 36 | import java.util.Set; |
34 | 37 | import java.util.regex.Pattern; |
| 38 | + |
| 39 | +import com.google.common.io.Files; |
35 | 40 | import org.junit.Rule; |
36 | 41 | import org.junit.Test; |
37 | 42 | import org.junit.rules.ExpectedException; |
@@ -283,4 +288,29 @@ public void testBaseHashCode() { |
283 | 288 | assertEquals(OPTIONS.hashCode(), OPTIONS_COPY.hashCode()); |
284 | 289 | assertNotEquals(DEFAULT_OPTIONS.hashCode(), OPTIONS.hashCode()); |
285 | 290 | } |
| 291 | + |
| 292 | + @Test |
| 293 | + public void testGetServiceAccountProjectId() throws Exception { |
| 294 | + File credentialsFile = File.createTempFile("credentials", ".json"); |
| 295 | + credentialsFile.deleteOnExit(); |
| 296 | + Files.write("{\"project_id\":\"my-project-id\"}".getBytes(), credentialsFile); |
| 297 | + |
| 298 | + assertEquals("my-project-id", ServiceOptions.getServiceAccountProjectId(credentialsFile.getPath())); |
| 299 | + } |
| 300 | + |
| 301 | + @Test |
| 302 | + public void testGetServiceAccountProjectId_badJson() throws Exception { |
| 303 | + File credentialsFile = File.createTempFile("credentials", ".json"); |
| 304 | + credentialsFile.deleteOnExit(); |
| 305 | + Files.write("asdfghj".getBytes(), credentialsFile); |
| 306 | + |
| 307 | + assertNull(ServiceOptions.getServiceAccountProjectId(credentialsFile.getPath())); |
| 308 | + } |
| 309 | + |
| 310 | + @Test |
| 311 | + public void testGetServiceAccountProjectId_nonExistentFile() throws Exception { |
| 312 | + File credentialsFile = new File("/doesnotexist"); |
| 313 | + |
| 314 | + assertNull(ServiceOptions.getServiceAccountProjectId(credentialsFile.getPath())); |
| 315 | + } |
286 | 316 | } |
0 commit comments