private void assertAnonymous() { Authentication currentAuthentication = SecurityContextHolder.getContext() .getAuthentication(); assertThat(currentAuthentication) .isInstanceOf(AnonymousAuthenticationToken.class); AnonymousAuthenticationToken anonymous = (AnonymousAuthenticationToken) currentAuthentication; assertThat(anonymous.getName()).isEqualTo(expectedAnonymous.getName()); assertThat(anonymous.getAuthorities()).containsOnlyElementsOf( expectedAnonymous.getAuthorities()); assertThat(anonymous.getKeyHash()).isEqualTo(expectedAnonymous.getKeyHash()); } }
private void validateAzkabanNode(final AzkabanNode azkabanNode, final String name, final String type, final Props props, final List<String> nodeList, final List<String> dependsOn) { assertThat(azkabanNode.getName()).isEqualTo(name); assertThat(azkabanNode.getType()).isEqualTo(type); assertThat(azkabanNode.getProps()).isEqualTo(props); if (azkabanNode instanceof AzkabanFlow) { assertThat(((AzkabanFlow) azkabanNode).getNodes().keySet()).containsOnlyElementsOf(nodeList); } if (dependsOn != null) { assertThat(azkabanNode.getDependsOn()).containsOnlyElementsOf(dependsOn); } }
private <T extends Traversable<Tuple2<Integer, String>>> void testMapType(GenericType<T> containerType) { T values = dbRule.getSharedHandle().createQuery("select intValue, name from something") .collectInto(containerType); assertThat(values).containsOnlyElementsOf(expectedMap); }
@Test public void authenticationWhenOAuth2UserFoundThenSuccess() { OAuth2AccessTokenResponse accessTokenResponse = OAuth2AccessTokenResponse.withToken("foo") .tokenType(OAuth2AccessToken.TokenType.BEARER) .build(); when(this.accessTokenResponseClient.getTokenResponse(any())).thenReturn(Mono.just(accessTokenResponse)); DefaultOAuth2User user = new DefaultOAuth2User(AuthorityUtils.createAuthorityList("ROLE_USER"), Collections.singletonMap("user", "rob"), "user"); when(this.userService.loadUser(any())).thenReturn(Mono.just(user)); OAuth2LoginAuthenticationToken result = (OAuth2LoginAuthenticationToken) this.manager.authenticate(loginToken()).block(); assertThat(result.getPrincipal()).isEqualTo(user); assertThat(result.getAuthorities()).containsOnlyElementsOf(user.getAuthorities()); assertThat(result.isAuthenticated()).isTrue(); }
private <T extends Iterable<Integer>> void testType(GenericType<T> containerType) { T values = dbRule.getSharedHandle().createQuery("select intValue from something") .collectInto(containerType); assertThat(values).containsOnlyElementsOf(expected); }
@Test public void test_C_client_capabilities_exposed() throws Exception { Capability[] capabilities = readTopology().getClient(ehcacheClientIdentifier).get().getManagementRegistry().get().getCapabilities().toArray(new Capability[0]); assertThat(capabilities.length).isEqualTo(6); assertThat(capabilities[0].getName()).isEqualTo("ActionsCapability"); assertThat(capabilities[1].getName()).isEqualTo("DiagnosticCalls"); assertThat(capabilities[2].getName()).isEqualTo("NmsAgentService"); assertThat(capabilities[3].getName()).isEqualTo("SettingsCapability"); assertThat(capabilities[4].getName()).isEqualTo("StatisticCollectorCapability"); assertThat(capabilities[5].getName()).isEqualTo("StatisticsCapability"); assertThat(capabilities[0].getDescriptors()).hasSize(4); Collection<? extends Descriptor> descriptors = capabilities[5].getDescriptors(); Collection<Descriptor> allDescriptors = new ArrayList<>(); allDescriptors.addAll(CACHE_DESCRIPTORS); allDescriptors.addAll(ONHEAP_DESCRIPTORS); allDescriptors.addAll(OFFHEAP_DESCRIPTORS); allDescriptors.addAll(CLUSTERED_DESCRIPTORS); assertThat(descriptors).containsOnlyElementsOf(allDescriptors); }
@Test public void immutableSet() { ImmutableSet<Integer> set = dbRule.getSharedHandle().createQuery("select intValue from something") .collectInto(new GenericType<ImmutableSet<Integer>>(){}); assertThat(set).containsOnlyElementsOf(expected); }
@Test public void authenticationWhenOAuth2UserFoundThenSuccess() { OAuth2AccessTokenResponse accessTokenResponse = OAuth2AccessTokenResponse.withToken("foo") .tokenType(OAuth2AccessToken.TokenType.BEARER) .additionalParameters(Collections.singletonMap(OidcParameterNames.ID_TOKEN, this.idToken.getTokenValue())) .build(); Map<String, Object> claims = new HashMap<>(); claims.put(IdTokenClaimNames.ISS, "https://issuer.example.com"); claims.put(IdTokenClaimNames.SUB, "rob"); claims.put(IdTokenClaimNames.AUD, Arrays.asList("client-id")); Instant issuedAt = Instant.now(); Instant expiresAt = Instant.from(issuedAt).plusSeconds(3600); Jwt idToken = new Jwt("id-token", issuedAt, expiresAt, claims, claims); when(this.accessTokenResponseClient.getTokenResponse(any())).thenReturn(Mono.just(accessTokenResponse)); DefaultOidcUser user = new DefaultOidcUser(AuthorityUtils.createAuthorityList("ROLE_USER"), this.idToken); when(this.userService.loadUser(any())).thenReturn(Mono.just(user)); when(this.jwtDecoder.decode(any())).thenReturn(Mono.just(idToken)); this.manager.setJwtDecoderFactory(c -> this.jwtDecoder); OAuth2LoginAuthenticationToken result = (OAuth2LoginAuthenticationToken) this.manager.authenticate(loginToken()).block(); assertThat(result.getPrincipal()).isEqualTo(user); assertThat(result.getAuthorities()).containsOnlyElementsOf(user.getAuthorities()); assertThat(result.isAuthenticated()).isTrue(); }
@Test public void authenticationWhenRefreshTokenThenRefreshTokenInAuthorizedClient() { OAuth2AccessTokenResponse accessTokenResponse = OAuth2AccessTokenResponse.withToken("foo") .tokenType(OAuth2AccessToken.TokenType.BEARER) .additionalParameters(Collections.singletonMap(OidcParameterNames.ID_TOKEN, this.idToken.getTokenValue())) .refreshToken("refresh-token") .build(); Map<String, Object> claims = new HashMap<>(); claims.put(IdTokenClaimNames.ISS, "https://issuer.example.com"); claims.put(IdTokenClaimNames.SUB, "rob"); claims.put(IdTokenClaimNames.AUD, Arrays.asList("client-id")); Instant issuedAt = Instant.now(); Instant expiresAt = Instant.from(issuedAt).plusSeconds(3600); Jwt idToken = new Jwt("id-token", issuedAt, expiresAt, claims, claims); when(this.accessTokenResponseClient.getTokenResponse(any())).thenReturn(Mono.just(accessTokenResponse)); DefaultOidcUser user = new DefaultOidcUser(AuthorityUtils.createAuthorityList("ROLE_USER"), this.idToken); when(this.userService.loadUser(any())).thenReturn(Mono.just(user)); when(this.jwtDecoder.decode(any())).thenReturn(Mono.just(idToken)); this.manager.setJwtDecoderFactory(c -> this.jwtDecoder); OAuth2LoginAuthenticationToken result = (OAuth2LoginAuthenticationToken) this.manager.authenticate(loginToken()).block(); assertThat(result.getPrincipal()).isEqualTo(user); assertThat(result.getAuthorities()).containsOnlyElementsOf(user.getAuthorities()); assertThat(result.isAuthenticated()).isTrue(); assertThat(result.getRefreshToken().getTokenValue()).isNotNull(); }
@Test public void descriptorOffHeapTest() { CacheConfiguration<Long, String> cacheConfiguration = CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class, newResourcePoolsBuilder().heap(5, MB).offheap(10, MB)) .build(); ManagementRegistryService managementRegistry = new DefaultManagementRegistryService(new DefaultManagementRegistryConfiguration().setCacheManagerAlias("myCM")); try(CacheManager cacheManager = CacheManagerBuilder.newCacheManagerBuilder() .withCache("aCache", cacheConfiguration) .using(managementRegistry) .build(true)) { List<Capability> capabilities = new ArrayList<>(managementRegistry.getCapabilities()); assertThat(capabilities).hasSize(4); assertThat(capabilities.get(0).getName()).isEqualTo("ActionsCapability"); assertThat(capabilities.get(1).getName()).isEqualTo("SettingsCapability"); assertThat(capabilities.get(2).getName()).isEqualTo("StatisticCollectorCapability"); assertThat(capabilities.get(3).getName()).isEqualTo("StatisticsCapability"); assertThat(capabilities.get(0).getDescriptors()).hasSize(4); Collection<? extends Descriptor> descriptors = capabilities.get(3).getDescriptors(); Collection<Descriptor> allDescriptors = new ArrayList<>(); allDescriptors.addAll(ONHEAP_DESCRIPTORS); allDescriptors.addAll(OFFHEAP_DESCRIPTORS); allDescriptors.addAll(CACHE_DESCRIPTORS); allDescriptors.add(new StatisticDescriptor("OnHeap:OccupiedByteSize" , "GAUGE")); assertThat(descriptors).containsOnlyElementsOf(allDescriptors); } }
@Test public void testTuple1CollectorWithMultiSelectShouldSucceed() { List<Tuple1<Integer>> firstColumnTuples = expected.map(Tuple1::new); List<Tuple1<Integer>> tupleProjection = dbRule.getSharedHandle() .createQuery("select * from tuples") .collectInto(new GenericType<List<Tuple1<Integer>>>() {}); assertThat(tupleProjection).containsOnlyElementsOf(firstColumnTuples); }
@Test public void descriptorOnHeapTest() { CacheConfiguration<Long, String> cacheConfiguration = CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class, heap(10)) .add(new StoreStatisticsConfiguration(true)) .build(); ManagementRegistryService managementRegistry = new DefaultManagementRegistryService(new DefaultManagementRegistryConfiguration().setCacheManagerAlias("myCM")); try(CacheManager cacheManager = CacheManagerBuilder.newCacheManagerBuilder() .withCache("aCache", cacheConfiguration) .using(managementRegistry) .build(true)) { List<Capability> capabilities = new ArrayList<>(managementRegistry.getCapabilities()); assertThat(capabilities).hasSize(4); assertThat(capabilities.get(0).getName()).isEqualTo("ActionsCapability"); assertThat(capabilities.get(1).getName()).isEqualTo("SettingsCapability"); assertThat(capabilities.get(2).getName()).isEqualTo("StatisticCollectorCapability"); assertThat(capabilities.get(3).getName()).isEqualTo("StatisticsCapability"); assertThat(capabilities.get(0).getDescriptors()).hasSize(4); Collection<? extends Descriptor> descriptors = capabilities.get(3).getDescriptors(); Collection<Descriptor> allDescriptors = new ArrayList<>(); allDescriptors.addAll(ONHEAP_DESCRIPTORS); allDescriptors.addAll(CACHE_DESCRIPTORS); assertThat(descriptors).containsOnlyElementsOf(allDescriptors); } }
@Test public void descriptorOnHeapTest_withoutStats() { CacheConfiguration<Long, String> cacheConfiguration = CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class, heap(10)) .build(); ManagementRegistryService managementRegistry = new DefaultManagementRegistryService(new DefaultManagementRegistryConfiguration().setCacheManagerAlias("myCM")); try(CacheManager cacheManager = CacheManagerBuilder.newCacheManagerBuilder() .withCache("aCache", cacheConfiguration) .using(managementRegistry) .build(true)) { List<Capability> capabilities = new ArrayList<>(managementRegistry.getCapabilities()); assertThat(managementRegistry.getCapabilities()).hasSize(4); assertThat(capabilities.get(0).getName()).isEqualTo("ActionsCapability"); assertThat(capabilities.get(1).getName()).isEqualTo("SettingsCapability"); assertThat(capabilities.get(2).getName()).isEqualTo("StatisticCollectorCapability"); assertThat(capabilities.get(3).getName()).isEqualTo("StatisticsCapability"); assertThat(capabilities.get(0).getDescriptors()).hasSize(4); Collection<? extends Descriptor> descriptors = capabilities.get(3).getDescriptors(); Collection<Descriptor> allDescriptors = new ArrayList<>(); allDescriptors.addAll(ONHEAP_NO_STATS_DESCRIPTORS); allDescriptors.addAll(CACHE_DESCRIPTORS); assertThat(descriptors).containsOnlyElementsOf(allDescriptors); } }
@Test public void testMapCollectorWithGlobalKeyValueShouldSucceed() { Jdbi jdbiWithKeyColAndValCol = dbRule.getJdbi() .setMapKeyColumn("key_c") .setMapValueColumn("val_c"); Boolean executed = jdbiWithKeyColAndValCol.withHandle(h -> { HashMap<String, String> valueMap = h.createQuery("select val_c, key_c from keyval") .collectInto(new GenericType<HashMap<String, String>>() {}); assertThat(valueMap).containsOnlyElementsOf(expectedMap); return true; }); assertTrue(executed); }
@Test public void testTuple3CollectorWithSelectedKeyValueShouldSucceed() { List<Tuple3<Integer, String, String>> expectedTuples = expected.map(i -> new Tuple3<>(i, "t2" + i, "t3" + (i + 1))); List<Tuple3<Integer, String, String>> tupleProjection = dbRule.getSharedHandle() .createQuery("select t1, t2, t3 from tuples") .collectInto(new GenericType<List<Tuple3<Integer, String, String>>>() {}); assertThat(tupleProjection).containsOnlyElementsOf(expectedTuples); }
@Test public void testTuple1CollectorWithSingleSelectShouldSucceed() { List<Tuple1<String>> expectedTuples = expected.map(i -> new Tuple1<>("t2" + i)); List<Tuple1<String>> tupleProjection = dbRule.getSharedHandle() .createQuery("select t2 from tuples") .collectInto(new GenericType<List<Tuple1<String>>>() {}); assertThat(tupleProjection).containsOnlyElementsOf(expectedTuples); }
@Test public void descriptorDiskStoreTest() throws Exception { ManagementRegistryService managementRegistry = new DefaultManagementRegistryService(new DefaultManagementRegistryConfiguration().setCacheManagerAlias("myCM")); try(PersistentCacheManager persistentCacheManager = CacheManagerBuilder.newCacheManagerBuilder() .with(CacheManagerBuilder.persistence(getStoragePath() + File.separator + "myData")) .withCache("persistent-cache", CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class, ResourcePoolsBuilder.newResourcePoolsBuilder() .heap(10, EntryUnit.ENTRIES) .disk(10, MemoryUnit.MB, true)) ) .using(managementRegistry) .build(true)) { List<Capability> capabilities = new ArrayList<>(managementRegistry.getCapabilities()); assertThat(capabilities).hasSize(4); assertThat(capabilities.get(0).getName()).isEqualTo("ActionsCapability"); assertThat(capabilities.get(1).getName()).isEqualTo("SettingsCapability"); assertThat(capabilities.get(2).getName()).isEqualTo("StatisticCollectorCapability"); assertThat(capabilities.get(3).getName()).isEqualTo("StatisticsCapability"); assertThat(capabilities.get(0).getDescriptors()).hasSize(4); Collection<? extends Descriptor> descriptors = capabilities.get(3).getDescriptors(); Collection<Descriptor> allDescriptors = new ArrayList<>(); allDescriptors.addAll(ONHEAP_DESCRIPTORS); allDescriptors.addAll(DISK_DESCRIPTORS); allDescriptors.addAll(CACHE_DESCRIPTORS); assertThat(descriptors).containsOnlyElementsOf(allDescriptors); } }
@Test public void testMapCollectorWithCorrespondingTupleColsShouldSucceed() { HashMap<String, String> valueMap = dbRule.getSharedHandle() .configure(TupleMappers.class, c -> c.setColumn(1, "key_c").setColumn(2, "val_c")) .createQuery("select val_c, key_c from keyval") .collectInto(new GenericType<HashMap<String, String>>() {}); assertThat(valueMap).containsOnlyElementsOf(expectedMap); }
@Test public void testMultimapValuesAddAnotherDataSetShouldHave2ValuesForEachKey() { final int offset = 10; for (Integer i : expected) { dbRule.getSharedHandle().execute("insert into something(name, intValue) values (?, ?)", Integer.toString(i + offset) + "asString", i); } Multimap<Integer, String> result = dbRule.getSharedHandle().createQuery("select intValue, name from something") .collectInto(new GenericType<Multimap<Integer, String>>() {}); assertThat(result).hasSize(expected.size() * 2); expected.forEach(i -> assertThat(result.apply(i)) .containsOnlyElementsOf(List.of(i + "asString", (i + 10) + "asString"))); } }
@Test public void testMapCollectorWithTupleConfigShouldSucceed() { HashMap<String, String> valueMap = dbRule.getSharedHandle() .configure(TupleMappers.class, c -> c.setKeyColumn("key_c").setValueColumn("val_c")) .createQuery("select val_c, key_c from keyval") .collectInto(new GenericType<HashMap<String, String>>() {}); assertThat(valueMap).containsOnlyElementsOf(expectedMap); }