CachedCollection implements ICachedCollection { @Override public void delete(final IObject message) throws DeleteCacheItemException { try { try (IPoolGuard poolGuard = new PoolGuard(connectionPool)) { IDatabaseTask deleteTask = IOC.resolve( Keys.getOrAdd("db.cached_collection.delete"), poolGuard.getObject(), collectionName, message ); deleteTask.execute(); } catch (PoolGuardException e) { throw new DeleteCacheItemException("Can't get connection from pool.", e); } catch (TaskExecutionException e) { throw new DeleteCacheItemException("Error during execution delete task.", e); } String key = specificKeyNameField.in(message); List<IObject> items = map.get(key); if (items != null) { for (IObject obj : items) { if (idField.in(obj).equals(idField.in(message))) { items.remove(obj); break; } } } } catch (ResolutionException e) { throw new DeleteCacheItemException("Can't resolve cached object.", e); } catch (InvalidArgumentException | ReadValueException e) { throw new DeleteCacheItemException("Can't delete 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 ShouldDeleteObject() throws Exception { IObject query = mock(IObject.class); IObject deleteQuery = 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(deleteQuery); IDatabaseTask deleteTask = mock(IDatabaseTask.class); IKey keyTask = mock(IKey.class); when(Keys.getOrAdd("db.cached_collection.delete")).thenReturn(keyTask); when(IOC.resolve(eq(keyTask), any(), eq(collectionName), eq(query))).thenReturn(deleteTask); when(specificKeyNameField.in(query)).thenReturn("key"); collection.delete(query); verify(deleteTask).execute(); }
@Test(expected = DeleteCacheItemException.class) public void ShouldThrowDeleteItemException_When_NestedExceptionIsOccurred() throws Exception { IObject query = mock(IObject.class); when(IOC.resolve(any(), any(), any(), any())).thenThrow(new ResolutionException("")); collection.delete(query); }