Search in sources :

Example 1 with GenericAdWordsServices

use of com.google.api.ads.adwords.lib.utils.testing.GenericAdWordsServices in project googleads-java-lib by googleads.

the class AdHocReportDownloadHelperTest method testDownloadReportWithBadEndpoint_fails.

/**
 * Tests that the helper will throw a {@link ConnectException} wrapped in a
 * {@link ReportException} when the endpoint is for an unused port on localhost.
 */
@Test
public void testDownloadReportWithBadEndpoint_fails() throws Throwable {
    int port = TestPortFinder.getInstance().checkOutUnusedPort();
    try {
        // Construct the session to use an endpoint that is NOT in use on localhost.
        AdWordsSession sessionWithBadEndpoint = new AdWordsSession.Builder().withUserAgent("TEST_APP").withOAuth2Credential(credential).withEndpoint("https://localhost:" + port).withDeveloperToken("TEST_DEVELOPER_TOKEN").withClientCustomerId("TEST_CLIENT_CUSTOMER_ID").build();
        helper = new GenericAdWordsServices().getBootstrapper().getInstanceOf(sessionWithBadEndpoint, AdHocReportDownloadHelper.class);
        // Set up the ReportRequest so it will pass basic validation.
        when(reportRequest.getRequestType()).thenReturn(RequestType.AWQL);
        String awqlString = "SELECT BadField1 FROM NOT_A_REPORT DURING NOT_A_TIME_PERIOD";
        when(reportRequest.getReportRequestString()).thenReturn(awqlString);
        // The cause should be a ConnectException (see expected annotation) since the endpoint
        // port is not in use.
        thrown.expect(ReportException.class);
        thrown.expectCause(Matchers.<Exception>instanceOf(ConnectException.class));
        downloadReport();
    } finally {
        TestPortFinder.getInstance().releaseUnusedPort(port);
    }
}
Also used : GenericAdWordsServices(com.google.api.ads.adwords.lib.utils.testing.GenericAdWordsServices) AdWordsSession(com.google.api.ads.adwords.lib.client.AdWordsSession) Builder(com.google.api.ads.adwords.lib.utils.DetailedReportDownloadResponseException.Builder) Matchers.containsString(org.hamcrest.Matchers.containsString) ConnectException(java.net.ConnectException) MockHttpIntegrationTest(com.google.api.ads.common.lib.testing.MockHttpIntegrationTest) Test(org.junit.Test)

Example 2 with GenericAdWordsServices

use of com.google.api.ads.adwords.lib.utils.testing.GenericAdWordsServices in project googleads-java-lib by googleads.

the class ReportDownloaderTest method testGetFromAdWordsServices.

@Test
public void testGetFromAdWordsServices() throws Exception {
    AdWordsServicesInterface adWordsServices = new GenericAdWordsServices();
    ReportDownloaderInterface downloader = adWordsServices.getUtility(adWordsSession, ReportDownloaderInterface.class);
    assertNotNull("Null downloader from AdWordsServices", downloader);
}
Also used : GenericAdWordsServices(com.google.api.ads.adwords.lib.utils.testing.GenericAdWordsServices) AdWordsServicesInterface(com.google.api.ads.adwords.lib.factory.AdWordsServicesInterface) Test(org.junit.Test)

Example 3 with GenericAdWordsServices

use of com.google.api.ads.adwords.lib.utils.testing.GenericAdWordsServices in project googleads-java-lib by googleads.

the class AdHocReportDownloadHelperTest method setUp.

