### Question:
TransactionalPrivateMessageService extends AbstractTransactionalEntityService<PrivateMessage, PrivateMessageDao> implements PrivateMessageService { @Override public int currentUserNewPmCount() { String username = securityService.getCurrentUserUsername(); if (username == null) { return DEFAULT_MESSAGE_COUNT; } Integer count = userDataCache.getNewPmCountFor(username); if (count != null) { return count; } count = this.getDao().getNewMessagesCountFor(username); userDataCache.putNewPmCount(username, count); return count; } TransactionalPrivateMessageService(PrivateMessageDao pmDao,
                                              SecurityService securityService,
                                              UserService userService,
                                              UserDataCacheService userDataCache,
                                              MailService mailService,
                                              JCommuneProperty sendingNotificationsEnabledProperty); @Override Page<PrivateMessage> getInboxForCurrentUser(String page); @Override Page<PrivateMessage> getOutboxForCurrentUser(String page); @Override @PreAuthorize("hasPermission(#userFrom.id, 'USER', 'ProfilePermission.SEND_PRIVATE_MESSAGES')") PrivateMessage sendMessage(String title, String body, JCUser recipient, JCUser userFrom); @Override Page<PrivateMessage> getDraftsForCurrentUser(String page); @Override @PreAuthorize("hasPermission(#userFrom.id, 'USER', 'ProfilePermission.SEND_PRIVATE_MESSAGES')") void saveDraft(long id, JCUser userTo, String title, String body, JCUser userFrom); @Override int currentUserNewPmCount(); @Override @PreAuthorize("hasPermission(#userFrom.id, 'USER', 'ProfilePermission.SEND_PRIVATE_MESSAGES')") PrivateMessage sendDraft(long id, String title, String body,
                                    JCUser recipient, JCUser userFrom); @Override @PreAuthorize("hasPermission(#id, 'PRIVATE_MESSAGE', 'GeneralPermission.READ')") PrivateMessage get(Long id); @Override String delete(List<Long> ids); @Override @PreAuthorize("hasPermission(#senderId, 'USER', 'ProfilePermission.SEND_PRIVATE_MESSAGES')") void checkPermissionsToSend(Long senderId); static final int DEFAULT_MESSAGE_COUNT; }### Answer:
@Test public void testCurrentUserNewPmCount() { int expectedPmCount = 2; when(securityService.getCurrentUserUsername()).thenReturn(USERNAME); when(pmDao.getNewMessagesCountFor(USERNAME)).thenReturn(expectedPmCount); when(userDataCache.getNewPmCountFor(USERNAME)).thenReturn(null); int newPmCount = pmService.currentUserNewPmCount(); assertEquals(newPmCount, expectedPmCount); verify(securityService).getCurrentUserUsername(); verify(pmDao).getNewMessagesCountFor(USERNAME); verify(userDataCache).putNewPmCount(USERNAME, newPmCount); }
@Test public void testCurrentUserNewPmCountCached() { int expectedPmCount = 2; when(securityService.getCurrentUserUsername()).thenReturn(USERNAME); when(userDataCache.getNewPmCountFor(USERNAME)).thenReturn(expectedPmCount); int newPmCount = pmService.currentUserNewPmCount(); assertEquals(newPmCount, expectedPmCount); verify(pmDao, never()).getNewMessagesCountFor(anyString()); verify(userDataCache).getNewPmCountFor(USERNAME); }
@Test public void testCurrentUserNewPmCountWithoutUser() { when(securityService.getCurrentUserUsername()).thenReturn(null); int newPmCount = pmService.currentUserNewPmCount(); assertEquals(newPmCount, 0); verify(securityService).getCurrentUserUsername(); }