Preconditions.checkNotNull(rawResponse, "Null response"); Preconditions.checkNotNull(exceptionBuilder, "Null exception builder"); if (rawResponse.getHttpStatus() == HttpURLConnection.HTTP_OK) { return new ReportDownloadResponse(rawResponse); if (rawResponse.getInputStream() == null) { responseText = ""; } else { try { responseText = Streams.readAll(rawResponse.getInputStream(), rawResponse.getCharset()); } catch (IOException e1) { throw new ReportDownloadResponseException(rawResponse.getHttpStatus(), e1); exceptionBuilder.build(rawResponse.getHttpStatus(), responseText); Map<String, String> fields = xmlFieldExtractor.extract( new ByteArrayInputStream(responseText.getBytes(rawResponse.getCharset())), new String[] {"fieldPath", "trigger", "type"}); exception.setFieldPath(fields.get("fieldPath"));
private boolean isGzipped() { return rawResponse.getDownloadFormat().startsWith("GZIPPED"); } }
@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); } }
public int getHttpStatus() { return rawResponse.getHttpStatus(); }
/** * Returns the contents of the response as a String. Inflates the response if it is in one of the * gzip formats. * * @throws IOException if unable to read the response contents */ public String getAsString() throws IOException { InputStream inputStream = isGzipped() ? new GZIPInputStream(getInputStream()) : getInputStream(); return Streams.readAll(inputStream, rawResponse.getCharset()); }
@Nullable public InputStream getInputStream() { return rawResponse.getInputStream(); }
/** * 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()); }
public int getHttpStatus() { return rawResponse.getHttpStatus(); }
/** * Returns the contents of the response as a String. Inflates the response if it is in one of the * gzip formats. * * @throws IOException if unable to read the response contents */ public String getAsString() throws IOException { InputStream inputStream = isGzipped() ? new GZIPInputStream(getInputStream()) : getInputStream(); return Streams.readAll(inputStream, rawResponse.getCharset()); }
@Nullable public InputStream getInputStream() { return rawResponse.getInputStream(); }
Preconditions.checkNotNull(rawResponse, "Null response"); Preconditions.checkNotNull(exceptionBuilder, "Null exception builder"); if (rawResponse.getHttpStatus() == HttpURLConnection.HTTP_OK) { return new ReportDownloadResponse(rawResponse); if (rawResponse.getInputStream() == null) { responseText = ""; } else { try { responseText = Streams.readAll(rawResponse.getInputStream(), rawResponse.getCharset()); } catch (IOException e1) { throw new ReportDownloadResponseException(rawResponse.getHttpStatus(), e1); exceptionBuilder.build(rawResponse.getHttpStatus(), responseText); Map<String, String> fields = xmlFieldExtractor.extract( new ByteArrayInputStream(responseText.getBytes(rawResponse.getCharset())), new String[] {"fieldPath", "trigger", "type"}); exception.setFieldPath(fields.get("fieldPath"));
/** * 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()); }
/** * Constructs a new instance from a successful raw response. * * @throws NullPointerException if {@code rawResponse} is null. * @throws IllegalArgumentException if {@code rawResponse} does not have a successful HTTP status. */ public ReportDownloadResponse(RawReportDownloadResponse rawResponse) { this.rawResponse = Preconditions.checkNotNull(rawResponse, "Null raw response"); Preconditions.checkArgument(rawResponse.getHttpStatus() == HttpURLConnection.HTTP_OK, "Expected a successful raw response but raw response status is: %s", rawResponse.getHttpStatus()); }
private boolean isGzipped() { return rawResponse.getDownloadFormat().startsWith("GZIPPED"); } }
/** * Tests that the helper will properly capture an internal server error (500) status returned by * the mock HTTP server. */ @Test public void testDownloadReportWithServerErrorStatus() throws Exception { when(reportRequest.getRequestType()).thenReturn(RequestType.AWQL); String awqlString = "SELECT BadField1 FROM NOT_A_REPORT DURING NOT_A_TIME_PERIOD"; when(reportRequest.getReportRequestString()).thenReturn(awqlString); // Do not set the next response body on the test server. This will trigger an error // (500) from the test server. RawReportDownloadResponse response = helper.downloadReport(reportRequest); assertEquals("Response status code not failure", 500, response.getHttpStatus()); assertEquals("", Streams.readAll(response.getInputStream(), response.getCharset())); }
@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); }
/** * Constructs a new instance from a successful raw response. * * @throws NullPointerException if {@code rawResponse} is null. * @throws IllegalArgumentException if {@code rawResponse} does not have a successful HTTP status. */ public ReportDownloadResponse(RawReportDownloadResponse rawResponse) { this.rawResponse = Preconditions.checkNotNull(rawResponse, "Null raw response"); Preconditions.checkArgument(rawResponse.getHttpStatus() == HttpURLConnection.HTTP_OK, "Expected a successful raw response but raw response status is: %s", rawResponse.getHttpStatus()); }
assertEquals("Response status code not success", 200, response.getHttpStatus()); assertEquals( "Response charset incorrect", REPORT_CHARSET, response.getCharset()); assertEquals( "Response contents incorrect", "test", Streams.readAll(response.getInputStream(), response.getCharset())); } else { ReportDownloadResponse response = helper.downloadReport(reportRequest, 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()); } }
RawReportDownloadResponse rawResponse, String expectedErrorText) throws ReportException, ReportDownloadResponseException { if (rawResponse.getHttpStatus() == 200) { adHocDownloadHelper.downloadReport( Matchers.any(ReportRequest.class), Matchers.any(Builder.class))) .thenThrow(new Builder().build(rawResponse.getHttpStatus(), expectedErrorText));