Search in sources :

Example 6 with WTFDYUMException

use of com.jeanchampemont.wtfdyum.utils.WTFDYUMException in project WTFDYUM by jchampemont.

the class MainController method signin.

@RequestMapping(value = "/signin", method = RequestMethod.GET)
public RedirectView signin(final HttpServletRequest request) throws WTFDYUMException {
    if (authenticationService.isAuthenticated()) {
        return new RedirectView("/user", true);
    }
    if (maxMembers > 0 && principalService.countMembers() >= maxMembers) {
        throw new WTFDYUMException(WTFDYUMExceptionType.MEMBER_LIMIT_EXCEEDED);
    }
    final RequestToken requestToken = twitterService.signin("/signin/callback");
    request.getSession().setAttribute(SESSION_REQUEST_TOKEN, requestToken);
    return new RedirectView(requestToken.getAuthenticationURL());
}
Also used : RequestToken(twitter4j.auth.RequestToken) WTFDYUMException(com.jeanchampemont.wtfdyum.utils.WTFDYUMException) RedirectView(org.springframework.web.servlet.view.RedirectView) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 7 with WTFDYUMException

use of com.jeanchampemont.wtfdyum.utils.WTFDYUMException in project WTFDYUM by jchampemont.

the class CronServiceTest method cronTestTwitterError.

@Test
public void cronTestTwitterError() throws Exception {
    principal(2L);
    featureEnabled(2L, true, Feature.NOTIFY_UNFOLLOW);
    when(featureService.cron(2L, Feature.NOTIFY_UNFOLLOW)).thenThrow(new WTFDYUMException(WTFDYUMExceptionType.TWITTER_ERROR));
    sut.cron();
    // should have an event TWITTER_ERROR
    verify(userService, times(1)).addEvent(2L, new Event(EventType.TWITTER_ERROR, null));
}
Also used : WTFDYUMException(com.jeanchampemont.wtfdyum.utils.WTFDYUMException) Event(com.jeanchampemont.wtfdyum.dto.Event) Test(org.junit.Test)

Example 8 with WTFDYUMException

use of com.jeanchampemont.wtfdyum.utils.WTFDYUMException in project WTFDYUM by jchampemont.

the class TwitterServiceTest method getUsersTwitterExceptionTest.

@Test
public void getUsersTwitterExceptionTest() throws Exception {
    final User userMock = mock(User.class);
    when(userMock.getName()).thenReturn("name");
    when(userMock.getScreenName()).thenReturn("screenName");
    when(userMock.getProfileImageURL()).thenReturn("profile img url");
    when(userMock.getURL()).thenReturn("user url");
    final ResponseList<User> users = new ResponseListMockForTest<User>();
    final long[] ids = new long[100];
    final Random rand = new Random();
    for (int i = 0; i < 100; i++) {
        users.add(userMock);
        final long id = rand.nextLong();
        when(userMock.getId()).thenReturn(id);
        ids[i] = id;
    }
    when(twitter.users()).thenReturn(usersResources);
    when(usersResources.lookupUsers(ids)).thenThrow(TwitterException.class);
    try {
        sut.getUsers(new Principal(1L, "", ""), ids);
        Assertions.failBecauseExceptionWasNotThrown(WTFDYUMException.class);
    } catch (WTFDYUMException e) {
        assertThat(e.getType()).isEqualTo(WTFDYUMExceptionType.TWITTER_ERROR);
    }
}
Also used : Random(java.util.Random) WTFDYUMException(com.jeanchampemont.wtfdyum.utils.WTFDYUMException) Principal(com.jeanchampemont.wtfdyum.dto.Principal) ResponseListMockForTest(com.jeanchampemont.wtfdyum.utils.ResponseListMockForTest) ResponseListMockForTest(com.jeanchampemont.wtfdyum.utils.ResponseListMockForTest) Test(org.junit.Test)

Example 9 with WTFDYUMException

use of com.jeanchampemont.wtfdyum.utils.WTFDYUMException in project WTFDYUM by jchampemont.

the class TwitterServiceTest method sendDirectMessageTestException.

@Test
public void sendDirectMessageTestException() throws Exception {
    when(twitter.sendDirectMessage(444L, "text")).thenThrow(new TwitterException("msg"));
    try {
        sut.sendDirectMessage(new Principal(412L, "", ""), 444L, "text");
        Assertions.failBecauseExceptionWasNotThrown(WTFDYUMException.class);
    } catch (final WTFDYUMException e) {
        assertThat(e.getType()).isEqualTo(WTFDYUMExceptionType.TWITTER_ERROR);
    }
}
Also used : WTFDYUMException(com.jeanchampemont.wtfdyum.utils.WTFDYUMException) Principal(com.jeanchampemont.wtfdyum.dto.Principal) ResponseListMockForTest(com.jeanchampemont.wtfdyum.utils.ResponseListMockForTest) Test(org.junit.Test)

Example 10 with WTFDYUMException

use of com.jeanchampemont.wtfdyum.utils.WTFDYUMException in project WTFDYUM by jchampemont.

the class UserController method index.

@RequestMapping(method = RequestMethod.GET)
@Secured
public ModelAndView index() {
    final ModelAndView result = new ModelAndView("user/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("events", userService.getRecentEvents(userId, 10));
    result.getModel().put("availableFeatures", Feature.values());
    final Map<String, Boolean> featuresStatus = new HashMap<>();
    for (final Feature f : Feature.values()) {
        featuresStatus.put(f.name(), featureService.isEnabled(userId, f));
    }
    result.getModel().put("featuresStatus", featuresStatus);
    return result;
}
Also used : HashMap(java.util.HashMap) WTFDYUMException(com.jeanchampemont.wtfdyum.utils.WTFDYUMException) ModelAndView(org.springframework.web.servlet.ModelAndView) Feature(com.jeanchampemont.wtfdyum.dto.Feature) 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