Search in sources :

Example 1 with ResponseInfo

use of com.google.api.ads.common.lib.client.ResponseInfo in project googleads-java-lib by googleads.

the class BatchJobLoggerTest method testLogUpload.

/**
 * Confirms an upload is logged as expected.
 */
@Test
public void testLogUpload() throws IOException {
    String contentsString = "some contents";
    InputStream responseContent = CharSource.wrap(contentsString).asByteSource(UTF_8).openStream();
    BatchJobUploadResponse response = new BatchJobUploadResponse(responseContent, statusCode, statusMessage, contentsString.length(), URI.create(url));
    ArgumentCaptor<RemoteCallReturn> returnCaptor = ArgumentCaptor.forClass(RemoteCallReturn.class);
    batchJobLogger.logUpload(contentsString, URI.create(url), response, exception);
    verify(loggerDelegate).logRequestSummary(returnCaptor.capture());
    RemoteCallReturn capturedReturn = returnCaptor.getValue();
    assertEquals(exception, capturedReturn.getException());
    RequestInfo requestInfo = capturedReturn.getRequestInfo();
    assertEquals(url, requestInfo.getUrl());
    assertEquals("clientCustomerId", requestInfo.getContextName());
    assertNull(requestInfo.getContextValue());
    assertThat(requestInfo.getPayload(), containsString(contentsString));
    assertThat(requestInfo.getServiceName(), containsString("upload"));
    ResponseInfo responseInfo = capturedReturn.getResponseInfo();
    assertNull(responseInfo.getRequestId());
    assertThat(responseInfo.getPayload(), startsWith(String.valueOf(response.getHttpStatus())));
    assertThat(responseInfo.getPayload(), containsString(response.getHttpResponseMessage()));
    verify(loggerDelegate).logRequestDetails(returnCaptor.capture());
    assertSame("The same RemoteCallReturn object was not passed to request details and request summary", capturedReturn, returnCaptor.getValue());
}
Also used : ResponseInfo(com.google.api.ads.common.lib.client.ResponseInfo) BatchJobUploadResponse(com.google.api.ads.adwords.lib.utils.BatchJobUploadResponse) InputStream(java.io.InputStream) Matchers.containsString(org.hamcrest.Matchers.containsString) RequestInfo(com.google.api.ads.common.lib.client.RequestInfo) RemoteCallReturn(com.google.api.ads.common.lib.client.RemoteCallReturn) Test(org.junit.Test)

Example 2 with ResponseInfo

use of com.google.api.ads.common.lib.client.ResponseInfo in project googleads-java-lib by googleads.

the class BatchJobLoggerTest method testLogDownload.

/**
 * Confirms a download is logged as expected.
 */
@SuppressWarnings("unchecked")
@Test
public void testLogDownload() {
    BatchJobMutateResponseInterface<String, IllegalArgumentException, BatchJobMutateResultInterface<String, IllegalArgumentException>> response = Mockito.mock(BatchJobMutateResponseInterface.class);
    // Cannot specify generic args for mocks
    @SuppressWarnings("rawtypes") BatchJobMutateResultInterface[] mutateResults = new BatchJobMutateResultInterface[] { Mockito.mock(BatchJobMutateResultInterface.class), Mockito.mock(BatchJobMutateResultInterface.class) };
    when(response.getMutateResults()).thenReturn(mutateResults);
    batchJobLogger.logDownload(url, response, exception);
    ArgumentCaptor<RemoteCallReturn> returnCaptor = ArgumentCaptor.forClass(RemoteCallReturn.class);
    verify(loggerDelegate).logRequestSummary(returnCaptor.capture());
    RemoteCallReturn capturedReturn = returnCaptor.getValue();
    assertEquals(exception, capturedReturn.getException());
    RequestInfo requestInfo = capturedReturn.getRequestInfo();
    assertEquals(url, requestInfo.getUrl());
    assertEquals("clientCustomerId", requestInfo.getContextName());
    assertNull(requestInfo.getContextValue());
    assertNull(requestInfo.getPayload());
    assertThat(requestInfo.getServiceName(), containsString("download"));
    ResponseInfo responseInfo = capturedReturn.getResponseInfo();
    assertNull(responseInfo.getRequestId());
    assertEquals("Results count: 2", responseInfo.getPayload());
    verify(loggerDelegate).logRequestDetails(returnCaptor.capture());
    assertSame("The same RemoteCallReturn object was not passed to request details and request summary", capturedReturn, returnCaptor.getValue());
}
Also used : ResponseInfo(com.google.api.ads.common.lib.client.ResponseInfo) BatchJobMutateResultInterface(com.google.api.ads.adwords.lib.utils.BatchJobMutateResultInterface) Matchers.containsString(org.hamcrest.Matchers.containsString) RequestInfo(com.google.api.ads.common.lib.client.RequestInfo) RemoteCallReturn(com.google.api.ads.common.lib.client.RemoteCallReturn) Test(org.junit.Test)

Example 3 with ResponseInfo

use of com.google.api.ads.common.lib.client.ResponseInfo in project googleads-java-lib by googleads.

the class BatchJobLoggerTest method testLogDownload_nullablesNull.

/**
 * Confirms that passing {@code null} to {@code logUpload} for parameters marked {@code Nullable}
 * does not result in any exceptions.
 */
