CreateAsyncOperationTask implements IDatabaseTask { @Override public void prepare(final IObject query) throws TaskPrepareException { try { task = IOC.resolve( Keys.getOrAdd("db.collection.upsert"), connection, collectionNameField.in(query), documentField.in(query) ); } catch (ResolutionException e) { throw new TaskPrepareException("Can't create ISearchQuery from input query", e); } catch (ReadValueException | InvalidArgumentException e) { throw new TaskPrepareException("Can't change value in one of IObjects", e); } } CreateAsyncOperationTask(final IStorageConnection connection); @Override void prepare(final IObject query); @Override void execute();  }
@Test public void MustCorrectPrepare() throws ResolutionException, ReadValueException, InvalidArgumentException, TaskPrepareException { IObject query = mock(IObject.class); String collectionName = "examplecn"; Object document = mock(Object.class); when(collectionNameField.in(query)).thenReturn(collectionName); when(documentField.in(query)).thenReturn(document); IKey upsertTaskKey = mock(IKey.class); when(Keys.getOrAdd("db.collection.upsert")).thenReturn(upsertTaskKey); when(IOC.resolve(upsertTaskKey, connection, collectionName, document)).thenReturn(targetTask); testTask.prepare(query); verifyStatic(); Keys.getOrAdd("db.collection.upsert"); verify(collectionNameField).in(query); verify(documentField).in(query); verifyStatic(); IOC.resolve(upsertTaskKey, connection, collectionName, document); }
@Test public void MustInCorrectPrepareWhenKeysGetOrAddThrowException() throws ResolutionException { IObject query = mock(IObject.class); when(Keys.getOrAdd("db.collection.upsert")).thenThrow(new ResolutionException("")); try { testTask.prepare(query); } catch (TaskPrepareException e) { verifyStatic(); Keys.getOrAdd("db.collection.upsert"); return; } assertTrue(false); }
@Test public void MustInCorrectPrepareWhenFieldInThrowReadValueException() throws ResolutionException, ReadValueException, InvalidArgumentException { IObject query = mock(IObject.class); when(collectionNameField.in(query)).thenThrow(new ReadValueException()); IKey upsertTaskKey = mock(IKey.class); when(Keys.getOrAdd("db.collection.upsert")).thenReturn(upsertTaskKey); try { testTask.prepare(query); } catch (TaskPrepareException e) { verifyStatic(); Keys.getOrAdd("db.collection.upsert"); verify(collectionNameField).in(query); return; } assertTrue(false); }
@Test public void MustInCorrectPrepareWhenFieldInThrowInvlidArgumentException() throws ResolutionException, ReadValueException, InvalidArgumentException { IObject query = mock(IObject.class); when(collectionNameField.in(query)).thenThrow(new InvalidArgumentException("")); IKey upsertTaskKey = mock(IKey.class); when(Keys.getOrAdd("db.collection.upsert")).thenReturn(upsertTaskKey); try { testTask.prepare(query); } catch (TaskPrepareException e) { verifyStatic(); Keys.getOrAdd("db.collection.upsert"); verify(collectionNameField).in(query); return; } assertTrue(false); }
@Test public void MustInCorrectPrepareWhenIOCResolveThrowException() throws ResolutionException, ReadValueException, InvalidArgumentException { IObject query = mock(IObject.class); String collectionName = "examplecn"; Object document = mock(Object.class); when(collectionNameField.in(query)).thenReturn(collectionName); when(documentField.in(query)).thenReturn(document); IKey upsertTaskKey = mock(IKey.class); when(Keys.getOrAdd("db.collection.upsert")).thenReturn(upsertTaskKey); when(IOC.resolve(upsertTaskKey, connection, collectionName, document)).thenThrow(new ResolutionException("")); try { testTask.prepare(query); } catch (TaskPrepareException e) { verifyStatic(); Keys.getOrAdd("db.collection.upsert"); verify(collectionNameField).in(query); verify(documentField).in(query); verifyStatic(); IOC.resolve(upsertTaskKey, connection, collectionName, document); return; } assertTrue(false); }