836 String expiration1 =
"2014-09-10T22:49:44.090+0000";
837 Date expirationDate1 =
formatter.parse(expiration1, Locale.ENGLISH);
839 ClientDetailsEntity mockedClient1 = mock(ClientDetailsEntity.class);
840 when(mockedClient1.getClientId()).thenReturn(
"mocked_client_1");
842 OAuth2Request req1 =
new OAuth2Request(
new HashMap<String, String>(),
"client1",
new ArrayList<GrantedAuthority>(),
843 true,
new HashSet<String>(),
new HashSet<String>(),
"http://foo.com",
844 new HashSet<String>(), null);
845 Authentication mockAuth1 = mock(Authentication.class, withSettings().serializable());
846 OAuth2Authentication auth1 =
new OAuth2Authentication(req1, mockAuth1);
848 AuthenticationHolderEntity holder1 =
new AuthenticationHolderEntity();
850 holder1.setAuthentication(auth1);
852 OAuth2RefreshTokenEntity token1 =
new OAuth2RefreshTokenEntity();
854 token1.setClient(mockedClient1);
855 token1.setExpiration(expirationDate1);
856 token1.setJwt(JWTParser.parse(
"eyJhbGciOiJub25lIn0.eyJqdGkiOiJmOTg4OWQyOS0xMTk1LTQ4ODEtODgwZC1lZjVlYzAwY2Y4NDIifQ."));
857 token1.setAuthenticationHolder(holder1);
859 String expiration2 =
"2015-01-07T18:31:50.079+0000";
860 Date expirationDate2 =
formatter.parse(expiration2, Locale.ENGLISH);
862 ClientDetailsEntity mockedClient2 = mock(ClientDetailsEntity.class);
863 when(mockedClient2.getClientId()).thenReturn(
"mocked_client_2");
865 OAuth2Request req2 =
new OAuth2Request(
new HashMap<String, String>(),
"client2",
new ArrayList<GrantedAuthority>(),
866 true,
new HashSet<String>(),
new HashSet<String>(),
"http://bar.com",
867 new HashSet<String>(), null);
868 Authentication mockAuth2 = mock(Authentication.class, withSettings().serializable());
869 OAuth2Authentication auth2 =
new OAuth2Authentication(req2, mockAuth2);
871 AuthenticationHolderEntity holder2 =
new AuthenticationHolderEntity();
873 holder2.setAuthentication(auth2);
875 OAuth2RefreshTokenEntity token2 =
new OAuth2RefreshTokenEntity();
877 token2.setClient(mockedClient2);
878 token2.setExpiration(expirationDate2);
879 token2.setJwt(JWTParser.parse(
"eyJhbGciOiJub25lIn0.eyJqdGkiOiJlYmEyYjc3My0xNjAzLTRmNDAtOWQ3MS1hMGIxZDg1OWE2MDAifQ."));
880 token2.setAuthenticationHolder(holder2);
882 String configJson =
"{" +
883 "\"" + MITREidDataService.SYSTEMSCOPES +
"\": [], " +
884 "\"" + MITREidDataService.ACCESSTOKENS +
"\": [], " +
885 "\"" + MITREidDataService.CLIENTS +
"\": [], " +
886 "\"" + MITREidDataService.GRANTS +
"\": [], " +
887 "\"" + MITREidDataService.WHITELISTEDSITES +
"\": [], " +
888 "\"" + MITREidDataService.BLACKLISTEDSITES +
"\": [], " +
889 "\"" + MITREidDataService.AUTHENTICATIONHOLDERS +
"\": [" +
891 "{\"id\":1,\"authentication\":{\"clientAuthorization\":{\"clientId\":\"client1\",\"redirectUri\":\"http://foo.com\"}," 892 +
"\"userAuthentication\":null}}," +
893 "{\"id\":2,\"authentication\":{\"clientAuthorization\":{\"clientId\":\"client2\",\"redirectUri\":\"http://bar.com\"}," 894 +
"\"userAuthentication\":null}}" +
896 "\"" + MITREidDataService.REFRESHTOKENS +
"\": [" +
898 "{\"id\":1,\"clientId\":\"mocked_client_1\",\"expiration\":\"2014-09-10T22:49:44.090+0000\"," 899 +
"\"authenticationHolderId\":1,\"value\":\"eyJhbGciOiJub25lIn0.eyJqdGkiOiJmOTg4OWQyOS0xMTk1LTQ4ODEtODgwZC1lZjVlYzAwY2Y4NDIifQ.\"}," +
900 "{\"id\":2,\"clientId\":\"mocked_client_2\",\"expiration\":\"2015-01-07T18:31:50.079+0000\"," 901 +
"\"authenticationHolderId\":2,\"value\":\"eyJhbGciOiJub25lIn0.eyJqdGkiOiJlYmEyYjc3My0xNjAzLTRmNDAtOWQ3MS1hMGIxZDg1OWE2MDAifQ.\"}" +
905 System.err.println(configJson);
907 JsonReader reader =
new JsonReader(
new StringReader(configJson));
908 final Map<Long, OAuth2RefreshTokenEntity> fakeRefreshTokenTable =
new HashMap<>();
909 final Map<Long, AuthenticationHolderEntity> fakeAuthHolderTable =
new HashMap<>();
913 public OAuth2RefreshTokenEntity answer(InvocationOnMock invocation)
throws Throwable {
914 OAuth2RefreshTokenEntity _token = (OAuth2RefreshTokenEntity) invocation.getArguments()[0];
915 if(_token.getId() == null) {
918 fakeRefreshTokenTable.put(_token.getId(), _token);
924 public OAuth2RefreshTokenEntity answer(InvocationOnMock invocation)
throws Throwable {
925 Long _id = (Long) invocation.getArguments()[0];
926 return fakeRefreshTokenTable.get(_id);
931 public ClientDetailsEntity answer(InvocationOnMock invocation)
throws Throwable {
932 String _clientId = (String) invocation.getArguments()[0];
933 ClientDetailsEntity _client = mock(ClientDetailsEntity.class);
934 when(_client.getClientId()).thenReturn(_clientId);
938 when(
authHolderRepository.
save(isA(AuthenticationHolderEntity.class))).thenAnswer(
new Answer<AuthenticationHolderEntity>() {
941 public AuthenticationHolderEntity answer(InvocationOnMock invocation)
throws Throwable {
942 AuthenticationHolderEntity _holder = (AuthenticationHolderEntity) invocation.getArguments()[0];
943 if(_holder.getId() == null) {
946 fakeAuthHolderTable.put(_holder.getId(), _holder);
952 public AuthenticationHolderEntity answer(InvocationOnMock invocation)
throws Throwable {
953 Long _id = (Long) invocation.getArguments()[0];
954 return fakeAuthHolderTable.get(_id);
959 List<OAuth2RefreshTokenEntity> savedRefreshTokens =
new ArrayList(fakeRefreshTokenTable.values());
960 Collections.sort(savedRefreshTokens,
new refreshTokenIdComparator());
962 assertThat(savedRefreshTokens.get(0).getAuthenticationHolder().getId(), equalTo(356L));
963 assertThat(savedRefreshTokens.get(1).getAuthenticationHolder().getId(), equalTo(357L));
ClientDetailsEntity getClientByClientId(String clientId)
OAuth2RefreshTokenEntity saveRefreshToken(OAuth2RefreshTokenEntity refreshToken)
OAuth2ClientRepository clientRepository
Definition: TestMITREidDataService_1_1.java:94
MITREidDataService_1_1 dataService
Definition: TestMITREidDataService_1_1.java:126
OAuth2TokenRepository tokenRepository
Definition: TestMITREidDataService_1_1.java:104
AuthenticationHolderEntity save(AuthenticationHolderEntity a)
AuthenticationHolderEntity getById(Long id)
AuthenticationHolderRepository authHolderRepository
Definition: TestMITREidDataService_1_1.java:102
void importData(JsonReader reader)
Definition: MITREidDataService_1_1.java:131
DateFormatter formatter
Definition: TestMITREidDataService_1_1.java:127
OAuth2RefreshTokenEntity getRefreshTokenById(Long Id)