MultiIndexDao implements IndexDao { @Override public Map<Document, Optional<String>> batchUpdate(Map<Document, Optional<String>> updates) throws IOException { List<String> exceptions = indices.parallelStream().map(dao -> { try { dao.batchUpdate(updates); return null; } catch (Throwable e) { return dao.getClass() + ": " + e.getMessage() + "\n" + ExceptionUtils.getStackTrace(e); } }).filter(e -> e != null).collect(Collectors.toList()); if (exceptions.size() > 0) { throw new IOException(Joiner.on("\n").join(exceptions)); } return updates; } MultiIndexDao(IndexDao... composedDao);  MultiIndexDao(Iterable<IndexDao> composedDao);  MultiIndexDao(Iterable<IndexDao> composedDao, Function<IndexDao, IndexDao> decoratorTransformation); @Override Document update(final Document update, Optional<String> index); @Override Map<Document, Optional<String>> batchUpdate(Map<Document, Optional<String>> updates); @Override Map<String, FieldType> getColumnMetadata(List<String> in); @Override Document addCommentToAlert(CommentAddRemoveRequest request); @Override Document addCommentToAlert(CommentAddRemoveRequest request, Document latest); @Override Document removeCommentFromAlert(CommentAddRemoveRequest request); @Override Document removeCommentFromAlert(CommentAddRemoveRequest request, Document latest); @Override SearchResponse search(SearchRequest searchRequest); @Override GroupResponse group(GroupRequest groupRequest); @Override void init(AccessConfig config); @Override Document getLatest(final String guid, String sensorType); @Override Iterable<Document> getAllLatest(List<GetRequest> getRequests); List<IndexDao> getIndices();  }
@Test public void shouldBatchUpdateAll() throws IOException { Map<Document, Optional<String>> updates = new HashMap<Document, Optional<String>>() {{ put(document1, Optional.of("bro")); put(document2, Optional.of("bro")); }}; Map<Document, Optional<String>> actual = multiIndexDao.batchUpdate(updates); assertEquals(updates, actual); verify(dao1).batchUpdate(eq(updates)); verify(dao2).batchUpdate(eq(updates)); }
@Test public void shouldThrowExceptionWithPartialFailureOnBatchUpdate() throws IOException { when(dao2.batchUpdate(any())).thenThrow(new IllegalStateException()); Map<Document, Optional<String>> updates = new HashMap<Document, Optional<String>>() {{ put(document1, Optional.of("bro")); put(document2, Optional.of("bro")); }}; assertThrows(IOException.class, () -> multiIndexDao.batchUpdate(updates)); }