gt:         ModelAndView mav = controller.showNewTopicPage(BRANCH_ID);
context: /**
   * Copyright (C) 2011  JTalks.org Team
   * This library is free software; you can redistribute it and/or
   * modify it under the terms of the GNU Lesser General Public
   * License as published by the Free Software Foundation; either
   * version 2.1 of the License, or (at your option) any later version.
   * This library is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   * Lesser General Public License for more details.
   * You should have received a copy of the GNU Lesser General Public
   * License along with this library; if not, write to the Free Software
   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
   */
  package org.jtalks.jcommune.web.controller;
  
  import org.jtalks.common.model.entity.Section;
  import org.jtalks.jcommune.model.dto.PageRequest;
  import org.jtalks.jcommune.model.entity.*;
  import org.jtalks.jcommune.service.*;
  import org.jtalks.jcommune.plugin.api.exceptions.NotFoundException;
  import org.jtalks.jcommune.service.nontransactional.LocationService;
  import org.jtalks.jcommune.plugin.api.web.dto.Breadcrumb;
  import org.jtalks.jcommune.plugin.api.web.dto.TopicDto;
  import org.jtalks.jcommune.plugin.api.web.util.BreadcrumbBuilder;
  import org.jtalks.jcommune.service.dto.EntityToDtoConverter;
  import org.jtalks.jcommune.plugin.api.web.dto.json.JsonResponse;
  import org.jtalks.jcommune.plugin.api.web.dto.json.JsonResponseStatus;
  import org.mockito.Matchers;
  import org.mockito.Mock;
  import org.springframework.beans.propertyeditors.StringTrimmerEditor;
  import org.springframework.data.domain.Page;
  import org.springframework.data.domain.PageImpl;
  import org.springframework.retry.policy.NeverRetryPolicy;
  import org.springframework.retry.support.RetryTemplate;
  import org.springframework.security.access.AccessDeniedException;
  import org.springframework.security.core.session.SessionRegistry;
  import org.springframework.validation.BeanPropertyBindingResult;
  import org.springframework.validation.BindingResult;
  import org.springframework.web.bind.WebDataBinder;
  import org.springframework.web.context.request.WebRequest;
  import org.springframework.web.servlet.ModelAndView;
  import org.testng.annotations.BeforeMethod;
  import org.testng.annotations.Test;
  
  import java.util.*;
  
  import static org.mockito.Matchers.any;
  import static org.mockito.Matchers.anyLong;
  import static org.mockito.Matchers.eq;
  import static org.mockito.Mockito.*;
  import static org.mockito.MockitoAnnotations.initMocks;
  import static org.springframework.test.web.ModelAndViewAssert.*;
  import static org.testng.Assert.*;
  
  /**
   * @author Teterin Alexandre
   * @author Kirill Afonin
   * @author Max Malakhov
   * @author Eugeny Batov
   */
  public class TopicControllerTest {
      public long BRANCH_ID = 1L;
      private long TOPIC_ID = 1L;
      private String TOPIC_CONTENT = "content here";
  
      private JCUser user;
      private Branch branch;
  
      @Mock
      private TopicModificationService topicModificationService;
      @Mock
      private TopicFetchService topicFetchService;
      @Mock
      private TopicDraftService topicDraftService;
      @Mock
      private PostService postService;
      @Mock
      private BranchService branchService;
      @Mock
      private UserService userService;
      @Mock
      private BreadcrumbBuilder breadcrumbBuilder;
      @Mock
      private LocationService locationService;
      @Mock
      private SessionRegistry registry;
      @Mock
      private LastReadPostService lastReadPostService;
      @Mock
      private EntityToDtoConverter converter;
  
      private TopicController controller;
      @Mock
      private BindingResult result;
  
      private RetryTemplate retryTemplate;
  
      @BeforeMethod
      public void initEnvironment() {
          initMocks(this);
          retryTemplate = new RetryTemplate();
          retryTemplate.setRetryPolicy(new NeverRetryPolicy());
          controller = new TopicController(
                  topicModificationService,
                  postService,
                  branchService,
                  lastReadPostService,
                  userService,
                  breadcrumbBuilder,
                  locationService,
                  registry,
                  topicFetchService,
                  topicDraftService,
                  converter,
                  retryTemplate);
      }
  
      @BeforeMethod
      public void prepareTestData() {
          branch = new Branch("", "description");
          branch.setSection(new Section("sectionname"));
          branch.setId(BRANCH_ID);
          user = new JCUser("username", "email@mail.com", "password");
      }
  
      @Test
      public void initBinder() {
          WebDataBinder binder = mock(WebDataBinder.class);
          controller.initBinder(binder);
          verify(binder).registerCustomEditor(eq(String.class), any(StringTrimmerEditor.class));
      }
  
      @Test
      public void delete() throws NotFoundException {
          Topic topic = new Topic(null, null);
          branch.addTopic(topic);
          when(topicFetchService.get(anyLong())).thenReturn(topic);
  
          ModelAndView actualMav = controller.deleteTopic(TOPIC_ID);
  
          assertViewName(actualMav, "redirect:/branches/" + BRANCH_ID);
          verify(topicModificationService).deleteTopic(topic);
      }
  
      //TODO: this has to be improved - too complicated test!=
      @Test
      public void showTopicPageShouldShowListOfPostsWithUpdatedInfoAboutLastReadPosts() throws NotFoundException {
          String page = "1";
          Topic topic = createTopic();
          prepareViewTopicMocks(topic, page);
  
          WebRequest request = mock(WebRequest.class);
  
          ModelAndView mav = controller.showTopicPage(request, TOPIC_ID, page);
  
          verify(topicFetchService).checkViewTopicPermission(topic.getBranch().getId());
          verify(lastReadPostService).markTopicPageAsRead(topic, Integer.valueOf(page));
          //
          assertViewName(mav, "topic/postList");
          assertAndReturnModelAttributeOfType(mav, "postsPage", Page.class);
          //
          Topic actualTopic = assertAndReturnModelAttributeOfType(mav, "topic", Topic.class);
          assertEquals(actualTopic, topic);
          assertModelAttributeAvailable(mav, "breadcrumbList");
      }
  
      @Test
      public void showTopicPageShouldReturnNullIfModifiedSinceOlderThenLastUpdate() throws NotFoundException {
          String page = "1";
          Topic topic = createTopic();
          prepareViewTopicMocks(topic, page);
  
          WebRequest request = mock(WebRequest.class);
          doReturn(true).when(request).checkNotModified(anyLong());
  
          ModelAndView mav = controller.showTopicPage(request, TOPIC_ID, page);
  
          assertNull(mav);
      }
  
      @Test
      public void showTopicPageShouldReturnNotNullDataIfIfModifiedSinceOlderThenLastUpdate() throws NotFoundException {
          String page = "1";
          Topic topic = createTopic();
          prepareViewTopicMocks(topic, page);
  
          WebRequest request = mock(WebRequest.class);
          when(request.checkNotModified(topic.getModificationDate().getMillis())).thenReturn(false);
  
          ModelAndView mav = controller.showTopicPage(request, TOPIC_ID, page);
  
          assertNotNull(mav);
      }
  
      @Test
      public void createTopicShouldPassAndRedirectToNewTopicIfItIsValid() throws Exception {
          Branch branch = createBranch();
          Topic topic = createTopic();
          TopicDto dto = getDto();
          BindingResult result = mock(BindingResult.class);
          when(branchService.get(BRANCH_ID)).thenReturn(branch);
          when(topicModificationService.createTopic(topic, TOPIC_CONTENT)).thenReturn(topic);
  
          ModelAndView mav = controller.createTopic(dto, result, BRANCH_ID);
  
          verify(topicModificationService).createTopic(topic, TOPIC_CONTENT);
          //
          assertViewName(mav, "redirect:/topics/1");
      }
  
      @Test
      public void createTopicShouldNotPassAndMustShowTopicErrorIfItIsInvalid() throws NotFoundException {
          BindingResult result = mock(BindingResult.class);
          when(result.hasErrors()).thenReturn(true);
          when(branchService.get(BRANCH_ID)).thenReturn(branch);
          List<Breadcrumb> breadcrumbs = new BreadcrumbBuilder().getNewTopicBreadcrumb(branch);
          when(breadcrumbBuilder.getNewTopicBreadcrumb(branch)).thenReturn(breadcrumbs);
  
          ModelAndView mav = controller.createTopic(getDto(), result, BRANCH_ID);
  
          verify(branchService).get(BRANCH_ID);
          verify(breadcrumbBuilder).getNewTopicBreadcrumb(branch);
  
          List<Breadcrumb> breadcrumbsFromModel = (List) mav.getModel().get("breadcrumbList");
          for (int i = 0; i < breadcrumbs.size(); i++) {
              assertEquals(breadcrumbs.get(i).getValue(), breadcrumbsFromModel.get(i).getValue());
          }
          assertEquals(breadcrumbsFromModel.get(1).getValue(), branch.getSection().getName());
          assertEquals(breadcrumbsFromModel.get(2).getValue(), branch.getName());
          assertViewName(mav, "topic/topicForm");
          long branchId = assertAndReturnModelAttributeOfType(mav, "branchId", Long.class);
          assertEquals(branchId, BRANCH_ID);
      }
  
      @Test
      public void testSaveDraft() throws Exception {
          TopicDraft savedDraft = createTopicDraft();
          when(topicDraftService.saveOrUpdateDraft(savedDraft)).thenReturn(savedDraft);
  
          JsonResponse response = controller.saveDraft(savedDraft, result);
  
          assertEquals(response.getStatus(), JsonResponseStatus.SUCCESS);
          assertEquals((long) response.getResult(), savedDraft.getId());
      }
  
      @Test
      public void saveDraftShouldReturnFailResponseIfValidationErrorsOccurred() throws Exception {
          when(result.hasErrors()).thenReturn(true);
  
          JsonResponse response = controller.saveDraft(createTopicDraft(), result);
  
          assertEquals(response.getStatus(), JsonResponseStatus.FAIL);
      }
  
      @Test
      public void testDeleteDraft() {
          controller.deleteDraft();
  
          verify(topicDraftService).deleteDraft();
      }
  
      @Test
      public void showNewTopicPageShouldReturnTemplateForNewTopic() throws NotFoundException {
          when(branchService.get(BRANCH_ID)).thenReturn(branch);
          when(breadcrumbBuilder.getNewTopicBreadcrumb(branch)).thenReturn(new ArrayList<Breadcrumb>());
  
  