Search in sources :

Example 1 with ForApiBuilder

use of com.google.api.ads.common.lib.auth.OfflineCredentials.ForApiBuilder in project googleads-java-lib by googleads.

the class OfflineCredentialsTest method testReadPropertiesFromFile_clientSecretTokenBadWithFilePath.

/**
 * 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);
    ForApiBuilder builder = new OfflineCredentials.ForApiBuilder(configurationHelper, OfflineCredentials.Api.AD_MANAGER, oAuth2Helper);
    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/project");
    builder.fromFile("/home/user/path").build();
}
Also used : ForApiBuilder(com.google.api.ads.common.lib.auth.OfflineCredentials.ForApiBuilder) PropertiesConfiguration(org.apache.commons.configuration.PropertiesConfiguration) Test(org.junit.Test)

Example 2 with ForApiBuilder

use of com.google.api.ads.common.lib.auth.OfflineCredentials.ForApiBuilder in project googleads-java-lib by googleads.

the class OfflineCredentialsTest method testReadPropertiesFromFile_withOverride.

/**
 * Tests that the builder correctly reads from a file.
 */
@Test
public void testReadPropertiesFromFile_withOverride() throws Exception {
    PropertiesConfiguration config = new PropertiesConfiguration();
    config.setProperty("api.admanager.clientId", "clientId");
    config.setProperty("api.admanager.clientSecret", "clientSecret");
    config.setProperty("api.admanager.refreshToken", "refreshToken");
    when(configurationHelper.fromFile("path")).thenReturn(config);
    ForApiBuilder builder = new OfflineCredentials.ForApiBuilder(configurationHelper, OfflineCredentials.Api.AD_MANAGER, oAuth2Helper);
    OfflineCredentials offlineCredentials = builder.fromFile("path").withRefreshToken("overrideRefreshToken").build();
    assertEquals("clientId", offlineCredentials.getClientId());
    assertEquals("clientSecret", offlineCredentials.getClientSecret());
    assertEquals("overrideRefreshToken", offlineCredentials.getRefreshToken());
}
Also used : ForApiBuilder(com.google.api.ads.common.lib.auth.OfflineCredentials.ForApiBuilder) PropertiesConfiguration(org.apache.commons.configuration.PropertiesConfiguration) Test(org.junit.Test)

Example 3 with ForApiBuilder

use of com.google.api.ads.common.lib.auth.OfflineCredentials.ForApiBuilder in project googleads-java-lib by googleads.

the class OfflineCredentialsTest method testReadPropertiesFromFile_refreshTokenNoFilePath.

/**
 * Tests that the builder correctly leaves out the file path.
 */
@Test
public void testReadPropertiesFromFile_refreshTokenNoFilePath() throws Exception {
    PropertiesConfiguration config = new PropertiesConfiguration();
    config.setProperty("api.admanager.clientId", "clientId");
    config.setProperty("api.admanager.clientSecret", "clientSecret");
    ForApiBuilder builder = new OfflineCredentials.ForApiBuilder(configurationHelper, OfflineCredentials.Api.AD_MANAGER, oAuth2Helper);
    thrown.expect(ValidationException.class);
    thrown.expectMessage("A refresh token must be set." + "\nIt is required for offline credentials. If you need to create one, " + "see the GetRefreshToken example.");
    builder.from(config).build();
}
Also used : ForApiBuilder(com.google.api.ads.common.lib.auth.OfflineCredentials.ForApiBuilder) PropertiesConfiguration(org.apache.commons.configuration.PropertiesConfiguration) Test(org.junit.Test)

Example 4 with ForApiBuilder

use of com.google.api.ads.common.lib.auth.OfflineCredentials.ForApiBuilder in project googleads-java-lib by googleads.

the class OfflineCredentialsTest method testReadPropertiesFromFile.

/**
 * Tests that the builder correctly reads from a file.
 */
@Test
public void testReadPropertiesFromFile() throws Exception {
    PropertiesConfiguration config = new PropertiesConfiguration();
    config.setProperty("api.admanager.clientId", "clientId");
    config.setProperty("api.admanager.clientSecret", "clientSecret");
    config.setProperty("api.admanager.refreshToken", "refreshToken");
    when(configurationHelper.fromFile("path")).thenReturn(config);
    ForApiBuilder builder = new OfflineCredentials.ForApiBuilder(configurationHelper, OfflineCredentials.Api.AD_MANAGER, oAuth2Helper);
    OfflineCredentials offlineCredentials = builder.fromFile("path").build();
    assertEquals("clientId", offlineCredentials.getClientId());
    assertEquals("clientSecret", offlineCredentials.getClientSecret());
    assertEquals("refreshToken", offlineCredentials.getRefreshToken());
}
Also used : ForApiBuilder(com.google.api.ads.common.lib.auth.OfflineCredentials.ForApiBuilder) PropertiesConfiguration(org.apache.commons.configuration.PropertiesConfiguration) Test(org.junit.Test)

Example 5 with ForApiBuilder

use of com.google.api.ads.common.lib.auth.OfflineCredentials.ForApiBuilder in project googleads-java-lib by googleads.

the class OfflineCredentialsTest method testReadPropertiesFromFile_clientSecretNoFilePath.

/**
 * 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");
    ForApiBuilder builder = new OfflineCredentials.ForApiBuilder(configurationHelper, OfflineCredentials.Api.AD_MANAGER, oAuth2Helper);
    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/project");
    builder.from(config).build();
}
Also used : ForApiBuilder(com.google.api.ads.common.lib.auth.OfflineCredentials.ForApiBuilder) PropertiesConfiguration(org.apache.commons.configuration.PropertiesConfiguration) Test(org.junit.Test)

Aggregations

ForApiBuilder (com.google.api.ads.common.lib.auth.OfflineCredentials.ForApiBuilder)8 PropertiesConfiguration (org.apache.commons.configuration.PropertiesConfiguration)8 Test (org.junit.Test)8