@Test
  @DirtiesContext
  public void testHasParentStoryConflict_differentProduct() {
    Product product = new Product();
    Product targetProduct = new Product();
    Project project = new Project();
    Project targetProject = new Project();

    Story parentStory = new Story();
    Story story = new Story();

    targetProject.setParent(targetProduct);
    project.setParent(product);

    parentStory.setBacklog(project);
    story.setBacklog(project);
    story.setParent(parentStory);

    expect(this.backlogBusiness.getParentProduct(targetProject)).andReturn(targetProduct);
    expect(this.backlogBusiness.getParentProduct(project)).andReturn(product);

    replayAll();
    assertTrue(this.testable.hasParentStoryConflict(story, targetProject));
    verifyAll();
  }
  @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
 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
  @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
  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
  @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
  @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());
  }
 private void setStoryBacklogs() {
   story_11.setBacklog(product);
   story_12.setBacklog(product);
   story_21.setBacklog(product);
   story_31.setBacklog(project1);
   story_41.setBacklog(iteration);
   story_22.setBacklog(project2);
   story_33.setBacklog(project2);
   story_23.setBacklog(product);
 }
 @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 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 = 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 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(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 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());
  }
  @Before
  public void setUpStorysProjectResponsiblesData() {
    Iteration iter = new Iteration();
    Project proj = new Project();
    Product prod = new Product();
    iter.setParent(proj);
    proj.setParent(prod);

    assignedUser = new User();
    assignedUser.setId(2233);

    storyInIteration = new Story();
    storyInProject = new Story();
    storyInProduct = new Story();

    storyInIteration.setId(868);
    storyInProduct.setId(951);
    storyInProject.setId(3);

    storyInIteration.setIteration(iter);
    storyInIteration.setBacklog(proj);
    storyInProject.setBacklog(proj);
    storyInProduct.setBacklog(prod);
  }
  @Test
  @DirtiesContext
  public void testHasParentStoryConflict_toIteration() {
    Product product = new Product();
    Project project = new Project();
    Iteration iteration = new Iteration();

    Story parentStory = new Story();
    Story story = new Story();

    iteration.setParent(project);
    project.setParent(product);

    parentStory.setBacklog(project);
    story.setBacklog(project);
    story.setParent(parentStory);

    expect(this.backlogBusiness.getParentProduct(iteration)).andReturn(product);
    expect(this.backlogBusiness.getParentProduct(project)).andReturn(product);

    replayAll();
    assertFalse(this.testable.hasParentStoryConflict(story, iteration));
    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
  @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
  @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(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();
  }