@Test
  public void testCopyFileEntryWithoutExtensionInRootFolderToFolder() throws Exception {

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

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

    DLFolder destFolder =
        DLFolderLocalServiceUtil.addFolder(
            TestPropsValues.getUserId(),
            _group.getGroupId(),
            _group.getGroupId(),
            false,
            DLFolderConstants.DEFAULT_PARENT_FOLDER_ID,
            RandomTestUtil.randomString(),
            RandomTestUtil.randomString(),
            false,
            serviceContext);

    DLFileEntryServiceUtil.copyFileEntry(
        _group.getGroupId(),
        _group.getGroupId(),
        dlFileEntry.getFileEntryId(),
        destFolder.getFolderId(),
        serviceContext);
  }
  @Test
  public void testFindByPrimaryKeyExisting() throws Exception {
    DLFileEntry newDLFileEntry = addDLFileEntry();

    DLFileEntry existingDLFileEntry = _persistence.findByPrimaryKey(newDLFileEntry.getPrimaryKey());

    Assert.assertEquals(existingDLFileEntry, newDLFileEntry);
  }
  @Test
  public void testCreateRepositoryFromExistingFileEntryId() throws Exception {
    DLFolder dlFolder = DLTestUtil.addDLFolder(_group.getGroupId());

    DLFileEntry dlFileEntry = DLTestUtil.addDLFileEntry(dlFolder.getFolderId());

    RepositoryProviderUtil.getFileEntryLocalRepository(dlFileEntry.getFileEntryId());
  }
  @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"));
  }
  @Test
  public void testCreate() throws Exception {
    long pk = RandomTestUtil.nextLong();

    DLFileEntry dlFileEntry = _persistence.create(pk);

    Assert.assertNotNull(dlFileEntry);

    Assert.assertEquals(dlFileEntry.getPrimaryKey(), pk);
  }
  protected void addFileEntryResources(DLFileEntry dlFileEntry, ServiceContext serviceContext)
      throws PortalException {

    if (serviceContext.isAddGroupPermissions() || serviceContext.isAddGuestPermissions()) {

      resourceLocalService.addResources(
          dlFileEntry.getCompanyId(),
          dlFileEntry.getGroupId(),
          dlFileEntry.getUserId(),
          DLFileEntry.class.getName(),
          dlFileEntry.getFileEntryId(),
          false,
          serviceContext.isAddGroupPermissions(),
          serviceContext.isAddGuestPermissions());
    } else {
      if (serviceContext.isDeriveDefaultPermissions()) {
        serviceContext.deriveDefaultPermissions(
            dlFileEntry.getRepositoryId(), DLFileEntryConstants.getClassName());
      }

      resourceLocalService.addModelResources(
          dlFileEntry.getCompanyId(),
          dlFileEntry.getGroupId(),
          dlFileEntry.getUserId(),
          DLFileEntry.class.getName(),
          dlFileEntry.getFileEntryId(),
          serviceContext.getModelPermissions());
    }
  }
  @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 testRemove() throws Exception {
    DLFileEntry newDLFileEntry = addDLFileEntry();

    _persistence.remove(newDLFileEntry);

    DLFileEntry existingDLFileEntry =
        _persistence.fetchByPrimaryKey(newDLFileEntry.getPrimaryKey());

    Assert.assertNull(existingDLFileEntry);
  }
  @Test
  public void testFetchByPrimaryKeysWithOnePrimaryKey() throws Exception {
    DLFileEntry newDLFileEntry = addDLFileEntry();

    Set<Serializable> primaryKeys = new HashSet<Serializable>();

    primaryKeys.add(newDLFileEntry.getPrimaryKey());

    Map<Serializable, DLFileEntry> dlFileEntries = _persistence.fetchByPrimaryKeys(primaryKeys);

    Assert.assertEquals(1, dlFileEntries.size());
    Assert.assertEquals(newDLFileEntry, dlFileEntries.get(newDLFileEntry.getPrimaryKey()));
  }
  @Test
  public void testFindByG_U_F_M_StatusInTrash() throws Exception {
    QueryDefinition<DLFileEntry> queryDefinition = new QueryDefinition<>();

    queryDefinition.setStatus(WorkflowConstants.STATUS_IN_TRASH, true);

    List<DLFileEntry> dlFileEntries = doFindBy_G_U_F_M(0, ContentTypes.TEXT_PLAIN, queryDefinition);

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

    DLFileEntry dlFileEntry = dlFileEntries.get(0);

    Assert.assertEquals("FE1.txt", dlFileEntry.getTitle());
  }
  @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());
  }
  @Test
  public void testFindByG_U_F_M_StatusApproved() throws Exception {
    QueryDefinition<DLFileEntry> queryDefinition = new QueryDefinition<>();

    queryDefinition.setStatus(WorkflowConstants.STATUS_APPROVED);

    List<DLFileEntry> dlFileEntries =
        doFindBy_G_U_F_M(_defaultRepositoryFolder.getUserId(), null, queryDefinition);

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

    DLFileEntry dlFileEntry = dlFileEntries.get(0);

    Assert.assertEquals("FE2.pdf", dlFileEntry.getTitle());
  }
  @Test
  public void testFindByG_U_F_M_StatusAny_NewRepository() throws Exception {
    QueryDefinition<DLFileEntry> queryDefinition = new QueryDefinition<>();

    queryDefinition.setStatus(WorkflowConstants.STATUS_ANY);

    List<DLFileEntry> dlFileEntries =
        doFindBy_G_U_R_F_M_NewRepository(
            _newRepositoryFolder.getUserId(), ContentTypes.TEXT_PLAIN, queryDefinition);

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

    DLFileEntry dlFileEntry = dlFileEntries.get(0);

    Assert.assertEquals("FE3.txt", dlFileEntry.getDescription());
  }
  @Test
  public void testFetchByPrimaryKeysWithMultiplePrimaryKeysWhereSomePrimaryKeysExist()
      throws Exception {
    DLFileEntry newDLFileEntry = addDLFileEntry();

    long pk = RandomTestUtil.nextLong();

    Set<Serializable> primaryKeys = new HashSet<Serializable>();

    primaryKeys.add(newDLFileEntry.getPrimaryKey());
    primaryKeys.add(pk);

    Map<Serializable, DLFileEntry> dlFileEntries = _persistence.fetchByPrimaryKeys(primaryKeys);

    Assert.assertEquals(1, dlFileEntries.size());
    Assert.assertEquals(newDLFileEntry, dlFileEntries.get(newDLFileEntry.getPrimaryKey()));
  }
  @Test
  public void testDynamicQueryByPrimaryKeyExisting() throws Exception {
    DLFileEntry newDLFileEntry = addDLFileEntry();

    DynamicQuery dynamicQuery =
        DynamicQueryFactoryUtil.forClass(DLFileEntry.class, _dynamicQueryClassLoader);

    dynamicQuery.add(RestrictionsFactoryUtil.eq("fileEntryId", newDLFileEntry.getFileEntryId()));

    List<DLFileEntry> result = _persistence.findWithDynamicQuery(dynamicQuery);

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

    DLFileEntry existingDLFileEntry = result.get(0);

    Assert.assertEquals(existingDLFileEntry, newDLFileEntry);
  }
  protected void testMigrateDL(long folderId) throws Exception {
    FileEntry fileEntry =
        addFileEntry(
            folderId,
            RandomTestUtil.randomString() + ".txt",
            ContentTypes.TEXT_PLAIN,
            RandomTestUtil.randomBytes(TikaSafeRandomizerBumper.INSTANCE));

    _convertProcess.convert();

    DLFileEntry dlFileEntry = (DLFileEntry) fileEntry.getModel();

    DLContentLocalServiceUtil.getContent(
        dlFileEntry.getCompanyId(),
        DLFolderConstants.getDataRepositoryId(
            dlFileEntry.getRepositoryId(), dlFileEntry.getFolderId()),
        dlFileEntry.getName());
  }
  @Test
  public void testDynamicQueryByProjectionExisting() throws Exception {
    DLFileEntry newDLFileEntry = addDLFileEntry();

    DynamicQuery dynamicQuery =
        DynamicQueryFactoryUtil.forClass(DLFileEntry.class, _dynamicQueryClassLoader);

    dynamicQuery.setProjection(ProjectionFactoryUtil.property("fileEntryId"));

    Object newFileEntryId = newDLFileEntry.getFileEntryId();

    dynamicQuery.add(RestrictionsFactoryUtil.in("fileEntryId", new Object[] {newFileEntryId}));

    List<Object> result = _persistence.findWithDynamicQuery(dynamicQuery);

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

    Object existingFileEntryId = result.get(0);

    Assert.assertEquals(existingFileEntryId, newFileEntryId);
  }
  @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);
    }
  }
  @Override
  public void validate(
      long groupId, String className, long classTypePK, long[] categoryIds, String[] entryNames)
      throws PortalException {

    if (className.equals(DLFileEntryConstants.getClassName())) {
      DLFileEntry dlFileEntry = _dlFileEntryLocalService.fetchDLFileEntry(classTypePK);

      if ((dlFileEntry == null) || (dlFileEntry.getRepositoryId() != groupId)) {

        return;
      }
    }

    List<AssetVocabulary> assetVocabularies =
        _assetVocabularyLocalService.getGroupVocabularies(groupId, false);

    Group group = _groupLocalService.getGroup(groupId);

    if (!group.isCompany()) {
      Group companyGroup = _groupLocalService.fetchCompanyGroup(group.getCompanyId());

      if (companyGroup != null) {
        assetVocabularies = ListUtil.copy(assetVocabularies);

        assetVocabularies.addAll(
            _assetVocabularyLocalService.getGroupVocabularies(companyGroup.getGroupId()));
      }
    }

    long classNameId = _classNameLocalService.getClassNameId(className);

    if (isCategorizable(classNameId)) {
      for (AssetVocabulary assetVocabulary : assetVocabularies) {
        validate(classNameId, classTypePK, categoryIds, assetVocabulary);
      }
    }
  }
  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 testUpdateFileNameWhenUpdatingFileVersionStatus() throws Exception {

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

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

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

    DLFileVersion dlFileVersion = dlFileEntry.getLatestFileVersion(true);

    String fileName = RandomTestUtil.randomString();

    dlFileVersion.setFileName(fileName);

    DLFileVersionLocalServiceUtil.updateDLFileVersion(dlFileVersion);

    dlFileEntry = updateStatus(dlFileVersion, serviceContext);

    Assert.assertEquals(
        DLUtil.getSanitizedFileName(fileName, dlFileEntry.getExtension()),
        dlFileEntry.getFileName());
  }
  @Test
  public void testFetchByPrimaryKeysWithMultiplePrimaryKeysWhereAllPrimaryKeysExist()
      throws Exception {
    DLFileEntry newDLFileEntry1 = addDLFileEntry();
    DLFileEntry newDLFileEntry2 = addDLFileEntry();

    Set<Serializable> primaryKeys = new HashSet<Serializable>();

    primaryKeys.add(newDLFileEntry1.getPrimaryKey());
    primaryKeys.add(newDLFileEntry2.getPrimaryKey());

    Map<Serializable, DLFileEntry> dlFileEntries = _persistence.fetchByPrimaryKeys(primaryKeys);

    Assert.assertEquals(2, dlFileEntries.size());
    Assert.assertEquals(newDLFileEntry1, dlFileEntries.get(newDLFileEntry1.getPrimaryKey()));
    Assert.assertEquals(newDLFileEntry2, dlFileEntries.get(newDLFileEntry2.getPrimaryKey()));
  }
  @Test
  public void testMigrateMB() throws Exception {
    MBMessage mbMessage = addMBMessageAttachment();

    _convertProcess.convert();

    DLFileEntry dlFileEntry = getDLFileEntry(mbMessage);

    String title = dlFileEntry.getTitle();

    Assert.assertTrue(title.endsWith(".docx"));

    DLContentLocalServiceUtil.getContent(
        dlFileEntry.getCompanyId(),
        DLFolderConstants.getDataRepositoryId(
            dlFileEntry.getRepositoryId(), dlFileEntry.getFolderId()),
        dlFileEntry.getName());
  }
  @Test
  public void testUpdateExisting() throws Exception {
    long pk = RandomTestUtil.nextLong();

    DLFileEntry newDLFileEntry = _persistence.create(pk);

    newDLFileEntry.setUuid(RandomTestUtil.randomString());

    newDLFileEntry.setGroupId(RandomTestUtil.nextLong());

    newDLFileEntry.setCompanyId(RandomTestUtil.nextLong());

    newDLFileEntry.setUserId(RandomTestUtil.nextLong());

    newDLFileEntry.setUserName(RandomTestUtil.randomString());

    newDLFileEntry.setCreateDate(RandomTestUtil.nextDate());

    newDLFileEntry.setModifiedDate(RandomTestUtil.nextDate());

    newDLFileEntry.setClassNameId(RandomTestUtil.nextLong());

    newDLFileEntry.setClassPK(RandomTestUtil.nextLong());

    newDLFileEntry.setRepositoryId(RandomTestUtil.nextLong());

    newDLFileEntry.setFolderId(RandomTestUtil.nextLong());

    newDLFileEntry.setTreePath(RandomTestUtil.randomString());

    newDLFileEntry.setName(RandomTestUtil.randomString());

    newDLFileEntry.setFileName(RandomTestUtil.randomString());

    newDLFileEntry.setExtension(RandomTestUtil.randomString());

    newDLFileEntry.setMimeType(RandomTestUtil.randomString());

    newDLFileEntry.setTitle(RandomTestUtil.randomString());

    newDLFileEntry.setDescription(RandomTestUtil.randomString());

    newDLFileEntry.setExtraSettings(RandomTestUtil.randomString());

    newDLFileEntry.setFileEntryTypeId(RandomTestUtil.nextLong());

    newDLFileEntry.setVersion(RandomTestUtil.randomString());

    newDLFileEntry.setSize(RandomTestUtil.nextLong());

    newDLFileEntry.setReadCount(RandomTestUtil.nextInt());

    newDLFileEntry.setSmallImageId(RandomTestUtil.nextLong());

    newDLFileEntry.setLargeImageId(RandomTestUtil.nextLong());

    newDLFileEntry.setCustom1ImageId(RandomTestUtil.nextLong());

    newDLFileEntry.setCustom2ImageId(RandomTestUtil.nextLong());

    newDLFileEntry.setManualCheckInRequired(RandomTestUtil.randomBoolean());

    newDLFileEntry.setLastPublishDate(RandomTestUtil.nextDate());

    _dlFileEntries.add(_persistence.update(newDLFileEntry));

    DLFileEntry existingDLFileEntry = _persistence.findByPrimaryKey(newDLFileEntry.getPrimaryKey());

    Assert.assertEquals(existingDLFileEntry.getUuid(), newDLFileEntry.getUuid());
    Assert.assertEquals(existingDLFileEntry.getFileEntryId(), newDLFileEntry.getFileEntryId());
    Assert.assertEquals(existingDLFileEntry.getGroupId(), newDLFileEntry.getGroupId());
    Assert.assertEquals(existingDLFileEntry.getCompanyId(), newDLFileEntry.getCompanyId());
    Assert.assertEquals(existingDLFileEntry.getUserId(), newDLFileEntry.getUserId());
    Assert.assertEquals(existingDLFileEntry.getUserName(), newDLFileEntry.getUserName());
    Assert.assertEquals(
        Time.getShortTimestamp(existingDLFileEntry.getCreateDate()),
        Time.getShortTimestamp(newDLFileEntry.getCreateDate()));
    Assert.assertEquals(
        Time.getShortTimestamp(existingDLFileEntry.getModifiedDate()),
        Time.getShortTimestamp(newDLFileEntry.getModifiedDate()));
    Assert.assertEquals(existingDLFileEntry.getClassNameId(), newDLFileEntry.getClassNameId());
    Assert.assertEquals(existingDLFileEntry.getClassPK(), newDLFileEntry.getClassPK());
    Assert.assertEquals(existingDLFileEntry.getRepositoryId(), newDLFileEntry.getRepositoryId());
    Assert.assertEquals(existingDLFileEntry.getFolderId(), newDLFileEntry.getFolderId());
    Assert.assertEquals(existingDLFileEntry.getTreePath(), newDLFileEntry.getTreePath());
    Assert.assertEquals(existingDLFileEntry.getName(), newDLFileEntry.getName());
    Assert.assertEquals(existingDLFileEntry.getFileName(), newDLFileEntry.getFileName());
    Assert.assertEquals(existingDLFileEntry.getExtension(), newDLFileEntry.getExtension());
    Assert.assertEquals(existingDLFileEntry.getMimeType(), newDLFileEntry.getMimeType());
    Assert.assertEquals(existingDLFileEntry.getTitle(), newDLFileEntry.getTitle());
    Assert.assertEquals(existingDLFileEntry.getDescription(), newDLFileEntry.getDescription());
    Assert.assertEquals(existingDLFileEntry.getExtraSettings(), newDLFileEntry.getExtraSettings());
    Assert.assertEquals(
        existingDLFileEntry.getFileEntryTypeId(), newDLFileEntry.getFileEntryTypeId());
    Assert.assertEquals(existingDLFileEntry.getVersion(), newDLFileEntry.getVersion());
    Assert.assertEquals(existingDLFileEntry.getSize(), newDLFileEntry.getSize());
    Assert.assertEquals(existingDLFileEntry.getReadCount(), newDLFileEntry.getReadCount());
    Assert.assertEquals(existingDLFileEntry.getSmallImageId(), newDLFileEntry.getSmallImageId());
    Assert.assertEquals(existingDLFileEntry.getLargeImageId(), newDLFileEntry.getLargeImageId());
    Assert.assertEquals(
        existingDLFileEntry.getCustom1ImageId(), newDLFileEntry.getCustom1ImageId());
    Assert.assertEquals(
        existingDLFileEntry.getCustom2ImageId(), newDLFileEntry.getCustom2ImageId());
    Assert.assertEquals(
        existingDLFileEntry.getManualCheckInRequired(), newDLFileEntry.getManualCheckInRequired());
    Assert.assertEquals(
        Time.getShortTimestamp(existingDLFileEntry.getLastPublishDate()),
        Time.getShortTimestamp(newDLFileEntry.getLastPublishDate()));
  }
  @Test
  public void testResetOriginalValues() throws Exception {
    DLFileEntry newDLFileEntry = addDLFileEntry();

    _persistence.clearCache();

    DLFileEntry existingDLFileEntry = _persistence.findByPrimaryKey(newDLFileEntry.getPrimaryKey());

    Assert.assertTrue(
        Objects.equals(
            existingDLFileEntry.getUuid(),
            ReflectionTestUtil.invoke(existingDLFileEntry, "getOriginalUuid", new Class<?>[0])));
    Assert.assertEquals(
        Long.valueOf(existingDLFileEntry.getGroupId()),
        ReflectionTestUtil.<Long>invoke(
            existingDLFileEntry, "getOriginalGroupId", new Class<?>[0]));

    Assert.assertEquals(
        Long.valueOf(existingDLFileEntry.getGroupId()),
        ReflectionTestUtil.<Long>invoke(
            existingDLFileEntry, "getOriginalGroupId", new Class<?>[0]));
    Assert.assertEquals(
        Long.valueOf(existingDLFileEntry.getFolderId()),
        ReflectionTestUtil.<Long>invoke(
            existingDLFileEntry, "getOriginalFolderId", new Class<?>[0]));
    Assert.assertTrue(
        Objects.equals(
            existingDLFileEntry.getName(),
            ReflectionTestUtil.invoke(existingDLFileEntry, "getOriginalName", new Class<?>[0])));

    Assert.assertEquals(
        Long.valueOf(existingDLFileEntry.getGroupId()),
        ReflectionTestUtil.<Long>invoke(
            existingDLFileEntry, "getOriginalGroupId", new Class<?>[0]));
    Assert.assertEquals(
        Long.valueOf(existingDLFileEntry.getFolderId()),
        ReflectionTestUtil.<Long>invoke(
            existingDLFileEntry, "getOriginalFolderId", new Class<?>[0]));
    Assert.assertTrue(
        Objects.equals(
            existingDLFileEntry.getFileName(),
            ReflectionTestUtil.invoke(
                existingDLFileEntry, "getOriginalFileName", new Class<?>[0])));

    Assert.assertEquals(
        Long.valueOf(existingDLFileEntry.getGroupId()),
        ReflectionTestUtil.<Long>invoke(
            existingDLFileEntry, "getOriginalGroupId", new Class<?>[0]));
    Assert.assertEquals(
        Long.valueOf(existingDLFileEntry.getFolderId()),
        ReflectionTestUtil.<Long>invoke(
            existingDLFileEntry, "getOriginalFolderId", new Class<?>[0]));
    Assert.assertTrue(
        Objects.equals(
            existingDLFileEntry.getTitle(),
            ReflectionTestUtil.invoke(existingDLFileEntry, "getOriginalTitle", new Class<?>[0])));
  }
  protected DLFileEntry addDLFileEntry() throws Exception {
    long pk = RandomTestUtil.nextLong();

    DLFileEntry dlFileEntry = _persistence.create(pk);

    dlFileEntry.setUuid(RandomTestUtil.randomString());

    dlFileEntry.setGroupId(RandomTestUtil.nextLong());

    dlFileEntry.setCompanyId(RandomTestUtil.nextLong());

    dlFileEntry.setUserId(RandomTestUtil.nextLong());

    dlFileEntry.setUserName(RandomTestUtil.randomString());

    dlFileEntry.setCreateDate(RandomTestUtil.nextDate());

    dlFileEntry.setModifiedDate(RandomTestUtil.nextDate());

    dlFileEntry.setClassNameId(RandomTestUtil.nextLong());

    dlFileEntry.setClassPK(RandomTestUtil.nextLong());

    dlFileEntry.setRepositoryId(RandomTestUtil.nextLong());

    dlFileEntry.setFolderId(RandomTestUtil.nextLong());

    dlFileEntry.setTreePath(RandomTestUtil.randomString());

    dlFileEntry.setName(RandomTestUtil.randomString());

    dlFileEntry.setFileName(RandomTestUtil.randomString());

    dlFileEntry.setExtension(RandomTestUtil.randomString());

    dlFileEntry.setMimeType(RandomTestUtil.randomString());

    dlFileEntry.setTitle(RandomTestUtil.randomString());

    dlFileEntry.setDescription(RandomTestUtil.randomString());

    dlFileEntry.setExtraSettings(RandomTestUtil.randomString());

    dlFileEntry.setFileEntryTypeId(RandomTestUtil.nextLong());

    dlFileEntry.setVersion(RandomTestUtil.randomString());

    dlFileEntry.setSize(RandomTestUtil.nextLong());

    dlFileEntry.setReadCount(RandomTestUtil.nextInt());

    dlFileEntry.setSmallImageId(RandomTestUtil.nextLong());

    dlFileEntry.setLargeImageId(RandomTestUtil.nextLong());

    dlFileEntry.setCustom1ImageId(RandomTestUtil.nextLong());

    dlFileEntry.setCustom2ImageId(RandomTestUtil.nextLong());

    dlFileEntry.setManualCheckInRequired(RandomTestUtil.randomBoolean());

    dlFileEntry.setLastPublishDate(RandomTestUtil.nextDate());

    _dlFileEntries.add(_persistence.update(dlFileEntry));

    return dlFileEntry;
  }
  @Override
  public SyncDLObject addSyncDLObject(
      long companyId,
      long userId,
      String userName,
      long modifiedTime,
      long repositoryId,
      long parentFolderId,
      String treePath,
      String name,
      String extension,
      String mimeType,
      String description,
      String changeLog,
      String extraSettings,
      String version,
      long versionId,
      long size,
      String checksum,
      String event,
      Date lockExpirationDate,
      long lockUserId,
      String lockUserName,
      String type,
      long typePK,
      String typeUuid)
      throws PortalException {

    if (!isDefaultRepository(parentFolderId)) {
      return null;
    }

    SyncDLObject syncDLObject = syncDLObjectPersistence.fetchByT_T(type, typePK);

    if (syncDLObject == null) {
      long syncDLObjectId = counterLocalService.increment();

      syncDLObject = syncDLObjectPersistence.create(syncDLObjectId);

      syncDLObject.setCompanyId(companyId);
      syncDLObject.setCreateTime(modifiedTime);
      syncDLObject.setRepositoryId(repositoryId);
      syncDLObject.setType(type);
      syncDLObject.setTypePK(typePK);
      syncDLObject.setTypeUuid(typeUuid);

      if (type.equals(SyncDLObjectConstants.TYPE_PRIVATE_WORKING_COPY)) {
        SyncDLObject approvedSyncDLObject =
            syncDLObjectPersistence.fetchByT_T(SyncDLObjectConstants.TYPE_FILE, typePK);

        approvedSyncDLObject.setModifiedTime(modifiedTime);
        approvedSyncDLObject.setLockExpirationDate(lockExpirationDate);
        approvedSyncDLObject.setLockUserId(lockUserId);
        approvedSyncDLObject.setLockUserName(lockUserName);

        syncDLObjectPersistence.update(approvedSyncDLObject);
      }
    } else if (syncDLObject.getModifiedTime() >= modifiedTime) {
      return null;
    } else if (type.equals(SyncDLObjectConstants.TYPE_FILE)) {
      SyncDLObject pwcSyncDLObject =
          syncDLObjectPersistence.fetchByT_T(
              SyncDLObjectConstants.TYPE_PRIVATE_WORKING_COPY, typePK);

      if (pwcSyncDLObject != null) {
        DLFileEntry dlFileEntry = dlFileEntryLocalService.fetchDLFileEntry(typePK);

        if ((dlFileEntry == null) || !dlFileEntry.isCheckedOut()) {
          syncDLObjectPersistence.remove(pwcSyncDLObject);
        }
      }
    } else if (type.equals(SyncDLObjectConstants.TYPE_PRIVATE_WORKING_COPY)) {
      if (event.equals(SyncDLObjectConstants.EVENT_RESTORE)
          || event.equals(SyncDLObjectConstants.EVENT_TRASH)) {

        SyncDLObject approvedSyncDLObject =
            syncDLObjectPersistence.fetchByT_T(SyncDLObjectConstants.TYPE_FILE, typePK);

        approvedSyncDLObject.setEvent(event);

        syncDLObjectPersistence.update(approvedSyncDLObject);
      }
    }

    syncDLObject.setUserId(userId);
    syncDLObject.setUserName(userName);
    syncDLObject.setModifiedTime(modifiedTime);
    syncDLObject.setParentFolderId(parentFolderId);
    syncDLObject.setTreePath(treePath);
    syncDLObject.setName(name);
    syncDLObject.setExtension(extension);
    syncDLObject.setMimeType(mimeType);
    syncDLObject.setDescription(description);
    syncDLObject.setChangeLog(changeLog);
    syncDLObject.setVersion(version);
    syncDLObject.setVersionId(versionId);
    syncDLObject.setSize(size);
    syncDLObject.setChecksum(checksum);
    syncDLObject.setEvent(event);
    syncDLObject.setLockExpirationDate(lockExpirationDate);
    syncDLObject.setLockUserId(lockUserId);
    syncDLObject.setLockUserName(lockUserName);

    syncDLObject = syncDLObjectPersistence.update(syncDLObject);

    if (type.equals(SyncDLObjectConstants.TYPE_FILE)
        && ArrayUtil.contains(
            PortletPropsValues.SYNC_MAC_PACKAGE_METADATA_FILE_NAMES, syncDLObject.getName())) {

      SyncDLObject parentFolderSyncDLObject =
          syncDLObjectPersistence.fetchByT_T(
              SyncDLObjectConstants.TYPE_FOLDER, syncDLObject.getParentFolderId());

      String parentFolderExtension = FileUtil.getExtension(parentFolderSyncDLObject.getName());

      if (ArrayUtil.contains(
          PortletPropsValues.SYNC_MAC_PACKAGE_FOLDER_EXTENSIONS, parentFolderExtension)) {

        JSONObject extraSettingsJSONObject =
            JSONFactoryUtil.createJSONObject(parentFolderSyncDLObject.getExtraSettings());

        extraSettingsJSONObject.put("macPackage", true);

        parentFolderSyncDLObject.setExtraSettings(extraSettingsJSONObject.toString());

        syncDLObjectPersistence.update(parentFolderSyncDLObject);
      }
    }

    if (type.equals(SyncDLObjectConstants.TYPE_FOLDER)) {
      if (Validator.isNull(treePath)) {
        return syncDLObject;
      }

      if (event.equals(SyncDLObjectConstants.EVENT_MOVE)) {
        moveSyncDLObjects(syncDLObject);
      } else if (event.equals(SyncDLObjectConstants.EVENT_RESTORE)) {
        restoreSyncDLObjects(syncDLObject);
      } else if (event.equals(SyncDLObjectConstants.EVENT_TRASH)) {
        trashSyncDLObjects(syncDLObject);
      }
    } else if (event.equals(SyncDLObjectConstants.EVENT_DELETE)) {
      try {
        syncDLFileVersionDiffLocalService.deleteSyncDLFileVersionDiffs(typePK);
      } catch (Exception e) {
        _log.error(e, e);
      }
    }

    return syncDLObject;
  }
  protected void testMigrateAndCheckOldRepositoryFiles(Boolean delete) throws Exception {

    _convertProcess.setParameterValues(new String[] {_CLASS_NAME_DB_STORE, delete.toString()});

    FileEntry rootFileEntry =
        addFileEntry(
            DLFolderConstants.DEFAULT_PARENT_FOLDER_ID,
            RandomTestUtil.randomString() + ".txt",
            ContentTypes.TEXT_PLAIN,
            RandomTestUtil.randomBytes(TikaSafeRandomizerBumper.INSTANCE));

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

    Folder folder =
        DLAppServiceUtil.addFolder(
            _group.getGroupId(),
            DLFolderConstants.DEFAULT_PARENT_FOLDER_ID,
            RandomTestUtil.randomString(),
            RandomTestUtil.randomString(),
            serviceContext);

    FileEntry folderFileEntry =
        addFileEntry(
            folder.getFolderId(),
            "liferay.jpg",
            ContentTypes.IMAGE_JPEG,
            FileUtil.getBytes(getClass(), "dependencies/liferay.jpg"));

    ImageProcessorUtil.generateImages(null, folderFileEntry.getFileVersion());

    _convertProcess.convert();

    DLFileEntry rootDLFileEntry = (DLFileEntry) rootFileEntry.getModel();

    Assert.assertNotEquals(
        delete,
        _sourceStore.hasFile(
            rootDLFileEntry.getCompanyId(),
            rootDLFileEntry.getDataRepositoryId(),
            rootDLFileEntry.getName()));

    DLFileEntry folderDLFileEntry = (DLFileEntry) folderFileEntry.getModel();

    Assert.assertNotEquals(
        delete,
        _sourceStore.hasDirectory(
            folderDLFileEntry.getCompanyId(),
            DLPreviewableProcessor.REPOSITORY_ID,
            DLPreviewableProcessor.THUMBNAIL_PATH));
    Assert.assertNotEquals(
        delete,
        _sourceStore.hasFile(
            folderDLFileEntry.getCompanyId(),
            folderDLFileEntry.getDataRepositoryId(),
            folderDLFileEntry.getName()));

    DLContentLocalServiceUtil.getContent(
        folderDLFileEntry.getCompanyId(),
        DLFolderConstants.getDataRepositoryId(
            folderDLFileEntry.getRepositoryId(), folderDLFileEntry.getFolderId()),
        folderDLFileEntry.getName());

    DLContentLocalServiceUtil.getContent(
        rootDLFileEntry.getCompanyId(),
        DLFolderConstants.getDataRepositoryId(
            rootDLFileEntry.getRepositoryId(), rootDLFileEntry.getFolderId()),
        rootDLFileEntry.getName());
  }
  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};
  }