protected DLFileEntry addDLFileEntry(long folderId, boolean appendExtension) throws Exception {

    ServiceContext serviceContext = ServiceContextTestUtil.getServiceContext(_group.getGroupId());

    String sourceFileName = RandomTestUtil.randomString();

    if (appendExtension) {
      sourceFileName = sourceFileName.concat(".pdf");
    }

    String fileEntryTitle = RandomTestUtil.randomString();

    return DLFileEntryLocalServiceUtil.addFileEntry(
        TestPropsValues.getUserId(),
        _group.getGroupId(),
        _group.getGroupId(),
        folderId,
        sourceFileName,
        null,
        fileEntryTitle,
        RandomTestUtil.randomString(),
        StringPool.BLANK,
        DLFileEntryTypeConstants.FILE_ENTRY_TYPE_ID_BASIC_DOCUMENT,
        null,
        null,
        new ByteArrayInputStream(_CONTENT.getBytes()),
        0,
        serviceContext);
  }
  public static DLFileEntry addDLFileEntry(long dlFolderId) throws Exception {
    DLFolder dlFolder = DLFolderLocalServiceUtil.fetchDLFolder(dlFolderId);

    byte[] bytes = RandomTestUtil.randomBytes(TikaSafeRandomizerBumper.INSTANCE);

    InputStream is = new ByteArrayInputStream(bytes);

    ServiceContext serviceContext = ServiceContextTestUtil.getServiceContext(dlFolder.getGroupId());

    return DLFileEntryLocalServiceUtil.addFileEntry(
        TestPropsValues.getUserId(),
        dlFolder.getGroupId(),
        dlFolder.getRepositoryId(),
        dlFolder.getFolderId(),
        RandomTestUtil.randomString(),
        ContentTypes.TEXT_PLAIN,
        RandomTestUtil.randomString(),
        StringPool.BLANK,
        StringPool.BLANK,
        DLFileEntryTypeConstants.FILE_ENTRY_TYPE_ID_BASIC_DOCUMENT,
        null,
        null,
        is,
        bytes.length,
        serviceContext);
  }
  @Test
  public void testRestoreFileNameWhenDeletingLatestFileVersion() throws Exception {

    DLFileEntry dlFileEntry = addDLFileEntry(DLFolderConstants.DEFAULT_PARENT_FOLDER_ID, false);

    ServiceContext serviceContext = ServiceContextTestUtil.getServiceContext(_group.getGroupId());

    dlFileEntry = updateStatus(dlFileEntry.getLatestFileVersion(true), serviceContext);

    String initialFileName = dlFileEntry.getFileName();

    dlFileEntry.setTitle(RandomTestUtil.randomString());

    dlFileEntry = updateDLFileEntry(dlFileEntry, serviceContext);

    dlFileEntry = updateStatus(dlFileEntry.getLatestFileVersion(true), serviceContext);

    DLFileVersion dlFileVersion = dlFileEntry.getLatestFileVersion(true);

    dlFileEntry =
        DLFileEntryLocalServiceUtil.deleteFileVersion(
            dlFileEntry.getUserId(), dlFileEntry.getFileEntryId(), dlFileVersion.getVersion());

    Assert.assertEquals(initialFileName, dlFileEntry.getFileName());
  }
  @Test
  public void testFindByAnyImageId() throws Exception {
    DLFileEntry dlFileEntry =
        DLFileEntryLocalServiceUtil.fetchFileEntryByAnyImageId(_SMALL_IMAGE_ID);

    String title = dlFileEntry.getTitle();

    Assert.assertTrue(title.equals("FE1.txt") || title.equals("FE1.txt-NewRepository"));
  }
  protected DLFileEntry updateStatus(DLFileVersion dlFileVersion, ServiceContext serviceContext)
      throws Exception {

    return DLFileEntryLocalServiceUtil.updateStatus(
        dlFileVersion.getUserId(),
        dlFileVersion.getFileVersionId(),
        WorkflowConstants.STATUS_APPROVED,
        serviceContext,
        new HashMap<String, Serializable>());
  }
  protected DLFileEntry getDLFileEntry(Object object) throws Exception {
    List<FileEntry> fileEntries = new ArrayList<>();

    if (object instanceof MBMessage) {
      fileEntries = ((MBMessage) object).getAttachmentsFileEntries(0, 1);
    }

    Assert.assertFalse(fileEntries.isEmpty());

    FileEntry fileEntry = fileEntries.get(0);

    return DLFileEntryLocalServiceUtil.getDLFileEntry(fileEntry.getFileEntryId());
  }
  protected int doCountBy_G_U_F_M(
      long userId, String mimeType, QueryDefinition<DLFileEntry> queryDefinition) throws Exception {

    List<Long> folderIds = ListUtil.toList(new long[] {_defaultRepositoryFolder.getFolderId()});

    String[] mimeTypes = null;

    if (mimeType != null) {
      mimeTypes = new String[] {mimeType};
    }

    return DLFileEntryLocalServiceUtil.getFileEntriesCount(
        _group.getGroupId(), userId, folderIds, mimeTypes, queryDefinition);
  }
  @Test
  public void testFindByNoAssets() throws Exception {
    AssetEntryLocalServiceUtil.deleteEntry(
        DLFileEntry.class.getName(), _defaultRepositoryDLFileVersion.getFileEntryId());

    LastSessionRecorderUtil.syncLastSessionState();

    List<DLFileEntry> dlFileEntries = DLFileEntryLocalServiceUtil.getNoAssetFileEntries();

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

    DLFileEntry dlFileEntry = dlFileEntries.get(0);

    Assert.assertEquals("FE1.txt", dlFileEntry.getTitle());
  }
  protected List<DLFileEntry> doFindBy_G_U_R_F_M(
      long userId,
      List<Long> repositoryIds,
      List<Long> folderIds,
      String mimeType,
      QueryDefinition<DLFileEntry> queryDefinition)
      throws Exception {

    String[] mimeTypes = null;

    if (mimeType != null) {
      mimeTypes = new String[] {mimeType};
    }

    return DLFileEntryLocalServiceUtil.getFileEntries(
        _group.getGroupId(), userId, repositoryIds, folderIds, mimeTypes, queryDefinition);
  }
  protected DLFileEntry updateDLFileEntry(DLFileEntry dlFileEntry, ServiceContext serviceContext)
      throws Exception {

    return DLFileEntryLocalServiceUtil.updateFileEntry(
        dlFileEntry.getUserId(),
        dlFileEntry.getFileEntryId(),
        dlFileEntry.getTitle(),
        dlFileEntry.getMimeType(),
        dlFileEntry.getTitle(),
        dlFileEntry.getDescription(),
        StringPool.BLANK,
        false,
        dlFileEntry.getFileEntryTypeId(),
        null,
        null,
        dlFileEntry.getContentStream(),
        dlFileEntry.getSize(),
        serviceContext);
  }
  @Test
  public void testActionableDynamicQuery() throws Exception {
    final IntegerWrapper count = new IntegerWrapper();

    ActionableDynamicQuery actionableDynamicQuery =
        DLFileEntryLocalServiceUtil.getActionableDynamicQuery();

    actionableDynamicQuery.setPerformActionMethod(
        new ActionableDynamicQuery.PerformActionMethod<DLFileEntry>() {
          @Override
          public void performAction(DLFileEntry dlFileEntry) {
            Assert.assertNotNull(dlFileEntry);

            count.increment();
          }
        });

    actionableDynamicQuery.performActions();

    Assert.assertEquals(count.getValue(), _persistence.countAll());
  }
  @Test
  public void testFindByMisversioned() throws Exception {
    long oldFileEntryId = _defaultRepositoryDLFileVersion.getFileEntryId();

    try {
      _defaultRepositoryDLFileVersion.setFileEntryId(RandomTestUtil.randomLong());

      DLFileVersionLocalServiceUtil.updateDLFileVersion(_defaultRepositoryDLFileVersion);

      List<DLFileEntry> dlFileEntries = DLFileEntryLocalServiceUtil.getMisversionedFileEntries();

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

      DLFileEntry dlFileEntry = dlFileEntries.get(0);

      Assert.assertEquals("FE1.txt", dlFileEntry.getTitle());
    } finally {
      _defaultRepositoryDLFileVersion.setFileEntryId(oldFileEntryId);

      DLFileVersionLocalServiceUtil.updateDLFileVersion(_defaultRepositoryDLFileVersion);
    }
  }
  protected static Object[] setUp(
      long repositoryId, String titleSuffix, ServiceContext serviceContext) throws Exception {

    Folder folder =
        DLAppLocalServiceUtil.addFolder(
            TestPropsValues.getUserId(),
            repositoryId,
            DLFolderConstants.DEFAULT_PARENT_FOLDER_ID,
            "Folder A",
            StringPool.BLANK,
            serviceContext);

    DLAppLocalServiceUtil.addFolder(
        TestPropsValues.getUserId(),
        repositoryId,
        folder.getFolderId(),
        "Folder B",
        StringPool.BLANK,
        serviceContext);

    Folder folderC =
        DLAppLocalServiceUtil.addFolder(
            TestPropsValues.getUserId(),
            repositoryId,
            folder.getFolderId(),
            "Folder C",
            StringPool.BLANK,
            serviceContext);

    DLTrashServiceUtil.moveFolderToTrash(folderC.getFolderId());

    FileEntry fileEntry =
        addFileEntry(
            _user.getUserId(),
            repositoryId,
            folder.getFolderId(),
            "FE1.txt",
            titleSuffix,
            ContentTypes.TEXT_PLAIN,
            DLFileEntryTypeConstants.FILE_ENTRY_TYPE_ID_BASIC_DOCUMENT);

    LiferayFileEntry liferayFileEntry = (LiferayFileEntry) fileEntry;

    DLFileEntry dlFileEntry = liferayFileEntry.getDLFileEntry();

    dlFileEntry.setExtraSettings("hello=world");
    dlFileEntry.setSmallImageId(_SMALL_IMAGE_ID);

    dlFileEntry = DLFileEntryLocalServiceUtil.updateDLFileEntry(dlFileEntry);

    DLFileVersion dlFileVersion = dlFileEntry.getFileVersion();

    addFileEntry(
        TestPropsValues.getUserId(),
        repositoryId,
        folder.getFolderId(),
        "FE2.pdf",
        titleSuffix,
        ContentTypes.APPLICATION_PDF,
        DLFileEntryTypeConstants.FILE_ENTRY_TYPE_ID_ALL);

    fileEntry =
        addFileEntry(
            TestPropsValues.getUserId(),
            repositoryId,
            folder.getFolderId(),
            "FE3.txt",
            titleSuffix,
            ContentTypes.TEXT_PLAIN,
            DLFileEntryTypeConstants.FILE_ENTRY_TYPE_ID_ALL);

    fileEntry =
        DLAppServiceUtil.updateFileEntry(
            fileEntry.getFileEntryId(),
            "FE3.txt",
            ContentTypes.TEXT_PLAIN,
            "FE3.txt".concat(titleSuffix),
            StringPool.BLANK,
            StringPool.BLANK,
            false,
            RandomTestUtil.randomBytes(TikaSafeRandomizerBumper.INSTANCE),
            serviceContext);

    dlFileEntry = ((LiferayFileEntry) fileEntry).getDLFileEntry();

    dlFileEntry.setDescription("FE3.txt");

    DLFileEntryLocalServiceUtil.updateDLFileEntry(dlFileEntry);

    DLFileVersion dlFileVersion3 = dlFileEntry.getFileVersion();

    dlFileVersion3.setExtraSettings("hello=world");

    DLFileVersionLocalServiceUtil.updateDLFileVersion(dlFileVersion3);

    DLTrashServiceUtil.moveFileEntryToTrash(fileEntry.getFileEntryId());

    return new Object[] {folder, dlFileVersion};
  }
 @Test
 public void testCountByExtraSettings() throws Exception {
   Assert.assertEquals(4, DLFileEntryLocalServiceUtil.getExtraSettingsFileEntriesCount());
 }
  @Override
  public InputStream getContentStream(boolean incrementCounter) throws PortalException {

    return DLFileEntryLocalServiceUtil.getFileAsStream(
        getFileEntryId(), getVersion(), incrementCounter);
  }
 @Override
 public DLFileEntry getFileEntry() throws PortalException {
   return DLFileEntryLocalServiceUtil.getFileEntry(getFileEntryId());
 }