Search in sources :

Example 6 with RestEntry

use of org.apache.gobblin.converter.http.RestEntry in project incubator-gobblin by apache.

the class SalesforceRestWriterTest method testBatchInsertFailure.

public void testBatchInsertFailure() throws IOException, URISyntaxException {
    final int recordSize = 25;
    final int batchSize = recordSize;
    State state = new State();
    state.appendToSetProp(CONF_PREFIX + BATCH_SIZE, Integer.toString(batchSize));
    state.appendToSetProp(CONF_PREFIX + BATCH_RESOURCE_PATH, "test");
    setup(SalesforceRestWriter.Operation.INSERT_ONLY_NOT_EXIST, state);
    CloseableHttpResponse response = mock(CloseableHttpResponse.class);
    StatusLine statusLine = mock(StatusLine.class);
    when(client.execute(any(HttpUriRequest.class))).thenReturn(response);
    when(response.getStatusLine()).thenReturn(statusLine);
    when(statusLine.getStatusCode()).thenReturn(200);
    HttpEntity entity = mock(HttpEntity.class);
    when(response.getEntity()).thenReturn(entity);
    JsonObject jsonResponse = new JsonObject();
    jsonResponse.addProperty("hasErrors", true);
    JsonArray resultJsonArr = new JsonArray();
    jsonResponse.add("results", resultJsonArr);
    JsonObject subResult1 = new JsonObject();
    // Success
    subResult1.addProperty("statusCode", 201);
    JsonObject subResult2 = new JsonObject();
    // Failure
    subResult2.addProperty("statusCode", 500);
    resultJsonArr.add(subResult1);
    resultJsonArr.add(subResult2);
    when(entity.getContent()).thenReturn(new ByteArrayInputStream(jsonResponse.toString().getBytes()));
    RestEntry<JsonObject> restEntry = new RestEntry<JsonObject>("test", new JsonObject());
    writer = spy(writer);
    for (int i = 0; i < recordSize - 1; i++) {
        writer.write(restEntry);
    }
    try {
        writer.write(restEntry);
        Assert.fail("Should have failed with failed response. " + jsonResponse.toString());
    } catch (Exception e) {
        Assert.assertTrue(e instanceof RuntimeException);
    }
    Assert.assertEquals(writer.recordsWritten(), (long) 0L);
    verify(writer, times(recordSize)).writeImpl(restEntry);
    verify(writer, times(recordSize)).onNewRecord(restEntry);
    double sendCount = ((double) recordSize) / ((double) batchSize);
    sendCount = Math.ceil(sendCount);
    verify(writer, times((int) sendCount)).sendRequest(any(HttpUriRequest.class));
    verify(client, times((int) sendCount)).execute(any(HttpUriRequest.class));
    verify(writer, times((int) sendCount)).waitForResponse(any(ListenableFuture.class));
    verify(writer, times((int) sendCount)).processResponse(any(CloseableHttpResponse.class));
    verify(writer, never()).flush();
    verify(writer, never()).onConnect(any(URI.class));
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) HttpEntity(org.apache.http.HttpEntity) JsonObject(com.google.gson.JsonObject) URI(java.net.URI) ClientProtocolException(org.apache.http.client.ClientProtocolException) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) StatusLine(org.apache.http.StatusLine) JsonArray(com.google.gson.JsonArray) ByteArrayInputStream(java.io.ByteArrayInputStream) State(org.apache.gobblin.configuration.State) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) RestEntry(org.apache.gobblin.converter.http.RestEntry)

Example 7 with RestEntry

use of org.apache.gobblin.converter.http.RestEntry in project incubator-gobblin by apache.

the class SalesforceRestWriterTest method testAccessTokenReacquire.

