private void store_createMockStoryBusiness() {
    this.storyBusiness =
        new StoryBusinessImpl() {
          @Override
          public void moveStoryAway(Story story, Backlog backlog) {
            storyBacklogUpdated = true;
          }

          //            @Override
          //            public void updateStoryPriority(Story story, int insertAtPriority) {
          //                storyPriorityUpdated = true;
          //            }
        };
    backlogBusiness = createMock(BacklogBusiness.class);
    storyBusiness.setBacklogBusiness(backlogBusiness);

    storyDAO = createMock(StoryDAO.class);
    storyBusiness.setStoryDAO(storyDAO);

    userDAO = createMock(UserDAO.class);
    storyBusiness.setUserDAO(userDAO);

    iterationBusiness = createMock(IterationHistoryEntryBusiness.class);
    storyBusiness.setIterationHistoryEntryBusiness(iterationBusiness);

    blheBusiness = createMock(BacklogHistoryEntryBusiness.class);
    storyBusiness.setBacklogHistoryEntryBusiness(blheBusiness);
  }
  /** TEST FORCE DELETING */
  @Test
  public void testForceDelete() {
    Story story = new Story();
    story.setId(1);

    Story child = new Story();
    child.setParent(story);
    story.setChildren(new ArrayList<Story>(Arrays.asList(child)));

    story.setTasks(new HashSet<Task>(Arrays.asList(new Task(), new Task())));
    story.setHourEntries(
        new HashSet<StoryHourEntry>(
            Arrays.asList(new StoryHourEntry(), new StoryHourEntry(), new StoryHourEntry())));

    taskBusiness.delete(EasyMock.isA(Task.class), EasyMock.same(HourEntryHandlingChoice.DELETE));
    expectLastCall().times(2);

    hourEntryBusiness.deleteAll(story.getHourEntries());

    storyDAO.remove(1);

    replayAll();
    storyBusiness.forceDelete(story);
    verifyAll();

    assertNull("Child story's parent not null", child.getParent());
    assertEquals("Parent story's children not empty", 0, story.getChildren().size());
  }
  @Test
  @DirtiesContext
  public void moveFromIterationToIteration_inProject() {
    firstIteration.setParent(firstProject);
    secondIteration.setParent(firstProject);
    story.setBacklog(firstIteration);

    expect(storyTreeIntegrityBusiness.canStoryBeMovedToBacklog(story, secondIteration))
        .andReturn(true);
    storyDAO.store(story);
    storyRankBusiness.removeRank(story, firstIteration);
    storyRankBusiness.rankToBottom(story, secondIteration);

    backlogHistoryEntryBusiness.updateHistory(firstIteration.getId());
    backlogHistoryEntryBusiness.updateHistory(secondIteration.getId());

    iterationHistoryBusiness.updateIterationHistory(firstIteration.getId());
    iterationHistoryBusiness.updateIterationHistory(secondIteration.getId());

    replayAll();

    storyBusiness.moveStoryToBacklog(story, secondIteration);
    verifyAll();
    assertEquals(secondIteration, story.getBacklog());
  }
  @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(expected = OperationNotPermittedException.class)
  @DirtiesContext
  public void testMoveStoryAndChildren_toIteration() {
    Story parent = new Story();
    Story child1 = new Story();
    Story child2 = new Story();

    parent.getChildren().add(story);

    story.setParent(parent);
    story.getChildren().add(child1);

    child1.setParent(story);
    child1.getChildren().add(child2);

    child2.setParent(child1);

    parent.setBacklog(firstProject);
    story.setBacklog(firstProject);
    child1.setBacklog(firstProject);
    child2.setBacklog(firstIteration);

    firstIteration.setParent(firstProduct);
    expect(backlogBusiness.getParentProduct(firstProject)).andReturn(firstProduct);
    expect(backlogBusiness.getParentProduct(secondIteration)).andReturn(firstProduct);
    replayAll();
    storyBusiness.moveStoryAndChildren(story, secondIteration);
    verifyAll();
  }
  @Test
  @DirtiesContext
  public void moveSingleFromProjectToProjectNoViolation() {
    Story parent = new Story();
    Story child = new Story();
    story.setBacklog(secondProject);
    story.setParent(parent);
    story.getChildren().add(child);
    child.setParent(story);

    expect(backlogBusiness.getParentProduct(secondProject)).andReturn(firstProduct).anyTimes();
    expect(backlogBusiness.getParentProduct(firstProject)).andReturn(firstProduct).anyTimes();

    storyDAO.store(child);
    storyDAO.store(story);
    expect(storyTreeIntegrityBusiness.hasParentStoryConflict(story, firstProject)).andReturn(false);
    storyHierarchyBusiness.updateChildrenTreeRanks(parent);
    backlogHistoryEntryBusiness.updateHistory(secondProject.getId());
    backlogHistoryEntryBusiness.updateHistory(firstProject.getId());

    storyRankBusiness.removeRank(story, secondProject);
    storyRankBusiness.rankToBottom(story, firstProject);
    replayAll();

    storyBusiness.moveSingleStoryToBacklog(story, firstProject);
    verifyAll();
    assertEquals(firstProject, story.getBacklog());
    assertEquals(parent, story.getParent());
    assertEquals(parent, child.getParent());
  }
  @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(expected = ObjectNotFoundException.class)
 public void testStore_noSuchStory() {
   this.store_createMockStoryBusiness();
   expect(storyDAO.get(222)).andReturn(null);
   replayAll();
   storyBusiness.store(222, new Story(), 123, new HashSet<Integer>(), false);
   verifyAll();
 }
 @Test(expected = IllegalArgumentException.class)
 public void testRankStory_underNull() {
   Backlog blog = new Iteration();
   Story story = new Story();
   story.setBacklog(blog);
   replayAll();
   storyBusiness.rankStoryUnder(story, null, null);
   verifyAll();
 }
 @Test
 public void testRankStoryToBottom() {
   Story story = new Story();
   storyRankBusiness.rankToBottom(story, backlog);
   replayAll();
   Story actual = storyBusiness.rankStoryToBottom(story, backlog);
   verifyAll();
   assertSame(actual, story);
 }
 @Test
 public void testUpdateRanks_noChildren_NoRank() {
   Story story = new Story();
   Project backlog = new Project();
   story.setBacklog(backlog);
   storyRankBusiness.rankToBottom(story, backlog);
   replayAll();
   storyBusiness.updateStoryRanks(story);
   verifyAll();
 }
  @Test(expected = OperationNotPermittedException.class)
  @DirtiesContext
  public void moveFromProjectToProduct_integrityViolation() {
    story.setBacklog(firstProject);
    expect(storyTreeIntegrityBusiness.canStoryBeMovedToBacklog(story, firstProduct))
        .andReturn(false);
    replayAll();

    storyBusiness.moveStoryToBacklog(story, firstProduct);
    verifyAll();
  }
 @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 testUpdateRanks_noChildren_HasRank() {
   Story story = new Story();
   Project backlog = new Project();
   story.setBacklog(backlog);
   StoryRank rank = new StoryRank();
   story.getStoryRanks().add(rank);
   replayAll();
   storyBusiness.updateStoryRanks(story);
   verifyAll();
 }
 @Test
 public void testDelete_hourEntryChoice_delete() {
   storyInIteration.getHourEntries().add(new StoryHourEntry());
   hourEntryBusiness.deleteAll(storyInIteration.getHourEntries());
   //        storyRankBusiness.removeStoryRanks(storyInIteration);
   storyDAO.remove(storyInIteration);
   replayAll();
   storyBusiness.delete(storyInIteration, null, HourEntryHandlingChoice.DELETE, null, null);
   verifyAll();
   assertTrue(storyInIteration.getHourEntries().isEmpty());
 }
 @Test
 public void checksThatStorysIterationMatches_rankOver() {
   Iteration ite = new Iteration();
   Story story = new Story();
   story.setIteration(ite);
   Story ref = new Story();
   ref.setIteration(ite);
   storyRankBusiness.rankAbove(story, ite, ref);
   replayAll();
   storyBusiness.rankStoryOver(story, ref, null);
   verifyAll();
 }
 @Test
 public void testRankStoryOver() {
   Backlog blog = new Project();
   Story story = new Story();
   story.setBacklog(blog);
   Story ref = new Story();
   ref.setBacklog(blog);
   storyRankBusiness.rankAbove(story, blog, ref);
   replayAll();
   storyBusiness.rankStoryOver(story, ref, blog);
   verifyAll();
 }
 @Test
 public void testDelete_taskChoice_move() {
   Task task = new Task();
   storyInIteration.getTasks().add(task);
   expect(taskBusiness.move(task, storyInIteration.getBacklog().getId(), null)).andReturn(task);
   //        storyRankBusiness.removeStoryRanks(storyInIteration);
   storyDAO.remove(storyInIteration);
   replayAll();
   storyBusiness.delete(storyInIteration, TaskHandlingChoice.MOVE, null, null, null);
   verifyAll();
   assertTrue(storyInIteration.getTasks().isEmpty());
 }
  @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
  @DirtiesContext
  public void testMoveStoryAndChildren() {
    Story parent = new Story();
    Story child1 = new Story();
    Story child2 = new Story();

    parent.getChildren().add(story);

    story.setParent(parent);
    story.getChildren().add(child1);

    child1.setParent(story);
    child1.getChildren().add(child2);

    child2.setParent(child1);

    parent.setBacklog(firstProject);
    story.setBacklog(firstProject);
    child1.setBacklog(firstProject);
    child2.setBacklog(firstIteration);

    firstIteration.setParent(firstProduct);

    expect(storyTreeIntegrityBusiness.hasParentStoryConflict(story, secondProject)).andReturn(true);
    storyHierarchyBusiness.updateChildrenTreeRanks(parent);

    expect(backlogBusiness.getParentProduct(firstProject)).andReturn(firstProduct).anyTimes();
    expect(backlogBusiness.getParentProduct(secondProject)).andReturn(firstProduct).anyTimes();

    storyDAO.store(child2);

    storyRankBusiness.removeRank(child2, firstIteration);
    storyRankBusiness.removeRank(child2, firstIteration.getParent());

    storyRankBusiness.rankToBottom(child2, secondProject);

    storyDAO.store(child1);
    storyDAO.store(story);

    backlogHistoryEntryBusiness.updateHistory(firstIteration.getId());
    backlogHistoryEntryBusiness.updateHistory(secondProject.getId());
    iterationHistoryBusiness.updateIterationHistory(firstIteration.getId());
    backlogHistoryEntryBusiness.updateHistory(firstProject.getId());
    EasyMock.expectLastCall().times(2);
    backlogHistoryEntryBusiness.updateHistory(secondProject.getId());
    EasyMock.expectLastCall().times(2);

    replayAll();
    storyBusiness.moveStoryAndChildren(story, secondProject);
    verifyAll();
  }
 @Test
 @DirtiesContext
 public void moveFromProductToProduct() {
   Product prod = new Product();
   prod.setId(313);
   story.setBacklog(firstProduct);
   expect(storyTreeIntegrityBusiness.canStoryBeMovedToBacklog(story, prod)).andReturn(true);
   storyDAO.store(story);
   backlogHistoryEntryBusiness.updateHistory(prod.getId());
   backlogHistoryEntryBusiness.updateHistory(firstProduct.getId());
   replayAll();
   storyBusiness.moveStoryToBacklog(story, prod);
   assertEquals(prod, story.getBacklog());
   verifyAll();
 }
 @Test
 public void testDelete_taskChoice_delete() {
   Task task = new Task();
   storyInIteration.getTasks().add(task);
   hourEntryBusiness.moveToBacklog(task.getHourEntries(), storyInIteration.getBacklog());
   taskBusiness.delete(task.getId(), HourEntryHandlingChoice.MOVE);
   //        storyRankBusiness.removeStoryRanks(storyInIteration);
   storyDAO.remove(storyInIteration);
   replayAll();
   storyBusiness.delete(
       storyInIteration, TaskHandlingChoice.DELETE, null, HourEntryHandlingChoice.MOVE, null);
   verifyAll();
   assertNull(task.getStory());
   assertTrue(storyInIteration.getTasks().isEmpty());
 }
  @Test
  @DirtiesContext
  public void moveFromProductToProject() {
    story.setBacklog(firstProduct);

    expect(storyTreeIntegrityBusiness.canStoryBeMovedToBacklog(story, firstProject))
        .andReturn(true);
    storyDAO.store(story);
    storyRankBusiness.rankToBottom(story, firstProject);

    backlogHistoryEntryBusiness.updateHistory(firstProduct.getId());
    backlogHistoryEntryBusiness.updateHistory(firstProject.getId());

    replayAll();
    storyBusiness.moveStoryToBacklog(story, firstProject);
    verifyAll();
    assertEquals(firstProject, story.getBacklog());
  }
  @Test(expected = IllegalArgumentException.class)
  public void testRankStoryUnder_invalidbacklogs() {
    Project proj1 = new Project();
    Iteration iter = new Iteration();
    iter.setParent(proj1);

    Project proj2 = new Project();
    Iteration iter2 = new Iteration();
    iter2.setParent(proj2);

    Story story1 = new Story();
    story1.setBacklog(proj1);
    Story story2 = new Story();
    story2.setBacklog(proj2);

    replayAll();
    storyBusiness.rankStoryUnder(story1, story2, proj1);
    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
  @DirtiesContext
  public void moveFromProjectToProject_hasChildren() {
    story.setBacklog(secondProject);
    Story child = new Story();
    story.getChildren().add(child);

    expect(backlogBusiness.getParentProduct(secondProject)).andReturn(firstProduct);
    expect(backlogBusiness.getParentProduct(firstProject)).andReturn(firstProduct);

    expect(storyTreeIntegrityBusiness.canStoryBeMovedToBacklog(story, firstProject))
        .andReturn(true);
    storyDAO.store(story);
    backlogHistoryEntryBusiness.updateHistory(secondProject.getId());
    backlogHistoryEntryBusiness.updateHistory(firstProject.getId());
    replayAll();

    storyBusiness.moveStoryToBacklog(story, firstProject);
    verifyAll();
    assertEquals(firstProject, story.getBacklog());
  }
  @Test
  public void testDelete_onlyChildRemoved() {
    Story parent = new Story();
    Story child = new Story();
    Backlog backlog = new Project();
    backlog.setId(1);

    parent.getChildren().add(child);
    child.setParent(parent);
    child.setBacklog(backlog);
    parent.setBacklog(backlog);

    storyRankBusiness.rankToBottom(parent, backlog);
    //        storyRankBusiness.removeStoryRanks(child);
    storyHierarchyBusiness.updateChildrenTreeRanks(parent);
    storyDAO.remove(child);
    replayAll();
    storyBusiness.delete(child, null, null, null, null);
    verifyAll();
    assertTrue(parent.getChildren().isEmpty());
  }
  @Test(expected = OperationNotPermittedException.class)
  @DirtiesContext
  public void moveWithChildrenToAnotherProduct() {
    Product oldParent = new Product();
    Backlog oldBacklog = new Project();
    oldBacklog.setId(8482);
    oldBacklog.setParent(oldParent);

    Product newBacklog = new Product();
    newBacklog.setId(1904);

    Story movable = new Story();
    movable.setBacklog(oldBacklog);

    movable.setChildren(Arrays.asList(new Story(), new Story()));

    expect(backlogBusiness.getParentProduct(oldBacklog)).andReturn(oldParent);
    expect(backlogBusiness.getParentProduct(newBacklog)).andReturn(newBacklog);
    replayAll();
    storyBusiness.moveStoryToBacklog(movable, newBacklog);
    verifyAll();
  }
  @Test
  public void testDelete_deleteChoices_withChildren() {
    Story child = new Story();
    Story storyParent = new Story();
    storyParent.setBacklog(new Product());
    child.setParent(storyInIteration);
    storyInIteration.setParent(storyParent);
    storyInIteration.getChildren().add(child);

    storyHierarchyBusiness.updateChildrenTreeRanks(storyParent);
    hourEntryBusiness.deleteAll(storyInIteration.getHourEntries());
    //        storyRankBusiness.removeStoryRanks(storyInIteration);
    storyDAO.remove(storyInIteration);
    replayAll();
    storyBusiness.delete(
        storyInIteration,
        TaskHandlingChoice.DELETE,
        HourEntryHandlingChoice.DELETE,
        HourEntryHandlingChoice.DELETE,
        ChildHandlingChoice.MOVE);
    assertEquals(storyParent, child.getParent());
    assertTrue(storyInIteration.getChildren().isEmpty());
    verifyAll();
  }
  @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());
    }
  }