Skip to content

Commit

Permalink
fix: improve test
Browse files Browse the repository at this point in the history
  • Loading branch information
gschlueter-jaconi committed Jul 27, 2022
1 parent fc5c85e commit 6d9197d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 30 deletions.
7 changes: 3 additions & 4 deletions src/test/java/io/jaconi/morp/MorpReactiveUserServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@
import static io.jaconi.morp.MorpReactiveUserService.ROLE_PROXY;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.*;

@SuppressWarnings("unchecked")
@SpringBootTest
Expand Down Expand Up @@ -55,7 +54,7 @@ void testNoClaimsConfigured() {

@Test
void testClaimsMatch() {
doReturn(Map.of("sub", List.of("match"))).when(tenantService).getClaimConstraints(anyString());
when(tenantService.getClaimConstraints(anyString())).thenReturn(Map.of("sub", List.of("match")));

OidcUserRequest oidcUserRequest = getOidcUserRequest();

Expand All @@ -72,7 +71,7 @@ void testClaimsMatch() {

@Test
void testClaimsDontMatch() {
doReturn(Map.of("sub", List.of("nomatch"))).when(tenantService).getClaimConstraints(anyString());
when(tenantService.getClaimConstraints(anyString())).thenReturn(Map.of("sub", List.of("nomatch")));

OidcUserRequest oidcUserRequest = getOidcUserRequest();

Expand Down
15 changes: 8 additions & 7 deletions src/test/java/io/jaconi/morp/oauth/RegistrationResolverTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
import java.util.Set;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;
import static org.mockito.Mockito.anyString;
import static org.mockito.Mockito.when;

@SpringBootTest
class RegistrationResolverTest {
Expand Down Expand Up @@ -57,7 +58,7 @@ void testNoRegistrations() {
@Test
void testTenantOnly() {
OAuth2ClientProperties.Registration tenant1 = tenantRegistration();
doReturn(tenant1).when(tenantService).getRegistration(anyString());
when(tenantService.getRegistration(anyString())).thenReturn(tenant1);

OAuth2ClientProperties.Registration registration = registrationResolver.getRegistration("tenant1");

Expand All @@ -75,8 +76,8 @@ void testTenantOnly() {
@Test
void testOnlyGlobal() {
OAuth2ClientProperties.Registration global = globalRegistration();
doReturn("default").when(tenantService).getRegistrationId(anyString());
doReturn(Map.of("default", global)).when(properties).getRegistration();
when(tenantService.getRegistrationId(anyString())).thenReturn("default");
when(properties.getRegistration()).thenReturn(Map.of("default", global));

OAuth2ClientProperties.Registration registration = registrationResolver.getRegistration("tenant1");

Expand All @@ -95,9 +96,9 @@ void testOnlyGlobal() {
void testMerge() {
OAuth2ClientProperties.Registration global = globalRegistration();
OAuth2ClientProperties.Registration tenant1 = tenantRegistration();
doReturn(tenant1).when(tenantService).getRegistration(anyString());
doReturn("tenant1").when(tenantService).getRegistrationId(anyString());
doReturn(Map.of("tenant1", global)).when(properties).getRegistration();
when(tenantService.getRegistration(anyString())).thenReturn(tenant1);
when(tenantService.getRegistrationId(anyString())).thenReturn("tenant1");
when(properties.getRegistration()).thenReturn(Map.of("tenant1", global));

OAuth2ClientProperties.Registration registration = registrationResolver.getRegistration("tenant1");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@ class TenantAwareClientRegistrationRepositoryTest {
private void clearCaches() {
cacheManager.getCache(TenantAwareClientRegistrationRepository.REGISTRATIONS).clear();
cacheManager.getCache(TenantAwareClientRegistrationRepository.SOURCE_HASHES).clear();
when(providerResolver.getProvider(anyString(), anyString())).thenReturn(buildProvider("i"));
when(registrationResolver.getRegistration(anyString())).thenReturn(buildRegistration(TENANT));
when(clientRegistrationFetcher.getRegistration(eq(TENANT), any())).thenReturn(clientRegistration);
}

@Test
void testFresh() {
doReturn(buildProvider("i")).when(providerResolver).getProvider(anyString(), anyString());
doReturn(buildRegistration(TENANT)).when(registrationResolver).getRegistration(anyString());
doReturn(clientRegistration).when(clientRegistrationFetcher).getRegistration(eq(TENANT), any());
StepVerifier.create(tenantAwareClientRegistrationRepository.findByRegistrationId(TENANT))
.assertNext(r -> {
assertNotNull(r);
Expand All @@ -68,9 +68,6 @@ void testFresh() {

@Test
void testCached() {
doReturn(buildProvider("i")).when(providerResolver).getProvider(anyString(), anyString());
doReturn(buildRegistration(TENANT)).when(registrationResolver).getRegistration(anyString());
doReturn(clientRegistration).when(clientRegistrationFetcher).getRegistration(eq(TENANT), any());
StepVerifier.create(tenantAwareClientRegistrationRepository.findByRegistrationId(TENANT))
.assertNext(r -> {
assertNotNull(r);
Expand All @@ -81,7 +78,7 @@ void testCached() {

verify(clientRegistrationFetcher, times(1)).getRegistration(eq(TENANT), any());

doThrow(RuntimeException.class).when(clientRegistrationFetcher).getRegistration(eq(TENANT), any());
when(clientRegistrationFetcher.getRegistration(eq(TENANT), any())).thenThrow(RuntimeException.class);
StepVerifier.create(tenantAwareClientRegistrationRepository.findByRegistrationId(TENANT))
.assertNext(Assertions::assertNotNull)
.verifyComplete();
Expand All @@ -92,9 +89,6 @@ void testCached() {

@Test
void testChange() {
doReturn(buildProvider("i")).when(providerResolver).getProvider(anyString(), anyString());
doReturn(buildRegistration(TENANT)).when(registrationResolver).getRegistration(anyString());
doReturn(clientRegistration).when(clientRegistrationFetcher).getRegistration(eq(TENANT), any());
StepVerifier.create(tenantAwareClientRegistrationRepository.findByRegistrationId(TENANT))
.assertNext(r -> {
assertNotNull(r);
Expand All @@ -106,7 +100,7 @@ void testChange() {
verify(clientRegistrationFetcher, times(1)).getRegistration(eq(TENANT), any());

// Provider changes
doReturn(buildProvider("newIssuer")).when(providerResolver).getProvider(anyString(), anyString());
when(providerResolver.getProvider(anyString(), anyString())).thenReturn(buildProvider("newIssuer"));

// Client rebuilt
StepVerifier.create(tenantAwareClientRegistrationRepository.findByRegistrationId(TENANT))
Expand All @@ -119,9 +113,6 @@ void testChange() {

@Test
void testChangeButError() {
doReturn(buildProvider("i")).when(providerResolver).getProvider(anyString(), anyString());
doReturn(buildRegistration(TENANT)).when(registrationResolver).getRegistration(anyString());
doReturn(clientRegistration).when(clientRegistrationFetcher).getRegistration(eq(TENANT), any());
StepVerifier.create(tenantAwareClientRegistrationRepository.findByRegistrationId(TENANT))
.assertNext(r -> {
assertNotNull(r);
Expand All @@ -133,9 +124,9 @@ void testChangeButError() {
verify(clientRegistrationFetcher, times(1)).getRegistration(eq(TENANT), any());

// Provider changes
doReturn(buildProvider("newIssuer")).when(providerResolver).getProvider(anyString(), anyString());
when(providerResolver.getProvider(anyString(), anyString())).thenReturn(buildProvider("newIssuer"));
// But method returns error
doThrow(RuntimeException.class).when(clientRegistrationFetcher).getRegistration(eq(TENANT), any());
when(clientRegistrationFetcher.getRegistration(eq(TENANT), any())).thenThrow(RuntimeException.class);

// Client returned from cache
StepVerifier.create(tenantAwareClientRegistrationRepository.findByRegistrationId(TENANT))
Expand All @@ -148,9 +139,7 @@ void testChangeButError() {

@Test
void testErrorNoCache() {
doReturn(buildProvider("i")).when(providerResolver).getProvider(anyString(), anyString());
doReturn(buildRegistration(TENANT)).when(registrationResolver).getRegistration(anyString());
doThrow(RuntimeException.class).when(clientRegistrationFetcher).getRegistration(eq(TENANT), any());
when(clientRegistrationFetcher.getRegistration(eq(TENANT), any())).thenThrow(RuntimeException.class);
// Method throws error and nothing in cache -> No registration returned
StepVerifier.create(tenantAwareClientRegistrationRepository.findByRegistrationId(TENANT))
.verifyComplete();
Expand Down

0 comments on commit 6d9197d

Please sign in to comment.