UpsertIntoCachedCollectionTask implements IDatabaseTask { @Override public void prepare(final IObject query) throws TaskPrepareException { try { if (startDateTimeField.in(query) == null) { startDateTimeField.out(query, LocalDateTime.now().format(formatter)); } upsertTask = IOC.resolve( Keys.getOrAdd("db.collection.upsert"), connection, collectionNameField.in(query), documentField.in(query) ); } catch (InvalidArgumentException | ReadValueException | ChangeValueException | ResolutionException e) { throw new TaskPrepareException("Can't prepare query for upsert into cached collection", e); } } UpsertIntoCachedCollectionTask(final IStorageConnection connection); @Override void prepare(final IObject query); @Override void execute();  }
@Test public void ShouldPrepareUpsertQuery() throws Exception { IObject query = mock(IObject.class); IObject doc = mock(IObject.class); when(collectionNameField.in(query)).thenReturn("collectionName"); when(documentField.in(query)).thenReturn(doc); when(startDateTimeField.in(query)).thenReturn(null); task.prepare(query); verifyStatic(); IOC.resolve( Keys.getOrAdd("db.collection.upsert"), connection, "collectionName", doc ); }
@Test(expected = TaskPrepareException.class) public void ShouldThrowException_When_ResolutionExceptionIsThrown() throws Exception { IObject rawQuery = mock(IObject.class); when(startDateTimeField.in(rawQuery)).thenThrow(ChangeValueException.class); task.prepare(rawQuery); }