-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix @Cacheable self invocation (#2949)
* DTSCCI-242: Fix @Cacheable self invocation * DTSCCI-242: Fix contract test config * Re-enable pact tests on PR * DTSCCI-242: Fix @Cacheable self invocation
- Loading branch information
1 parent
bd45c9b
commit 26019e8
Showing
8 changed files
with
195 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
src/main/java/uk/gov/hmcts/cmc/claimstore/services/UserAuthorisationTokenService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package uk.gov.hmcts.cmc.claimstore.services; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.cache.annotation.Cacheable; | ||
import org.springframework.stereotype.Component; | ||
import uk.gov.hmcts.cmc.claimstore.models.idam.Oauth2; | ||
import uk.gov.hmcts.cmc.claimstore.models.idam.TokenExchangeResponse; | ||
import uk.gov.hmcts.cmc.claimstore.requests.idam.IdamApi; | ||
import uk.gov.hmcts.cmc.claimstore.stereotypes.LogExecutionTime; | ||
|
||
@Component | ||
public class UserAuthorisationTokenService { | ||
public static final String GRANT_TYPE_PASSWORD = "password"; | ||
public static final String DEFAULT_SCOPE = "openid profile roles"; | ||
public static final String BEARER = "Bearer "; | ||
|
||
private final IdamApi idamApi; | ||
private final Oauth2 oauth2; | ||
Logger logger = LoggerFactory.getLogger(this.getClass()); | ||
|
||
public UserAuthorisationTokenService(IdamApi idamApi, Oauth2 oauth2) { | ||
this.idamApi = idamApi; | ||
this.oauth2 = oauth2; | ||
} | ||
|
||
@LogExecutionTime | ||
@Cacheable(value = "userOIDTokenCache") | ||
public String getAuthorisationToken(String username, String password) { | ||
logger.info("IDAM /o/token invoked."); | ||
TokenExchangeResponse tokenExchangeResponse = idamApi.exchangeToken( | ||
oauth2.getClientId(), | ||
oauth2.getClientSecret(), | ||
oauth2.getRedirectUrl(), | ||
GRANT_TYPE_PASSWORD, | ||
username, | ||
password, | ||
DEFAULT_SCOPE); | ||
return BEARER + tokenExchangeResponse.getAccessToken(); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
src/main/java/uk/gov/hmcts/cmc/claimstore/services/UserInfoService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package uk.gov.hmcts.cmc.claimstore.services; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.cache.annotation.Cacheable; | ||
import org.springframework.stereotype.Component; | ||
import uk.gov.hmcts.cmc.claimstore.models.idam.UserInfo; | ||
import uk.gov.hmcts.cmc.claimstore.requests.idam.IdamApi; | ||
import uk.gov.hmcts.cmc.claimstore.stereotypes.LogExecutionTime; | ||
|
||
@Component | ||
public class UserInfoService { | ||
|
||
private final IdamApi idamApi; | ||
Logger logger = LoggerFactory.getLogger(this.getClass()); | ||
|
||
public UserInfoService(IdamApi idamApi) { | ||
this.idamApi = idamApi; | ||
} | ||
|
||
@LogExecutionTime | ||
@Cacheable(value = "userInfoCache") | ||
public UserInfo getUserInfo(String bearerToken) { | ||
logger.info("IDAM /o/userinfo invoked"); | ||
return idamApi.retrieveUserInfo(bearerToken); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
src/test/java/uk/gov/hmcts/cmc/claimstore/services/UserAuthorisationTokenServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package uk.gov.hmcts.cmc.claimstore.services; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import uk.gov.hmcts.cmc.claimstore.models.idam.Oauth2; | ||
import uk.gov.hmcts.cmc.claimstore.models.idam.TokenExchangeResponse; | ||
import uk.gov.hmcts.cmc.claimstore.requests.idam.IdamApi; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.ArgumentMatchers.eq; | ||
import static org.mockito.Mockito.when; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class UserAuthorisationTokenServiceTest { | ||
private static final String TOKEN = "I am a valid token"; | ||
public static final String BEARER = "Bearer "; | ||
private static final String USERNAME = "Username"; | ||
private static final String PASSWORD = "Password"; | ||
|
||
@Mock | ||
private IdamApi idamApi; | ||
@Mock | ||
private Oauth2 oauth2; | ||
@InjectMocks | ||
private UserAuthorisationTokenService userAuthorisationTokenService; | ||
|
||
@Mock | ||
TokenExchangeResponse tokenExchangeResponse; | ||
|
||
@Test | ||
void findsUserInfoForAuthToken() { | ||
when(idamApi.exchangeToken( | ||
any(), | ||
any(), | ||
any(), | ||
any(), | ||
eq(USERNAME), | ||
eq(PASSWORD), | ||
any()) | ||
).thenReturn(tokenExchangeResponse); | ||
when(tokenExchangeResponse.getAccessToken()).thenReturn(TOKEN); | ||
|
||
final String authorisationToken = userAuthorisationTokenService.getAuthorisationToken(USERNAME, PASSWORD); | ||
assertEquals(BEARER + TOKEN, authorisationToken); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
src/test/java/uk/gov/hmcts/cmc/claimstore/services/UserInfoServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package uk.gov.hmcts.cmc.claimstore.services; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import uk.gov.hmcts.cmc.claimstore.requests.idam.IdamApi; | ||
|
||
import static org.mockito.Mockito.verify; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class UserInfoServiceTest { | ||
private static final String AUTHORISATION = "Bearer I am a valid token"; | ||
|
||
@Mock | ||
private IdamApi idamApi; | ||
@InjectMocks | ||
private UserInfoService userInfoService; | ||
|
||
@Test | ||
void findsUserInfoForAuthToken() { | ||
userInfoService.getUserInfo(AUTHORISATION); | ||
verify(idamApi).retrieveUserInfo(AUTHORISATION); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters