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());
}
}
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));
}
}
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);
}
}
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());
}
Aggregations