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