@Test(expected = NoSuchFolderException.class)
  public void testMoveKBFolderToParentKBArticle() throws Exception {
    KBFolder kbFolder = addKBFolder(_kbFolder.getKbFolderId());
    KBArticle kbArticle = addKBArticle(_kbFolder.getKbFolderId(), RandomTestUtil.randomString());

    KBFolderLocalServiceUtil.moveKBFolder(kbFolder.getKbFolderId(), kbArticle.getResourcePrimKey());
  }
  @Test
  public void testGetKBFoldersAndKBArticlesOrderedByDescendingViewCount() throws Exception {

    KBArticle kbArticle1 = addKBArticle(KBFolderConstants.DEFAULT_PARENT_FOLDER_ID, "A");
    KBArticle kbArticle2 = addKBArticle(KBFolderConstants.DEFAULT_PARENT_FOLDER_ID, "B");
    KBArticle kbArticle3 = addKBArticle(KBFolderConstants.DEFAULT_PARENT_FOLDER_ID, "C");

    KBArticleLocalServiceUtil.updateViewCount(
        kbArticle2.getUserId(), kbArticle2.getResourcePrimKey(), kbArticle2.getViewCount() + 1000);

    List<Object> kbFolderAndKBArticles =
        KBFolderLocalServiceUtil.getKBFoldersAndKBArticles(
            _group.getGroupId(),
            KBFolderConstants.DEFAULT_PARENT_FOLDER_ID,
            WorkflowConstants.STATUS_ANY,
            QueryUtil.ALL_POS,
            QueryUtil.ALL_POS,
            new KBObjectsViewCountComparator(false));

    KBFolder currentKBFolder = (KBFolder) kbFolderAndKBArticles.get(0);
    KBArticle currentKBArticle1 = (KBArticle) kbFolderAndKBArticles.get(2);
    KBArticle currentKBArticle2 = (KBArticle) kbFolderAndKBArticles.get(1);
    KBArticle currentKBArticle3 = (KBArticle) kbFolderAndKBArticles.get(3);

    Assert.assertEquals(_kbFolder.getKbFolderId(), currentKBFolder.getKbFolderId());
    Assert.assertEquals(kbArticle1.getKbArticleId(), currentKBArticle1.getKbArticleId());
    Assert.assertEquals(kbArticle2.getKbArticleId(), currentKBArticle2.getKbArticleId());
    Assert.assertEquals(kbArticle3.getKbArticleId(), currentKBArticle3.getKbArticleId());
  }
  @Test
  public void testGetKBFoldersAndKBArticlesOrderedByAscendingPriority() throws Exception {

    KBArticle kbArticle1 =
        addKBArticle(KBFolderConstants.DEFAULT_PARENT_FOLDER_ID, RandomTestUtil.randomString());
    KBArticle kbArticle2 =
        addKBArticle(KBFolderConstants.DEFAULT_PARENT_FOLDER_ID, RandomTestUtil.randomString());
    KBArticle kbArticle3 =
        addKBArticle(KBFolderConstants.DEFAULT_PARENT_FOLDER_ID, RandomTestUtil.randomString());

    KBArticleLocalServiceUtil.updatePriority(kbArticle2.getResourcePrimKey(), 10.0);

    List<Object> kbFolderAndKBArticles =
        KBFolderLocalServiceUtil.getKBFoldersAndKBArticles(
            _group.getGroupId(),
            KBFolderConstants.DEFAULT_PARENT_FOLDER_ID,
            WorkflowConstants.STATUS_ANY,
            QueryUtil.ALL_POS,
            QueryUtil.ALL_POS,
            new KBObjectsPriorityComparator(true));

    KBFolder currentKBFolder = (KBFolder) kbFolderAndKBArticles.get(0);
    KBArticle currentKBArticle1 = (KBArticle) kbFolderAndKBArticles.get(1);
    KBArticle currentKBArticle2 = (KBArticle) kbFolderAndKBArticles.get(3);
    KBArticle currentKBArticle3 = (KBArticle) kbFolderAndKBArticles.get(2);

    Assert.assertEquals(_kbFolder.getKbFolderId(), currentKBFolder.getKbFolderId());
    Assert.assertEquals(kbArticle1.getKbArticleId(), currentKBArticle1.getKbArticleId());
    Assert.assertEquals(kbArticle2.getKbArticleId(), currentKBArticle2.getKbArticleId());
    Assert.assertEquals(kbArticle3.getKbArticleId(), currentKBArticle3.getKbArticleId());
  }
  protected KBArticle updateKBArticle(KBArticle kbArticle, String title) throws Exception {

    ServiceContext serviceContext =
        ServiceContextTestUtil.getServiceContext(_group, _user.getUserId());

    return KBArticleLocalServiceUtil.updateKBArticle(
        kbArticle.getUserId(),
        kbArticle.getResourcePrimKey(),
        title,
        kbArticle.getContent(),
        kbArticle.getDescription(),
        kbArticle.getSourceURL(),
        null,
        new String[0],
        new long[0],
        serviceContext);
  }
  protected KBArticle addChildKBArticle(KBArticle kbArticle, String title) throws Exception {

    ServiceContext serviceContext =
        ServiceContextTestUtil.getServiceContext(_group, _user.getUserId());

    return KBArticleLocalServiceUtil.addKBArticle(
        _user.getUserId(),
        PortalUtil.getClassNameId(KBArticleConstants.getClassName()),
        kbArticle.getResourcePrimKey(),
        title,
        title,
        RandomTestUtil.randomString(),
        RandomTestUtil.randomString(),
        null,
        new String[0],
        new String[0],
        serviceContext);
  }
  @Test
  public void testGetKBFoldersAndKBArticlesCountKBArticleImmediateChildren() throws Exception {

    KBArticle parentKBArticle =
        addKBArticle(_kbFolder.getKbFolderId(), RandomTestUtil.randomString());

    addChildKBArticle(parentKBArticle, RandomTestUtil.randomString());
    addChildKBArticle(parentKBArticle, RandomTestUtil.randomString());

    addKBFolder(_kbFolder.getKbFolderId());

    Assert.assertEquals(
        2,
        KBFolderLocalServiceUtil.getKBFoldersAndKBArticlesCount(
            _group.getGroupId(),
            parentKBArticle.getResourcePrimKey(),
            WorkflowConstants.STATUS_ANY));
  }
  @Test
  public void testGetKBFoldersAndKBArticlesReturnKBArticleImmediateChildren() throws Exception {

    KBArticle parentKBArticle =
        addKBArticle(_kbFolder.getKbFolderId(), RandomTestUtil.randomString());

    KBArticle kbArticle1 = addChildKBArticle(parentKBArticle, "A");
    KBArticle kbArticle2 = addChildKBArticle(parentKBArticle, "B");

    List<Object> kbFolderAndKBArticles =
        KBFolderLocalServiceUtil.getKBFoldersAndKBArticles(
            _group.getGroupId(),
            parentKBArticle.getResourcePrimKey(),
            WorkflowConstants.STATUS_APPROVED,
            QueryUtil.ALL_POS,
            QueryUtil.ALL_POS,
            new KBObjectsTitleComparator<KBArticle>(true, true));

    KBArticle currentKBArticle1 = (KBArticle) kbFolderAndKBArticles.get(0);
    KBArticle currentKBArticle2 = (KBArticle) kbFolderAndKBArticles.get(1);

    Assert.assertEquals(kbArticle1.getKbArticleId(), currentKBArticle1.getKbArticleId());
    Assert.assertEquals(kbArticle2.getKbArticleId(), currentKBArticle2.getKbArticleId());
  }