Skip to content

Commit

Permalink
Code Refactory
Browse files Browse the repository at this point in the history
refactory

NPE

message & checkStyle & newline Remove

Write Test Code

Change test case name from Empty to Null

SuppressWarnings unchecked apply
  • Loading branch information
dukbong committed May 7, 2024
1 parent aaf7044 commit c343da1
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -96,12 +96,9 @@ public Mono<Void> removeAuthorizedClient(String clientRegistrationId, Authentica

@SuppressWarnings("unchecked")
private Map<String, OAuth2AuthorizedClient> getAuthorizedClients(WebSession session) {
Map<String, OAuth2AuthorizedClient> authorizedClients = (session != null)
? (Map<String, OAuth2AuthorizedClient>) session.getAttribute(this.sessionAttributeName) : null;
if (authorizedClients == null) {
authorizedClients = new HashMap<>();
}
return authorizedClients;
Assert.notNull(session, "session cannot be null");
Map<String, OAuth2AuthorizedClient> authorizedClients = session.getAttribute(this.sessionAttributeName);
return (authorizedClients != null) ? authorizedClients : new HashMap<>();
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -25,10 +25,12 @@
import org.springframework.security.oauth2.client.registration.TestClientRegistrations;
import org.springframework.security.oauth2.core.OAuth2AccessToken;
import org.springframework.web.server.WebSession;
import reactor.core.publisher.Mono;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

/**
* @author Rob Winch
Expand Down Expand Up @@ -201,5 +203,25 @@ public void removeAuthorizedClientWhenClient1Client2SavedAndClient1RemovedThenCl
assertThat(loadedAuthorizedClient2).isNotNull();
assertThat(loadedAuthorizedClient2).isSameAs(authorizedClient2);
}

@Test
public void saveAuthorizedClientWhenSessionIsNullThenThrowIllegalArgumentException() {
MockServerWebExchange mockedExchange = mock(MockServerWebExchange.class);
when(mockedExchange.getSession()).thenReturn(Mono.empty());
OAuth2AuthorizedClient authorizedClient = new OAuth2AuthorizedClient(this.registration1, this.principalName1,
mock(OAuth2AccessToken.class));
assertThatIllegalArgumentException().isThrownBy(
() -> authorizedClientRepository.saveAuthorizedClient(authorizedClient, null, mockedExchange).block())
.withMessage("session cannot be null");
}

@Test
public void removeAuthorizedClientWhenSessionIsNullThenThrowIllegalArgumentException() {
MockServerWebExchange mockedExchange = mock(MockServerWebExchange.class);
when(mockedExchange.getSession()).thenReturn(Mono.empty());
assertThatIllegalArgumentException().isThrownBy(
() -> authorizedClientRepository.removeAuthorizedClient(this.registrationId1, null, mockedExchange).block())
.withMessage("session cannot be null");
}

}

0 comments on commit c343da1

Please sign in to comment.