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));
}
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));
}
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));
}
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));
}
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));
}
Aggregations