@Override public GoogleClientSecretsForApiBuilder fromFile(File path) throws ConfigurationLoadException { return from(configHelper.fromFile(path), path.getAbsolutePath()); }
/** * Validates the {@code GoogleClientSecrets} object. * @throws ValidationException if the {@code GoogleClientSecrets} did not * validate */ private void validate() throws ValidationException { if (Strings.isNullOrEmpty(clientId) || clientId.startsWith("INSERT_CLIENT_ID_HERE")) { throw new ValidationException(String.format( "Client ID must be set%s\nIf you do not have a client ID or secret, " + "please create one in the API console: https://console.developers.google.com", filePath != null ? generateFilePathWarning("clientId") : "."), "clientId"); } if (Strings.isNullOrEmpty(clientSecret) || clientId.startsWith("INSERT_CLIENT_SECRET_HERE")) { throw new ValidationException(String.format( "Client secret must be set%s\nIf you do not have a client ID or secret, " + "please create one in the API console: https://console.developers.google.com", filePath != null ? generateFilePathWarning("clientSecret") : "."), "clientSecret"); } }
/** * Reads properties from the provided {@link Configuration} object * <br><br> * Understands the following properties suffixes: * <br><br> * <ul> * <li>clientId</li> * <li>clientSecret</li> * </ul><br> * For example, the AdWords OAuth2 client ID can be read from: * <code>api.adwords.clientId</code> * * @param config the configuration * @return Builder populated from the Configuration */ @Override public GoogleClientSecretsForApiBuilder from(Configuration config) { clientId = config.getString(getPropertyKey("clientId"), null); clientSecret = config.getString(getPropertyKey("clientSecret"), null); return this; }
/** * Reads properties from the provided {@link Configuration} object * <br><br> * Understands the following properties suffixes: * <br><br> * <ul> * <li>clientId</li> * <li>clientSecret</li> * </ul><br> * For example, the AdWords OAuth2 client ID can be read from: * <code>api.adwords.clientId</code> * * @param config the configuration * @param filePath the file path of the configuration * @return Builder populated from the Configuration */ private GoogleClientSecretsForApiBuilder from(Configuration config, String filePath) { from(config); this.filePath = filePath; return this; }
/** * Reads properties from the provided {@link Configuration} object * <br><br> * Understands the following properties suffixes: * <br><br> * <ul> * <li>clientId</li> * <li>clientSecret</li> * </ul><br> * For example, the AdWords OAuth2 client ID can be read from: * <code>api.adwords.clientId</code> * * @param config the configuration * @return Builder populated from the Configuration */ @Override public GoogleClientSecretsForApiBuilder from(Configuration config) { clientId = config.getString(getPropertyKey("clientId"), null); clientSecret = config.getString(getPropertyKey("clientSecret"), null); return this; }
/** * Validates the {@code GoogleClientSecrets} object. * @throws ValidationException if the {@code GoogleClientSecrets} did not * validate */ private void validate() throws ValidationException { if (Strings.isNullOrEmpty(clientId) || clientId.startsWith("INSERT_CLIENT_ID_HERE")) { throw new ValidationException(String.format( "Client ID must be set%s\nIf you do not have a client ID or secret, " + "please create one in the API console: https://console.developers.google.com", filePath != null ? generateFilePathWarning("clientId") : "."), "clientId"); } if (Strings.isNullOrEmpty(clientSecret) || clientId.startsWith("INSERT_CLIENT_SECRET_HERE")) { throw new ValidationException(String.format( "Client secret must be set%s\nIf you do not have a client ID or secret, " + "please create one in the API console: https://console.developers.google.com", filePath != null ? generateFilePathWarning("clientSecret") : "."), "clientSecret"); } }
/** * Reads properties from the provided {@link Configuration} object * <br><br> * Understands the following properties suffixes: * <br><br> * <ul> * <li>clientId</li> * <li>clientSecret</li> * </ul><br> * For example, the AdWords OAuth2 client ID can be read from: * <code>api.adwords.clientId</code> * * @param config the configuration * @param filePath the file path of the configuration * @return Builder populated from the Configuration */ private GoogleClientSecretsForApiBuilder from(Configuration config, String filePath) { from(config); this.filePath = filePath; return this; }
/** * Tests that the builder correctly reads properties from a configuration. */ @Test public void testGoogleSecretsReadPropertiesFromConfiguration_properPrefix() throws ValidationException { PropertiesConfiguration config = new PropertiesConfiguration(); config.setProperty("api.admanager.clientId", "clientIdDfp"); config.setProperty("api.admanager.clientSecret", "clientSecretDfp"); config.setProperty("api.adwords.clientId", "clientIdAdWords"); config.setProperty("api.adwords.clientSecret", "clientSecretAdWords"); GoogleClientSecrets googleClientSecrets = new GoogleClientSecretsBuilder() .forApi(GoogleClientSecretsBuilder.Api.AD_MANAGER) .from(config) .build(); assertEquals("clientIdDfp", googleClientSecrets.getInstalled().getClientId()); assertEquals("clientSecretDfp", googleClientSecrets.getInstalled().getClientSecret()); }
/** * Tests that the builder correctly throws exception when client secret is a * default value. */ @Test public void testReadPropertiesFromFile_defaultClientSecret() throws Exception { PropertiesConfiguration config = new PropertiesConfiguration(); config.setProperty("api.admanager.clientId", "clientId"); config.setProperty("api.admanager.clientId", "INSERT_CLIENT_SECRET_HERE"); GoogleClientSecretsForApiBuilder builder = new GoogleClientSecretsForApiBuilder( configurationHelper, GoogleClientSecretsBuilder.Api.AD_MANAGER); thrown.expect(ValidationException.class); thrown.expectMessage("Client secret must be set." + "\nIf you do not have a client ID or secret, please create one in the API " + "console: https://console.developers.google.com"); builder.from(config).build(); } }
/** * Tests that the builder correctly identifies the file path. */ @Test public void testReadPropertiesFromFile_clientSecretTokenBadWithFilePath() throws Exception { PropertiesConfiguration config = new PropertiesConfiguration(); config.setProperty("api.admanager.clientId", "clientId"); config.setProperty("api.admanager.refreshToken", "refreshToken"); when(configurationHelper.fromFile("/home/user/path")).thenReturn(config); GoogleClientSecretsForApiBuilder builder = new GoogleClientSecretsForApiBuilder( configurationHelper, GoogleClientSecretsBuilder.Api.AD_MANAGER); thrown.expect(ValidationException.class); thrown.expectMessage( "Client secret must be set as api.admanager.clientSecret in /home/user/path." + "\nIf you do not have a client ID or secret, please create one in the API " + "console: https://console.developers.google.com"); builder.fromFile("/home/user/path").build(); }
/** * Tests that the builder correctly identifies the file path. */ @Test public void testReadPropertiesFromFile_clientIdTokenBadWithFilePath() throws Exception { PropertiesConfiguration config = new PropertiesConfiguration(); config.setProperty("api.admanager.clientSecret", "clientSecret"); config.setProperty("api.admanager.refreshToken", "refreshToken"); when(configurationHelper.fromFile("/home/user/path")).thenReturn(config); GoogleClientSecretsForApiBuilder builder = new GoogleClientSecretsForApiBuilder( configurationHelper, GoogleClientSecretsBuilder.Api.AD_MANAGER); thrown.expect(ValidationException.class); thrown.expectMessage( "Client ID must be set as api.admanager.clientId in /home/user/path." + "\nIf you do not have a client ID or secret, please create one in the API " + "console: https://console.developers.google.com"); builder.fromFile("/home/user/path").build(); }
/** * Tests that the builder correctly throws exception when client ID is a * default value. */ @Test public void testReadPropertiesFromFile_defaultClientId() throws Exception { PropertiesConfiguration config = new PropertiesConfiguration(); config.setProperty("api.admanager.clientId", "INSERT_CLIENT_ID_HERE"); GoogleClientSecretsForApiBuilder builder = new GoogleClientSecretsForApiBuilder( configurationHelper, GoogleClientSecretsBuilder.Api.AD_MANAGER); thrown.expect(ValidationException.class); thrown.expectMessage("Client ID must be set." + "\nIf you do not have a client ID or secret, please create one in the API " + "console: https://console.developers.google.com"); builder.from(config).build(); }
/** * Tests that the builder correctly leaves out the file path. */ @Test public void testReadPropertiesFromFile_clientSecretNoFilePath() throws Exception { PropertiesConfiguration config = new PropertiesConfiguration(); config.setProperty("api.admanager.clientId", "clientId"); config.setProperty("api.admanager.refreshToken", "refreshToken"); GoogleClientSecretsForApiBuilder builder = new GoogleClientSecretsForApiBuilder( configurationHelper, GoogleClientSecretsBuilder.Api.AD_MANAGER); thrown.expect(ValidationException.class); thrown.expectMessage("Client secret must be set." + "\nIf you do not have a client ID or secret, please create one in the API " + "console: https://console.developers.google.com"); builder.from(config).build(); }
/** * Tests that the builder correctly leaves out the file path. */ @Test public void testReadPropertiesFromFile_clientIdNoFilePath() throws Exception { PropertiesConfiguration config = new PropertiesConfiguration(); config.setProperty("api.admanager.clientSecret", "clientSecret"); config.setProperty("api.admanager.refreshToken", "refreshToken"); GoogleClientSecretsForApiBuilder builder = new GoogleClientSecretsForApiBuilder( configurationHelper, GoogleClientSecretsBuilder.Api.AD_MANAGER); thrown.expect(ValidationException.class); thrown.expectMessage("Client ID must be set." + "\nIf you do not have a client ID or secret, please create one in the API " + "console: https://console.developers.google.com"); builder.from(config).build(); }
/** * Tests that the builder correctly reads properties from a configuration. */ @Test public void testGoogleSecretsReadPropertiesFromConfiguration() throws ValidationException { PropertiesConfiguration config = new PropertiesConfiguration(); config.setProperty("api.admanager.clientId", "clientId"); config.setProperty("api.admanager.clientSecret", "clientSecret"); GoogleClientSecrets googleClientSecrets = new GoogleClientSecretsBuilder() .forApi(GoogleClientSecretsBuilder.Api.AD_MANAGER) .from(config) .build(); assertEquals("clientId", googleClientSecrets.getInstalled().getClientId()); assertEquals("clientSecret", googleClientSecrets.getInstalled().getClientSecret()); }
/** * Tests that the builder correctly reads from a file. */ @Test public void testGoogleSecretsReadPropertiesFromFile() throws Exception { PropertiesConfiguration config = new PropertiesConfiguration(); config.setProperty("api.admanager.clientId", "clientId"); config.setProperty("api.admanager.clientSecret", "clientSecret"); when(configurationHelper.fromFile("path")).thenReturn(config); GoogleClientSecretsForApiBuilder builder = new GoogleClientSecretsForApiBuilder( configurationHelper, GoogleClientSecretsBuilder.Api.AD_MANAGER); GoogleClientSecrets googleClientSecrets = builder.fromFile("path").build(); assertEquals("clientId", googleClientSecrets.getInstalled().getClientId()); assertEquals("clientSecret", googleClientSecrets.getInstalled().getClientSecret()); }
/** * Tests that the builder correctly fails on a bad configuration. */ @Test public void testGoogleSecretsReadPropertiesFromConfiguration_missingClientSecret() throws Exception { PropertiesConfiguration config = new PropertiesConfiguration(); config.setProperty("api.admanager.clientId", "clientId"); thrown.expect(ValidationException.class); new GoogleClientSecretsBuilder() .forApi(GoogleClientSecretsBuilder.Api.AD_MANAGER) .from(config) .build(); }
/** * Tests that the builder correctly throws exception when client ID or client * secret is a default value. */ @Test public void testBuilder_defaultClientIdAndSecret() throws Exception { thrown.expect(ValidationException.class); thrown.expectMessage("Client ID must be set." + "\nIf you do not have a client ID or secret, please create one in the API " + "console: https://console.developers.google.com"); new GoogleClientSecretsBuilder() .forApi(GoogleClientSecretsBuilder.Api.AD_MANAGER) .withClientSecrets("INSERT_CLIENT_ID_HERE", "INSERT_CLIENT_SECRET_HERE") .build(); }
@Override public GoogleClientSecretsForApiBuilder fromFile(URL path) throws ConfigurationLoadException { return from(configHelper.fromFile(path), path.toString()); }
@Override public GoogleClientSecretsForApiBuilder fromFile(File path) throws ConfigurationLoadException { return from(configHelper.fromFile(path), path.getAbsolutePath()); }