@Before
public void setUp() throws Exception {
    MockitoAnnotations.initMocks(this);
    Enum<?> downloadFormat = TestDownloadFormat.CSV;
    Mockito.<Enum<?>>when(reportRequest.getDownloadFormat()).thenReturn(downloadFormat);
    credential = new GoogleCredential.Builder().setTransport(new NetHttpTransport()).setJsonFactory(new JacksonFactory()).build();
    credential.setAccessToken("TEST_ACCESS_TOKEN");
    AdWordsSession session = new AdWordsSession.Builder().withUserAgent("TEST_APP").withOAuth2Credential(credential).withEndpoint(testHttpServer.getServerUrl()).withDeveloperToken("TEST_DEVELOPER_TOKEN").withClientCustomerId("TEST_CLIENT_CUSTOMER_ID").build();
    helper = new GenericAdWordsServices().getBootstrapper().getInstanceOf(session, AdHocReportDownloadHelper.class);
    exceptionBuilder = DetailedReportDownloadResponseException::new;
}
Also used : GenericAdWordsServices(com.google.api.ads.adwords.lib.utils.testing.GenericAdWordsServices) NetHttpTransport(com.google.api.client.http.javanet.NetHttpTransport) Builder(com.google.api.ads.adwords.lib.utils.DetailedReportDownloadResponseException.Builder) AdWordsSession(com.google.api.ads.adwords.lib.client.AdWordsSession) JacksonFactory(com.google.api.client.json.jackson2.JacksonFactory) Before(org.junit.Before)

Example 4 with GenericAdWordsServices

use of com.google.api.ads.adwords.lib.utils.testing.GenericAdWordsServices in project googleads-java-lib by googleads.

the class AdWordsPluginModuleTest method testCombinedUserAgentBehavior.

/**
 * Verifies that {@link UserAgentCombiner} instances behave as expected when an {@link
 * AdWordsPluginModule} is or is not provided to the injector.
 */
@Test
public void testCombinedUserAgentBehavior() {
    // Create a mock ExtensionUserAgentProvider that returns a specific string from getUserAgent.
    String pluginUserAgentSnippet = "MyCustomExtension";
    when(extensionUserAgentProvider.getUserAgent()).thenReturn(pluginUserAgentSnippet);
    // Create an AdWordsServicesInterface using an AdWordsPluginModule configured with the
    // ExtensionUserAgentProvider.
    GenericAdWordsServices genericAdWordsServices = (GenericAdWordsServices) new GenericAdWordsServices().withPluginModule(new AdWordsPluginModule(extensionUserAgentProvider));
    // Get the combined user agent from the UserAgentCombiner provided by the
    // AdWordsServicesInterface, and confirm the combined user agent it returns includes the
    // string from the mock ExtensionUserAgentProvider.
    UserAgentCombiner userAgentCombiner = genericAdWordsServices.getBootstrapper().getInstanceOf(session, UserAgentCombiner.class);
    String combinedUserAgent = userAgentCombiner.getUserAgent("foo");
    assertThat("Combined user agent does not contain the string from the plugin", combinedUserAgent, Matchers.containsString(pluginUserAgentSnippet));
    // Get a UserAgentCombiner from an AdWordsServicesInterface without a plugin and confirm the
    // combined user agent it returns does not contain the string from the mock
    // ExtensionUserAgentProvider.
    genericAdWordsServices = new GenericAdWordsServices();
    userAgentCombiner = genericAdWordsServices.getBootstrapper().getInstanceOf(session, UserAgentCombiner.class);
    combinedUserAgent = userAgentCombiner.getUserAgent("foo");
    assertThat("Combined user agent contains string from the plugin, but plugin was not provided", combinedUserAgent, Matchers.not(Matchers.containsString(pluginUserAgentSnippet)));
}
Also used : GenericAdWordsServices(com.google.api.ads.adwords.lib.utils.testing.GenericAdWordsServices) UserAgentCombiner(com.google.api.ads.common.lib.useragent.UserAgentCombiner) Test(org.junit.Test)

Aggregations

GenericAdWordsServices (com.google.api.ads.adwords.lib.utils.testing.GenericAdWordsServices)4 Test (org.junit.Test)3 AdWordsSession (com.google.api.ads.adwords.lib.client.AdWordsSession)2 Builder (com.google.api.ads.adwords.lib.utils.DetailedReportDownloadResponseException.Builder)2 AdWordsServicesInterface (com.google.api.ads.adwords.lib.factory.AdWordsServicesInterface)1 MockHttpIntegrationTest (com.google.api.ads.common.lib.testing.MockHttpIntegrationTest)1 UserAgentCombiner (com.google.api.ads.common.lib.useragent.UserAgentCombiner)1 NetHttpTransport (com.google.api.client.http.javanet.NetHttpTransport)1 JacksonFactory (com.google.api.client.json.jackson2.JacksonFactory)1 ConnectException (java.net.ConnectException)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1 Before (org.junit.Before)1