use of com.jeanchampemont.wtfdyum.utils.WTFDYUMException in project WTFDYUM by jchampemont.
the class CronServiceTest method cronTestRateLimitError.
@Test
public void cronTestRateLimitError() throws Exception {
principal(3L);
featureEnabled(3L, true, Feature.NOTIFY_UNFOLLOW);
when(featureService.cron(3L, Feature.NOTIFY_UNFOLLOW)).thenThrow(new WTFDYUMException(WTFDYUMExceptionType.GET_FOLLOWERS_RATE_LIMIT_EXCEEDED));
sut.cron();
// should have an event RATE_LIMIT_EXCEEDED
verify(userService, times(1)).addEvent(3L, new Event(EventType.RATE_LIMIT_EXCEEDED, null));
}
use of com.jeanchampemont.wtfdyum.utils.WTFDYUMException in project WTFDYUM by jchampemont.
the class UserControllerTest method indexTestTwitterErrorException.
@Test
public void indexTestTwitterErrorException() throws Exception {
final Principal principal = new Principal(1L, "tok", "toksec");
SessionManager.setPrincipal(principal);
when(authenticationService.getCurrentUserId()).thenReturn(12340L);
when(twitterService.getUser(principal, 12340L)).thenThrow(new WTFDYUMException(WTFDYUMExceptionType.TWITTER_ERROR));
mockMvc.perform(get("/user")).andExpect(status().is3xxRedirection()).andExpect(redirectedUrl("/"));
verify(authenticationService, times(1)).logOut();
}
use of com.jeanchampemont.wtfdyum.utils.WTFDYUMException in project WTFDYUM by jchampemont.
the class TwitterServiceImpl method getFollowers.
@Override
public Set<Long> getFollowers(final Long userId, final Optional<Principal> principal) throws WTFDYUMException {
Preconditions.checkNotNull(userId);
final Twitter twitter = principal.isPresent() ? twitter(principal.get()) : twitter();
final Set<Long> result = new HashSet<>();
try {
IDs followersIDs = null;
long cursor = -1;
do {
followersIDs = twitter.getFollowersIDs(userId, cursor);
if (followersIDs.hasNext()) {
cursor = followersIDs.getNextCursor();
checkRateLimitStatus(followersIDs.getRateLimitStatus(), WTFDYUMExceptionType.GET_FOLLOWERS_RATE_LIMIT_EXCEEDED);
}
final Set<Long> currentFollowers = Arrays.stream(followersIDs.getIDs()).boxed().collect(Collectors.toCollection(() -> new HashSet<>()));
result.addAll(currentFollowers);
} while (followersIDs.hasNext());
} catch (final TwitterException e) {
log.debug("Error while getFollowers", e);
throw new WTFDYUMException(e, WTFDYUMExceptionType.TWITTER_ERROR);
}
return result;
}
use of com.jeanchampemont.wtfdyum.utils.WTFDYUMException in project WTFDYUM by jchampemont.
the class TwitterServiceImpl method getUsers.
@Override
public List<User> getUsers(final Principal principal, final long... ids) throws WTFDYUMException {
final List<User> result = new ArrayList<>();
if (ids.length == 0) {
return result;
}
try {
final List<twitter4j.User> users = new ArrayList<>();
for (int i = 0; i <= (ids.length - 1) / 100; i++) {
final ResponseList<twitter4j.User> lookupUsers = twitter(principal).users().lookupUsers(Arrays.copyOfRange(ids, i * 100, Math.min((i + 1) * 100, ids.length)));
users.addAll(lookupUsers);
}
for (final twitter4j.User u : users) {
result.add(mapper.map(u, User.class));
}
} catch (final TwitterException e) {
if (e.getErrorCode() == 17) {
log.debug("Error while getUsers for ids: " + Arrays.toString(ids) + ". Seems like those users are not on twitter anymore.");
} else {
log.debug("Error while getUsers for ids: " + Arrays.toString(ids), e);
throw new WTFDYUMException(e, WTFDYUMExceptionType.TWITTER_ERROR);
}
}
return result;
}
use of com.jeanchampemont.wtfdyum.utils.WTFDYUMException in project WTFDYUM by jchampemont.
the class AdminController method index.
@RequestMapping(method = RequestMethod.GET)
@Secured
public ModelAndView index() {
if (!authenticationService.isAdmin()) {
return new ModelAndView("redirect:/");
}
ModelAndView result = new ModelAndView("admin/index");
final Long userId = authenticationService.getCurrentUserId();
try {
result.getModel().put("user", twitterService.getUser(SessionManager.getPrincipal(), userId));
} catch (final WTFDYUMException e) {
authenticationService.logOut();
return new ModelAndView("redirect:/");
}
result.getModel().put("membersCount", principalService.countMembers());
Map<String, Integer> featureEnabledCount = adminService.countEnabledFeature(principalService.getMembers()).entrySet().stream().collect(toMap(e -> e.getKey().name(), Map.Entry::getValue));
result.getModel().put("availableFeatures", Feature.values());
result.getModel().put("featureEnabledCount", featureEnabledCount);
return result;
}
Aggregations