GetAsyncOperationTask implements IDatabaseTask { @Override public void prepare(final IObject query) throws TaskPrepareException { try { IObject queryForNestedTask = IOC.resolve(Keys.getOrAdd("info.smart_tools.smartactors.iobject.iobject.IObject")); IObject filterObject = IOC.resolve(Keys.getOrAdd("info.smart_tools.smartactors.iobject.iobject.IObject")); String token = tokenField.in(query); IObject eqKeyObject = IOC.resolve(Keys.getOrAdd("info.smart_tools.smartactors.iobject.iobject.IObject")); equalsField.out(eqKeyObject, token); tokenField.out(filterObject, eqKeyObject); filterField.out(queryForNestedTask, filterObject); getItemTask = IOC.resolve( Keys.getOrAdd("db.collection.search"), connection, collectionNameField.in(query), queryForNestedTask, callbackField.in(query) ); } catch (ResolutionException e) { throw new TaskPrepareException("Can't create ISearchQuery from input query", e); } catch (ReadValueException | ChangeValueException | InvalidArgumentException e) { throw new TaskPrepareException("Can't change value in one of IObjects", e); } } GetAsyncOperationTask(final IStorageConnection connection); @Override void prepare(final IObject query); @Override void execute();  }
@Test public void MustCorrectPrepare() throws ResolutionException, ReadValueException, InvalidArgumentException, TaskPrepareException, ChangeValueException { IObject query = mock(IObject.class); IObject queryForNestedTask = mock(IObject.class); IObject filterObject = mock(IObject.class); IObject eqKeyObject = mock(IObject.class); IKey iObjectKey = mock(IKey.class); when(Keys.getOrAdd("info.smart_tools.smartactors.iobject.iobject.IObject")).thenReturn(iObjectKey); when(IOC.resolve(iObjectKey)).thenReturn(queryForNestedTask).thenReturn(filterObject).thenReturn(eqKeyObject); String token = "token"; when(tokenField.in(query)).thenReturn(token); String collectionName = "collectionName"; when(collectionNameField.in(query)).thenReturn(collectionName); Object callback = mock(Object.class); when(callbackField.in(query)).thenReturn(callback); IKey searchTaskKey = mock(IKey.class); when(Keys.getOrAdd("db.collection.search")).thenReturn(searchTaskKey); when(IOC.resolve(searchTaskKey, connection, collectionName, queryForNestedTask, callback)).thenReturn(targetTask); testTask.prepare(query); verifyStatic(times(3)); Keys.getOrAdd("info.smart_tools.smartactors.iobject.iobject.IObject"); verifyStatic(times(3)); IOC.resolve(iObjectKey); verify(tokenField).in(query); verify(equalsField).out(eqKeyObject, token); verify(tokenField).out(filterObject, eqKeyObject); verify(filterField).out(queryForNestedTask, filterObject); verifyStatic(); Keys.getOrAdd("db.collection.search"); verifyStatic(); IOC.resolve(searchTaskKey, connection, collectionName, queryForNestedTask, callback); }
@Test public void MustInCorrectPrepareWhenKeysGetOrAddThrowException() throws ResolutionException { IObject query = mock(IObject.class); when(Keys.getOrAdd("info.smart_tools.smartactors.iobject.iobject.IObject")).thenThrow(new ResolutionException("")); try { testTask.prepare(query); } catch (TaskPrepareException e) { verifyStatic(); Keys.getOrAdd("info.smart_tools.smartactors.iobject.iobject.IObject"); return; } assertTrue(false); }
@Test public void MustInCorrectPrepareWhenIOCResolveThrowException() throws ResolutionException { IObject query = mock(IObject.class); IKey iObjectKey = mock(IKey.class); when(Keys.getOrAdd("info.smart_tools.smartactors.iobject.iobject.IObject")).thenReturn(iObjectKey); when(IOC.resolve(iObjectKey)).thenThrow(new ResolutionException("")); try { testTask.prepare(query); } catch (TaskPrepareException e) { verifyStatic(); Keys.getOrAdd("info.smart_tools.smartactors.iobject.iobject.IObject"); verifyStatic(); IOC.resolve(iObjectKey); return; } assertTrue(false); }
@Test public void MustInCorrectPrepareWhenFieldInThrowReadValueException() throws ReadValueException, InvalidArgumentException, ResolutionException { IObject query = mock(IObject.class); IObject queryForNestedTask = mock(IObject.class); IObject filterObject = mock(IObject.class); IKey iObjectKey = mock(IKey.class); when(Keys.getOrAdd("info.smart_tools.smartactors.iobject.iobject.IObject")).thenReturn(iObjectKey); when(IOC.resolve(iObjectKey)).thenReturn(queryForNestedTask).thenReturn(filterObject); when(tokenField.in(query)).thenThrow(new ReadValueException()); try { testTask.prepare(query); } catch (TaskPrepareException e) { verifyStatic(times(2)); Keys.getOrAdd("info.smart_tools.smartactors.iobject.iobject.IObject"); verifyStatic(times(2)); IOC.resolve(iObjectKey); verify(tokenField).in(query); return; } assertTrue(false); }
@Test public void MustInCorrectPrepareWhenFieldInThrowInvalidArgumentException() throws ReadValueException, InvalidArgumentException, ResolutionException { IObject query = mock(IObject.class); IObject queryForNestedTask = mock(IObject.class); IObject filterObject = mock(IObject.class); IKey iObjectKey = mock(IKey.class); when(Keys.getOrAdd("info.smart_tools.smartactors.iobject.iobject.IObject")).thenReturn(iObjectKey); when(IOC.resolve(iObjectKey)).thenReturn(queryForNestedTask).thenReturn(filterObject); when(tokenField.in(query)).thenThrow(new InvalidArgumentException("")); try { testTask.prepare(query); } catch (TaskPrepareException e) { verifyStatic(times(2)); Keys.getOrAdd("info.smart_tools.smartactors.iobject.iobject.IObject"); verifyStatic(times(2)); IOC.resolve(iObjectKey); verify(tokenField).in(query); return; } assertTrue(false); }
@Test public void MustInCorrectPrepareWhenFieldOutThrowException() throws ResolutionException, ReadValueException, InvalidArgumentException, TaskPrepareException, ChangeValueException { IObject query = mock(IObject.class); IObject queryForNestedTask = mock(IObject.class); IObject filterObject = mock(IObject.class); IObject eqKeyObject = mock(IObject.class); IKey iObjectKey = mock(IKey.class); when(Keys.getOrAdd("info.smart_tools.smartactors.iobject.iobject.IObject")).thenReturn(iObjectKey); when(IOC.resolve(iObjectKey)).thenReturn(queryForNestedTask).thenReturn(filterObject).thenReturn(eqKeyObject); String token = "token"; when(tokenField.in(query)).thenReturn(token); doThrow(new ChangeValueException()).when(equalsField).out(eqKeyObject, token); try { testTask.prepare(query); } catch (TaskPrepareException e) { verifyStatic(times(3)); Keys.getOrAdd("info.smart_tools.smartactors.iobject.iobject.IObject"); verifyStatic(times(3)); IOC.resolve(iObjectKey); verify(tokenField).in(query); verify(equalsField).out(eqKeyObject, token); return; } assertTrue(false); }