@Test
public void testImportGrants() throws IOException {
Date creationDate1 = DateUtil.utcToDate("2014-09-10T22:49:44.090+0000");
Date accessDate1 = DateUtil.utcToDate("2014-09-10T23:49:44.090+0000");
WhitelistedSite mockWlSite1 = mock(WhitelistedSite.class);
when(mockWlSite1.getId()).thenReturn(1L);
OAuth2AccessTokenEntity mockToken1 = mock(OAuth2AccessTokenEntity.class);
when(mockToken1.getId()).thenReturn(1L);
ApprovedSite site1 = new ApprovedSite();
site1.setId(1L);
site1.setClientId("foo");
site1.setCreationDate(creationDate1);
site1.setAccessDate(accessDate1);
site1.setUserId("user1");
site1.setWhitelistedSite(mockWlSite1);
site1.setAllowedScopes(ImmutableSet.of("openid", "phone"));
site1.setApprovedAccessTokens(ImmutableSet.of(mockToken1));
Date creationDate2 = DateUtil.utcToDate("2014-09-11T18:49:44.090+0000");
Date accessDate2 = DateUtil.utcToDate("2014-09-11T20:49:44.090+0000");
Date timeoutDate2 = DateUtil.utcToDate("2014-10-01T20:49:44.090+0000");
ApprovedSite site2 = new ApprovedSite();
site2.setId(2L);
site2.setClientId("bar");
site2.setCreationDate(creationDate2);
site2.setAccessDate(accessDate2);
site2.setUserId("user2");
site2.setAllowedScopes(ImmutableSet.of("openid", "offline_access", "email", "profile"));
site2.setTimeoutDate(timeoutDate2);
String configJson = "{" +
"\"" + MITREidDataService.CLIENTS + "\": [], " +
"\"" + MITREidDataService.ACCESSTOKENS + "\": [], " +
"\"" + MITREidDataService.REFRESHTOKENS + "\": [], " +
"\"" + MITREidDataService.WHITELISTEDSITES + "\": [], " +
"\"" + MITREidDataService.BLACKLISTEDSITES + "\": [], " +
"\"" + MITREidDataService.SYSTEMSCOPES + "\": [], " +
"\"" + MITREidDataService.AUTHENTICATIONHOLDERS + "\": [], " +
"\"" + MITREidDataService.GRANTS + "\": [" +
"{\"id\":1,\"clientId\":\"foo\",\"creationDate\":\"2014-09-10T22:49:44.090+0000\",\"accessDate\":\"2014-09-10T23:49:44.090+0000\","
+ "\"userId\":\"user1\",\"whitelistedSiteId\":null,\"allowedScopes\":[\"openid\",\"phone\"], \"whitelistedSiteId\":1,"
+ "\"approvedAccessTokens\":[1]}," +
"{\"id\":2,\"clientId\":\"bar\",\"creationDate\":\"2014-09-11T18:49:44.090+0000\",\"accessDate\":\"2014-09-11T20:49:44.090+0000\","
+ "\"timeoutDate\":\"2014-10-01T20:49:44.090+0000\",\"userId\":\"user2\","
+ "\"allowedScopes\":[\"openid\",\"offline_access\",\"email\",\"profile\"]}" +
" ]" +
"}";
System.err.println(configJson);
JsonReader reader = new JsonReader(new StringReader(configJson));
final Map<Long, ApprovedSite> fakeDb = new HashMap<Long, ApprovedSite>();
when(approvedSiteRepository.save(isA(ApprovedSite.class))).thenAnswer(new Answer<ApprovedSite>() {
Long id = 364L;
@Override
public ApprovedSite answer(InvocationOnMock invocation) throws Throwable {
ApprovedSite _site = (ApprovedSite) invocation.getArguments()[0];
if(_site.getId() == null) {
_site.setId(id++);
}
fakeDb.put(_site.getId(), _site);
return _site;
}
});
when(approvedSiteRepository.getById(anyLong())).thenAnswer(new Answer<ApprovedSite>() {
@Override
public ApprovedSite answer(InvocationOnMock invocation) throws Throwable {
Long _id = (Long) invocation.getArguments()[0];
return fakeDb.get(_id);
}
});
when(wlSiteRepository.getById(isNull(Long.class))).thenAnswer(new Answer<WhitelistedSite>() {
Long id = 432L;
@Override
public WhitelistedSite answer(InvocationOnMock invocation) throws Throwable {
WhitelistedSite _site = mock(WhitelistedSite.class);
when(_site.getId()).thenReturn(id++);
return _site;
}
});
when(tokenRepository.getAccessTokenById(isNull(Long.class))).thenAnswer(new Answer<OAuth2AccessTokenEntity>() {
Long id = 245L;