return anySet();
private Context mockContext() { SharedPreferences preferences = mock(SharedPreferences.class); when(preferences.getStringSet(anyString(), ArgumentMatchers.<String>anySet())).thenReturn(new HashSet<String>()); Context context = mock(MockContext.class); when(context.getApplicationContext()).thenReturn(context); when(context.getSharedPreferences(anyString(), anyInt())).thenReturn(preferences); return context; } }
@Test public void do_not_call_db_for_project_permission_filtering_if_there_is_no_project_subscriber() { String globalPermission = RandomStringUtils.randomAlphanumeric(4); String projectPermission = RandomStringUtils.randomAlphanumeric(5); String projectUuid = "uuid_45"; when(propertiesDao.findUsersForNotification("NewViolations", "Email", projectUuid)) .thenReturn(newHashSet(new Subscriber("user3", true))); when(propertiesDao.findUsersForNotification("NewViolations", "Twitter", projectUuid)) .thenReturn(newHashSet(new Subscriber("user3", true))); when(authorizationDao.keepAuthorizedLoginsOnProject(dbSession, newHashSet("user3"), projectUuid, globalPermission)) .thenReturn(newHashSet("user3")); Multimap<String, NotificationChannel> multiMap = underTest.findSubscribedRecipientsForDispatcher(dispatcher, projectUuid, new SubscriberPermissionsOnProject(globalPermission, projectPermission)); assertThat(multiMap.entries()).hasSize(2); Map<String, Collection<NotificationChannel>> map = multiMap.asMap(); assertThat(map.get("user3")).containsOnly(emailChannel, twitterChannel); verify(authorizationDao, times(1)).keepAuthorizedLoginsOnProject(eq(dbSession), anySet(), anyString(), eq(globalPermission)); verify(authorizationDao, times(0)).keepAuthorizedLoginsOnProject(eq(dbSession), anySet(), anyString(), eq(projectPermission)); }
private TableSizeReader.TableSizeDetails testRunner(final String[] servers, String table) throws InvalidConfigException { when(helix.getServerToSegmentsMap(anyString())).thenAnswer(new Answer<Object>() { @Override public Object answer(InvocationOnMock invocationOnMock) throws Throwable { return subsetOfServerSegments(servers); } }); when(helix.getDataInstanceAdminEndpoints(ArgumentMatchers.<String>anySet())).thenAnswer(new Answer<Object>() { @Override public Object answer(InvocationOnMock invocationOnMock) throws Throwable { return serverEndpoints(servers); } }); TableSizeReader reader = new TableSizeReader(executor, connectionManager, _controllerMetrics, helix); return reader.getTableSizeDetails(table, timeoutMsec); }
@Test public void do_not_call_db_for_project_permission_filtering_if_there_is_no_global_subscriber() { String globalPermission = RandomStringUtils.randomAlphanumeric(4); String projectPermission = RandomStringUtils.randomAlphanumeric(5); String projectUuid = "uuid_45"; when(propertiesDao.findUsersForNotification("NewViolations", "Email", projectUuid)) .thenReturn(newHashSet(new Subscriber("user3", false))); when(propertiesDao.findUsersForNotification("NewViolations", "Twitter", projectUuid)) .thenReturn(newHashSet(new Subscriber("user3", false))); when(authorizationDao.keepAuthorizedLoginsOnProject(dbSession, newHashSet("user3"), projectUuid, projectPermission)) .thenReturn(newHashSet("user3")); Multimap<String, NotificationChannel> multiMap = underTest.findSubscribedRecipientsForDispatcher(dispatcher, projectUuid, new SubscriberPermissionsOnProject(globalPermission, projectPermission)); assertThat(multiMap.entries()).hasSize(2); Map<String, Collection<NotificationChannel>> map = multiMap.asMap(); assertThat(map.get("user3")).containsOnly(emailChannel, twitterChannel); verify(authorizationDao, times(0)).keepAuthorizedLoginsOnProject(eq(dbSession), anySet(), anyString(), eq(globalPermission)); verify(authorizationDao, times(1)).keepAuthorizedLoginsOnProject(eq(dbSession), anySet(), anyString(), eq(projectPermission)); } }
@Test public void shouldFindSubscribedRecipientForGivenResource() { String projectUuid = "uuid_45"; when(propertiesDao.findUsersForNotification("NewViolations", "Email", projectUuid)) .thenReturn(newHashSet(new Subscriber("user1", false), new Subscriber("user3", false), new Subscriber("user3", true))); when(propertiesDao.findUsersForNotification("NewViolations", "Twitter", "uuid_56")) .thenReturn(newHashSet(new Subscriber("user2", false))); when(propertiesDao.findUsersForNotification("NewViolations", "Twitter", projectUuid)) .thenReturn(newHashSet(new Subscriber("user3", true))); when(propertiesDao.findUsersForNotification("NewAlerts", "Twitter", projectUuid)) .thenReturn(newHashSet(new Subscriber("user4", false))); when(authorizationDao.keepAuthorizedLoginsOnProject(dbSession, newHashSet("user1", "user3"), projectUuid, "user")) .thenReturn(newHashSet("user1", "user3")); Multimap<String, NotificationChannel> multiMap = underTest.findSubscribedRecipientsForDispatcher(dispatcher, projectUuid, SubscriberPermissionsOnProject.ALL_MUST_HAVE_ROLE_USER); assertThat(multiMap.entries()).hasSize(3); Map<String, Collection<NotificationChannel>> map = multiMap.asMap(); assertThat(map.get("user1")).containsOnly(emailChannel); assertThat(map.get("user2")).isNull(); assertThat(map.get("user3")).containsOnly(emailChannel, twitterChannel); assertThat(map.get("user4")).isNull(); // code is optimized to perform only 1 SQL requests for all channels verify(authorizationDao, times(1)).keepAuthorizedLoginsOnProject(eq(dbSession), anySet(), anyString(), anyString()); }
@Test public void should_apply_distinct_permission_filtering_global_or_project_subscribers() { String globalPermission = RandomStringUtils.randomAlphanumeric(4); String projectPermission = RandomStringUtils.randomAlphanumeric(5); String projectUuid = "uuid_45"; when(propertiesDao.findUsersForNotification("NewViolations", "Email", projectUuid)) .thenReturn(newHashSet(new Subscriber("user1", false), new Subscriber("user3", false), new Subscriber("user3", true))); when(propertiesDao.findUsersForNotification("NewViolations", "Twitter", "uuid_56")) .thenReturn(newHashSet(new Subscriber("user2", false))); when(propertiesDao.findUsersForNotification("NewViolations", "Twitter", projectUuid)) .thenReturn(newHashSet(new Subscriber("user3", true))); when(propertiesDao.findUsersForNotification("NewAlerts", "Twitter", projectUuid)) .thenReturn(newHashSet(new Subscriber("user4", false))); when(authorizationDao.keepAuthorizedLoginsOnProject(dbSession, newHashSet("user3", "user4"), projectUuid, globalPermission)) .thenReturn(newHashSet("user3")); when(authorizationDao.keepAuthorizedLoginsOnProject(dbSession, newHashSet("user1", "user3"), projectUuid, projectPermission)) .thenReturn(newHashSet("user1", "user3")); Multimap<String, NotificationChannel> multiMap = underTest.findSubscribedRecipientsForDispatcher(dispatcher, projectUuid, new SubscriberPermissionsOnProject(globalPermission, projectPermission)); assertThat(multiMap.entries()).hasSize(3); Map<String, Collection<NotificationChannel>> map = multiMap.asMap(); assertThat(map.get("user1")).containsOnly(emailChannel); assertThat(map.get("user2")).isNull(); assertThat(map.get("user3")).containsOnly(emailChannel, twitterChannel); assertThat(map.get("user4")).isNull(); // code is optimized to perform only 2 SQL requests for all channels verify(authorizationDao, times(1)).keepAuthorizedLoginsOnProject(eq(dbSession), anySet(), anyString(), eq(globalPermission)); verify(authorizationDao, times(1)).keepAuthorizedLoginsOnProject(eq(dbSession), anySet(), anyString(), eq(projectPermission)); }
any(LuceneDestroyIndexFunction.class), any(LuceneDestroyIndexInfo.class), anySet()); doReturn(cliFunctionResults).when(mockResultCollector).getResult();
any(LuceneDestroyIndexFunction.class), any(LuceneDestroyIndexInfo.class), anySet()); doReturn(cliFunctionResults).when(mockResultCollector).getResult();
private void invalidateLockTokens() { when(timelockService.refreshLockLeases(anySet())).thenReturn(ImmutableSet.of()); } }
/** * Delegates call to {@link ArgumentMatchers#anySet()}. */ default <T> Set<T> anySet() { return ArgumentMatchers.anySet(); }
/** * Delegates call to {@link ArgumentMatchers#anySet()}. */ default <T> Set<T> anySet() { return ArgumentMatchers.anySet(); }
@Test @DisplayName("should configure rate limiter") public void shouldReportOnly() { when(requestRateLimiterFactory.getInstance(anySet())).thenReturn(requestRateLimiter); when(requestRateLimiter.overLimitWhenIncremented(anyString())).thenReturn(true); Response response = rule.getJerseyTest().target("/test/reportOnly/{id}").resolveTemplate("id", 1) .request(MediaType.APPLICATION_JSON_TYPE) .get(); assertThat(response.getStatus()).isEqualTo(200); }
@Test @DisplayName("should not limit if the backing rate limiter throws exception") public void shouldNotLimitIfBackingRateLimiterFails() { when(requestRateLimiterFactory.getInstance(anySet())).thenReturn(requestRateLimiter); when(requestRateLimiter.overLimitWhenIncremented(anyString())).thenThrow(new RuntimeException()); Response response = rule.getJerseyTest().target("/test/{id}").resolveTemplate("id", 1) .request(MediaType.APPLICATION_JSON_TYPE) .get(); assertThat(response.getStatus()).isEqualTo(200); }
@Test public void metricInfoStoreShouldCreateANewPcpMetricInfoV2() { IdentifierSourceSet identifierSourceSet = mock(IdentifierSourceSet.class); PcpStringStore stringStore = mock(PcpStringStore.class); PcpString pcpString = mock(PcpString.class); IdentifierSource identifierSource = mock(IdentifierSource.class); when(identifierSourceSet.metricSource()).thenReturn(identifierSource); when(identifierSource.calculateId(eq("my.metric"), ArgumentMatchers.<Integer>anySet())).thenReturn(123); when(stringStore.createPcpString("my.metric")).thenReturn(pcpString); MetricInfoStoreV2 metricInfoStoreV2 = new MetricInfoStoreV2(identifierSourceSet, stringStore); PcpMetricInfo actual = metricInfoStoreV2.byName("my.metric"); PcpMetricInfoV2 expected = new PcpMetricInfoV2("my.metric", 123, pcpString); assertReflectionEquals(expected, actual); } }
@Test @DisplayName("should limit request returning a 429") public void shouldLimit() { when(requestRateLimiterFactory.getInstance(anySet())).thenReturn(requestRateLimiter); when(requestRateLimiter.overLimitWhenIncremented(anyString())).thenReturn(true); Response response = rule.getJerseyTest().target("/test/{id}").resolveTemplate("id", 1) .request(MediaType.APPLICATION_JSON_TYPE) .get(); assertThat(response.getStatus()).isEqualTo(429); }
@Test @DisplayName("should not limit request") public void shouldNotLimit() { when(requestRateLimiterFactory.getInstance(anySet())).thenReturn(requestRateLimiter); when(requestRateLimiter.overLimitWhenIncremented(anyString())).thenReturn(false); Response response = rule.getJerseyTest().target("/test/{id}").resolveTemplate("id", 1) .request(MediaType.APPLICATION_JSON_TYPE) .get(); assertThat(response.getStatus()).isEqualTo(200); }
@Test public void instanceStoreShouldCreateANewInstance() { IdentifierSourceSet identifierSourceSet = mock(IdentifierSourceSet.class); IdentifierSource identifierSource = mock(IdentifierSource.class); InstanceDomain instanceDomain = mock(InstanceDomain.class); PcpStringStore stringStore = mock(PcpStringStore.class); PcpString pcpString = mock(PcpString.class); when(identifierSourceSet.instanceSource(INSTANCE_NAME)).thenReturn(identifierSource); when(identifierSource.calculateId(eq(INSTANCE_NAME), ArgumentMatchers.<Integer>anySet())).thenReturn(123); when(stringStore.createPcpString(INSTANCE_NAME)).thenReturn(pcpString); InstanceV2.InstanceStoreV2 instanceStore = new InstanceV2.InstanceStoreV2(identifierSourceSet, INSTANCE_NAME, instanceDomain, stringStore); Instance actual = instanceStore.byName(INSTANCE_NAME); InstanceV2 expected = new InstanceV2(instanceDomain, INSTANCE_NAME, 123, pcpString); assertReflectionEquals(expected, actual); }