### Question:
TransactionalTopicDraftService implements TopicDraftService, PluginTopicDraftService { @Override @PreAuthorize("( (not #draft.codeReview) and (not #draft.plugable) " + "and hasPermission(#draft.branchId, 'BRANCH', 'BranchPermission.CREATE_POSTS')) " + "or (#draft.codeReview " + "and hasPermission(#draft.branchId, 'BRANCH', 'BranchPermission.CREATE_CODE_REVIEW')) " + " or #draft.plugable") public TopicDraft saveOrUpdateDraft(TopicDraft draft) { assertCreationAllowedForPlugableTopic(draft); JCUser user = userService.getCurrentUser(); TopicDraft currentDraft = topicDraftDao.getForUser(user); if (currentDraft == null) { currentDraft = draft; currentDraft.setTopicStarter(user); } else { currentDraft.setContent(draft.getContent()); currentDraft.setTitle(draft.getTitle()); currentDraft.setBranchId(draft.getBranchId()); currentDraft.setTopicType(draft.getTopicType()); if (draft.getPollTitle() != null || draft.getPollItemsValue() != null) { currentDraft.setPollTitle(draft.getPollTitle()); currentDraft.setPollItemsValue(draft.getPollItemsValue()); } } currentDraft.updateLastSavedTime(); topicDraftDao.saveOrUpdate(currentDraft); return currentDraft; } TransactionalTopicDraftService(UserService userService,
                                          TopicDraftDao topicDraftDao,
                                          SecurityContextFacade securityContextFacade,
                                          PermissionEvaluator permissionEvaluator,
                                          PluginLoader pluginLoader); @Override @PreAuthorize("isAuthenticated()") TopicDraft getDraft(); @Override @PreAuthorize("( (not #draft.codeReview) and (not #draft.plugable) " + "and hasPermission(#draft.branchId, 'BRANCH', 'BranchPermission.CREATE_POSTS')) " + "or (#draft.codeReview " + "and hasPermission(#draft.branchId, 'BRANCH', 'BranchPermission.CREATE_CODE_REVIEW')) " + " or #draft.plugable") TopicDraft saveOrUpdateDraft(TopicDraft draft); @Override @PreAuthorize("isAuthenticated()") void deleteDraft();  }### Answer:
@Test public void saveOrUpdateDraftShouldCreateNewDraftIfUserStillHasNoDraft() { when(topicDraftDao.getForUser(currentUser)).thenReturn(null); TopicDraft topicDraft = topicDraftService.saveOrUpdateDraft(createTopicDraft()); verify(topicDraftDao).saveOrUpdate(topicDraft); }
@Test public void saveOrUpdateDraftShouldUpdateDraftIfUserAlreadyHasOne() { TopicDraft topicDraft = createTopicDraft(); when(topicDraftDao.getForUser(currentUser)).thenReturn(topicDraft); topicDraftService.saveOrUpdateDraft(topicDraft); verify(topicDraftDao).saveOrUpdate(topicDraft); }
@Test public void saveOrUpdateDraftShouldNotRewritePollFieldsWithNullValues() { TopicDraft topicDraftWithPoll = createTopicDraft(); topicDraftWithPoll.setPollTitle(RandomStringUtils.random(5)); topicDraftWithPoll.setPollItemsValue(RandomStringUtils.random(5)); when(topicDraftDao.getForUser(currentUser)).thenReturn(topicDraftWithPoll); TopicDraft topicDraftWithoutPoll = createTopicDraft(); topicDraftWithoutPoll.setPollTitle(null); topicDraftWithoutPoll.setPollItemsValue(null); topicDraftService.saveOrUpdateDraft(topicDraftWithoutPoll); assertNotNull(topicDraftWithPoll.getPollTitle()); assertNotNull(topicDraftWithPoll.getPollItemsValue()); }