Search in sources :

Example 1 with RequestInfo

use of com.google.api.ads.common.lib.client.RequestInfo 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 RequestInfo

use of com.google.api.ads.common.lib.client.RequestInfo 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 RequestInfo

use of com.google.api.ads.common.lib.client.RequestInfo 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 4 with RequestInfo

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

the class BatchJobLogger method logUpload.

/**
 * Logs a batch job operations upload.
 *
 * @param uploadContents the contents of the upload.
 * @param uploadUri the URI for the upload.
 * @param batchJobUploadResponse the upload response.
 * @param throwable the throwable that occurred during upload, or {@code null} if the upload
 *     succeeded.
 */
public void logUpload(String uploadContents, URI uploadUri, @Nullable BatchJobUploadResponse batchJobUploadResponse, @Nullable Throwable throwable) {
    RequestInfo requestInfo = new RequestInfo.Builder().withServiceName("batchjobupload").withContext(CONTEXT_NAME, null).withUrl(uploadUri.toString()).withPayload(Strings.nullToEmpty(uploadContents).trim()).build();
    ResponseInfo.Builder responseInfoBuilder = new ResponseInfo.Builder();
    if (batchJobUploadResponse != null) {
        responseInfoBuilder.withPayload(String.format("%d %s%n", batchJobUploadResponse.getHttpStatus(), batchJobUploadResponse.getHttpResponseMessage()));
    }
    RemoteCallReturn remoteCallReturn = new RemoteCallReturn.Builder().withRequestInfo(requestInfo).withResponseInfo(responseInfoBuilder.build()).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)

Example 5 with RequestInfo

use of com.google.api.ads.common.lib.client.RequestInfo 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

RequestInfo (com.google.api.ads.common.lib.client.RequestInfo)16 ResponseInfo (com.google.api.ads.common.lib.client.ResponseInfo)11 Test (org.junit.Test)11 RemoteCallReturn (com.google.api.ads.common.lib.client.RemoteCallReturn)6 SOAPException (javax.xml.soap.SOAPException)3 SOAPHeader (javax.xml.soap.SOAPHeader)3 SOAPMessage (javax.xml.soap.SOAPMessage)3 SOAPPart (javax.xml.soap.SOAPPart)3 StreamResult (javax.xml.transform.stream.StreamResult)3 Before (org.junit.Before)3 AdsApiConfiguration (com.google.api.ads.common.lib.conf.AdsApiConfiguration)2 RequestInfoXPathSet (com.google.api.ads.common.lib.soap.RequestInfoXPathSet)2 ResponseInfoXPathSet (com.google.api.ads.common.lib.soap.ResponseInfoXPathSet)2 NodeExtractor (com.google.api.ads.common.lib.utils.NodeExtractor)2 Streams (com.google.api.ads.common.lib.utils.Streams)2 Supplier (com.google.common.base.Supplier)2 OutputStream (java.io.OutputStream)2 Charset (java.nio.charset.Charset)2 HashSet (java.util.HashSet)2 Set (java.util.Set)2