/** * 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; } }
@Test public void testRemoveUnfamiliarContact() { UserContact userContact = new UserContact(CONTACT, createUserContactType()); userContact.setId(1L); user.addContact(userContact); userContactsService.removeContact(2L); assertEquals(user.getUserContacts().size(), 1); }
@Test public void testRemoveContact() { UserContact userContact = new UserContact(CONTACT, createUserContactType()); userContact.setId(1L); user.addContact(userContact); when(userContactsDao.getContactById(1L)).thenReturn(userContact); userContactsService.removeContact(1L); assertEquals(user.getUserContacts().size(), 0); }
/** {@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 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)); }
/** {@inheritDoc} */ @SuppressWarnings("unchecked") @Override public JCUser getByUsername(String username) { List<JCUser> users = session() .createCriteria(JCUser.class) .add(Restrictions.eq("username", username).ignoreCase()) .list(); if (users.size() == 1) { return users.get(0); } else { // see JavaDocs to get understanding on when this situation can happen for (JCUser user : users) { if (user.getUsername().equals(username)) { return user; } } return null; } }
@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 }
@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 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)); }
private JCUser getUser() { JCUser newUser = new JCUser(USER_NAME, EMAIL, PASSWORD); newUser.setFirstName(FIRST_NAME); newUser.setLastName(LAST_NAME); return newUser; }