Search in sources :

Example 1 with RawReportDownloadResponse

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

the class ReportDownloaderTest method testFailure_cannotReadStream.

@Test
public void testFailure_cannotReadStream() throws Exception {
    InputStream stream = Mockito.mock(InputStream.class);
    when(stream.read(isA(byte[].class))).thenThrow(new IOException());
    RawReportDownloadResponse rawResponse = new RawReportDownloadResponse(400, stream, AdHocReportDownloadHelper.REPORT_CHARSET, DownloadFormat.CSV.name());
    try {
        downloadReport(DownloadFormat.CSV, rawResponse, null);
        fail("Should have thrown an exception");
    } catch (ReportDownloadResponseException e) {
        assertEquals(400, e.getHttpStatus());
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) RawReportDownloadResponse(com.google.api.ads.adwords.lib.utils.RawReportDownloadResponse) IOException(java.io.IOException) ReportDownloadResponseException(com.google.api.ads.adwords.lib.utils.ReportDownloadResponseException) Test(org.junit.Test)

Example 2 with RawReportDownloadResponse

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

the class ReportDownloaderTest method testFailure_failedStatusCode.

@Test
public void testFailure_failedStatusCode() throws Exception {
    InputStream stream = new ByteArrayInputStream(ERROR_TEXT.getBytes(AdHocReportDownloadHelper.REPORT_CHARSET));
    int statusCode = 400;
    RawReportDownloadResponse rawResponse = new RawReportDownloadResponse(statusCode, stream, AdHocReportDownloadHelper.REPORT_CHARSET, DownloadFormat.XML.name());
    try {
        downloadReport(DownloadFormat.XML, rawResponse, ERROR_TEXT);
        fail("Should have thrown an exception");
    } catch (DetailedReportDownloadResponseException e) {
        assertEquals(statusCode, e.getHttpStatus());
        assertEquals(ERROR_TEXT, e.getErrorText());
        assertThat(e.toString(), org.hamcrest.Matchers.containsString(String.valueOf(statusCode)));
        assertThat(e.toString(), org.hamcrest.Matchers.containsString(ERROR_TEXT));
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) RawReportDownloadResponse(com.google.api.ads.adwords.lib.utils.RawReportDownloadResponse) Test(org.junit.Test)

Example 3 with RawReportDownloadResponse

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

the class ReportDownloaderTest method downloadReport.

/**
 * Invokes {@link ReportDownloader#downloadReport(ReportDefinition)} or
 * {@link ReportDownloader#downloadReport(String, DownloadFormat)}, depending on whether this
 * instance is configured to use AWQL.
 *
 * @param downloadFormat the DownloadFormat for the request
 * @param rawResponse the response to return from the mocked ad hoc helper
 */
private ReportDownloadResponse downloadReport(DownloadFormat downloadFormat, RawReportDownloadResponse rawResponse, String expectedErrorText) throws ReportException, ReportDownloadResponseException {
    if (rawResponse.getHttpStatus() == 200) {
        // Response indicates success, so return a ReportDownloadResponse.
        when(adHocDownloadHelper.downloadReport(ArgumentMatchers.any(ReportRequest.class), ArgumentMatchers.any(Builder.class))).thenReturn(new ReportDownloadResponse(rawResponse));
    } else {
        // Response indicates failure, so throw an exception.
        when(adHocDownloadHelper.downloadReport(ArgumentMatchers.any(ReportRequest.class), ArgumentMatchers.any(Builder.class))).thenThrow(new Builder().build(rawResponse.getHttpStatus(), expectedErrorText));
    }
    if (isUseAwql) {
        return reportDownloader.downloadReport(AWQL_REQUEST, downloadFormat);
    } else {
        ReportDefinition reportDefinition = new ReportDefinition();
        reportDefinition.setSelector(new Selector());
        reportDefinition.getSelector().getFields().addAll(Arrays.asList("CampaignId", "CampaignName", "Impressions"));
        reportDefinition.setDateRangeType(ReportDefinitionDateRangeType.LAST_7_DAYS);
        reportDefinition.setReportName("Custom report");
        reportDefinition.setReportType(ReportDefinitionReportType.CAMPAIGN_PERFORMANCE_REPORT);
        return reportDownloader.downloadReport(reportDefinition);
    }
}
Also used : RawReportDownloadResponse(com.google.api.ads.adwords.lib.utils.RawReportDownloadResponse) ReportDownloadResponse(com.google.api.ads.adwords.lib.utils.ReportDownloadResponse) ReportRequest(com.google.api.ads.adwords.lib.utils.ReportRequest) Builder(com.google.api.ads.adwords.lib.utils.v201809.DetailedReportDownloadResponseException.Builder) ReportDefinition(com.google.api.ads.adwords.lib.jaxb.v201809.ReportDefinition) Selector(com.google.api.ads.adwords.lib.jaxb.v201809.Selector)

Example 4 with RawReportDownloadResponse

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

the class ReportDownloaderTest method testSuccess.

@Test
public void testSuccess() throws Exception {
    ByteArrayInputStream stream = new ByteArrayInputStream("Report data".getBytes(AdHocReportDownloadHelper.REPORT_CHARSET));
    RawReportDownloadResponse rawResponse = new RawReportDownloadResponse(200, stream, AdHocReportDownloadHelper.REPORT_CHARSET, DownloadFormat.CSV.name());
    ReportDownloadResponse response = downloadReport(DownloadFormat.CSV, rawResponse, null);
    assertEquals(200, response.getHttpStatus());
    assertEquals(stream, response.getInputStream());
    assertEquals("SUCCESS", response.getHttpResponseMessage());
}
Also used : RawReportDownloadResponse(com.google.api.ads.adwords.lib.utils.RawReportDownloadResponse) ReportDownloadResponse(com.google.api.ads.adwords.lib.utils.ReportDownloadResponse) ByteArrayInputStream(java.io.ByteArrayInputStream) RawReportDownloadResponse(com.google.api.ads.adwords.lib.utils.RawReportDownloadResponse) Test(org.junit.Test)

Aggregations

RawReportDownloadResponse (com.google.api.ads.adwords.lib.utils.RawReportDownloadResponse)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 Test (org.junit.Test)3 ReportDownloadResponse (com.google.api.ads.adwords.lib.utils.ReportDownloadResponse)2 InputStream (java.io.InputStream)2 ReportDefinition (com.google.api.ads.adwords.lib.jaxb.v201809.ReportDefinition)1 Selector (com.google.api.ads.adwords.lib.jaxb.v201809.Selector)1 ReportDownloadResponseException (com.google.api.ads.adwords.lib.utils.ReportDownloadResponseException)1 ReportRequest (com.google.api.ads.adwords.lib.utils.ReportRequest)1 Builder (com.google.api.ads.adwords.lib.utils.v201809.DetailedReportDownloadResponseException.Builder)1 IOException (java.io.IOException)1