Search in sources :

Example 1 with WTFDYUMException

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));
}
Also used : WTFDYUMException(com.jeanchampemont.wtfdyum.utils.WTFDYUMException) Event(com.jeanchampemont.wtfdyum.dto.Event) Test(org.junit.Test)

Example 2 with WTFDYUMException

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();
}
Also used : WTFDYUMException(com.jeanchampemont.wtfdyum.utils.WTFDYUMException) Principal(com.jeanchampemont.wtfdyum.dto.Principal) Test(org.junit.Test)

Example 3 with WTFDYUMException

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;
}
Also used : WTFDYUMException(com.jeanchampemont.wtfdyum.utils.WTFDYUMException)

Example 4 with WTFDYUMException

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;
}
Also used : User(com.jeanchampemont.wtfdyum.dto.User) WTFDYUMException(com.jeanchampemont.wtfdyum.utils.WTFDYUMException) twitter4j(twitter4j)

Example 5 with WTFDYUMException

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;
}
Also used : PrincipalService(com.jeanchampemont.wtfdyum.service.PrincipalService) WTFDYUMException(com.jeanchampemont.wtfdyum.utils.WTFDYUMException) Secured(com.jeanchampemont.wtfdyum.security.Secured) AdminService(com.jeanchampemont.wtfdyum.service.AdminService) Autowired(org.springframework.beans.factory.annotation.Autowired) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) RequestMethod(org.springframework.web.bind.annotation.RequestMethod) Controller(org.springframework.stereotype.Controller) Collectors(java.util.stream.Collectors) SessionManager(com.jeanchampemont.wtfdyum.utils.SessionManager) ModelAndView(org.springframework.web.servlet.ModelAndView) Collectors.toMap(java.util.stream.Collectors.toMap) AuthenticationService(com.jeanchampemont.wtfdyum.service.AuthenticationService) TwitterService(com.jeanchampemont.wtfdyum.service.TwitterService) Map(java.util.Map) Feature(com.jeanchampemont.wtfdyum.dto.Feature) WTFDYUMException(com.jeanchampemont.wtfdyum.utils.WTFDYUMException) ModelAndView(org.springframework.web.servlet.ModelAndView) Collectors.toMap(java.util.stream.Collectors.toMap) Map(java.util.Map) Secured(com.jeanchampemont.wtfdyum.security.Secured) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

WTFDYUMException (com.jeanchampemont.wtfdyum.utils.WTFDYUMException)12 Test (org.junit.Test)5 Event (com.jeanchampemont.wtfdyum.dto.Event)3 Feature (com.jeanchampemont.wtfdyum.dto.Feature)3 Principal (com.jeanchampemont.wtfdyum.dto.Principal)3 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)3 User (com.jeanchampemont.wtfdyum.dto.User)2 Secured (com.jeanchampemont.wtfdyum.security.Secured)2 ResponseListMockForTest (com.jeanchampemont.wtfdyum.utils.ResponseListMockForTest)2 ModelAndView (org.springframework.web.servlet.ModelAndView)2 twitter4j (twitter4j)2 AdminService (com.jeanchampemont.wtfdyum.service.AdminService)1 AuthenticationService (com.jeanchampemont.wtfdyum.service.AuthenticationService)1 PrincipalService (com.jeanchampemont.wtfdyum.service.PrincipalService)1 TwitterService (com.jeanchampemont.wtfdyum.service.TwitterService)1 SessionManager (com.jeanchampemont.wtfdyum.utils.SessionManager)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 Random (java.util.Random)1