UpdateAsyncOperationTask implements IDatabaseTask { @Override public void prepare(final IObject query) throws TaskPrepareException { try { IObject document = documentField.in(query); doneFlagField.out(document, true); upsertTask = IOC.resolve(Keys.getOrAdd("db.collection.upsert"), connection, collectionNameField.in(query), document); } catch (ReadValueException | ChangeValueException | ResolutionException | InvalidArgumentException e) { throw new TaskPrepareException("Can't prepare query for update into async operation collection", e); } } UpdateAsyncOperationTask(final IStorageConnection connection); @Override void prepare(final IObject query); @Override void execute();  }
@Test public void MustCorrectPrepare() throws ReadValueException, InvalidArgumentException, ResolutionException, TaskPrepareException, ChangeValueException { IObject query = mock(IObject.class); IObject document = mock(IObject.class); when(documentField.in(query)).thenReturn(document); String collectionName = "example"; IKey upsertTaskKey = mock(IKey.class); when(Keys.getOrAdd("db.collection.upsert")).thenReturn(upsertTaskKey); when(collectionNameField.in(query)).thenReturn(collectionName); when(IOC.resolve(upsertTaskKey, connection, collectionName, document)).thenReturn(targetTask); testTask.prepare(query); verify(documentField).in(query); verify(doneFlagField).out(document, true); verifyStatic(); Keys.getOrAdd("db.collection.upsert"); verifyStatic(); IOC.resolve(upsertTaskKey, connection, collectionName, document); }
@Test public void MustInCorrectPrepareWhenDocumentFieldInThrowReadValueException() throws ReadValueException, InvalidArgumentException { IObject query = mock(IObject.class); when(documentField.in(query)).thenThrow(new ReadValueException()); try { testTask.prepare(query); } catch (TaskPrepareException e) { verify(documentField).in(query); return; } assertTrue(false); }
@Test public void MustInCorrectPrepareWhenDocumentFieldInThrowInvalidArgumentException() throws ReadValueException, InvalidArgumentException { IObject query = mock(IObject.class); when(documentField.in(query)).thenThrow(new InvalidArgumentException("")); try { testTask.prepare(query); } catch (TaskPrepareException e) { verify(documentField).in(query); return; } assertTrue(false); }
@Test public void MustInCorrectPrepareWhenDoneFlagFieldOutThrowException() throws ReadValueException, InvalidArgumentException, ResolutionException, TaskPrepareException, ChangeValueException { IObject query = mock(IObject.class); IObject document = mock(IObject.class); when(documentField.in(query)).thenReturn(document); doThrow(new ChangeValueException()).when(doneFlagField).out(document, true); try { testTask.prepare(query); } catch (TaskPrepareException e) { verify(documentField).in(query); verify(doneFlagField).out(document, true); return; } assertTrue(false); }
@Test public void MustInCorrectPrepareWhenKeysGetOrAddThrowException() throws ReadValueException, InvalidArgumentException, ResolutionException, TaskPrepareException, ChangeValueException { IObject query = mock(IObject.class); IObject document = mock(IObject.class); when(documentField.in(query)).thenReturn(document); when(Keys.getOrAdd("db.collection.upsert")).thenThrow(new ResolutionException("")); try { testTask.prepare(query); } catch (TaskPrepareException e) { verify(documentField).in(query); verify(doneFlagField).out(document, true); verifyStatic(); Keys.getOrAdd("db.collection.upsert"); return; } assertTrue(false); }
@Test public void MustInCorrectPrepareWhenIOCResolveThrowException() throws ReadValueException, InvalidArgumentException, ResolutionException, TaskPrepareException, ChangeValueException { IObject query = mock(IObject.class); IObject document = mock(IObject.class); when(documentField.in(query)).thenReturn(document); String collectionName = "example"; IKey upsertTaskKey = mock(IKey.class); when(Keys.getOrAdd("db.collection.upsert")).thenReturn(upsertTaskKey); when(collectionNameField.in(query)).thenReturn(collectionName); when(IOC.resolve(upsertTaskKey, connection, collectionName, document)).thenThrow(new ResolutionException("")); try { testTask.prepare(query); } catch (TaskPrepareException e) { verify(documentField).in(query); verify(doneFlagField).out(document, true); verifyStatic(); Keys.getOrAdd("db.collection.upsert"); verifyStatic(); IOC.resolve(upsertTaskKey, connection, collectionName, document); return; } assertTrue(false); }