Revert KeycloakConfigurationTest (#2905)

This commit is contained in:
Grzegorz Piwowarek 2017-10-29 16:55:06 +01:00 committed by GitHub
parent 95cf16de28
commit 2f060a8f28
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 38 additions and 32 deletions

View File

@ -1,44 +1,50 @@
package com.baeldung.keycloak;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.keycloak.KeycloakPrincipal;
import org.keycloak.KeycloakSecurityContext;
import org.keycloak.adapters.springboot.client.KeycloakSecurityContextClientRequestInterceptor;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.Spy;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import java.util.Random;
import java.util.stream.IntStream;
import java.util.stream.Stream;
import static org.junit.Assert.assertNotNull;
import static org.mockito.Mockito.when;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = SpringBoot.class)
public class KeycloakConfigurationTest {
private static final Random RANDOM = new Random();
@Spy
private KeycloakSecurityContextClientRequestInterceptor factory;
private MockHttpServletRequest servletRequest;
@Mock
public KeycloakSecurityContext keycloakSecurityContext;
@Mock
private KeycloakPrincipal keycloakPrincipal;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
servletRequest = new MockHttpServletRequest();
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(servletRequest));
servletRequest.setUserPrincipal(keycloakPrincipal);
when(keycloakPrincipal.getKeycloakSecurityContext()).thenReturn(keycloakSecurityContext);
}
@Test
public void testGetKeycloakSecurityContext() throws Exception {
IntStream.generate(this::getResultWithSwitching)
.limit(10000000)
.average().ifPresent(System.out::println);
}
public int getResultWithoutSwitching() {
boolean[] gates = {false, false, false};
gates[RANDOM.nextInt(3)] = true;
int pick = RANDOM.nextInt(3);
return gates[pick] ? 1 : 0;
}
public int getResultWithSwitching() {
boolean[] gates = {false, false, false};
int win = RANDOM.nextInt(3);
gates[win] = true;
int pick = RANDOM.nextInt(3);
int empty = Stream.of(0, 1, 2)
.filter(i -> i != pick)
.filter(i -> !gates[i])
.findFirst().get();
int newPick = Stream.of(0, 1, 2)
.filter(i -> i != pick)
.filter(i -> i != empty)
.findFirst().get();
return gates[newPick] ? 1 : 0;
assertNotNull(keycloakPrincipal.getKeycloakSecurityContext());
}
}