@Override @Nullable public Long getLockId(String entryId, LockType lock) { return getLocks(entryId).entrySet().stream() .filter(entry -> entry.getValue().equals(lock)) .map(Entry::getKey) .findAny() .orElse(null); } }
handler.getLocks("non_exist_entry") ); handler.getLocks(entryId) ); Assert.assertTrue(handler.addLock(entryId, lock2)); final Map<Long, Map<String, Integer>> locks = handler.getLocks(entryId); Assert.assertEquals(2, locks.size()); locks.remove(lockId); final Map<Long, Map<String, Integer>> updated = handler.getLocks(entryId); Assert.assertEquals( new HashSet<>(locks.values()),
@Test public void testGetLockId() throws EntryExistsException { final String entryId = "ABC123"; Map<String, Integer> entry = ImmutableMap.of("a", 1); Map<String, Integer> status = ImmutableMap.of("count", 42); handler.insert(entryId, DateTimes.of("2014-01-01"), "test", entry, true, status); Assert.assertEquals( ImmutableMap.<Long, Map<String, Integer>>of(), handler.getLocks("non_exist_entry") ); Assert.assertEquals( ImmutableMap.<Long, Map<String, Integer>>of(), handler.getLocks(entryId) ); final ImmutableMap<String, Integer> lock1 = ImmutableMap.of("lock", 1); final ImmutableMap<String, Integer> lock2 = ImmutableMap.of("lock", 2); Assert.assertTrue(handler.addLock(entryId, lock1)); Assert.assertNotNull(handler.getLockId(entryId, lock1)); Assert.assertNull(handler.getLockId(entryId, lock2)); }
@Test public void testReplaceLock() throws EntryExistsException { final String entryId = "ABC123"; Map<String, Integer> entry = ImmutableMap.of("a", 1); Map<String, Integer> status = ImmutableMap.of("count", 42); handler.insert(entryId, DateTimes.of("2014-01-01"), "test", entry, true, status); Assert.assertEquals( ImmutableMap.<Long, Map<String, Integer>>of(), handler.getLocks("non_exist_entry") ); Assert.assertEquals( ImmutableMap.<Long, Map<String, Integer>>of(), handler.getLocks(entryId) ); final ImmutableMap<String, Integer> lock1 = ImmutableMap.of("lock", 1); final ImmutableMap<String, Integer> lock2 = ImmutableMap.of("lock", 2); Assert.assertTrue(handler.addLock(entryId, lock1)); final Long lockId1 = handler.getLockId(entryId, lock1); Assert.assertNotNull(lockId1); Assert.assertTrue(handler.replaceLock(entryId, lockId1, lock2)); }
@Test public void testLogs() throws Exception { final String entryId = "abcd"; Map<String, Integer> entry = ImmutableMap.of("a", 1); Map<String, Integer> status = ImmutableMap.of("count", 42); handler.insert(entryId, DateTimes.of("2014-01-01"), "test", entry, true, status); Assert.assertEquals( ImmutableList.of(), handler.getLogs("non_exist_entry") ); Assert.assertEquals( ImmutableMap.of(), handler.getLocks(entryId) ); final ImmutableMap<String, String> log1 = ImmutableMap.of("logentry", "created"); final ImmutableMap<String, String> log2 = ImmutableMap.of("logentry", "updated"); Assert.assertTrue(handler.addLog(entryId, log1)); Assert.assertTrue(handler.addLog(entryId, log2)); Assert.assertEquals( ImmutableList.of(log1, log2), handler.getLogs(entryId) ); }
@Override @Nullable public Long getLockId(String entryId, LockType lock) { return getLocks(entryId).entrySet().stream() .filter(entry -> entry.getValue().equals(lock)) .map(Entry::getKey) .findAny() .orElse(null); } }