private SectionContentEntity createSectionContent(ContentKey contentKey, String contentName) {

    ContentEntity content = new ContentEntity();
    content.setKey(contentKey);
    content.setName(contentName);

    SectionContentEntity sectionContent = new SectionContentEntity();
    sectionContent.setKey(new SectionContentKey(contentKey.toString()));
    sectionContent.setContent(content);

    return sectionContent;
  }
 private void VerifyFinalResultOfApproveUnapproveTest(int proKey, int againstKey) {
   MenuItemEntity testSection;
   Set<SectionContentEntity> storedSectionContents;
   Iterator<SectionContentEntity> secCntIterator;
   boolean proContentChecked = false;
   boolean againstContentChecked = false;
   testSection = fixture.findMenuItemByName("Opinion", 40);
   storedSectionContents = testSection.getSectionContents();
   assertEquals(2, storedSectionContents.size());
   secCntIterator = storedSectionContents.iterator();
   while (secCntIterator.hasNext()) {
     SectionContentEntity secCntLoopHolder = secCntIterator.next();
     int loopHolderKey = secCntLoopHolder.getContent().getKey().toInt();
     if (loopHolderKey == againstKey) {
       assertEquals(true, secCntLoopHolder.isApproved());
       againstContentChecked = true;
     } else if (loopHolderKey == proKey) {
       assertEquals(false, secCntLoopHolder.isApproved());
       proContentChecked = true;
     }
   }
   assertTrue(proContentChecked && againstContentChecked);
 }
 private void verifyFinalReorderingResult(
     int chessContentKey, int footballContentKey, int golfContentKey, int icehockeyContentKey) {
   MenuItemEntity testSection;
   Set<SectionContentEntity> storedSectionContents;
   Iterator<SectionContentEntity> secCntIterator;
   testSection = fixture.findMenuItemByName("Sports", 30);
   storedSectionContents = testSection.getSectionContents();
   assertEquals(4, storedSectionContents.size());
   secCntIterator = storedSectionContents.iterator();
   boolean chessContentChecked = false;
   boolean footballContentChecked = false;
   boolean golfContentChecked = false;
   boolean icehockeyContentChecked = false;
   while (secCntIterator.hasNext()) {
     SectionContentEntity secCntLoopHolder = secCntIterator.next();
     int loopHolderKey = secCntLoopHolder.getContent().getKey().toInt();
     if (loopHolderKey == chessContentKey) {
       assertEquals(2, secCntLoopHolder.getOrder());
       chessContentChecked = true;
     } else if (loopHolderKey == footballContentKey) {
       assertEquals(1, secCntLoopHolder.getOrder());
       footballContentChecked = true;
     } else if (loopHolderKey == golfContentKey) {
       assertEquals(4, secCntLoopHolder.getOrder());
       golfContentChecked = true;
     } else if (loopHolderKey == icehockeyContentKey) {
       assertEquals(3, secCntLoopHolder.getOrder());
       icehockeyContentChecked = true;
     }
   }
   assertTrue(
       chessContentChecked
           && footballContentChecked
           && golfContentChecked
           && icehockeyContentChecked);
 }
  @Test
  public void testApprovingAndUnapprovingContentInSection() {
    // Data for test:
    MenuItemEntity menuItemPolitics =
        factory.createSectionMenuItem(
            "Opinion",
            40,
            "Unordered",
            "Opinion",
            "The Newspaper",
            "aru",
            "aru",
            "en",
            "Hello World!",
            10,
            false,
            null,
            false,
            null);
    fixture.save(menuItemPolitics);

    MenuItemAccessEntity allRightsUserAccess =
        factory.createMenuItemAccess(
            "Opinion",
            40,
            fixture.findGroupByName("aru_group"),
            "read, create, update, delete, add, publish");
    MenuItemAccessEntity readOnlyUserAccess =
        factory.createMenuItemAccess("Opinion", 40, fixture.findGroupByName("nru_group"), "read");

    MenuItemAccessEntity publishOnlyUserAccess =
        factory.createMenuItemAccess(
            "Opinion", 40, fixture.findGroupByName("publish_group"), "read, publish");

    fixture.save(allRightsUserAccess, readOnlyUserAccess, publishOnlyUserAccess);

    ContentEntity contentOpinionPro =
        factory.createContent("Articles", "en", "aru", "0", new Date());
    ContentEntity contentOpinionAgainst =
        factory.createContent("Articles", "en", "aru", "0", new Date());
    fixture.save(contentOpinionPro, contentOpinionAgainst);
    ContentKey proKey = contentOpinionPro.getKey();
    ContentKey againstKey = contentOpinionAgainst.getKey();

    SectionContentEntity sectionContentPro =
        factory.createContentSection("Opinion", 40, proKey, true, 1);
    SectionContentEntity sectionContentAgainst =
        factory.createContentSection("Opinion", 40, againstKey, true, 2);
    fixture.save(sectionContentPro, sectionContentAgainst);
    fixture.flushAndClearHibernateSesssion();

    // Assert that all data is set up correctly
    MenuItemEntity section = fixture.findMenuItemByName("Opinion", 40);
    assertEquals(2, section.getSectionContents().size());

    final UserEntity publishOnlyUser = fixture.findUserByName("publish");

    // Unapprove content
    UnapproveSectionContentCommand unapproveCommand = new UnapproveSectionContentCommand();
    unapproveCommand.setUpdater(publishOnlyUser.getKey());
    unapproveCommand.setSection(fixture.findMenuItemByName("Opinion", 40).getMenuItemKey());
    unapproveCommand.addUnapprovedContentToUpdate(contentOpinionAgainst.getKey());
    menuItemService.unapproveSectionContent(unapproveCommand);
    fixture.flushAndClearHibernateSesssion();

    // Assert that all data is updated correctly
    MenuItemEntity testSection = fixture.findMenuItemByName("Opinion", 40);
    Set<SectionContentEntity> storedSectionContents = testSection.getSectionContents();
    assertEquals(2, storedSectionContents.size());
    Iterator<SectionContentEntity> secCntIterator = storedSectionContents.iterator();
    boolean proContentChecked = false, againstContentChecked = false;
    while (secCntIterator.hasNext()) {
      SectionContentEntity secCntLoopHolder = secCntIterator.next();
      int loopHolderKey = secCntLoopHolder.getContent().getKey().toInt();
      if (loopHolderKey == contentOpinionAgainst.getKey().toInt()) {
        assertEquals(false, secCntLoopHolder.isApproved());
        againstContentChecked = true;
      } else if (loopHolderKey == contentOpinionPro.getKey().toInt()) {
        assertEquals(true, secCntLoopHolder.isApproved());
        proContentChecked = true;
      }
    }
    assertTrue(proContentChecked && againstContentChecked);

    // Switch approved and unapproved content:
    unapproveCommand = new UnapproveSectionContentCommand();
    unapproveCommand.setUpdater(publishOnlyUser.getKey());
    unapproveCommand.setSection(fixture.findMenuItemByName("Opinion", 40).getMenuItemKey());
    unapproveCommand.addUnapprovedContentToUpdate(contentOpinionPro.getKey());
    menuItemService.unapproveSectionContent(unapproveCommand);

    ApproveSectionContentCommand approveCommand = new ApproveSectionContentCommand();
    approveCommand.setUpdater(publishOnlyUser.getKey());
    approveCommand.setSection(fixture.findMenuItemByName("Opinion", 40).getMenuItemKey());
    approveCommand.addApprovedContentToUpdate(contentOpinionAgainst.getKey());
    menuItemService.approveSectionContent(approveCommand);

    fixture.flushAndClearHibernateSesssion();

    // Assert that all data is updated correctly
    VerifyFinalResultOfApproveUnapproveTest(
        contentOpinionPro.getKey().toInt(), contentOpinionAgainst.getKey().toInt());

    // Try switching the approved unapproved setting, with a user that does not have the necessary
    // rights to do so:
    unapproveCommand = new UnapproveSectionContentCommand();
    unapproveCommand.setUpdater(fixture.findUserByName("nru").getKey());
    unapproveCommand.setSection(fixture.findMenuItemByName("Opinion", 40).getMenuItemKey());
    unapproveCommand.addUnapprovedContentToUpdate(contentOpinionAgainst.getKey());
    try {
      menuItemService.unapproveSectionContent(unapproveCommand);
      fail("Not in the catch block.  Unapproving content should not be allowed for this user.");
    } catch (MenuItemAccessException e) {
      // All OK
    }

    approveCommand = new ApproveSectionContentCommand();
    approveCommand.setUpdater(fixture.findUserByName("nru").getKey());
    approveCommand.setSection(fixture.findMenuItemByName("Opinion", 40).getMenuItemKey());
    approveCommand.addApprovedContentToUpdate(contentOpinionPro.getKey());
    try {
      menuItemService.approveSectionContent(approveCommand);
      fail("Not in the catch block.  Approving content should not be allowed for this user.");
    } catch (MenuItemAccessException e) {
      // All OK
    }

    // That nothing has changed
    VerifyFinalResultOfApproveUnapproveTest(
        contentOpinionPro.getKey().toInt(), contentOpinionAgainst.getKey().toInt());
  }
  @Test
  public void testReorderingContentInASection() {
    MenuItemEntity menuItemArchive =
        factory.createSectionMenuItem(
            "Sports",
            30,
            "Ordered",
            "Sports",
            "The Newspaper",
            "aru",
            "aru",
            "en",
            "Hello World!",
            10,
            true,
            new Date(),
            false,
            null);
    fixture.save(menuItemArchive);

    MenuItemAccessEntity aruMenuAR =
        factory.createMenuItemAccess(
            "Sports",
            30,
            fixture.findGroupByName("aru_group"),
            "read, create, update, delete, add, publish");
    MenuItemAccessEntity nruMenuAR =
        factory.createMenuItemAccess("Sports", 30, fixture.findGroupByName("nru_group"), "read");
    fixture.save(aruMenuAR, nruMenuAR);

    ContentEntity sportsContentChess =
        factory.createContent("Articles", "en", "aru", "0", new Date());
    ContentEntity sportsContentFootball =
        factory.createContent("Articles", "en", "aru", "0", new Date());
    ContentEntity sportsContentGolf =
        factory.createContent("Articles", "en", "aru", "0", new Date());
    ContentEntity sportsContentIcehockey =
        factory.createContent("Articles", "en", "aru", "0", new Date());
    fixture.save(
        sportsContentChess, sportsContentFootball, sportsContentGolf, sportsContentIcehockey);

    int chessContentKey = sportsContentChess.getKey().toInt();
    int footballContentKey = sportsContentFootball.getKey().toInt();
    int golfContentKey = sportsContentGolf.getKey().toInt();
    int icehockeyContentKey = sportsContentIcehockey.getKey().toInt();

    SectionContentEntity secCntChess =
        factory.createContentSection("Sports", 30, sportsContentChess.getKey(), true, 1);
    SectionContentEntity secCntFootball =
        factory.createContentSection("Sports", 30, sportsContentFootball.getKey(), true, 2);
    SectionContentEntity secCntGolf =
        factory.createContentSection("Sports", 30, sportsContentGolf.getKey(), true, 3);
    SectionContentEntity secCntIcehockey =
        factory.createContentSection("Sports", 30, sportsContentIcehockey.getKey(), true, 4);
    fixture.save(secCntChess, secCntFootball, secCntGolf, secCntIcehockey);
    fixture.flushAndClearHibernateSesssion();

    // Assert that all data is set up correctly
    MenuItemEntity testSection = fixture.findMenuItemByName("Sports", 30);
    Set<SectionContentEntity> storedSectionContents = testSection.getSectionContents();
    assertEquals(4, storedSectionContents.size());
    Iterator<SectionContentEntity> secCntIterator = storedSectionContents.iterator();
    boolean chessContentChecked = false,
        footballContentChecked = false,
        golfContentChecked = false,
        icehockeyContentChecked = false;
    while (secCntIterator.hasNext()) {
      SectionContentEntity secCntLoopHolder = secCntIterator.next();
      int loopHolderKey = secCntLoopHolder.getContent().getKey().toInt();
      if (loopHolderKey == chessContentKey) {
        assertEquals(1, secCntLoopHolder.getOrder());
        chessContentChecked = true;
      } else if (loopHolderKey == footballContentKey) {
        assertEquals(2, secCntLoopHolder.getOrder());
        footballContentChecked = true;
      } else if (loopHolderKey == golfContentKey) {
        assertEquals(3, secCntLoopHolder.getOrder());
        golfContentChecked = true;
      } else if (loopHolderKey == icehockeyContentKey) {
        assertEquals(4, secCntLoopHolder.getOrder());
        icehockeyContentChecked = true;
      }
    }
    assertTrue(
        chessContentChecked
            && footballContentChecked
            && golfContentChecked
            && icehockeyContentChecked);

    // Execute reorder command
    ApproveSectionContentCommand approveCommand = new ApproveSectionContentCommand();
    approveCommand.setUpdater(fixture.findUserByName("aru").getKey());
    approveCommand.setSection(fixture.findMenuItemByName("Sports", 30).getMenuItemKey());
    approveCommand.addApprovedContentToUpdate(sportsContentFootball.getKey());
    approveCommand.addApprovedContentToUpdate(sportsContentChess.getKey());
    approveCommand.addApprovedContentToUpdate(sportsContentIcehockey.getKey());
    approveCommand.addApprovedContentToUpdate(sportsContentGolf.getKey());
    menuItemService.approveSectionContent(approveCommand);

    fixture.flushAndClearHibernateSesssion();

    // Assert that all data is updated correctly
    verifyFinalReorderingResult(
        chessContentKey, footballContentKey, golfContentKey, icehockeyContentKey);

    // Execute reorder command without enough rights.
    approveCommand = new ApproveSectionContentCommand();
    approveCommand.setUpdater(fixture.findUserByName("nru").getKey());
    approveCommand.setSection(fixture.findMenuItemByName("Sports", 30).getMenuItemKey());
    approveCommand.addApprovedContentToUpdate(sportsContentIcehockey.getKey());
    approveCommand.addApprovedContentToUpdate(sportsContentGolf.getKey());
    approveCommand.addApprovedContentToUpdate(sportsContentChess.getKey());
    approveCommand.addApprovedContentToUpdate(sportsContentFootball.getKey());
    try {
      menuItemService.approveSectionContent(approveCommand);
      fail(
          "Not in the catch block.  Updating the order of the content should not be allowed for this user.");
    } catch (MenuItemAccessException e) {
      // All Ok
    }

    // Assert that data has not been changed:
    verifyFinalReorderingResult(
        chessContentKey, footballContentKey, golfContentKey, icehockeyContentKey);
  }
  @Test
  public void testRemoveContentFromSection() {
    // Data for test:
    MenuItemEntity menuItemPolitics =
        factory.createSectionMenuItem(
            "News",
            20,
            "Unordered",
            "News",
            "The Newspaper",
            "aru",
            "aru",
            "en",
            "Hello World!",
            10,
            false,
            null,
            false,
            null);
    fixture.save(menuItemPolitics);

    MenuItemAccessEntity aruMenuAR =
        factory.createMenuItemAccess(
            "News",
            20,
            fixture.findGroupByName("aru_group"),
            "read, create, update, delete, add, publish");
    MenuItemAccessEntity nruMenuAR =
        factory.createMenuItemAccess("News", 20, fixture.findGroupByName("nru_group"), "read");
    fixture.save(aruMenuAR, nruMenuAR);

    ContentEntity contentPolitics1 =
        factory.createContent("Articles", "en", "aru", "0", new Date());
    ContentEntity contentPolitics2 =
        factory.createContent("Articles", "en", "aru", "0", new Date());
    fixture.save(contentPolitics1, contentPolitics2);
    ContentKey politicsKey1 = contentPolitics1.getKey();
    ContentKey politicsKey2 = contentPolitics2.getKey();

    SectionContentEntity sectionContent1 =
        factory.createContentSection("News", 20, politicsKey1, true, 1);
    SectionContentEntity sectionContent2 =
        factory.createContentSection("News", 20, politicsKey2, true, 2);
    fixture.save(sectionContent1, sectionContent2);
    fixture.flushAndClearHibernateSesssion();

    // Assert that all data is set up correctly
    MenuItemEntity section = fixture.findMenuItemByName("News", 20);
    assertEquals(2, section.getSectionContents().size());

    // Execute remove command
    RemoveContentFromSectionCommand allRightsRemoveCommand = new RemoveContentFromSectionCommand();
    allRightsRemoveCommand.setRemover(fixture.findUserByName("aru").getKey());
    allRightsRemoveCommand.setSection(fixture.findMenuItemByName("News", 20).getMenuItemKey());
    allRightsRemoveCommand.addContentToRemove(contentPolitics1.getKey());
    menuItemService.removeContentFromSection(allRightsRemoveCommand);

    fixture.flushAndClearHibernateSesssion();

    // Assert result (Only 1 content left in section, and correct content)
    section = fixture.findMenuItemByName("News", 20);
    Set<SectionContentEntity> testResultAfterRemovingOneContent = section.getSectionContents();
    assertEquals(1, testResultAfterRemovingOneContent.size());
    SectionContentEntity testDetailResult = testResultAfterRemovingOneContent.iterator().next();
    assertEquals(politicsKey2, testDetailResult.getContent().getKey());

    // Verify that a user with only list rights can not remove from the section.
    RemoveContentFromSectionCommand noRightsRemoveCommand = new RemoveContentFromSectionCommand();
    noRightsRemoveCommand.setRemover(fixture.findUserByName("nru").getKey());
    noRightsRemoveCommand.setSection(fixture.findMenuItemByName("News", 20).getMenuItemKey());
    noRightsRemoveCommand.addContentToRemove(contentPolitics2.getKey());
    try {
      menuItemService.removeContentFromSection(noRightsRemoveCommand);
      fail(
          "Not in the catch block.  The removeContentFromSection method should throw a MenuItemAccessException in this case.");
    } catch (MenuItemAccessException e) {
      // All ok!
    }

    fixture.flushAndClearHibernateSesssion();

    // Assert result
    section = fixture.findMenuItemByName("News", 20);
    Set<SectionContentEntity> testResultAfterFailingToRemoveOneContent =
        section.getSectionContents();
    assertEquals(1, testResultAfterFailingToRemoveOneContent.size());
    SectionContentEntity testDetailResult2 =
        testResultAfterFailingToRemoveOneContent.iterator().next();
    assertEquals(politicsKey2, testDetailResult2.getContent().getKey());
  }