/** * Tests that the span propagates between under asynchronous callbacks (even if explicitly) */ @Test public void async() throws Exception { Response response = get("/async"); assertThat(response.isSuccessful()).withFailMessage("not successful: " + response).isTrue(); takeSpan(); }
private void testGetGrantedAuthorities( MapBasedAttributes2GrantedAuthoritiesMapper mapper, String[] roles, String[] expectedGas) { List<GrantedAuthority> result = mapper .getGrantedAuthorities(Arrays.asList(roles)); Collection resultColl = new ArrayList(result.size()); for (GrantedAuthority auth : result) { resultColl.add(auth.getAuthority()); } Collection expectedColl = Arrays.asList(expectedGas); assertThat(resultColl.containsAll(expectedColl)).withFailMessage("Role collections should match; result: " + resultColl + ", expected: " + expectedColl).isTrue(); } }
private void assertNotFound( final Map<Integer, Pair<ExecutionReference, ExecutableFlow>> activeFlows, final ExecutableFlow flow, final String failMessage) { assertThat(activeFlows.containsKey(flow.getExecutionId())).withFailMessage(failMessage) .isFalse(); }
private void testGetGrantedAuthorities( SimpleAttributes2GrantedAuthoritiesMapper mapper, String[] roles, String[] expectedGas) { List<GrantedAuthority> result = mapper .getGrantedAuthorities(Arrays.asList(roles)); Collection<String> resultColl = new ArrayList<>(result.size()); for (int i = 0; i < result.size(); i++) { resultColl.add(result.get(i).getAuthority()); } Collection<String> expectedColl = Arrays.asList(expectedGas); assertThat(expectedColl.containsAll(resultColl) && resultColl.containsAll(expectedColl)).withFailMessage("Role collections do not match; result: " + resultColl + ", expected: " + expectedColl).isTrue(); }
@Test public void testLoginSuccess() throws Exception { SecurityContextHolder.getContext().setAuthentication(this.auth); assertThat(this.module.login()) .as("Login should succeed, there is an authentication set").isTrue(); assertThat(this.module.commit()) .withFailMessage( "The authentication is not null, this should return true") .isTrue(); assertThat(this.subject.getPrincipals().contains(this.auth)) .withFailMessage("Principals should contain the authentication").isTrue(); }
@After public void doTearDown() { assertThat(h.isClosed()).isTrue().withFailMessage("Handle was not closed correctly!"); }
@Test public void testLogout() throws Exception { SecurityContextHolder.getContext().setAuthentication(this.auth); this.module.login(); assertThat(this.module.logout()).as("Should return true as it succeeds").isTrue(); assertThat(this.module.getAuthentication()).as("Authentication should be null") .isNull(); assertThat(this.subject.getPrincipals().contains(this.auth)) .withFailMessage( "Principals should not contain the authentication after logout") .isFalse(); }
@Test public final void testGetSetMappableRoles() { Set<String> roles = StringUtils.commaDelimitedListToSet("Role1,Role2"); SimpleMappableAttributesRetriever r = new SimpleMappableAttributesRetriever(); r.setMappableAttributes(roles); Set<String> result = r.getMappableAttributes(); assertThat( roles.containsAll(result) && result.containsAll(roles)).withFailMessage( "Role collections do not match; result: " + result + ", expected: " + roles).isTrue(); }
@Test public void testRespectsRolePrefix() throws Exception { UsernamePasswordAuthenticationToken inputToken = new UsernamePasswordAuthenticationToken( "Test", "Password", AuthorityUtils.createAuthorityList("ONE", "TWO")); RunAsManagerImpl runAs = new RunAsManagerImpl(); runAs.setKey("my_password"); runAs.setRolePrefix("FOOBAR_"); Authentication result = runAs.buildRunAs(inputToken, new Object(), SecurityConfig.createList("RUN_AS_SOMETHING")); assertThat(result instanceof RunAsUserToken).withFailMessage( "Should have returned a RunAsUserToken").isTrue(); assertThat(result.getPrincipal()).isEqualTo(inputToken.getPrincipal()); assertThat(result.getCredentials()).isEqualTo(inputToken.getCredentials()); Set<String> authorities = AuthorityUtils.authorityListToSet( result.getAuthorities()); assertThat(authorities.contains("FOOBAR_RUN_AS_SOMETHING")).isTrue(); assertThat(authorities.contains("ONE")).isTrue(); assertThat(authorities.contains("TWO")).isTrue(); RunAsUserToken resultCast = (RunAsUserToken) result; assertThat(resultCast.getKeyHash()).isEqualTo("my_password".hashCode()); }
@Test public void syncPollAfterTerminateCalled() { AtomicBoolean onAfterTerminate = new AtomicBoolean(); ConnectableFlux<Integer> f = Flux.just(1) .doAfterTerminate(() -> onAfterTerminate.set(true)) .publish(); StepVerifier.create(f) .then(f::connect) .expectNext(1) .verifyComplete(); assertThat(onAfterTerminate.get()).withFailMessage("onAfterTerminate not called back").isTrue(); }
@Test public void testFull() throws Exception { UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken( "user", "password", AuthorityUtils.createAuthorityList("ROLE_ONE")); assertThat(jaasProvider.supports(UsernamePasswordAuthenticationToken.class)).isTrue(); Authentication auth = jaasProvider.authenticate(token); assertThat(jaasProvider.getAuthorityGranters()).isNotNull(); assertThat(jaasProvider.getCallbackHandlers()).isNotNull(); assertThat(jaasProvider.getLoginConfig()).isNotNull(); assertThat(jaasProvider.getLoginContextName()).isNotNull(); Collection<? extends GrantedAuthority> list = auth.getAuthorities(); Set<String> set = AuthorityUtils.authorityListToSet(list); assertThat(set.contains("ROLE_ONE")).withFailMessage("GrantedAuthorities should not contain ROLE_ONE").isFalse(); assertThat(set.contains("ROLE_TEST1")).withFailMessage("GrantedAuthorities should contain ROLE_TEST1").isTrue(); assertThat(set.contains("ROLE_TEST2")).withFailMessage("GrantedAuthorities should contain ROLE_TEST2").isTrue(); boolean foundit = false; for (GrantedAuthority a : list) { if (a instanceof JaasGrantedAuthority) { JaasGrantedAuthority grant = (JaasGrantedAuthority) a; assertThat(grant.getPrincipal()).withFailMessage("Principal was null on JaasGrantedAuthority").isNotNull(); foundit = true; } } assertThat(foundit).as("Could not find a JaasGrantedAuthority").isTrue(); assertThat(eventCheck.successEvent).as("Success event should be fired").isNotNull(); assertThat(eventCheck.successEvent.getAuthentication()).withFailMessage("Auth objects should be equal").isEqualTo(auth); assertThat(eventCheck.failedEvent).as("Failure event should not be fired").isNull(); }
@Test public void syncPollCompleteCalled() { AtomicBoolean onComplete = new AtomicBoolean(); ConnectableFlux<Integer> f = Flux.just(1) .doOnComplete(() -> onComplete.set(true)) .publish(); StepVerifier.create(f) .then(f::connect) .expectNext(1) .verifyComplete(); assertThat(onComplete.get()).withFailMessage("onComplete not called back").isTrue(); }
@Test public void syncPollConditionalCompleteCalled() { AtomicBoolean onComplete = new AtomicBoolean(); ConnectableFlux<Integer> f = Flux.just(1) .doOnComplete(() -> onComplete.set(true)) .filter(v -> true) .publish(); StepVerifier.create(f) .then(f::connect) .expectNext(1) .verifyComplete(); assertThat(onComplete.get()).withFailMessage("onComplete not called back").isTrue(); }
@Test public void syncPollConditionalAfterTerminateCalled() { AtomicBoolean onAfterTerminate = new AtomicBoolean(); ConnectableFlux<Integer> f = Flux.just(1) .doAfterTerminate(() -> onAfterTerminate.set(true)) .filter(v -> true) .publish(); StepVerifier.create(f) .then(f::connect) .expectNext(1) .verifyComplete(); assertThat(onAfterTerminate.get()).withFailMessage("onAfterTerminate not called back").isTrue(); }
assertThat(Math.abs(userNotFoundAvg - userFoundAvg) <= 3).withFailMessage( "User not found average " + userNotFoundAvg + " should be within 3ms of user found average "
assertThat(caughtEventObjectHolder[0] != null && caughtEventObjectHolder[0] instanceof WorkItem).isTrue().withFailMessage("Event was not passed to Event Subprocess."); workItem = (WorkItem) caughtEventObjectHolder[0]; Object throwObj = workItem.getParameter(ExceptionService.exceptionParameterName); assertThat(throwObj instanceof Throwable).isTrue().withFailMessage("WorkItem doesn't contain Throwable."); assertThat(((Throwable) throwObj).getMessage().endsWith(input)).isTrue().withFailMessage("Exception message does not match service input."); assertThat(processInstance == null || processInstance.getState() == ProcessInstance.STATE_ABORTED).isTrue().withFailMessage("Process instance has not been aborted.");
assertThat(caughtEventObjectHolder[0] != null && caughtEventObjectHolder[0] instanceof WorkItem).isTrue().withFailMessage("Event was not passed to Event Subprocess."); WorkItem workItem = (WorkItem) caughtEventObjectHolder[0]; Object throwObj = workItem.getParameter(ExceptionService.exceptionParameterName); assertThat(throwObj instanceof Throwable).isTrue().withFailMessage("WorkItem doesn't contain Throwable."); assertThat(((Throwable) throwObj).getMessage().endsWith(input)).isTrue().withFailMessage("Exception message does not match service input.");
assertThat(componentTree.getMainThreadLayoutState().isForComponentId(newComponent.getId())) .isTrue() .withFailMessage( "The main thread should calculate a new layout synchronously because the async layout will not have compatible size specs");
@Test public void testMeasureWithIncompatibleSetRootAsyncThatFinishes() { ComponentTree componentTree = ComponentTree.create(mContext, mComponent).build(); componentTree.setLithoView(new LithoView(mContext)); int widthSpec1 = SizeSpec.makeSizeSpec(1000, SizeSpec.EXACTLY); int heightSpec1 = SizeSpec.makeSizeSpec(1000, SizeSpec.AT_MOST); int widthSpec2 = SizeSpec.makeSizeSpec(1000, SizeSpec.EXACTLY); int heightSpec2 = SizeSpec.makeSizeSpec(0, SizeSpec.UNSPECIFIED); componentTree.measure(widthSpec2, heightSpec2, new int[2], false); componentTree.attach(); componentTree.measure(widthSpec1, heightSpec1, new int[2], false); Component newComponent = TestDrawableComponent.create(mContext).color(1234).build(); componentTree.setRootAsync(newComponent); runOnBackgroundThreadSync( new Runnable() { @Override public void run() { // "Commit" layout (it will fail since it doesn't have compatible size specs) mLayoutThreadShadowLooper.runToEndOfTasks(); } }); componentTree.measure(widthSpec2, heightSpec2, new int[2], false); assertThat(componentTree.getRoot()).isEqualTo(newComponent); assertThat(componentTree.hasCompatibleLayout(widthSpec2, heightSpec2)).isTrue(); assertThat(componentTree.getMainThreadLayoutState().isForComponentId(newComponent.getId())) .isTrue() .withFailMessage( "The main thread should calculate a new layout synchronously because the async layout will not be used once it completes"); }
@Override public final synchronized void check() throws Exception { file.generate(); Assertions.assertThat(Files.exists(path)).withFailMessage("source file <%s> was not created", path).isTrue(); final String contents = new String(Files.readAllBytes(path)); Assertions.assertThat(contents).isEqualToNormalizingNewlines(this.contents); } }