@Test
 public void testApproveSubscription() throws Exception {
   expectInitialListCreation();
   Capture<RosterListener> listenerCapture = new Capture<RosterListener>();
   roster.addRosterListener(capture(listenerCapture));
   expectLastCall();
   subscriptionRequestListener.onSubscriptionApproved(TEST_CONTACT);
   expectLastCall().times(2);
   smackCon.sendPacket(anyObject(Packet.class));
   expectLastCall().times(2);
   expect(roster.getGroups()).andReturn(new ArrayList<RosterGroup>());
   expect(roster.getUnfiledEntryCount()).andStubReturn(0);
   roster.createEntry(
       eq(TEST_CONTACT), eq(TEST_CONTACT_NAME), aryEq(new String[] {DEFAULT_GROUP_NAME}));
   expectLastCall().times(2);
   listener.onContactChange(
       eq(ContactListListener.LIST_CONTACT_ADDED),
       anyObject(ContactList.class),
       anyObject(Contact.class));
   expectLastCall();
   replayAll();
   contactListManager.listenToRoster(roster);
   contactListManager.loadContactLists();
   contactListManager.approveSubscriptionRequest(TEST_CONTACT);
   // Second time should not call notifyContactListUpdated, since contact
   // already exists
   contactListManager.approveSubscriptionRequest(TEST_CONTACT);
   assertEquals(1, contactListManager.getContactLists().size());
   assertNotNull(contactListManager.getContactList(DEFAULT_GROUP_NAME));
   assertTrue(con.joinGracefully());
   verifyAll();
 }
 // Approve a subscription while the server already has a Buddies group
 @Test
 public void testApproveSubscription_serverBuddies() throws Exception {
   expectInitialListCreation();
   Capture<RosterListener> listenerCapture = new Capture<RosterListener>();
   roster.addRosterListener(capture(listenerCapture));
   expectLastCall();
   subscriptionRequestListener.onSubscriptionApproved(TEST_CONTACT);
   expectLastCall();
   smackCon.sendPacket(anyObject(Packet.class));
   expectLastCall();
   final ArrayList<RosterGroup> groups = new ArrayList<RosterGroup>();
   RosterGroup buddiesGroup = createNiceMock(RosterGroup.class);
   expect(buddiesGroup.getName()).andStubReturn(DEFAULT_GROUP_NAME);
   expect(buddiesGroup.getEntries()).andStubReturn(new ArrayList<RosterEntry>());
   groups.add(buddiesGroup);
   expect(roster.getGroups()).andReturn(groups);
   expect(roster.getUnfiledEntryCount()).andStubReturn(0);
   roster.createEntry(
       eq(TEST_CONTACT), eq(TEST_CONTACT_NAME), aryEq(new String[] {DEFAULT_GROUP_NAME}));
   expectLastCall();
   listener.onContactChange(
       eq(ContactListListener.LIST_CONTACT_ADDED),
       anyObject(ContactList.class),
       anyObject(Contact.class));
   expectLastCall();
   replayAll();
   contactListManager.listenToRoster(roster);
   contactListManager.loadContactLists();
   contactListManager.approveSubscriptionRequest(TEST_CONTACT);
   assertEquals(1, contactListManager.getContactLists().size());
   assertNotNull(contactListManager.getContactList(DEFAULT_GROUP_NAME));
   assertTrue(con.joinGracefully());
   verifyAll();
 }
  // Call when not tracking state
  @Test
  public void testRunNotTracking() {
    sLogger.setWriter(new StringWriter());
    sLogger.start();
    assertTrue(sLogger.isRunning());
    ArrayList<StateGroup> grouplist = new ArrayList<>();
    grouplist.add(group);

    resetAll();
    expect(mockTracker.isTrackingState()).andReturn(false).anyTimes();
    expect(mockTracker.getIoSet()).andReturn(ioSet).anyTimes();
    expect(mockTracker.getStateList()).andReturn(grouplist).anyTimes();
    replayAll();

    Thread t =
        new Thread(
            new Runnable() {

              @Override
              public void run() {
                try {
                  Thread.sleep(2500);
                } catch (InterruptedException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
                }

                sLogger.stop();
              }
            });
    t.start();
    sLogger.run();
  }
  @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
 @DirtiesContext
 public void testChangeBacklog_moveToAnotherProduct_noConflict() {
   replayAll();
   messages = testable.checkChangeBacklog(story_12, new Product());
   verifyAll();
   assertNumberOfMessages(messages, 0);
 }
  @Test
  public void testIsAuthenticatedNotAuthenticated() {
    expect(mockHttpSession.getAttribute(eq("authenticated"))).andReturn("false");
    expect(mockHttpServletRequest.getSession(eq(false))).andReturn(mockHttpSession);

    replayAll();

    assertFalse(sampleServlet.isAuthenticated(mockHttpServletRequest));
  }
  @Test
  public void testOpslaan() throws Exception {
    PedicureSalon pedicureSalon = new PedicureSalon();

    bedrijfClient.opslaanPedicureSalon(pedicureSalon);
    expectLastCall();

    replayAll();

    bedrijfService.opslaan(pedicureSalon);

    verifyAll();
  }
  @Test
  @DirtiesContext
  public void testChangeBacklog_moveToAnotherProduct_withConflict() {
    Product another = new Product();

    expect(backlogBusiness.getParentProduct(story_23.getBacklog())).andReturn(product);
    expect(backlogBusiness.getParentProduct(another)).andReturn(another);

    replayAll();
    messages = testable.checkChangeBacklog(story_23, another);
    verifyAll();
    assertNumberOfMessages(messages, 1);
  }
  @Test
  public void testZoekOpApiCode() throws Exception {
    String apiCode = "apiCodeee";
    PedicureSalon pedicureSalon = new PedicureSalon();

    expect(bedrijfClient.zoekOpApiCode(apiCode)).andReturn(pedicureSalon);

    replayAll();

    assertEquals(pedicureSalon, bedrijfService.zoekOpApiCode(apiCode));

    verifyAll();
  }
  @Test
  @DirtiesContext
  public void testChangeBacklog_moveToDifferentBranch() {
    expect(backlogBusiness.getParentProduct(story_33.getBacklog())).andReturn(product);
    expect(backlogBusiness.getParentProduct(project1)).andReturn(product);

    replayAll();
    messages = testable.checkChangeBacklog(story_33, project1);
    verifyAll();

    assertNumberOfMessages(messages, 1);
    assertMessagesContain(
        messages, StoryHierarchyIntegrityViolationType.PARENT_IN_WRONG_BRANCH, story_33, story_22);
  }
  @Test
  @DirtiesContext
  public void testChangeParent_moveToDifferentBranch() {
    // 41 -> 22

    replayAll();
    messages = testable.checkChangeParentStory(story_41, story_22);
    verifyAll();

    assertNumberOfMessages(messages, 1);
    assertMessagesContain(
        messages,
        StoryHierarchyIntegrityViolationType.TARGET_PARENT_IN_WRONG_BRANCH,
        story_41,
        story_22);
  }
  @Test
  @DirtiesContext
  public void testChangeParent_moveUnderIterationStory() {
    // 32 -> 41

    replayAll();
    messages = testable.checkChangeParentStory(story_32, story_41);
    verifyAll();

    assertNumberOfMessages(messages, 1);
    assertMessagesContain(
        messages,
        StoryHierarchyIntegrityViolationType.TARGET_PARENT_IN_ITERATION,
        story_32,
        story_41);
  }
  @Test
  @DirtiesContext
  public void testChangeBacklog_moveToProduct_parentInProject() {
    expect(backlogBusiness.getParentProduct(story_41.getBacklog())).andReturn(product);
    expect(backlogBusiness.getParentProduct(product)).andReturn(product);

    replayAll();
    messages = testable.checkChangeBacklog(story_41, product);
    verifyAll();

    assertNumberOfMessages(messages, 1);
    assertMessagesContain(
        messages,
        StoryHierarchyIntegrityViolationType.PARENT_DEEPER_IN_HIERARCHY,
        story_41,
        story_31);
  }
  @Test
  @DirtiesContext
  public void testChangeBacklog_hasChildren_movingToIteration() {
    expect(backlogBusiness.getParentProduct(story_31.getBacklog())).andReturn(product);
    expect(backlogBusiness.getParentProduct(iteration)).andReturn(product);

    replayAll();
    messages = testable.checkChangeBacklog(story_31, iteration);
    verifyAll();

    assertNumberOfMessages(messages, 1);
    assertMessagesContain(
        messages,
        StoryHierarchyIntegrityViolationType.MOVE_TO_ITERATION_HAS_CHILDREN,
        story_31,
        null);
  }
  @Test
  @DirtiesContext
  public void testChangeParent_moveProductStoryUnderProjectStory() {
    // 23 -> 22
    // 34 vaihtaa branchia

    replayAll();
    messages = testable.checkChangeParentStory(story_23, story_22);
    verifyAll();

    assertNumberOfMessages(messages, 1);
    assertMessagesContain(
        messages,
        StoryHierarchyIntegrityViolationType.TARGET_PARENT_DEEPER_IN_HIERARCHY,
        story_23,
        story_22);
  }
  @Test
  @DirtiesContext
  public void testChangeBacklog_hasChildren_notUnderTargetBacklog() {
    expect(backlogBusiness.getParentProduct(story_21.getBacklog())).andReturn(product);
    expect(backlogBusiness.getParentProduct(project2)).andReturn(product);

    replayAll();
    messages = testable.checkChangeBacklog(story_21, project2);
    verifyAll();

    assertNumberOfMessages(messages, 3);
    assertMessagesContain(
        messages, StoryHierarchyIntegrityViolationType.CHILD_IN_WRONG_BRANCH, story_21, story_31);
    assertMessagesContain(
        messages, StoryHierarchyIntegrityViolationType.CHILD_IN_WRONG_BRANCH, story_31, story_41);
    assertMessagesContain(
        messages, StoryHierarchyIntegrityViolationType.CHILD_IN_WRONG_BRANCH, story_21, story_32);
  }
  @Test
  @DirtiesContext
  public void testChangeParent_moveBranchUnderAnother() {
    // 22 -> 31
    // 2 virhettä, 22->31, 33 -> 31

    replayAll();
    messages = testable.checkChangeParentStory(story_22, story_31);
    verifyAll();

    assertNumberOfMessages(messages, 2);
    assertMessagesContain(
        messages,
        StoryHierarchyIntegrityViolationType.TARGET_PARENT_IN_WRONG_BRANCH,
        story_22,
        story_31);
    assertMessagesContain(
        messages,
        StoryHierarchyIntegrityViolationType.TARGET_PARENT_IN_WRONG_BRANCH,
        story_22,
        story_33);
  }
  @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();
  }