Search in sources :

Example 1 with MockResponse

use of com.google.api.ads.common.lib.testing.MockResponse in project googleads-java-lib by googleads.

the class BatchJobUploaderTest method testUploadIncrementalBatchJobOperations_firstAndLast.

@Test
public void testUploadIncrementalBatchJobOperations_firstAndLast() throws Exception {
    BatchJobUploadStatus status = new BatchJobUploadStatus(0, URI.create(mockHttpServer.getServerUrl()));
    String uploadRequestBody = "testUpload";
    when(uploadBodyProvider.getHttpContent(request, true, true)).thenReturn(new ByteArrayContent(null, uploadRequestBody.getBytes(UTF_8)));
    List<MockResponse> expectedResponses = Lists.newArrayList(new MockResponse("ignore"), new MockResponse("testUploadResponse"));
    mockHttpServer.setMockResponses(expectedResponses);
    // Invoked the incremental upload method.
    BatchJobUploadResponse response = uploader.uploadIncrementalBatchJobOperations(request, true, status);
    assertEquals("Should have made two requests", 2, mockHttpServer.getAllResponses().size());
    // Check the first request.
    String firstRequest = mockHttpServer.getAllResponses().get(0).getRequestBody();
    assertEquals("First request should have an empty body", "", firstRequest);
    assertEquals("First request should include resumable header", "start", mockHttpServer.getAllResponses().get(0).getRequestHeader("x-goog-resumable").get(0));
    // Check the second request.
    assertEquals("Second request body is incorrect", uploadRequestBody, mockHttpServer.getLastResponse().getRequestBody());
    assertEquals("Last request should have succeeded", 200, response.getHttpStatus());
    // Check the BatchJobUploadStatus.
    BatchJobUploadStatus expectedStatus = new BatchJobUploadStatus(uploadRequestBody.getBytes(UTF_8).length, URI.create(mockHttpServer.getServerUrl()));
    BatchJobUploadStatus actualStatus = response.getBatchJobUploadStatus();
    assertEquals("Status total content length is incorrect", expectedStatus.getTotalContentLength(), actualStatus.getTotalContentLength());
    assertEquals("Status resumable upload URI is incorrect", expectedStatus.getResumableUploadUri(), actualStatus.getResumableUploadUri());
}
Also used : MockResponse(com.google.api.ads.common.lib.testing.MockResponse) ByteArrayContent(com.google.api.client.http.ByteArrayContent) Test(org.junit.Test)

Example 2 with MockResponse

use of com.google.api.ads.common.lib.testing.MockResponse in project googleads-java-lib by googleads.

the class HttpHandlerTest method testInvokeReturnsValidXml.

/**
 * Tests that a valid XML response results in a successful invocation of the handler that produces
 * a valid SOAP envelope.
 */
@Test
public void testInvokeReturnsValidXml() throws IOException {
    // Unlike the failure tests below, create the MessageContext here with an actual AxisClient,
    // not a mock AxisEngine. Otherwise, the call to getSOAPEnvelope below will fail.
    MessageContext messageContext = new MessageContext(new AxisClient());
    messageContext.setRequestMessage(requestMessage);
    messageContext.setProperty(MessageContext.TRANS_URL, mockHttpServer.getServerUrl());
    SoapResponseXmlProvider.getTestSoapResponse(API_VERSION);
    mockHttpServer.setMockResponse(new MockResponse(SoapResponseXmlProvider.getTestSoapResponse(API_VERSION)));
    httpHandler.invoke(messageContext);
    assertNotNull("SOAP envelope of response is null", messageContext.getResponseMessage().getSOAPEnvelope());
}
Also used : MockResponse(com.google.api.ads.common.lib.testing.MockResponse) AxisClient(org.apache.axis.client.AxisClient) MessageContext(org.apache.axis.MessageContext) Test(org.junit.Test)

Example 3 with MockResponse

use of com.google.api.ads.common.lib.testing.MockResponse in project googleads-java-lib by googleads.

the class HttpHandlerTest method testInvokeReturnsNonXmlResponse.

/**
 * Tests that a failed, non-XML response results in an AxisFault containing the HTTP status and
 * message.
 */
