OperationServiceImpl implements OperationService { public List<DbOperationRecordDto> getOperations(Long lastId, Integer limit) { Long mostRecentId = getMostRecentId(); if (mostRecentId == null) { return null; } long beginId = lastId + 1; long endId = limit != null ? Math.min(lastId + limit, mostRecentId) : mostRecentId; if (beginId > endId) { return null; } Map<String, Object> recordsByKey = memcacheService.getAll(getNewOperationRecordKeys(beginId, endId)); List<DbOperationRecordDto> records = new ArrayList<DbOperationRecordDto>(recordsByKey.size()); for (Object recordObject : recordsByKey.values()) { records.add((DbOperationRecordDto) recordObject); } return records; } @Inject  OperationServiceImpl(
            MemcacheService memcacheService); @Override Long getMostRecentId(); List<DbOperationRecordDto> getOperations(Long lastId, Integer limit);  }
@Test public void testNoMostRecentIdGetOperationReturnNull() { when(memcacheService.get(MemcacheKey.DB_OPERATION_COUNTER.getName())).thenReturn(null); List<DbOperationRecordDto> results = operationService.getOperations(0L, 1); verify(memcacheService).get(MemcacheKey.DB_OPERATION_COUNTER.getName()); assertNull(results); }
@Test public void testGetOperationLimitNullAndMostRecentIdEqualsLastId() { Long lastId = 1L; when(memcacheService.get(MemcacheKey.DB_OPERATION_COUNTER.getName())).thenReturn(lastId); List<DbOperationRecordDto> results = operationService.getOperations(lastId, null); verify(memcacheService).get(MemcacheKey.DB_OPERATION_COUNTER.getName()); assertNull(results); }
@Test public void testGetOperationLimitNotNullAndMostRecentIdEqualsLastId() { Long lastId = 1L; when(memcacheService.get(MemcacheKey.DB_OPERATION_COUNTER.getName())).thenReturn(lastId); List<DbOperationRecordDto> results = operationService.getOperations(lastId, 2); verify(memcacheService).get(MemcacheKey.DB_OPERATION_COUNTER.getName()); assertNull(results); }
@Test public void testGetOperationLimitNullAndMostRecentIdGreaterThanLastId(DbOperationRecordDto dtoFromMemCache) { Long lastId = 1L; when(memcacheService.get(MemcacheKey.DB_OPERATION_COUNTER.getName())).thenReturn(2L); Map<String, Object> mapFromMemCache = Maps.newHashMap(); mapFromMemCache.put("dummyKey", dtoFromMemCache); when(memcacheService.getAll(anyCollection())).thenReturn(mapFromMemCache); List<DbOperationRecordDto> results = operationService.getOperations(lastId, null); verify(memcacheService).getAll(Lists.newArrayList(MemcacheKey.DB_OPERATION_RECORD_PREFIX.getName() + 2)); verify(memcacheService).get(MemcacheKey.DB_OPERATION_COUNTER.getName()); assertEquals(1, results.size()); assertEquals(dtoFromMemCache, results.get(0)); }
@Test public void testGetOperationLimitNotNullAndMostRecentIdGreaterThanLastIdPlusLimit( DbOperationRecordDto dtoFromMemCache1, DbOperationRecordDto dtoFromMemCache2, DbOperationRecordDto dtoFromMemCache3) { Long lastId = 1L; when(memcacheService.get(MemcacheKey.DB_OPERATION_COUNTER.getName())).thenReturn(5L); Map<String, Object> mapFromMemCache = Maps.newHashMap(); mapFromMemCache.put("dummyKey1", dtoFromMemCache1); mapFromMemCache.put("dummyKey2", dtoFromMemCache2); mapFromMemCache.put("dummyKey3", dtoFromMemCache3); when(memcacheService.getAll(anyCollection())).thenReturn(mapFromMemCache); List<DbOperationRecordDto> results = operationService.getOperations(lastId, 3); verify(memcacheService).get(MemcacheKey.DB_OPERATION_COUNTER.getName()); verify(memcacheService).getAll(Lists.newArrayList(MemcacheKey.DB_OPERATION_RECORD_PREFIX.getName() + 2, MemcacheKey.DB_OPERATION_RECORD_PREFIX.getName() + 3, MemcacheKey.DB_OPERATION_RECORD_PREFIX.getName() + 4)); assertEquals(3, results.size()); assertEquals(dtoFromMemCache1, results.get(0)); assertEquals(dtoFromMemCache2, results.get(1)); assertEquals(dtoFromMemCache3, results.get(2)); }
@Test public void testGetOperationLimitNotNullAndMostRecentIdLessThanLastIdPlusLimit( DbOperationRecordDto dtoFromMemCache) { Long lastId = 1L; when(memcacheService.get(MemcacheKey.DB_OPERATION_COUNTER.getName())).thenReturn(2L); Map<String, Object> mapFromMemCache = Maps.newHashMap(); mapFromMemCache.put("dummyKey", dtoFromMemCache); when(memcacheService.getAll(anyCollection())).thenReturn(mapFromMemCache); List<DbOperationRecordDto> results = operationService.getOperations(lastId, 3); verify(memcacheService).get(MemcacheKey.DB_OPERATION_COUNTER.getName()); verify(memcacheService).getAll(Lists.newArrayList(MemcacheKey.DB_OPERATION_RECORD_PREFIX.getName() + 2)); assertEquals(1, results.size()); assertEquals(dtoFromMemCache, results.get(0)); }