CachedCollection implements ICachedCollection { @Override public List<IObject> getItems(final String key) throws GetCacheItemException { try { final List<IObject> items = map.getOrDefault(key, new ArrayList<>()); if (items.isEmpty()) { try (IPoolGuard poolGuard = new PoolGuard(connectionPool)) { IDatabaseTask getItemTask = IOC.resolve( Keys.getOrAdd("db.cached_collection.get_item"), poolGuard.getObject(), collectionName, keyName, key, (IAction<IObject[]>) foundDocs -> { try { items.addAll(Arrays.asList(foundDocs)); } catch (Exception e) { throw new ActionExecuteException(e); } } ); getItemTask.execute(); map.put(key, items); } catch (PoolGuardException e) { throw new GetCacheItemException("Can't get connection from pool.", e); } } return items; } catch (TaskExecutionException e) { throw new GetCacheItemException("Error during execution read task.", e); } catch (ResolutionException e) { throw new GetCacheItemException("Can't resolve cached object.", e); } } CachedCollection(final IObject config); @Override List<IObject> getItems(final String key); @Override void delete(final IObject message); @Override void upsert(final IObject message); @Override void clearCache();  }
@Test public void ShouldReadObject() throws Exception { IObject readQuery = mock(IObject.class); IKey keyIObject = mock(IKey.class); when(Keys.getOrAdd("info.smart_tools.smartactors.iobject.iobject.IObject")).thenReturn(keyIObject); when(IOC.resolve(keyIObject)).thenReturn(readQuery); final IObject searchResult = mock(IObject.class); IDatabaseTask readTask = mock(IDatabaseTask.class); IKey keyTask = mock(IKey.class); when(Keys.getOrAdd("db.cached_collection.get_item")).thenReturn(keyTask); final IAction[] callback = {mock(IAction.class)}; doAnswer(invocation -> { callback[0] = (IAction) invocation.getArguments()[5]; return readTask; }).when(IOC.class); IOC.resolve(eq(keyTask), any(), eq(collectionName), any(), any(), any(IAction.class)); doAnswer(invocation -> { callback[0].execute(new IObject[] {searchResult}); return null; }).when(readTask).execute(); when(searchResultField.in(readQuery)).thenReturn(Collections.singletonList(searchResult)); List<IObject> items = collection.getItems("key"); verify(readTask).execute(); }
@Test(expected = GetCacheItemException.class) public void ShouldThrowGetItemException_When_NestedTaskIsNull() throws Exception { when(IOC.resolve(any(), any(), any(), any(), any(), any())).thenThrow(new ResolutionException("")); collection.getItems("key"); }