### Question:
BufferedBulk extends Bulk { public BufferedBulkResult deserializeResponse(InputStream responseBody) throws IOException { return objectReader.readValue(responseBody); } BufferedBulk(BufferedBulk.Builder builder); BufferedBulkResult deserializeResponse(InputStream responseBody); ByteBuf serializeRequest(); String getURI(); void completed(); Collection<BulkableAction> getActions(); static final char LINE_SEPARATOR; }### Answer:
@Test public void canDeserializeErrorResponse() throws IOException { TestResult testResult = new TestResult(); testResult.errors = true; testResult.took = new Random().nextInt(1000); BufferedBulk.Builder builder = createDefaultTestMockedBuilder(); builder.withObjectReader(new BufferedBulkOperations(mock(PooledItemSourceFactory.class)).configuredReader()); BufferedBulk bulk = builder.build(); ObjectMapper mapper = new ObjectMapper(); mapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY); InputStream inputStream = new ByteArrayInputStream(mapper.writeValueAsBytes(testResult)); BufferedBulkResult result = bulk.deserializeResponse(inputStream); assertFalse(result.isSucceeded()); assertEquals(testResult.took, result.getTook()); }
@Test public void canDeserializeSuccessResponse() throws IOException { TestResult testResult = new TestResult(); testResult.errors = false; testResult.took = new Random().nextInt(1000); BufferedBulk.Builder builder = createDefaultTestMockedBuilder(); builder.withObjectReader(new BufferedBulkOperations(mock(PooledItemSourceFactory.class)).configuredReader()); BufferedBulk bulk = builder.build(); ObjectMapper mapper = new ObjectMapper(); mapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY); InputStream inputStream = new ByteArrayInputStream(mapper.writeValueAsBytes(testResult)); BufferedBulkResult result = bulk.deserializeResponse(inputStream); assertTrue(result.isSucceeded()); assertEquals(testResult.took, result.getTook()); }
@Test public void canDeserializeSuccessWithFailedItemsResponse() throws IOException { Random random = new Random(); BulkResultItem bulkResultItem = createTestErrorBulkResultItem(random.nextInt(1000)); int took = random.nextInt(1000); TestResult testResult = new TestResult(); testResult.errors = true; testResult.took = took; testResult.items = new ArrayList<>(); testResult.items.add(bulkResultItem); BufferedBulk.Builder builder = createDefaultTestMockedBuilder(); builder.withObjectReader(new BufferedBulkOperations(mock(PooledItemSourceFactory.class)).configuredReader()); BufferedBulk bulk = builder.build(); ObjectMapper mapper = new ObjectMapper() .setSerializationInclusion(JsonInclude.Include.NON_EMPTY) .addMixIn(BulkResultItem.class, BulkResultItemMixIn.class); InputStream inputStream = new ByteArrayInputStream(mapper.writeValueAsBytes(testResult)); BufferedBulkResult result = bulk.deserializeResponse(inputStream); assertFalse(result.isSucceeded()); assertEquals(testResult.took, result.getTook()); BulkResultItem resultItem = testResult.items.get(0); assertEquals(bulkResultItem, resultItem); }
@Test public void canDeserializeSuccessWithErrorResponse() throws IOException { TestResult testResult = new TestResult(); testResult.error = createTestBulkError(); testResult.status = new Random().nextInt(1000); BufferedBulk.Builder builder = createDefaultTestMockedBuilder(); builder.withObjectReader(new BufferedBulkOperations(mock(PooledItemSourceFactory.class)).configuredReader()); BufferedBulk bulk = builder.build(); ObjectMapper mapper = new ObjectMapper(); mapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY); InputStream inputStream = new ByteArrayInputStream(mapper.writeValueAsBytes(testResult)); BufferedBulkResult result = bulk.deserializeResponse(inputStream); assertFalse(result.isSucceeded()); assertEquals(testResult.took, result.getTook()); assertEquals(testResult.error.getType(), result.getError().getType()); assertEquals(testResult.error.getReason(), result.getError().getReason()); assertEquals(testResult.status, result.getStatus()); }