/** * Validates and builds a {@link MongoItemWriter}. * * @return a {@link MongoItemWriter} */ public MongoItemWriter<T> build() { Assert.notNull(this.template, "template is required."); MongoItemWriter<T> writer = new MongoItemWriter<>(); writer.setTemplate(this.template); writer.setDelete(this.delete); writer.setCollection(this.collection); return writer; }
@Test public void testWriteNoTransactionWithCollection() throws Exception { List<Object> items = new ArrayList<Object>() {{ add(new Object()); add(new Object()); }}; writer.setCollection("collection"); writer.write(items); verify(template).save(items.get(0), "collection"); verify(template).save(items.get(1), "collection"); }
@Test public void testRemoveNoTransactionWithCollection() throws Exception { writer.setDelete(true); List<Object> items = new ArrayList<Object>() {{ add(new Object()); add(new Object()); }}; writer.setCollection("collection"); writer.write(items); verify(template).remove(items.get(0), "collection"); verify(template).remove(items.get(1), "collection"); }
@Test public void testWriteTransactionWithCollection() throws Exception { final List<Object> items = new ArrayList<Object>() {{ add(new Object()); add(new Object()); }}; writer.setCollection("collection"); new TransactionTemplate(transactionManager).execute((TransactionCallback<Void>) status -> { try { writer.write(items); } catch (Exception e) { fail("An exception was thrown while writing: " + e.getMessage()); } return null; }); verify(template).save(items.get(0), "collection"); verify(template).save(items.get(1), "collection"); }
@Test public void testWriteTransactionFails() throws Exception { final List<Object> items = new ArrayList<Object>() {{ add(new Object()); add(new Object()); }}; writer.setCollection("collection"); try { new TransactionTemplate(transactionManager).execute((TransactionCallback<Void>) status -> { try { writer.write(items); } catch (Exception ignore) { fail("unexpected exception thrown"); } throw new RuntimeException("force rollback"); }); } catch (RuntimeException re) { assertEquals(re.getMessage(), "force rollback"); } catch (Throwable t) { fail("Unexpected exception was thrown"); } verifyZeroInteractions(template); }
/** * A pointless use case but validates that the flag is still honored. * */ @Test public void testWriteTransactionReadOnly() throws Exception { final List<Object> items = new ArrayList<Object>() {{ add(new Object()); add(new Object()); }}; writer.setCollection("collection"); try { TransactionTemplate transactionTemplate = new TransactionTemplate(transactionManager); transactionTemplate.setReadOnly(true); transactionTemplate.execute((TransactionCallback<Void>) status -> { try { writer.write(items); } catch (Exception ignore) { fail("unexpected exception thrown"); } return null; }); } catch (Throwable t) { fail("Unexpected exception was thrown"); } verifyZeroInteractions(template); }
/** * Validates and builds a {@link MongoItemWriter}. * * @return a {@link MongoItemWriter} */ public MongoItemWriter<T> build() { Assert.notNull(this.template, "template is required."); MongoItemWriter<T> writer = new MongoItemWriter<>(); writer.setTemplate(this.template); writer.setDelete(this.delete); writer.setCollection(this.collection); return writer; }