protected FileEntry updateFileEntry(
      long fileEntryId, String sourceFileName, String title, boolean majorVersion)
      throws Exception {

    String description = StringPool.BLANK;
    String changeLog = StringPool.BLANK;

    byte[] bytes = null;

    if (Validator.isNotNull(sourceFileName)) {
      bytes = CONTENT.getBytes();
    }

    ServiceContext serviceContext = new ServiceContext();

    serviceContext.setAddGroupPermissions(true);
    serviceContext.setAddGuestPermissions(true);

    return DLAppServiceUtil.updateFileEntry(
        fileEntryId,
        sourceFileName,
        ContentTypes.TEXT_PLAIN,
        title,
        description,
        changeLog,
        majorVersion,
        bytes,
        serviceContext);
  }
  @Test
  public void testAddFileEntryWithInvalidMimeType() throws Exception {
    long folderId = parentFolder.getFolderId();
    String description = StringPool.BLANK;
    String changeLog = StringPool.BLANK;

    ServiceContext serviceContext = new ServiceContext();

    serviceContext.setAddGroupPermissions(true);
    serviceContext.setAddGuestPermissions(true);
    serviceContext.setScopeGroupId(group.getGroupId());

    try {
      String name = "InvalidMime.txt";
      byte[] bytes = CONTENT.getBytes();

      FileEntry fileEntry =
          DLAppServiceUtil.addFileEntry(
              group.getGroupId(),
              folderId,
              name,
              ContentTypes.APPLICATION_OCTET_STREAM,
              name,
              description,
              changeLog,
              bytes,
              serviceContext);

      Assert.assertEquals(ContentTypes.TEXT_PLAIN, fileEntry.getMimeType());

      name = "InvalidMime";

      fileEntry =
          DLAppServiceUtil.updateFileEntry(
              fileEntry.getFileEntryId(),
              name,
              null,
              name,
              description,
              changeLog,
              true,
              bytes,
              serviceContext);

      Assert.assertEquals(ContentTypes.TEXT_PLAIN, fileEntry.getMimeType());
    } catch (Exception e) {
      Assert.fail("Unable to add file with invalid mime type " + StackTraceUtil.getStackTrace(e));
    }
  }
  @Test
  public void testUpdateTitle() throws Exception {
    FileEntry fileEntry =
        DLAppServiceUtil.updateFileEntry(
            _fileVersion.getFileEntryId(),
            _SOURCE_FILE_NAME,
            _fileVersion.getMimeType(),
            _UPDATE_VALUE,
            _fileVersion.getDescription(),
            _fileVersion.getChangeLog(),
            false,
            _DATA_VERSION_1,
            _serviceContext);

    Assert.assertNotEquals(DLFileEntryConstants.VERSION_DEFAULT, fileEntry.getVersion());
  }
  protected void testVersionUpdate(
      String addFileName,
      byte[] addBytes,
      String addMimeType,
      String updateFileName,
      byte[] updateBytes,
      String updateMimeType)
      throws PortalException, SystemException {

    String description = StringPool.BLANK;
    String changeLog = StringPool.BLANK;

    ServiceContext serviceContext = ServiceTestUtil.getServiceContext(group.getGroupId());

    FileEntry fileEntry =
        DLAppServiceUtil.addFileEntry(
            group.getGroupId(),
            parentFolder.getFolderId(),
            addFileName,
            addMimeType,
            addFileName,
            description,
            changeLog,
            addBytes,
            serviceContext);

    fileEntry =
        DLAppServiceUtil.updateFileEntry(
            fileEntry.getFileEntryId(),
            updateFileName,
            updateMimeType,
            updateFileName,
            description,
            changeLog,
            false,
            updateBytes,
            serviceContext);

    FileVersion fileVersion = fileEntry.getFileVersion();

    Assert.assertEquals(DLFileEntryConstants.VERSION_DEFAULT, fileVersion.getVersion());
    Assert.assertEquals(updateMimeType, fileVersion.getMimeType());
    Assert.assertEquals(updateBytes.length, fileVersion.getSize());
    Assert.assertEquals(fileVersion.getExtension(), fileEntry.getExtension());
    Assert.assertEquals(fileVersion.getMimeType(), fileEntry.getMimeType());
    Assert.assertEquals(fileVersion.getSize(), fileEntry.getSize());
  }
  @Test
  public void testUpdateFileEntryType() throws Exception {
    updateServiceContext(
        StringPool.BLANK,
        DLFileEntryTypeConstants.FILE_ENTRY_TYPE_ID_BASIC_DOCUMENT,
        StringPool.BLANK);

    FileEntry fileEntry =
        DLAppServiceUtil.updateFileEntry(
            _fileVersion.getFileEntryId(),
            _SOURCE_FILE_NAME,
            _fileVersion.getMimeType(),
            _fileVersion.getTitle(),
            _fileVersion.getDescription(),
            _fileVersion.getChangeLog(),
            false,
            _DATA_VERSION_1,
            _serviceContext);

    Assert.assertNotEquals(DLFileEntryConstants.VERSION_DEFAULT, fileEntry.getVersion());
  }
  @Test
  public void testAsstTags() throws Exception {
    long folderId = parentFolder.getFolderId();
    String name = "TestTags.txt";
    String description = StringPool.BLANK;
    String changeLog = StringPool.BLANK;
    byte[] bytes = CONTENT.getBytes();

    ServiceContext serviceContext = new ServiceContext();

    serviceContext.setAddGroupPermissions(true);
    serviceContext.setAddGuestPermissions(true);
    serviceContext.setScopeGroupId(group.getGroupId());

    String[] assetTagNames = new String[] {"hello", "world"};

    serviceContext.setAssetTagNames(assetTagNames);

    FileEntry fileEntry =
        DLAppServiceUtil.addFileEntry(
            group.getGroupId(),
            folderId,
            name,
            ContentTypes.TEXT_PLAIN,
            name,
            description,
            changeLog,
            bytes,
            serviceContext);

    AssetEntry assetEntry =
        AssetEntryLocalServiceUtil.fetchEntry(
            DLFileEntryConstants.getClassName(), fileEntry.getFileEntryId());

    AssertUtils.assertEqualsSorted(assetTagNames, assetEntry.getTagNames());

    Thread.sleep(1000 * TestPropsValues.JUNIT_DELAY_FACTOR);

    _fileEntry = fileEntry;

    search(_fileEntry, false, "hello", true);
    search(_fileEntry, false, "world", true);
    search(_fileEntry, false, "liferay", false);

    assetTagNames = new String[] {"hello", "world", "liferay"};

    serviceContext.setAssetTagNames(assetTagNames);

    fileEntry =
        DLAppServiceUtil.updateFileEntry(
            fileEntry.getFileEntryId(),
            name,
            ContentTypes.TEXT_PLAIN,
            name,
            description,
            changeLog,
            false,
            bytes,
            serviceContext);

    assetEntry =
        AssetEntryLocalServiceUtil.fetchEntry(
            DLFileEntryConstants.getClassName(), fileEntry.getFileEntryId());

    AssertUtils.assertEqualsSorted(assetTagNames, assetEntry.getTagNames());

    Thread.sleep(1000 * TestPropsValues.JUNIT_DELAY_FACTOR);

    _fileEntry = fileEntry;

    search(_fileEntry, false, "hello", true);
    search(_fileEntry, false, "world", true);
    search(_fileEntry, false, "liferay", true);

    DLAppServiceUtil.deleteFileEntry(_fileEntry.getFileEntryId());

    _fileEntry = null;
  }
  @Test
  public void testAddNullFileEntry() throws Exception {
    long folderId = parentFolder.getFolderId();
    String description = StringPool.BLANK;
    String changeLog = StringPool.BLANK;

    ServiceContext serviceContext = new ServiceContext();

    serviceContext.setAddGroupPermissions(true);
    serviceContext.setAddGuestPermissions(true);
    serviceContext.setScopeGroupId(group.getGroupId());

    try {
      String name = "Bytes-null.txt";
      byte[] bytes = null;

      FileEntry fileEntry =
          DLAppServiceUtil.addFileEntry(
              group.getGroupId(),
              folderId,
              name,
              ContentTypes.TEXT_PLAIN,
              name,
              description,
              changeLog,
              bytes,
              serviceContext);

      DLAppServiceUtil.updateFileEntry(
          fileEntry.getFileEntryId(),
          name,
          ContentTypes.TEXT_PLAIN,
          name,
          description,
          changeLog,
          true,
          bytes,
          serviceContext);

      String newName = "Bytes-changed.txt";

      bytes = CONTENT.getBytes();

      DLAppServiceUtil.updateFileEntry(
          fileEntry.getFileEntryId(),
          newName,
          ContentTypes.TEXT_PLAIN,
          newName,
          description,
          changeLog,
          true,
          bytes,
          serviceContext);
    } catch (Exception e) {
      Assert.fail("Unable to pass null byte[] " + StackTraceUtil.getStackTrace(e));
    }

    try {
      String name = "File-null.txt";
      File file = null;

      FileEntry fileEntry =
          DLAppServiceUtil.addFileEntry(
              group.getGroupId(),
              folderId,
              name,
              ContentTypes.TEXT_PLAIN,
              name,
              description,
              changeLog,
              file,
              serviceContext);

      DLAppServiceUtil.updateFileEntry(
          fileEntry.getFileEntryId(),
          name,
          ContentTypes.TEXT_PLAIN,
          name,
          description,
          changeLog,
          true,
          file,
          serviceContext);

      try {
        String newName = "File-changed.txt";

        file = FileUtil.createTempFile(CONTENT.getBytes());

        DLAppServiceUtil.updateFileEntry(
            fileEntry.getFileEntryId(),
            newName,
            ContentTypes.TEXT_PLAIN,
            newName,
            description,
            changeLog,
            true,
            file,
            serviceContext);
      } finally {
        FileUtil.delete(file);
      }
    } catch (Exception e) {
      Assert.fail("Unable to pass null File " + StackTraceUtil.getStackTrace(e));
    }

    try {
      String name = "IS-null.txt";
      InputStream is = null;

      FileEntry fileEntry =
          DLAppServiceUtil.addFileEntry(
              group.getGroupId(),
              folderId,
              name,
              ContentTypes.TEXT_PLAIN,
              name,
              description,
              changeLog,
              is,
              0,
              serviceContext);

      DLAppServiceUtil.updateFileEntry(
          fileEntry.getFileEntryId(),
          name,
          ContentTypes.TEXT_PLAIN,
          name,
          description,
          changeLog,
          true,
          is,
          0,
          serviceContext);

      try {
        String newName = "IS-changed.txt";

        is = new ByteArrayInputStream(CONTENT.getBytes());

        DLAppServiceUtil.updateFileEntry(
            fileEntry.getFileEntryId(),
            newName,
            ContentTypes.TEXT_PLAIN,
            newName,
            description,
            changeLog,
            true,
            is,
            0,
            serviceContext);
      } finally {
        if (is != null) {
          is.close();
        }
      }
    } catch (Exception e) {
      Assert.fail("Unable to pass null InputStream " + StackTraceUtil.getStackTrace(e));
    }
  }
  protected void revertVersion(boolean versioned, boolean leaveCheckedOut) throws Exception {

    _fileEntry = addFileEntry(parentFolder.getFolderId(), _VERSION_1_0);

    long fileEntryId = _fileEntry.getFileEntryId();

    if (versioned) {
      ServiceContext serviceContext =
          ServiceContextTestUtil.getServiceContext(group.getGroupId(), TestPropsValues.getUserId());

      DLAppServiceUtil.updateFileEntry(
          fileEntryId,
          null,
          ContentTypes.TEXT_PLAIN,
          _VERSION_1_1,
          StringPool.BLANK,
          StringPool.BLANK,
          false,
          (byte[]) null,
          serviceContext);
    }

    if (leaveCheckedOut) {
      DLAppServiceUtil.checkOutFileEntry(fileEntryId, new ServiceContext());

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

      DLAppServiceUtil.updateFileEntry(
          fileEntryId,
          null,
          ContentTypes.TEXT_PLAIN,
          _VERSION_PWC,
          StringPool.BLANK,
          StringPool.BLANK,
          false,
          (byte[]) null,
          serviceContext);
    }

    if (versioned && leaveCheckedOut) {
      Assert.assertEquals(3, getFileVersionsCount());

      failRevertFileVersion("PWC");
      revertFileVersion("1.1", _VERSION_1_1);
      revertFileVersion("1.0", _VERSION_1_0);

      Assert.assertEquals(3, getFileVersionsCount());
    } else if (versioned) {
      Assert.assertEquals(2, getFileVersionsCount());

      failRevertFileVersion("1.1");
      revertFileVersion("1.0", _VERSION_1_0);

      Assert.assertEquals(3, getFileVersionsCount());
    } else if (leaveCheckedOut) {
      Assert.assertEquals(2, getFileVersionsCount());

      failRevertFileVersion("PWC");
      revertFileVersion("1.0", _VERSION_1_0);

      Assert.assertEquals(2, getFileVersionsCount());
    } else {
      Assert.assertEquals(1, getFileVersionsCount());

      failRevertFileVersion("1.0");

      Assert.assertEquals(1, getFileVersionsCount());
    }
  }