### Question:
CredentialsFactory { public GoogleCredentials getGoogleCredentials(final Map<PropertyDescriptor, String> properties) throws IOException { final CredentialsStrategy primaryStrategy = selectPrimaryStrategy(properties); return primaryStrategy.getGoogleCredentials(properties); } CredentialsFactory(); CredentialsStrategy selectPrimaryStrategy(final Map<PropertyDescriptor, String> properties); CredentialsStrategy selectPrimaryStrategy(final ValidationContext validationContext); Collection<ValidationResult> validate(final ValidationContext validationContext); GoogleCredentials getGoogleCredentials(final Map<PropertyDescriptor, String> properties);  }### Answer:
@Test public void testImplicitApplicationDefaultCredentials() throws Exception { final TestRunner runner = TestRunners.newTestRunner(MockCredentialsFactoryProcessor.class); runner.assertValid(); Map<PropertyDescriptor, String> properties = runner.getProcessContext().getProperties(); final CredentialsFactory factory = new CredentialsFactory(); final GoogleCredentials credentials = factory.getGoogleCredentials(properties); assertNotNull(credentials); }
@Test public void testExplicitApplicationDefaultCredentials() throws Exception { final TestRunner runner = TestRunners.newTestRunner(MockCredentialsFactoryProcessor.class); runner.setProperty(CredentialPropertyDescriptors.USE_APPLICATION_DEFAULT_CREDENTIALS, "true"); runner.assertValid(); Map<PropertyDescriptor, String> properties = runner.getProcessContext().getProperties(); final CredentialsFactory factory = new CredentialsFactory(); final GoogleCredentials credentials = factory.getGoogleCredentials(properties); assertNotNull(credentials); }
@Test public void testJsonFileCredentials() throws Exception { final TestRunner runner = TestRunners.newTestRunner(MockCredentialsFactoryProcessor.class); runner.setProperty(CredentialPropertyDescriptors.SERVICE_ACCOUNT_JSON_FILE, "src/test/resources/mock-gcp-service-account.json"); runner.assertValid(); Map<PropertyDescriptor, String> properties = runner.getProcessContext().getProperties(); final CredentialsFactory factory = new CredentialsFactory(); final GoogleCredentials credentials = factory.getGoogleCredentials(properties); assertNotNull(credentials); assertEquals("credentials class should be equal", ServiceAccountCredentials.class, credentials.getClass()); }
@Test public void testJsonStringCredentials() throws Exception { final String jsonRead = new String( Files.readAllBytes(Paths.get("src/test/resources/mock-gcp-service-account.json")) ); final TestRunner runner = TestRunners.newTestRunner(MockCredentialsFactoryProcessor.class); runner.setProperty(CredentialPropertyDescriptors.SERVICE_ACCOUNT_JSON, jsonRead); runner.assertValid(); Map<PropertyDescriptor, String> properties = runner.getProcessContext().getProperties(); final CredentialsFactory factory = new CredentialsFactory(); final GoogleCredentials credentials = factory.getGoogleCredentials(properties); assertNotNull(credentials); assertEquals("credentials class should be equal", ServiceAccountCredentials.class, credentials.getClass()); }
@Test public void testComputeEngineCredentials() throws Exception { final TestRunner runner = TestRunners.newTestRunner(MockCredentialsFactoryProcessor.class); runner.setProperty(CredentialPropertyDescriptors.USE_COMPUTE_ENGINE_CREDENTIALS, "true"); runner.assertValid(); Map<PropertyDescriptor, String> properties = runner.getProcessContext().getProperties(); final CredentialsFactory factory = new CredentialsFactory(); final GoogleCredentials credentials = factory.getGoogleCredentials(properties); assertNotNull(credentials); assertEquals("credentials class should be equal", ComputeEngineCredentials.class, credentials.getClass()); }