public void testAccessTokenReacquire() throws IOException, URISyntaxException {
    setup(SalesforceRestWriter.Operation.INSERT_ONLY_NOT_EXIST);
    CloseableHttpResponse response = mock(CloseableHttpResponse.class);
    StatusLine statusLine = mock(StatusLine.class);
    when(client.execute(any(HttpUriRequest.class))).thenReturn(response);
    when(response.getStatusLine()).thenReturn(statusLine);
    when(statusLine.getStatusCode()).thenReturn(401);
    RestEntry<JsonObject> restEntry = new RestEntry<JsonObject>("test", new JsonObject());
    Optional<HttpUriRequest> request = writer.onNewRecord(restEntry);
    Assert.assertTrue(request.isPresent(), "No HttpUriRequest from onNewRecord");
    Assert.assertEquals("POST", request.get().getMethod());
    writer = spy(writer);
    try {
        writer.write(restEntry);
    } catch (Exception e) {
    }
    verify(writer, times(1)).writeImpl(restEntry);
    verify(writer, times(1)).onNewRecord(restEntry);
    verify(writer, times(1)).sendRequest(any(HttpUriRequest.class));
    verify(client, times(1)).execute(any(HttpUriRequest.class));
    verify(writer, times(1)).waitForResponse(any(ListenableFuture.class));
    verify(writer, times(1)).processResponse(any(CloseableHttpResponse.class));
    verify(writer, times(1)).onConnect(any(URI.class));
}
Also used : StatusLine(org.apache.http.StatusLine) HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) JsonObject(com.google.gson.JsonObject) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) URI(java.net.URI) ClientProtocolException(org.apache.http.client.ClientProtocolException) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) RestEntry(org.apache.gobblin.converter.http.RestEntry)

Example 8 with RestEntry

use of org.apache.gobblin.converter.http.RestEntry in project incubator-gobblin by apache.

the class SalesforceRestWriterTest method testInsertDuplicate.

public void testInsertDuplicate() throws IOException, URISyntaxException {
    setup(SalesforceRestWriter.Operation.INSERT_ONLY_NOT_EXIST);
    CloseableHttpResponse response = mock(CloseableHttpResponse.class);
    StatusLine statusLine = mock(StatusLine.class);
    when(client.execute(any(HttpUriRequest.class))).thenReturn(response);
    when(response.getStatusLine()).thenReturn(statusLine);
    when(statusLine.getStatusCode()).thenReturn(400);
    HttpEntity entity = mock(HttpEntity.class);
    when(response.getEntity()).thenReturn(entity);
    JsonObject json = new JsonObject();
    json.addProperty("errorCode", SalesforceRestWriter.DUPLICATE_VALUE_ERR_CODE);
    JsonArray jsonArray = new JsonArray();
    jsonArray.add(json);
    when(entity.getContent()).thenReturn(new ByteArrayInputStream(jsonArray.toString().getBytes()));
    RestEntry<JsonObject> restEntry = new RestEntry<JsonObject>("test", new JsonObject());
    Optional<HttpUriRequest> request = writer.onNewRecord(restEntry);
    Assert.assertTrue(request.isPresent(), "No HttpUriRequest from onNewRecord");
    Assert.assertEquals("POST", request.get().getMethod());
    writer = spy(writer);
    writer.write(restEntry);
    verify(writer, times(1)).writeImpl(restEntry);
    verify(writer, times(1)).onNewRecord(restEntry);
    verify(writer, times(1)).sendRequest(any(HttpUriRequest.class));
    verify(client, times(1)).execute(any(HttpUriRequest.class));
    verify(writer, times(1)).waitForResponse(any(ListenableFuture.class));
    verify(writer, times(1)).processResponse(any(CloseableHttpResponse.class));
    verify(writer, never()).onConnect(any(URI.class));
}
Also used : StatusLine(org.apache.http.StatusLine) HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) JsonArray(com.google.gson.JsonArray) HttpEntity(org.apache.http.HttpEntity) ByteArrayInputStream(java.io.ByteArrayInputStream) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) JsonObject(com.google.gson.JsonObject) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) URI(java.net.URI) RestEntry(org.apache.gobblin.converter.http.RestEntry)

Aggregations

ListenableFuture (com.google.common.util.concurrent.ListenableFuture)8 JsonObject (com.google.gson.JsonObject)8 URI (java.net.URI)8 RestEntry (org.apache.gobblin.converter.http.RestEntry)8 StatusLine (org.apache.http.StatusLine)8 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)8 HttpUriRequest (org.apache.http.client.methods.HttpUriRequest)8 ByteArrayInputStream (java.io.ByteArrayInputStream)4 HttpEntity (org.apache.http.HttpEntity)4 JsonArray (com.google.gson.JsonArray)3 IOException (java.io.IOException)3 URISyntaxException (java.net.URISyntaxException)3 State (org.apache.gobblin.configuration.State)3 ClientProtocolException (org.apache.http.client.ClientProtocolException)3