CachedCollection implements ICachedCollection { @Override public void upsert(final IObject message) throws UpsertCacheItemException { try { try (IPoolGuard poolGuard = new PoolGuard(connectionPool)) { Boolean isActive = isActiveField.in(message); isActiveField.out(message, true); IDatabaseTask upsertTask = IOC.resolve( Keys.getOrAdd("db.cached_collection.upsert"), poolGuard.getObject(), collectionName, message ); try { upsertTask.execute(); } catch (TaskExecutionException e) { isActiveField.out(message, isActive); throw new UpsertCacheItemException("Error during execution upsert task.", e); } } catch (PoolGuardException e) { throw new UpsertCacheItemException("Can't get connection from pool.", e); } catch (InvalidArgumentException e) { throw new UpsertCacheItemException("Error during operate with isActive field.", e); } String key = specificKeyNameField.in(message); List<IObject> items = map.get(key); if (items != null && !items.isEmpty()) { Boolean isInsert = true; for (IObject obj : items) { if (idField.in(obj).equals(idField.in(message))) { items.remove(obj); items.add(message); isInsert = false; break; } } if (isInsert) { items.add(message); } } else { map.put(key, new ArrayList<>(Arrays.asList(message))); } } catch (InvalidArgumentException | ReadValueException | ChangeValueException e) { throw new UpsertCacheItemException("Can't add or update cached object.", e); } catch (ResolutionException e) { throw new UpsertCacheItemException("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 ShouldUpsertObject() throws Exception { IObject query = mock(IObject.class); IObject upsertQuery = 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(upsertQuery); when(specificKeyNameField.in(query)).thenReturn("key"); IDatabaseTask upsertTask = mock(IDatabaseTask.class); IKey keyTask = mock(IKey.class); when(Keys.getOrAdd("db.cached_collection.upsert")).thenReturn(keyTask); when(IOC.resolve(eq(keyTask), any(), eq(collectionName), eq(query))).thenReturn(upsertTask); collection.upsert(query); verify(upsertTask).execute(); verify(specificKeyNameField).in(eq(query)); verify(isActiveField).out(eq(query), eq(true)); }
@Test(expected = UpsertCacheItemException.class) public void ShouldSetPreviousActiveValue_When_ExecuteExceptionIsThrown() throws Exception { IObject query = mock(IObject.class); IObject upsertQuery = 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(upsertQuery); when(specificKeyNameField.in(query)).thenReturn("key"); IDatabaseTask upsertTask = mock(IDatabaseTask.class); IKey keyTask = mock(IKey.class); when(Keys.getOrAdd("db.cached_collection.upsert")).thenReturn(keyTask); when(IOC.resolve(eq(keyTask), any(), eq(collectionName), eq(query))).thenReturn(upsertTask); doThrow(new TaskExecutionException("")).when(upsertTask).execute(); try { collection.upsert(query); } catch (UpsertCacheItemException e) { verify(upsertTask).execute(); verify(specificKeyNameField, never()).in(eq(query)); verify(isActiveField, times(1)).out(eq(query), eq(true)); verify(isActiveField, times(1)).out(eq(query), eq(true)); throw e; } fail(); }
@Test(expected = UpsertCacheItemException.class) public void ShouldThrowUpsertItemException_When_NestedErrorIsOccurred() throws Exception { IObject query = mock(IObject.class); when(IOC.resolve(any(), any(), any(), any())).thenThrow(new ResolutionException("")); collection.upsert(query); }