@Test
public void testInvokeReturnsNonXmlResponse() throws AxisFault {
    MessageContext messageContext = new MessageContext(axisEngine);
    messageContext.setRequestMessage(requestMessage);
    messageContext.setProperty(MessageContext.TRANS_URL, mockHttpServer.getServerUrl());
    messageContext.setProperty(HTTPConstants.MC_GZIP_REQUEST, true);
    MockResponse mockResponse = new MockResponse("Something went wrong", 500);
    mockResponse.setContentType("text/html");
    mockHttpServer.setMockResponse(mockResponse);
    // Expect an AxisFault based on the status code and content type.
    thrown.expect(AxisFault.class);
    httpHandler.invoke(messageContext);
}
Also used : MockResponse(com.google.api.ads.common.lib.testing.MockResponse) MessageContext(org.apache.axis.MessageContext) Test(org.junit.Test)

Example 4 with MockResponse

use of com.google.api.ads.common.lib.testing.MockResponse in project googleads-java-lib by googleads.

the class BatchJobUploaderTest method testUploadIncrementalBatchJobOperations_notFirst.

@Test
public void testUploadIncrementalBatchJobOperations_notFirst() throws Exception {
    BatchJobUploadStatus status = new BatchJobUploadStatus(10, URI.create(mockHttpServer.getServerUrl()));
    String uploadRequestBody = "<mutate>testUpload</mutate>";
    when(uploadBodyProvider.getHttpContent(request, false, true)).thenReturn(new ByteArrayContent(null, uploadRequestBody.getBytes(UTF_8)));
    mockHttpServer.setMockResponse(new MockResponse("testUploadResponse"));
    // Invoked the incremental upload method.
    BatchJobUploadResponse response = uploader.uploadIncrementalBatchJobOperations(request, true, status);
    assertEquals("Should have made one request", 1, mockHttpServer.getAllResponses().size());
    // Check the request.
    String firstRequest = mockHttpServer.getLastResponse().getRequestBody();
    String expectedBody = "testUpload</mutate>";
    expectedBody = Strings.padEnd(expectedBody, BatchJobUploader.REQUIRED_CONTENT_LENGTH_INCREMENT, ' ');
    assertEquals("Request body is incorrect", expectedBody, firstRequest);
    assertEquals("Request should have succeeded", 200, response.getHttpStatus());
    // Check the BatchJobUploadStatus.
    BatchJobUploadStatus expectedStatus = new BatchJobUploadStatus(status.getTotalContentLength() + expectedBody.getBytes(UTF_8).length, URI.create(mockHttpServer.getServerUrl()));
    BatchJobUploadStatus actualStatus = response.getBatchJobUploadStatus();
    assertEquals("Status total content length is incorrect", expectedStatus.getTotalContentLength(), actualStatus.getTotalContentLength());
    assertEquals("Status resumable upload URI is incorrect", expectedStatus.getResumableUploadUri(), actualStatus.getResumableUploadUri());
}
Also used : MockResponse(com.google.api.ads.common.lib.testing.MockResponse) ByteArrayContent(com.google.api.client.http.ByteArrayContent) Test(org.junit.Test)

Example 5 with MockResponse

use of com.google.api.ads.common.lib.testing.MockResponse in project googleads-java-lib by googleads.

the class BatchJobUploaderTest method testUploadIncrementalBatchJobOperations_logging.

@Test
public void testUploadIncrementalBatchJobOperations_logging() throws Exception {
    BatchJobUploadStatus status = new BatchJobUploadStatus(10, URI.create(mockHttpServer.getServerUrl()));
    String uploadRequestBody = "<mutate>testUpload</mutate>";
    when(uploadBodyProvider.getHttpContent(request, false, true)).thenReturn(new ByteArrayContent(null, uploadRequestBody.getBytes(UTF_8)));
    mockHttpServer.setMockResponse(new MockResponse("testUploadResponse"));
    String expectedBody = "testUpload</mutate>";
    expectedBody = Strings.padEnd(expectedBody, BatchJobUploader.REQUIRED_CONTENT_LENGTH_INCREMENT, ' ');
    // Invoke the incremental upload method.
    BatchJobUploadResponse response = uploader.uploadIncrementalBatchJobOperations(request, true, status);
    verify(batchJobLogger, times(1)).logUpload(expectedBody, status.getResumableUploadUri(), response, null);
}
Also used : MockResponse(com.google.api.ads.common.lib.testing.MockResponse) ByteArrayContent(com.google.api.client.http.ByteArrayContent) Test(org.junit.Test)

Aggregations

MockResponse (com.google.api.ads.common.lib.testing.MockResponse)8 Test (org.junit.Test)8 ByteArrayContent (com.google.api.client.http.ByteArrayContent)4 MessageContext (org.apache.axis.MessageContext)4 AxisClient (org.apache.axis.client.AxisClient)1