RedisRateChecker implements RateChecker { @Override public boolean check(final String key, final Long maxRequests, final OptionsInterval interval) { final Boolean execute = redisOperations.execute(new Callback(key, maxRequests, interval)); if (execute == null) { return false; } return execute; } RedisRateChecker(final RedisOperations<String, String> redisOperations); @Override boolean check(final String key, final Long maxRequests, final OptionsInterval interval);  }
@Test public void shouldAllow() { PowerMockito.mockStatic(System.class); PowerMockito.mockStatic(UUID.class); @SuppressWarnings("unchecked") final RedisOperations<String, String> redisOperations = mock(RedisOperations.class); @SuppressWarnings("unchecked") final ZSetOperations<String, String> zSetOperations = mock(ZSetOperations.class); final UUID uuid = PowerMockito.mock(UUID.class); final OptionsInterval interval = mock(OptionsInterval.class); PowerMockito.when(System.currentTimeMillis()).thenReturn(1448037976717L); when(UUID.randomUUID()).thenReturn(uuid); when(uuid.toString()).thenReturn("43f5fe42-540a-47f8-a36e-2c1b0718103e"); when(redisOperations.execute(any(SessionCallback.class))).thenAnswer(new Answer<Object>() { @Override public Object answer(final InvocationOnMock invocation) throws Throwable { return invocation.getArgumentAt(0, SessionCallback.class).execute(redisOperations); } }); when(redisOperations.opsForZSet()).thenReturn(zSetOperations); when(redisOperations.exec()).thenReturn(Arrays.asList(new Object[]{0L, Boolean.TRUE, Boolean.TRUE, 1L})); when(interval.interval()).thenReturn(10L); when(interval.unit()).thenReturn(TimeUnit.SECONDS); final RateChecker rateChecker = new RedisRateChecker(redisOperations); assertThat(rateChecker.check("test", 10L, interval)).isTrue(); PowerMockito.verifyStatic(); System.currentTimeMillis(); PowerMockito.verifyStatic(); UUID.randomUUID(); verify(redisOperations).multi(); verify(redisOperations, times(3)).opsForZSet(); verify(redisOperations).exec(); verify(redisOperations).expire(eq("test"), eq(10L), eq(TimeUnit.SECONDS)); verify(zSetOperations).removeRangeByScore(eq("test"), eq(Double.MIN_VALUE), eq(1448037976717D - 10000)); verify(zSetOperations).add(eq("test"), eq("43f5fe42-540a-47f8-a36e-2c1b0718103e-1448037976717"), eq(1448037976717D)); verify(zSetOperations).count(eq("test"), eq(Double.MIN_VALUE), eq(Double.MAX_VALUE)); verify(interval, times(2)).interval(); verify(interval, times(2)).unit(); verify(redisOperations, times(1)).execute(any(RedisRateChecker.Callback.class)); PowerMockito.verifyNoMoreInteractions(redisOperations, zSetOperations, uuid, interval); }
@Test public void shouldDeny() { PowerMockito.mockStatic(System.class); PowerMockito.mockStatic(UUID.class); @SuppressWarnings("unchecked") final RedisOperations<String, String> redisOperations = mock(RedisOperations.class); @SuppressWarnings("unchecked") final ZSetOperations<String, String> zSetOperations = mock(ZSetOperations.class); final UUID uuid = PowerMockito.mock(UUID.class); final OptionsInterval interval = mock(OptionsInterval.class); PowerMockito.when(System.currentTimeMillis()).thenReturn(1448037976717L); when(UUID.randomUUID()).thenReturn(uuid); when(uuid.toString()).thenReturn("43f5fe42-540a-47f8-a36e-2c1b0718103e"); when(redisOperations.execute(any(SessionCallback.class))).thenAnswer(new Answer<Object>() { @Override public Object answer(final InvocationOnMock invocation) throws Throwable { return invocation.getArgumentAt(0, SessionCallback.class).execute(redisOperations); } }); when(redisOperations.opsForZSet()).thenReturn(zSetOperations); when(redisOperations.exec()).thenReturn(Arrays.asList(new Object[]{0L, Boolean.TRUE, Boolean.TRUE, 11L})); when(interval.interval()).thenReturn(10L); when(interval.unit()).thenReturn(TimeUnit.SECONDS); final RateChecker rateChecker = new RedisRateChecker(redisOperations); assertThat(rateChecker.check("test", 10L, interval)).isFalse(); PowerMockito.verifyStatic(); System.currentTimeMillis(); PowerMockito.verifyStatic(); UUID.randomUUID(); verify(redisOperations).multi(); verify(redisOperations, times(4)).opsForZSet(); verify(redisOperations).exec(); verify(redisOperations).expire(eq("test"), eq(10L), eq(TimeUnit.SECONDS)); verify(zSetOperations).removeRangeByScore(eq("test"), eq(Double.MIN_VALUE), eq(1448037976717D - 10000)); verify(zSetOperations).add(eq("test"), eq("43f5fe42-540a-47f8-a36e-2c1b0718103e-1448037976717"), eq(1448037976717D)); verify(zSetOperations).count(eq("test"), eq(Double.MIN_VALUE), eq(Double.MAX_VALUE)); verify(zSetOperations).remove(eq("test"), eq("43f5fe42-540a-47f8-a36e-2c1b0718103e-1448037976717")); verify(interval, times(2)).interval(); verify(interval, times(2)).unit(); verify(redisOperations, times(1)).execute(any(RedisRateChecker.Callback.class)); PowerMockito.verifyNoMoreInteractions(redisOperations, zSetOperations, uuid, interval); }
@Test public void shouldDenyInCaseOfError() { PowerMockito.mockStatic(System.class); PowerMockito.mockStatic(UUID.class); @SuppressWarnings("unchecked") final RedisOperations<String, String> redisOperations = mock(RedisOperations.class); @SuppressWarnings("unchecked") final ZSetOperations<String, String> zSetOperations = mock(ZSetOperations.class); final UUID uuid = PowerMockito.mock(UUID.class); final OptionsInterval interval = mock(OptionsInterval.class); PowerMockito.when(System.currentTimeMillis()).thenReturn(1448037976717L); when(UUID.randomUUID()).thenReturn(uuid); when(uuid.toString()).thenReturn("43f5fe42-540a-47f8-a36e-2c1b0718103e"); when(redisOperations.execute(any(SessionCallback.class))).thenAnswer(new Answer<Object>() { @Override public Object answer(final InvocationOnMock invocation) throws Throwable { return invocation.getArgumentAt(0, SessionCallback.class).execute(redisOperations); } }); when(redisOperations.opsForZSet()).thenReturn(zSetOperations); when(redisOperations.exec()).thenReturn(Arrays.asList(new Object[]{0L, Boolean.TRUE, 11L})); when(interval.interval()).thenReturn(10L); when(interval.unit()).thenReturn(TimeUnit.SECONDS); final RateChecker rateChecker = new RedisRateChecker(redisOperations); assertThat(rateChecker.check("test", 10L, interval)).isFalse(); PowerMockito.verifyStatic(); System.currentTimeMillis(); PowerMockito.verifyStatic(); UUID.randomUUID(); verify(redisOperations).multi(); verify(redisOperations, times(3)).opsForZSet(); verify(redisOperations).exec(); verify(redisOperations).expire(eq("test"), eq(10L), eq(TimeUnit.SECONDS)); verify(zSetOperations).removeRangeByScore(eq("test"), eq(Double.MIN_VALUE), eq(1448037976717D - 10000)); verify(zSetOperations).add(eq("test"), eq("43f5fe42-540a-47f8-a36e-2c1b0718103e-1448037976717"), eq(1448037976717D)); verify(zSetOperations).count(eq("test"), eq(Double.MIN_VALUE), eq(Double.MAX_VALUE)); verify(interval, times(2)).interval(); verify(interval, times(2)).unit(); verify(redisOperations, times(1)).execute(any(RedisRateChecker.Callback.class)); PowerMockito.verifyNoMoreInteractions(redisOperations, zSetOperations, uuid, interval); }