@Test
public void testLogDownload_nullablesNull() {
    batchJobLogger.logDownload(url, null, null);
    ArgumentCaptor<RemoteCallReturn> returnCaptor = ArgumentCaptor.forClass(RemoteCallReturn.class);
    verify(loggerDelegate).logRequestSummary(returnCaptor.capture());
    RemoteCallReturn capturedReturn = returnCaptor.getValue();
    assertNull(capturedReturn.getException());
    ResponseInfo responseInfo = capturedReturn.getResponseInfo();
    assertEquals("Results count: 0", responseInfo.getPayload());
    verify(loggerDelegate).logRequestDetails(returnCaptor.capture());
    assertSame("The same RemoteCallReturn object was not passed to request details and request summary", capturedReturn, returnCaptor.getValue());
}
Also used : ResponseInfo(com.google.api.ads.common.lib.client.ResponseInfo) RemoteCallReturn(com.google.api.ads.common.lib.client.RemoteCallReturn) Test(org.junit.Test)

Example 4 with ResponseInfo

use of com.google.api.ads.common.lib.client.ResponseInfo in project googleads-java-lib by googleads.

the class ReportServiceLogger method logRequest.

/**
 * Logs the specified request and response information.
 *
 * <p>Note that in order to avoid any temptation to consume the contents of the response, this
 * does <em>not</em> take an {@link com.google.api.client.http.HttpResponse} object, but instead
 * accepts the status code and message from the response.
 */
public void logRequest(@Nullable HttpRequest request, int statusCode, @Nullable String statusMessage) {
    boolean isSuccess = HttpStatusCodes.isSuccess(statusCode);
    if (!loggerDelegate.isSummaryLoggable(isSuccess) && !loggerDelegate.isDetailsLoggable(isSuccess)) {
        return;
    }
    // Populate the RequestInfo builder from the request.
    RequestInfo requestInfo = buildRequestInfo(request);
    // Populate the ResponseInfo builder from the response.
    ResponseInfo responseInfo = buildResponseInfo(request, statusCode, statusMessage);
    RemoteCallReturn.Builder remoteCallReturnBuilder = new RemoteCallReturn.Builder().withRequestInfo(requestInfo).withResponseInfo(responseInfo);
    if (!isSuccess) {
        remoteCallReturnBuilder.withException(new ReportException(String.format("%s: %s", statusCode, statusMessage)));
    }
    RemoteCallReturn remoteCallReturn = remoteCallReturnBuilder.build();
    loggerDelegate.logRequestSummary(remoteCallReturn);
    loggerDelegate.logRequestDetails(remoteCallReturn);
}
Also used : ResponseInfo(com.google.api.ads.common.lib.client.ResponseInfo) ReportException(com.google.api.ads.adwords.lib.utils.ReportException) RequestInfo(com.google.api.ads.common.lib.client.RequestInfo) RemoteCallReturn(com.google.api.ads.common.lib.client.RemoteCallReturn)

Example 5 with ResponseInfo

use of com.google.api.ads.common.lib.client.ResponseInfo in project googleads-java-lib by googleads.

the class BatchJobLogger method logDownload.

/**
 * Logs a batch job results download.
 *
 * @param downloadUrl the download URL for the batch job.
 * @param response the response - only not null if the download succeeded.
 * @param throwable the throwable that occurred during download, or {@code null} if the download
 *     succeeded.
 */
public <O, E, R extends BatchJobMutateResultInterface<O, E>> void logDownload(String downloadUrl, @Nullable BatchJobMutateResponseInterface<O, E, R> response, @Nullable Throwable throwable) {
    RequestInfo requestInfo = new RequestInfo.Builder().withServiceName("batchjobdownload").withContext(CONTEXT_NAME, null).withUrl(downloadUrl).build();
    int resultsCount = 0;
    if (response != null && response.getMutateResults() != null) {
        resultsCount = response.getMutateResults().length;
    }
    // The response payload could be massive, so simply indicate the number of results instead.
    ResponseInfo responseInfo = new ResponseInfo.Builder().withPayload(String.format("Results count: %d", resultsCount)).build();
    RemoteCallReturn remoteCallReturn = new RemoteCallReturn.Builder().withRequestInfo(requestInfo).withResponseInfo(responseInfo).withException(throwable).build();
    loggerDelegate.logRequestSummary(remoteCallReturn);
    loggerDelegate.logRequestDetails(remoteCallReturn);
}
Also used : ResponseInfo(com.google.api.ads.common.lib.client.ResponseInfo) RequestInfo(com.google.api.ads.common.lib.client.RequestInfo) RemoteCallReturn(com.google.api.ads.common.lib.client.RemoteCallReturn)

Aggregations

ResponseInfo (com.google.api.ads.common.lib.client.ResponseInfo)12 RequestInfo (com.google.api.ads.common.lib.client.RequestInfo)10 Test (org.junit.Test)8 RemoteCallReturn (com.google.api.ads.common.lib.client.RemoteCallReturn)6 Matchers.containsString (org.hamcrest.Matchers.containsString)2 BatchJobMutateResultInterface (com.google.api.ads.adwords.lib.utils.BatchJobMutateResultInterface)1 BatchJobUploadResponse (com.google.api.ads.adwords.lib.utils.BatchJobUploadResponse)1 ReportException (com.google.api.ads.adwords.lib.utils.ReportException)1 Builder (com.google.api.ads.common.lib.client.ResponseInfo.Builder)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 SOAPHeader (javax.xml.soap.SOAPHeader)1 SOAPMessage (javax.xml.soap.SOAPMessage)1 Before (org.junit.Before)1