@Test public void should_throw_error_when_cannot_bulk_save_offer_log() throws Exception { MongoCollection<Document> collection = mock(MongoCollection.class); doThrow(MongoWriteException.class).when(collection).insertMany(anyList(), any()); OfferLogDatabaseService offerLogDatabaseService = new OfferLogDatabaseService(collection); ThrowingCallable save = () -> offerLogDatabaseService.bulkSave("containerName", Collections.singletonList("robin"), DELETE, 15); assertThatThrownBy(save).isInstanceOf(ContentAddressableStorageDatabaseException.class); }
public void bulkSave(String containerName, List<String> fileNames, OfferLogAction action, long sequence) throws ContentAddressableStorageServerException, ContentAddressableStorageDatabaseException { try { List<Document> documents = new ArrayList<>(); for (String fileName : fileNames) { OfferLog offerLog = new OfferLog(sequence, LocalDateUtil.now(), containerName, fileName, action); documents.add(Document.parse(JsonHandler.writeAsString(offerLog))); sequence++; } mongoCollection.insertMany(documents, new InsertManyOptions().ordered(false)); } catch (MongoException e) { throw new ContentAddressableStorageDatabaseException(String.format( "Database Error while saving %s in OfferLog collection", fileNames), e); } catch (InvalidParseOperationException exc) { throw new ContentAddressableStorageServerException("Cannot parse storage log", exc); } }
OfferLogDatabaseService { public void bulkSave(String containerName, List<String> fileNames, OfferLogAction action, long sequence) throws ContentAddressableStorageServerException, ContentAddressableStorageDatabaseException { try { List<Document> documents = new ArrayList<>(); for (String fileName : fileNames) { OfferLog offerLog = new OfferLog(sequence, LocalDateUtil.now(), containerName, fileName, action); documents.add(Document.parse(JsonHandler.writeAsString(offerLog))); sequence++; } mongoCollection.insertMany(documents, new InsertManyOptions().ordered(false)); } catch (MongoException e) { throw new ContentAddressableStorageDatabaseException(String.format( "Database Error while saving %s in OfferLog collection", fileNames), e); } catch (InvalidParseOperationException exc) { throw new ContentAddressableStorageServerException("Cannot parse storage log", exc); } } }
OfferLogDatabaseService { public void bulkSave(String containerName, List<String> fileNames, OfferLogAction action, long sequence) throws ContentAddressableStorageServerException, ContentAddressableStorageDatabaseException { try { List<Document> documents = new ArrayList<>(); for (String fileName : fileNames) { OfferLog offerLog = new OfferLog(sequence, LocalDateUtil.now(), containerName, fileName, action); documents.add(Document.parse(JsonHandler.writeAsString(offerLog))); sequence++; } mongoCollection.insertMany(documents, new InsertManyOptions().ordered(false)); } catch (MongoException e) { throw new ContentAddressableStorageDatabaseException(String.format( "Database Error while saving %s in OfferLog collection", fileNames), e); } catch (InvalidParseOperationException exc) { throw new ContentAddressableStorageServerException("Cannot parse storage log", exc); } } OfferLogDatabaseService(MongoCollection<Document> mongoCollection); }
OfferLogDatabaseService { public void bulkSave(String containerName, List<String> fileNames, OfferLogAction action, long sequence) throws ContentAddressableStorageServerException, ContentAddressableStorageDatabaseException { try { List<Document> documents = new ArrayList<>(); for (String fileName : fileNames) { OfferLog offerLog = new OfferLog(sequence, LocalDateUtil.now(), containerName, fileName, action); documents.add(Document.parse(JsonHandler.writeAsString(offerLog))); sequence++; } mongoCollection.insertMany(documents, new InsertManyOptions().ordered(false)); } catch (MongoException e) { throw new ContentAddressableStorageDatabaseException(String.format( "Database Error while saving %s in OfferLog collection", fileNames), e); } catch (InvalidParseOperationException exc) { throw new ContentAddressableStorageServerException("Cannot parse storage log", exc); } } OfferLogDatabaseService(MongoCollection<Document> mongoCollection); void save(String containerName, String fileName, OfferLogAction action, long sequence); void bulkSave(String containerName, List<String> fileNames, OfferLogAction action, long sequence); CloseableIterable<OfferLog> getDescendingOfferLogsBy(String containerName, Long offset, int limit); CloseableIterable<OfferLog> getAscendingOfferLogsBy(String containerName, Long offset, int limit); CloseableIterable<OfferLog> getExpiredOfferLogByContainer(long expirationValue, ChronoUnit expirationUnit); }
OfferLogDatabaseService { public void bulkSave(String containerName, List<String> fileNames, OfferLogAction action, long sequence) throws ContentAddressableStorageServerException, ContentAddressableStorageDatabaseException { try { List<Document> documents = new ArrayList<>(); for (String fileName : fileNames) { OfferLog offerLog = new OfferLog(sequence, LocalDateUtil.now(), containerName, fileName, action); documents.add(Document.parse(JsonHandler.writeAsString(offerLog))); sequence++; } mongoCollection.insertMany(documents, new InsertManyOptions().ordered(false)); } catch (MongoException e) { throw new ContentAddressableStorageDatabaseException(String.format( "Database Error while saving %s in OfferLog collection", fileNames), e); } catch (InvalidParseOperationException exc) { throw new ContentAddressableStorageServerException("Cannot parse storage log", exc); } } OfferLogDatabaseService(MongoCollection<Document> mongoCollection); void save(String containerName, String fileName, OfferLogAction action, long sequence); void bulkSave(String containerName, List<String> fileNames, OfferLogAction action, long sequence); CloseableIterable<OfferLog> getDescendingOfferLogsBy(String containerName, Long offset, int limit); CloseableIterable<OfferLog> getAscendingOfferLogsBy(String containerName, Long offset, int limit); CloseableIterable<OfferLog> getExpiredOfferLogByContainer(long expirationValue, ChronoUnit expirationUnit);  }