Search in sources :

Example 1 with RestEntry

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

the class SalesforceRestWriterTest method testUpsertSuccess.

public void testUpsertSuccess() throws IOException, URISyntaxException {
    setup(SalesforceRestWriter.Operation.UPSERT);
    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);
    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("PATCH", 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) 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)

Example 2 with RestEntry

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

the class SalesforceRestWriterTest method testFailure.

public void testFailure() 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);
    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);
        Assert.fail("Should fail on 400 status code");
    } 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, never()).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 3 with RestEntry

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

the class SalesforceRestWriterTest method testBatchInsertDuplicate.

public void testBatchInsertDuplicate() 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();
    subResult1.addProperty("statusCode", 400);
    JsonArray subResultArr = new JsonArray();
    JsonObject errJson = new JsonObject();
    errJson.addProperty("errorCode", SalesforceRestWriter.DUPLICATE_VALUE_ERR_CODE);
    subResultArr.add(errJson);
    subResult1.add("result", subResultArr);
    JsonObject subResult2 = new JsonObject();
    subResult2.addProperty("statusCode", 400);
    subResult2.add("result", subResultArr);
    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; i++) {
        writer.write(restEntry);
    }
    writer.commit();
    Assert.assertEquals(writer.recordsWritten(), recordSize);
    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, times(1)).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) 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 4 with RestEntry

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

the class SalesforceRestWriterTest method testInsertSuccess.

public void testInsertSuccess() 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(200);
    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) 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)

Example 5 with RestEntry

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

the class SalesforceRestWriterTest method testBatchInsertSuccess.

public void testBatchInsertSuccess() throws IOException, URISyntaxException {
    final int recordSize = 113;
    final int batchSize = 25;
    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", false);
    ByteArrayInputStream[] streams = new ByteArrayInputStream[recordSize];
    for (int i = 0; i < recordSize - 1; i++) {
        streams[i] = new ByteArrayInputStream(jsonResponse.toString().getBytes());
    }
    when(entity.getContent()).thenReturn(new ByteArrayInputStream(jsonResponse.toString().getBytes()), streams);
    RestEntry<JsonObject> restEntry = new RestEntry<JsonObject>("test", new JsonObject());
    writer = spy(writer);
    for (int i = 0; i < recordSize; i++) {
        writer.write(restEntry);
    }
    writer.commit();
    Assert.assertEquals(writer.recordsWritten(), recordSize);
    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, times(1)).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) StatusLine(org.apache.http.StatusLine) 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)

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