@Test public void testFailedResponse_fails() { RawReportDownloadResponse rawResponse = new RawReportDownloadResponse( HttpURLConnection.HTTP_BAD_REQUEST, new ByteArrayInputStream("failed".getBytes(UTF_8)), UTF_8, "CSV"); thrown.expect(IllegalArgumentException.class); new ReportDownloadResponse(rawResponse); } }
/** * Test method for * {@link com.google.api.ads.adwords.lib.utils.ReportDownloadResponse#getAsString()} * when response data is compressed. */ @Test public void testGetAsString_gzippedCSV() throws IOException { String expectedContents = "a,b,c\nd,e,f\n"; ByteArrayOutputStream zippedBytesOut = new ByteArrayOutputStream(); Streams.copy(new ByteArrayInputStream(expectedContents.getBytes(UTF_8)), new GZIPOutputStream(zippedBytesOut)); RawReportDownloadResponse rawResponse = new RawReportDownloadResponse(HttpURLConnection.HTTP_OK, new ByteArrayInputStream(zippedBytesOut.toByteArray()), UTF_8, "GZIPPED_CSV"); ReportDownloadResponse response = new ReportDownloadResponse(rawResponse); assertEquals("contents as string are incorrect for gzipped format", expectedContents, response.getAsString()); }
/** * Test method for * {@link com.google.api.ads.adwords.lib.utils.ReportDownloadResponse#getAsString()}. */ @Test public void testGetAsString() throws IOException { String expectedContents = "a,b,c\nd,e,f\n"; InputStream inputStream = new ByteArrayInputStream(expectedContents.getBytes(UTF_8)); RawReportDownloadResponse rawResponse = new RawReportDownloadResponse(HttpURLConnection.HTTP_OK, inputStream, UTF_8, "CSV"); ReportDownloadResponse response = new ReportDownloadResponse(rawResponse); assertEquals("contents as string are incorrect", expectedContents, response.getAsString()); }
@Test public void testHandleResponseWithNullInputStream_fails() throws Exception { RawReportDownloadResponse rawResponse = new RawReportDownloadResponse( 500, (InputStream) null, AdHocReportDownloadHelper.REPORT_CHARSET, "CSV"); thrown.expect(DetailedReportDownloadResponseException.class); helper.handleResponse(rawResponse, exceptionBuilder); }
@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()); } }
@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()); } }
@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()); } }
/** * Test method for * {@link com.google.api.ads.adwords.lib.utils.ReportDownloadResponse#saveToFile(String)} */ @Test public void testSaveToFile() throws IOException { File outputFile = tempFolder.newFile("ReportOutput.csv"); String expectedContents = "a,b,c\nd,e,f\n"; InputStream inputStream = new ByteArrayInputStream(expectedContents.getBytes(UTF_8)); RawReportDownloadResponse rawResponse = new RawReportDownloadResponse(HttpURLConnection.HTTP_OK, inputStream, UTF_8, "CSV"); ReportDownloadResponse response = new ReportDownloadResponse(rawResponse); response.saveToFile(outputFile.getPath()); assertEquals("contents saved to file are incorrect", expectedContents, Streams.readAll(new FileInputStream(outputFile), UTF_8)); }
/** * Test method for * {@link com.google.api.ads.adwords.lib.utils.ReportDownloadResponse#getInputStream()}. */ @Test public void testGetInputStream() throws IOException { String expectedContents = "a,b,c\nd,e,f\n"; InputStream inputStream = new ByteArrayInputStream(expectedContents.getBytes(UTF_8)); RawReportDownloadResponse rawResponse = new RawReportDownloadResponse(HttpURLConnection.HTTP_OK, inputStream, UTF_8, "CSV"); ReportDownloadResponse response = new ReportDownloadResponse(rawResponse); String actualContents = Streams.readAll(response.getInputStream(), UTF_8); assertEquals("input stream contents are incorrect", expectedContents, actualContents); }
@Test public void testHandleSuccessfulResponse() throws Exception { String responseBody = "Successful,report,response"; RawReportDownloadResponse rawResponse = new RawReportDownloadResponse( 200, ByteSource.wrap(responseBody.getBytes(REPORT_CHARSET)).openStream(), REPORT_CHARSET, "CSV"); ReportDownloadResponse reportResponse = helper.handleResponse(rawResponse, exceptionBuilder); String actualResponseBody = new String(ByteStreams.toByteArray(reportResponse.getInputStream()), REPORT_CHARSET); assertEquals("Response body not expected value", responseBody, actualResponseBody); }
@Test public void testHandleResponseNullExceptionBuilder_fails() throws Exception { RawReportDownloadResponse rawResponse = new RawReportDownloadResponse( 200, ByteSource.wrap("a,b,c".getBytes(REPORT_CHARSET)).openStream(), REPORT_CHARSET, "CSV"); thrown.expect(NullPointerException.class); thrown.expectMessage("Null exception builder"); helper.handleResponse(rawResponse, null); } }
@Test public void testHandleErrorResponse_fails() throws Exception { RawReportDownloadResponse rawResponse = new RawReportDownloadResponse( 500, ByteSource.wrap(ERROR_XML.getBytes(REPORT_CHARSET)).openStream(), REPORT_CHARSET, "CSV"); thrown.expect(DetailedReportDownloadResponseException.class); thrown.expect(Matchers.hasProperty("fieldPath", Matchers.equalTo("foobar"))); thrown.expect(Matchers.hasProperty("trigger", Matchers.equalTo("AdFormatt"))); thrown.expect( Matchers.hasProperty( "type", Matchers.equalTo("ReportDefinitionError.INVALID_FIELD_NAME_FOR_REPORT"))); helper.handleResponse(rawResponse, exceptionBuilder); }
@Test public void testHandleResponseWithEmptyInputStream_fails() throws Exception { RawReportDownloadResponse rawResponse = new RawReportDownloadResponse( 500, ByteSource.empty().openStream(), REPORT_CHARSET, "CSV"); thrown.expect(DetailedReportDownloadResponseException.class); thrown.expect(Matchers.hasProperty("fieldPath", Matchers.nullValue())); thrown.expect(Matchers.hasProperty("trigger", Matchers.nullValue())); thrown.expect(Matchers.hasProperty("type", Matchers.nullValue())); helper.handleResponse(rawResponse, exceptionBuilder); }
@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()); }
@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()); }
@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)); } }
@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()); }
@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)); } }
@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)); } }
return new RawReportDownloadResponse(response.getStatusCode(), response.getContent(), charSet, reportRequest.getDownloadFormat().name()); } catch (MalformedURLException e) {