@Test
  public void testStore_updateBacklogAndClearResponsibles() {
    this.store_createMockStoryBusiness();
    Backlog newBacklog = new Project();
    newBacklog.setId(123);
    Set<User> users = new HashSet<User>(Arrays.asList(new User(), new User()));
    storyInIteration.setResponsibles(users);

    expect(storyDAO.get(storyInIteration.getId())).andReturn(storyInIteration);
    expect(backlogBusiness.retrieve(newBacklog.getId())).andReturn(newBacklog);

    storyDAO.store(EasyMock.isA(Story.class));

    blheBusiness.updateHistory(storyInIteration.getBacklog().getId());

    replayAll();
    Story actual =
        storyBusiness.store(
            storyInIteration.getId(),
            new Story(),
            newBacklog.getId(),
            new HashSet<Integer>(),
            false);
    verifyAll();

    assertEquals(0, actual.getResponsibles().size());

    assertTrue(storyBacklogUpdated);
  }
 @Test
 public void testRetrieveStoryTO() {
   StoryTO storyTo = new StoryTO(story1);
   expect(storyDAO.get(story1.getId())).andReturn(story1);
   expect(transferObjectBusiness.constructStoryTO(story1)).andReturn(storyTo);
   expect(storyDAO.calculateMetrics(story1.getId())).andReturn(new StoryMetrics());
   expect(hourEntryDAO.calculateSumByStory(story1.getId())).andReturn(100l);
   replayAll();
   assertEquals(storyTo, storyBusiness.retrieveStoryWithMetrics(story1.getId()));
   verifyAll();
 }
  @Test
  public void testDeleteAndUpdateHistory() {
    expect(storyDAO.get(storyInIteration.getId())).andReturn(storyInIteration);
    //        storyRankBusiness.removeStoryRanks(storyInIteration);
    storyDAO.remove(storyInIteration);
    blheBusiness.updateHistory(storyInIteration.getBacklog().getId());
    iheBusiness.updateIterationHistory(storyInIteration.getIteration().getId());
    replayAll();

    storyBusiness.deleteAndUpdateHistory(storyInIteration.getId(), null, null, null, null);
    verifyAll();
  }
  @Test
  public void testDelete_deleteChoices_withChildren_deleteChildren() {
    Story child = new Story();
    child.setBacklog(storyInIteration.getBacklog());
    child.setIteration(iteration);
    child.setId(2333);
    child.setParent(storyInIteration);
    storyInIteration.getChildren().add(child);
    storyInIteration.setIteration(null);

    blheBusiness.updateHistory(child.getBacklog().getId());
    iheBusiness.updateIterationHistory(iteration.getId());

    hourEntryBusiness.deleteAll(child.getHourEntries());
    hourEntryBusiness.deleteAll(storyInIteration.getHourEntries());

    //        storyRankBusiness.removeStoryRanks(child);
    //        storyRankBusiness.removeStoryRanks(storyInIteration);

    //        expect(storyDAO.get(2333)).andReturn(child);

    storyDAO.remove(child.getId());
    storyDAO.remove(storyInIteration);

    replayAll();
    storyBusiness.delete(
        storyInIteration,
        TaskHandlingChoice.DELETE,
        HourEntryHandlingChoice.DELETE,
        HourEntryHandlingChoice.DELETE,
        ChildHandlingChoice.DELETE);
    //        assertNull(child.getParent());
    assertTrue(storyInIteration.getChildren().isEmpty());
    verifyAll();
  }
  @Test
  public void testStore_updateResponsibles() {
    this.store_createMockStoryBusiness();

    Backlog backlog = storyInIteration.getBacklog();
    User user1 = new User();
    User user2 = new User();
    Set<User> users = new HashSet<User>(Arrays.asList(user1, user2));

    expect(storyDAO.get(storyInIteration.getId())).andReturn(storyInIteration);
    expect(userDAO.get(123)).andReturn(user1);
    expect(userDAO.get(222)).andReturn(user2);
    storyDAO.store(EasyMock.isA(Story.class));

    Story dataItem = new Story();
    dataItem.setName("Foo item");
    dataItem.setDescription("Fubar");
    dataItem.setStoryPoints(333);
    dataItem.setState(StoryState.PENDING);

    blheBusiness.updateHistory(storyInIteration.getBacklog().getId());

    replayAll();
    Story actual =
        storyBusiness.store(
            storyInIteration.getId(),
            dataItem,
            null,
            new HashSet<Integer>(Arrays.asList(123, 222)),
            false);
    verifyAll();

    assertSame("The backlogs don't match", backlog, actual.getBacklog());
    assertEquals("The responsibles don't match", users, actual.getResponsibles());

    assertEquals(dataItem.getName(), actual.getName());
    assertEquals(dataItem.getDescription(), actual.getDescription());
    assertEquals(dataItem.getStoryPoints(), actual.getStoryPoints());
    assertEquals(dataItem.getState(), actual.getState());

    assertFalse(storyBacklogUpdated);
  }
  @Test
  public void testStore_dontSetTasksToDone() {
    this.store_createMockStoryBusiness();
    Task task1 = new Task();
    task1.setId(11);
    task1.setState(TaskState.BLOCKED);

    Task task2 = new Task();
    task2.setId(12);
    task2.setState(TaskState.BLOCKED);

    story1.setIteration(iteration);
    story1.setTasks(new HashSet<Task>(Arrays.asList(task1, task2)));

    expect(storyDAO.get(story1.getId())).andReturn(story1);
    storyDAO.store(story1);
    replayAll();
    Story actual = storyBusiness.store(story1.getId(), story1, null, null, false);
    verifyAll();

    for (Task t : actual.getTasks()) {
      assertEquals(TaskState.BLOCKED, t.getState());
    }
  }
  @Test
  public void testStore_tasksToDone() {
    Task task1 = new Task();
    task1.setId(11);
    task1.setState(TaskState.BLOCKED);

    Task task2 = new Task();
    task2.setId(12);
    task2.setState(TaskState.PENDING);

    story1.setIteration(iteration);
    story1.setTasks(new HashSet<Task>(Arrays.asList(task1, task2)));

    expect(storyDAO.get(story1.getId())).andReturn(story1);
    storyDAO.store(story1);

    taskBusiness.setTaskToDone(task1);
    taskBusiness.setTaskToDone(task2);
    iheBusiness.updateIterationHistory(story1.getIteration().getId());

    replayAll();
    storyBusiness.store(story1.getId(), story1, null, null, true);
    verifyAll();
  }
  @SuppressWarnings("unchecked")
  @Transactional(readOnly = true)
  public List<StoryAccessCloudTO> calculateEditOccurences(
      DateTime start, DateTime end, User user, int numberOfItems) {
    Map<Integer, Long> data = this.storyHistoryDAO.calculateAccessCounts(start, end, user);

    Collection<Story> stories = this.storyBusiness.retrieveMultiple(data.keySet());

    List<StoryAccessCloudTO> res = new ArrayList<StoryAccessCloudTO>();
    for (Story story : stories) {
      res.add(new StoryAccessCloudTO(story, data.get(story.getId())));
    }
    Collections.sort(res, new PropertyComparator("count", true, false));
    if (res.size() > numberOfItems) {
      return res.subList(0, numberOfItems - 1);
    }
    return res;
  }