### Question:
LocalFileCredentialFactory implements CredentialFactory { @Override public GoogleCredential getCredential(Collection<String> scopes) throws GeneralSecurityException, IOException { JsonFactory jsonFactory = JacksonFactory.getDefaultInstance(); HttpTransport transport = proxy.getHttpTransport(); if (!isJsonKey) { return credentialHelper.getP12Credential( serviceAccountId, serviceAccountKeyFilePath, transport, jsonFactory, proxy.getHttpRequestInitializer(), scopes); } return credentialHelper.getJsonCredential( serviceAccountKeyFilePath, transport, jsonFactory, proxy.getHttpRequestInitializer(), scopes); } private  LocalFileCredentialFactory(Builder builder); static LocalFileCredentialFactory fromConfiguration(); @Override GoogleCredential getCredential(Collection<String> scopes); static final String SERVICE_ACCOUNT_ID_CONFIG; static final String SERVICE_ACCOUNT_KEY_FILE_CONFIG; }### Answer:
@Test public void testNullServiceAccountIdP12() throws Exception { File tmpfile = temporaryFolder.newFile("testNullServiceAccountIdP12" + SERVICE_ACCOUNT_FILE_P12); thrown.expect(NullPointerException.class); LocalFileCredentialFactory subject = new LocalFileCredentialFactory.Builder() .setServiceAccountKeyFilePath(tmpfile.getAbsolutePath()) .setServiceAccountId(null) .build(); subject.getCredential(Arrays.asList("email")); }
@Test public void testNullServiceAccountIdJson() throws Exception { File tmpfile = temporaryFolder.newFile("testNullServiceAccountIdJson" + SERVICE_ACCOUNT_FILE_JSON); GoogleCredential mockCredential = new MockGoogleCredential.Builder().build(); List<String> scopes = Arrays.asList("profile"); when(mockCredentialHelper.getJsonCredential( eq(tmpfile.toPath()), any(HttpTransport.class), any(JsonFactory.class), any(HttpRequestInitializer.class), eq(scopes))) .thenReturn(mockCredential); LocalFileCredentialFactory subject = new LocalFileCredentialFactory.Builder() .setServiceAccountKeyFilePath(tmpfile.getAbsolutePath()) .build(); subject.getCredential(Arrays.asList("email")); assertEquals(mockCredential, subject.getCredential(scopes)); }
@Test public void testGetCredentialJsonKey() throws Exception { File tmpfile = temporaryFolder.newFile(SERVICE_ACCOUNT_FILE_JSON); GoogleCredential mockCredential = new MockGoogleCredential.Builder().build(); List<String> scopes = Arrays.asList("profile"); when(mockCredentialHelper.getJsonCredential( eq(tmpfile.toPath()), any(HttpTransport.class), any(JsonFactory.class), any(HttpRequestInitializer.class), eq(scopes))) .thenReturn(mockCredential); LocalFileCredentialFactory credentialFactory = new LocalFileCredentialFactory.Builder() .setServiceAccountKeyFilePath(tmpfile.getAbsolutePath()) .setServiceAccountId("ServiceAccountID") .build(); assertEquals(mockCredential, credentialFactory.getCredential(scopes)); }
@Test public void testGetCredentialP12() throws Exception { File tmpfile = temporaryFolder.newFile(SERVICE_ACCOUNT_FILE_P12); GoogleCredential mockCredential = new MockGoogleCredential.Builder().build(); List<String> scopes = Arrays.asList("profile, calendar"); when(mockCredentialHelper.getP12Credential( eq("ServiceAccount"), eq(tmpfile.toPath()), any(HttpTransport.class), any(JsonFactory.class), any(HttpRequestInitializer.class), eq(scopes))) .thenReturn(mockCredential); LocalFileCredentialFactory credentialFactory = new LocalFileCredentialFactory.Builder() .setServiceAccountKeyFilePath(tmpfile.getAbsolutePath()) .setServiceAccountId("ServiceAccount") .build(); assertEquals(mockCredential, credentialFactory.getCredential(scopes)); }