@BeforeMethod
 public void init() {
   initMocks(this);
   topicFetchService = new TransactionalTopicFetchService(topicDao, userService, searchDao);
   user = new JCUser("username", "*****@*****.**", "password");
   when(userService.getCurrentUser()).thenReturn(user);
 }
 @BeforeMethod
 public void setUp() {
   initMocks(this);
   userContactsService = new TransactionalUserContactsService(userContactsDao, userService);
   user = new JCUser(USERNAME, EMAIL, PASSWORD);
   when(userService.getCurrentUser()).thenReturn(user);
 }
 /**
  * Get email of user by user's id.
  *
  * @param userId user's id
  * @return an email of user by user's id
  */
 private String getEmailOfUser(long userId) {
   try {
     JCUser user = userService.get(userId);
     return user.getEmail();
   } catch (NotFoundException e) {
     return null;
   }
 }
 @BeforeMethod
 public void init() {
   MockitoAnnotations.initMocks(this);
   Mockito.when(userService.getCurrentUser())
       .thenReturn(new JCUser("username", "email", userCurrentPassword));
   validator = new ChangedPasswordValidator(userService, encryptionService);
   //
   editUserProfileDto.setCurrentUserPassword(userCurrentPassword);
   editUserProfileDto.setNewUserPassword(userNewPassword);
 }
 @Override
 public void toggleSubscription(SubscriptionAwareEntity entityToSubscribe) {
   JCUser current = userService.getCurrentUser();
   if (entityToSubscribe.getSubscribers().contains(current)) {
     entityToSubscribe.getSubscribers().remove(current);
   } else {
     entityToSubscribe.getSubscribers().add(current);
   }
   saveChanges(entityToSubscribe);
 }
 /** {@inheritDoc} */
 @Override
 public void toggleBranchSubscription(Branch branch) {
   JCUser current = userService.getCurrentUser();
   if (branch.getSubscribers().contains(current)) {
     branch.getSubscribers().remove(current);
   } else {
     branch.getSubscribers().add(current);
   }
   branchDao.update(branch);
 }
 /** {@inheritDoc} */
 @Override
 public void toggleTopicSubscription(Topic topic) {
   JCUser current = userService.getCurrentUser();
   if (topic.userSubscribed(current)) {
     topic.getSubscribers().remove(current);
   } else {
     topic.getSubscribers().add(current);
   }
   topicDao.update(topic);
 }
 /** {@inheritDoc} */
 @Override
 public boolean isValid(String value, ConstraintValidatorContext context) {
   if (value == null) {
     // null emails checks are out of scope, pls use separate annotation for that
     return true;
   }
   JCUser user = userService.getCurrentUser();
   if (user.getEmail().equals(value)) {
     return true; // no changes in an email, that's ok
   } else {
     User found = userDao.getByEmail(value);
     return found == null;
   }
 }
  @Test
  public void testGetUnansweredTopics() {
    int pageNumber = 1;
    int pageSize = 20;
    List<Topic> expectedList = Collections.nCopies(2, new Topic(user, "title"));
    Page<Topic> expectedPage = new PageImpl<Topic>(expectedList);
    when(topicDao.getUnansweredTopics(Matchers.<JCommunePageRequest>any(), eq(user)))
        .thenReturn(expectedPage);
    user.setPageSize(pageSize);
    when(userService.getCurrentUser()).thenReturn(user);

    Page<Topic> actualPage = topicFetchService.getUnansweredTopics(pageNumber);
    assertNotNull(actualPage);
    assertEquals(actualPage, expectedPage);
  }
  @Test
  public void testGetTopics() {
    int pageSize = 50;
    Branch branch = createBranch();
    Page<Topic> expectedPage = new PageImpl<Topic>(Collections.<Topic>emptyList());

    JCUser currentUser = new JCUser("current", null, null);
    currentUser.setPageSize(pageSize);
    when(userService.getCurrentUser()).thenReturn(currentUser);
    when(topicDao.getTopics(Matchers.any(Branch.class), Matchers.any(JCommunePageRequest.class)))
        .thenReturn(expectedPage);

    Page<Topic> actualPage = topicFetchService.getTopics(branch, pageSize, true);

    assertEquals(
        actualPage, expectedPage, "Service returned incorrect data for one page of topics");
    verify(topicDao).getTopics(Matchers.any(Branch.class), Matchers.any(JCommunePageRequest.class));
  }
 private Topic createCodeReviewWithLockHandling(Topic topic, TopicDto topicDto)
     throws NotFoundException {
   for (int i = 0; i < UserController.LOGIN_TRIES_AFTER_LOCK; i++) {
     try {
       return topicModificationService.createTopic(topic, topicDto.getBodyText());
     } catch (HibernateOptimisticLockingFailureException e) {
     }
   }
   try {
     return topicModificationService.createTopic(topic, topicDto.getBodyText());
   } catch (HibernateOptimisticLockingFailureException e) {
     LOGGER.error(
         "User has been optimistically locked and can't be reread {} times. Username: {}",
         UserController.LOGIN_TRIES_AFTER_LOCK,
         userService.getCurrentUser().getUsername());
     throw e;
   }
 }
  @Test
  public void testGetAllTopicsPastLastDay() throws NotFoundException {
    int pageNumber = 1;
    int pageSize = 20;
    List<Topic> expectedList = Collections.nCopies(2, new Topic(user, "title"));
    Page<Topic> expectedPage = new PageImpl<Topic>(expectedList);
    when(topicDao.getTopicsUpdatedSince(
            Matchers.<DateTime>any(), Matchers.<JCommunePageRequest>any(), eq(user)))
        .thenReturn(expectedPage);
    user.setPageSize(pageSize);
    when(userService.getCurrentUser()).thenReturn(user);

    Page<Topic> actualPage = topicFetchService.getRecentTopics(pageNumber);

    assertNotNull(actualPage);
    assertEquals(expectedPage, actualPage);
    verify(topicDao)
        .getTopicsUpdatedSince(
            Matchers.<DateTime>any(), Matchers.<JCommunePageRequest>any(), eq(user));
  }
  @Test
  public void renderAvatarShouldNotReturnNotModifiedAvatarInResponse()
      throws IOException, NotFoundException {
    JCUser user = getUser();
    user.setAvatar(validAvatar);
    user.setAvatarLastModificationTime(new DateTime(0));
    when(userService.get(anyLong())).thenReturn(user);
    MockHttpServletResponse response = new MockHttpServletResponse();
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.addHeader(avatarController.IF_MODIFIED_SINCE_HEADER, new Date(1000));

    avatarController.renderAvatar(request, response, 0L);

    assertEquals(response.getStatus(), HttpServletResponse.SC_NOT_MODIFIED);
    assertNotSame(response.getContentAsByteArray(), validAvatar);
    assertEquals(response.getHeader("Pragma"), "public");
    List<String> cacheControlHeaders = response.getHeaders("Cache-Control");
    assertTrue(cacheControlHeaders.contains("public"));
    assertTrue(cacheControlHeaders.contains("must-revalidate"));
    assertTrue(cacheControlHeaders.contains("max-age=0"));
    assertNotNull(response.getHeader("Expires")); // System.currentTimeMillis() is used
    assertNotNull(response.getHeader("Last-Modified")); // depends on current timezone
  }
 @BeforeMethod
 public void init() throws NotFoundException {
   initMocks(this);
   when(userService.get(anyLong())).thenReturn(user);
   validator = new ChangedEmailValidator(userService, userDao);
 }