Пример #1
0
 /*
  * NOTE FOR DEVELOPERS:
  *
  * Never modify or reference this class directly. All methods that expect a k b folder model instance should use the {@link KBFolder} interface instead.
  */
 @Override
 public void persist() {
   if (this.isNew()) {
     KBFolderLocalServiceUtil.addKBFolder(this);
   } else {
     KBFolderLocalServiceUtil.updateKBFolder(this);
   }
 }
  @Test
  public void testMoveKBFolderToParentKBFolderInKBFolder() throws Exception {
    KBFolder kbFolder = addKBFolder(_kbFolder.getKbFolderId());
    KBFolder parentKBFolder = addKBFolder(_kbFolder.getKbFolderId());

    KBFolderLocalServiceUtil.moveKBFolder(kbFolder.getKbFolderId(), parentKBFolder.getKbFolderId());

    kbFolder = KBFolderLocalServiceUtil.getKBFolder(kbFolder.getKbFolderId());

    Assert.assertEquals(parentKBFolder.getKbFolderId(), kbFolder.getParentKBFolderId());
  }
  @Test
  public void testMoveKBFolderToParentKBFolderInHomeKBFolder() throws Exception {

    KBFolder kbFolder = addKBFolder(KBFolderConstants.DEFAULT_PARENT_FOLDER_ID);
    KBFolder parentKBFolder = addKBFolder(KBFolderConstants.DEFAULT_PARENT_FOLDER_ID);

    KBFolderLocalServiceUtil.moveKBFolder(kbFolder.getKbFolderId(), parentKBFolder.getKbFolderId());

    kbFolder = KBFolderLocalServiceUtil.getKBFolder(kbFolder.getKbFolderId());

    Assert.assertEquals(parentKBFolder.getKbFolderId(), kbFolder.getParentKBFolderId());
  }
  @Test
  public void testGetKBFoldersAndKBArticlesReturnKBFolderImmediateChildren() throws Exception {

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

    addChildKBArticle(parentKBArticle, RandomTestUtil.randomString());

    KBFolder kbFolder = addKBFolder(_kbFolder.getKbFolderId());

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

    KBFolder currentKBFolder = (KBFolder) kbFolderAndKBArticles.get(0);
    KBArticle currentKBArticle1 = (KBArticle) kbFolderAndKBArticles.get(1);

    Assert.assertEquals(kbFolder.getKbFolderId(), currentKBFolder.getKbFolderId());
    Assert.assertEquals(parentKBArticle.getKbArticleId(), currentKBArticle1.getKbArticleId());
    Assert.assertEquals(2, kbFolderAndKBArticles.size());
  }
  @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(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 testGetKBFoldersAndKBArticlesWithMultipleKBArticleVersions() throws Exception {

    KBArticle kbArticle =
        addKBArticle(KBFolderConstants.DEFAULT_PARENT_FOLDER_ID, RandomTestUtil.randomString());

    KBArticle newKBArticle = updateKBArticle(kbArticle, RandomTestUtil.randomString());

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

    KBFolder currentKBFolder = (KBFolder) kbFolderAndKBArticles.get(0);
    KBArticle currentKBArticle1 = (KBArticle) kbFolderAndKBArticles.get(1);

    Assert.assertEquals(2, kbFolderAndKBArticles.size());

    Assert.assertEquals(_kbFolder.getKbFolderId(), currentKBFolder.getKbFolderId());
    Assert.assertEquals(newKBArticle.getKbArticleId(), currentKBArticle1.getKbArticleId());
  }
  @Test
  public void testGetKBFoldersAndKBArticlesOrderedByDescendingModifiedDate() throws Exception {

    Date date = new Date();

    KBArticle kbArticle1 =
        addKBArticle(
            KBFolderConstants.DEFAULT_PARENT_FOLDER_ID, new Date(date.getTime() + Time.SECOND));
    KBArticle kbArticle2 =
        addKBArticle(
            KBFolderConstants.DEFAULT_PARENT_FOLDER_ID, new Date(date.getTime() + Time.SECOND * 3));
    KBArticle kbArticle3 =
        addKBArticle(
            KBFolderConstants.DEFAULT_PARENT_FOLDER_ID, new Date(date.getTime() + Time.SECOND * 2));

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

    KBFolder currentKBFolder = (KBFolder) kbFolderAndKBArticles.get(0);
    KBArticle currentKBArticle1 = (KBArticle) kbFolderAndKBArticles.get(3);
    KBArticle currentKBArticle2 = (KBArticle) kbFolderAndKBArticles.get(1);
    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());
  }
  @Test
  public void testGetKBFoldersAndKBArticlesOrderedByAscendingTitle() throws Exception {

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

    List<Object> kbFolderAndKBArticles =
        KBFolderLocalServiceUtil.getKBFoldersAndKBArticles(
            _group.getGroupId(),
            KBFolderConstants.DEFAULT_PARENT_FOLDER_ID,
            WorkflowConstants.STATUS_ANY,
            QueryUtil.ALL_POS,
            QueryUtil.ALL_POS,
            new KBObjectsTitleComparator(true, 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());
  }
  @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());
  }
  @Test(expected = KBFolderParentException.class)
  public void testMoveKBArticleToInvalidParentKBFolder() throws Exception {
    KBFolder kbFolder = addKBFolder(_kbFolder.getKbFolderId());

    KBFolder kbSubfolder = addKBFolder(kbFolder.getKbFolderId());

    KBFolderLocalServiceUtil.moveKBFolder(kbFolder.getKbFolderId(), kbSubfolder.getKbFolderId());
  }
  @Test
  public void testGetKBFoldersAndKBArticlesCountInRootKBFolderByDraftStatus() throws Exception {

    addKBArticle(KBFolderConstants.DEFAULT_PARENT_FOLDER_ID, RandomTestUtil.randomString());

    Assert.assertEquals(
        1,
        KBFolderLocalServiceUtil.getKBFoldersAndKBArticlesCount(
            _group.getGroupId(),
            KBFolderConstants.DEFAULT_PARENT_FOLDER_ID,
            WorkflowConstants.STATUS_DRAFT));
  }
  @Test
  public void testGetKBFoldersAndKBArticlesCountInKBFolder() throws Exception {

    addKBArticle(_kbFolder.getKbFolderId(), RandomTestUtil.randomString());
    addKBArticle(_kbFolder.getKbFolderId(), RandomTestUtil.randomString());

    addKBFolder(_kbFolder.getKbFolderId());

    Assert.assertEquals(
        3,
        KBFolderLocalServiceUtil.getKBFoldersAndKBArticlesCount(
            _group.getGroupId(), _kbFolder.getKbFolderId(), WorkflowConstants.STATUS_ANY));
  }
  protected KBFolder addKBFolder(long parentResourcePrimKey)
      throws com.liferay.portal.kernel.exception.PortalException {

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

    return KBFolderLocalServiceUtil.addKBFolder(
        _user.getUserId(),
        _group.getGroupId(),
        PortalUtil.getClassNameId(KBFolderConstants.getClassName()),
        parentResourcePrimKey,
        RandomTestUtil.randomString(),
        RandomTestUtil.randomString(),
        serviceContext);
  }
  @Test
  public void testGetKBFoldersAndKBArticlesCountWithMultipleKBArticleVersions() throws Exception {

    KBArticle kbArticle =
        addKBArticle(KBFolderConstants.DEFAULT_PARENT_FOLDER_ID, RandomTestUtil.randomString());

    updateKBArticle(kbArticle, RandomTestUtil.randomString());

    Assert.assertEquals(
        2,
        KBFolderLocalServiceUtil.getKBFoldersAndKBArticlesCount(
            _group.getGroupId(),
            KBFolderConstants.DEFAULT_PARENT_FOLDER_ID,
            WorkflowConstants.STATUS_ANY));
  }
  @Test
  public void testGetKBFoldersAndKBArticlesCountKBFolderImmediateChildren() throws Exception {

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

    addChildKBArticle(parentKBArticle, RandomTestUtil.randomString());

    addKBFolder(_kbFolder.getKbFolderId());

    Assert.assertEquals(
        2,
        KBFolderLocalServiceUtil.getKBFoldersAndKBArticlesCount(
            _group.getGroupId(), _kbFolder.getKbFolderId(), WorkflowConstants.STATUS_ANY));
  }
  @Test
  public void testGetKBFoldersAndKBArticlesInRootKBFolderByDraftStatus() throws Exception {

    addKBArticle(KBFolderConstants.DEFAULT_PARENT_FOLDER_ID, RandomTestUtil.randomString());

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

    KBFolder currentKBFolder = (KBFolder) kbFolderAndKBArticles.get(0);

    Assert.assertEquals(1, kbFolderAndKBArticles.size());

    Assert.assertEquals(_kbFolder.getKbFolderId(), currentKBFolder.getKbFolderId());
  }
  @Test
  public void testGetKBFoldersAndKBArticlesInKBFolder() throws Exception {
    KBArticle kbArticle1 = addKBArticle(_kbFolder.getKbFolderId(), "A");
    KBArticle kbArticle2 = addKBArticle(_kbFolder.getKbFolderId(), "B");

    KBFolder kbFolder = addKBFolder(_kbFolder.getKbFolderId());

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

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

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