public void tearDown() throws Exception {
    if (_fileEntry != null) {
      DLAppServiceUtil.deleteFileEntry(_fileEntry.getFileEntryId());
    }

    if (_folder != null) {
      DLAppServiceUtil.deleteFolder(_folder.getFolderId());
    }

    super.tearDown();
  }
  @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));
    }
  }
  protected FileEntry addFileEntry(
      long folderId, String sourceFileName, String title, byte[] bytes, int workflowAction)
      throws Exception {

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

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

    ServiceContext serviceContext = new ServiceContext();

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

    return DLAppServiceUtil.addFileEntry(
        TestPropsValues.getGroupId(),
        folderId,
        sourceFileName,
        ContentTypes.TEXT_PLAIN,
        title,
        description,
        changeLog,
        bytes,
        serviceContext);
  }
  protected void addFileEntry(boolean rootFolder) throws PortalException, SystemException {

    long folderId = DLFolderConstants.DEFAULT_PARENT_FOLDER_ID;

    if (!rootFolder) {
      folderId = _folder.getFolderId();
    }

    String fileName = "Title.txt";
    String description = StringPool.BLANK;
    String changeLog = StringPool.BLANK;

    String content = "Content: Enterprise. Open Source. For Life.";

    byte[] bytes = content.getBytes();

    ServiceContext serviceContext = new ServiceContext();

    serviceContext.setAddCommunityPermissions(true);
    serviceContext.setAddGuestPermissions(true);

    _fileEntry =
        DLAppServiceUtil.addFileEntry(
            _groupId, folderId, fileName, description, changeLog, bytes, serviceContext);
  }
  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);
  }
  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());
  }
  protected void revertFileVersion(String version, String fileName) throws PortalException {

    DLAppServiceUtil.revertFileEntry(_fileEntry.getFileEntryId(), version, new ServiceContext());

    if (fileName != null) {
      assertLatestFileVersionTitle(fileName);
    }
  }
  @After
  @Override
  public void tearDown() throws Exception {
    if (_fileEntry != null) {
      DLAppServiceUtil.deleteFileEntry(_fileEntry.getFileEntryId());
    }

    super.tearDown();
  }
  protected void searchFile(boolean rootFolder) throws Exception {
    FileEntry fileEntry = addFileEntry(rootFolder);

    Thread.sleep(1000 * TestPropsValues.JUNIT_DELAY_FACTOR);

    search(fileEntry, rootFolder, "title", true);
    search(fileEntry, rootFolder, "content", true);

    DLAppServiceUtil.deleteFileEntry(fileEntry.getFileEntryId());
  }
  protected DLFileShortcut addDLFileShortcut(FileEntry fileEntry, long folderId) throws Exception {

    ServiceContext serviceContext = new ServiceContext();

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

    return DLAppServiceUtil.addFileShortcut(
        TestPropsValues.getGroupId(), folderId, fileEntry.getFileEntryId(), serviceContext);
  }
  protected Folder addFolder(long parentFolderId, String name, boolean deleteExisting)
      throws Exception {

    String description = StringPool.BLANK;

    ServiceContext serviceContext = new ServiceContext();

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

    if (deleteExisting) {
      try {
        DLAppServiceUtil.deleteFolder(TestPropsValues.getGroupId(), parentFolderId, name);
      } catch (NoSuchFolderException nsfe) {
      }
    }

    return DLAppServiceUtil.addFolder(
        TestPropsValues.getGroupId(), parentFolderId, name, description, serviceContext);
  }
  protected void deleteFileVersion(String version, String fileName, boolean pwc)
      throws PortalException {

    DLAppServiceUtil.deleteFileVersion(_fileEntry.getFileEntryId(), version);

    if (fileName != null) {
      if (pwc) {
        assertLatestFileVersionTitle(fileName);
      } else {
        assertFileEntryTitle(fileName);
      }
    }
  }
  @Before
  @Override
  public void setUp() throws Exception {
    super.setUp();

    SimpleAction simpleAction = new AddDefaultDocumentLibraryStructuresAction();

    String companyIdString = String.valueOf(TestPropsValues.getCompanyId());

    simpleAction.run(new String[] {companyIdString});

    List<DLFileEntryType> dlFileEntryTypes =
        DLFileEntryTypeLocalServiceUtil.getFileEntryTypes(
            PortalUtil.getCurrentAndAncestorSiteGroupIds(group.getGroupId()));

    for (DLFileEntryType dlFileEntryType : dlFileEntryTypes) {
      String name = dlFileEntryType.getName(LocaleUtil.getSiteDefault());

      if (name.equals(DLFileEntryTypeConstants.NAME_CONTRACT)) {
        _contractDLFileEntryTypeId = dlFileEntryType.getFileEntryTypeId();
      }
    }

    ExpandoTable expandoTable =
        ExpandoTableLocalServiceUtil.addDefaultTable(
            PortalUtil.getDefaultCompanyId(), DLFileEntry.class.getName());

    ExpandoColumnLocalServiceUtil.addColumn(
        expandoTable.getTableId(),
        _EXPANDO_ATTRIBUTE_NAME,
        ExpandoColumnConstants.STRING,
        StringPool.BLANK);

    _serviceContext = getServiceContext();

    FileEntry fileEntry =
        DLAppServiceUtil.addFileEntry(
            group.getGroupId(),
            parentFolder.getFolderId(),
            _SOURCE_FILE_NAME,
            ContentTypes.APPLICATION_OCTET_STREAM,
            _TITLE,
            StringPool.BLANK,
            StringPool.BLANK,
            _DATA_VERSION_1,
            _serviceContext);

    _fileVersion =
        DLFileVersionLocalServiceUtil.getFileVersion(
            fileEntry.getFileEntryId(), DLFileEntryConstants.VERSION_DEFAULT);
  }
  public void setUp() throws Exception {
    super.setUp();

    String name = "Test Folder";
    String description = "This is a test folder.";

    ServiceContext serviceContext = new ServiceContext();

    serviceContext.setAddCommunityPermissions(true);
    serviceContext.setAddGuestPermissions(true);

    try {
      DLAppServiceUtil.deleteFolder(_groupId, DLFolderConstants.DEFAULT_PARENT_FOLDER_ID, name);
    } catch (NoSuchFolderException nsfe) {
    }

    _folder =
        DLAppServiceUtil.addFolder(
            _groupId,
            DLFolderConstants.DEFAULT_PARENT_FOLDER_ID,
            name,
            description,
            serviceContext);
  }
  @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());
  }
    @Override
    protected void doRun() throws Exception {
      try {
        FileEntry fileEntry = DLAppServiceUtil.getFileEntry(_fileEntryIds[_index]);

        InputStream is = fileEntry.getContentStream();

        String content = StringUtil.read(is);

        if (CONTENT.equals(content)) {
          _log.debug("Retrieved file " + _index);

          _success = true;
        }
      } catch (Exception e) {
        _log.error("Unable to add file " + _index, e);
      }
    }
  @Test
  public void testAddFileEntryWithDuplicateName() throws Exception {
    addFileEntry(false);

    try {
      addFileEntry(false);

      Assert.fail("Able to add two files of the same name");
    } catch (DuplicateFileException dfe) {
    }

    try {
      addFileEntry(true);

      DLAppServiceUtil.deleteFileEntry(_fileEntry.getFileEntryId());
    } catch (DuplicateFileException dfe) {
      Assert.fail("Unable to add two files of the same name in different " + "folders");
    }

    _fileEntry = null;
  }
  @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());
  }
  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());
    }
  }
  @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 assertFileEntryTitle(String fileName) throws PortalException {

    FileEntry fileEntry = DLAppServiceUtil.getFileEntry(_fileEntry.getFileEntryId());

    Assert.assertEquals(fileName, fileEntry.getTitle());
  }
  @Test
  public void testFileEntryTypeRestrictions() throws Exception {

    // Configure folder

    DLFolderLocalServiceUtil.updateFolder(
        _folder.getFolderId(),
        _folder.getParentFolderId(),
        _folder.getName(),
        _folder.getDescription(),
        _contractDLFileEntryType.getPrimaryKey(),
        ListUtil.toList(
            new long[] {
              _contractDLFileEntryType.getPrimaryKey(),
              _marketingBannerDLFileEntryType.getPrimaryKey()
            }),
        true,
        ServiceContextTestUtil.getServiceContext(_group.getGroupId()));

    // Add file to folder

    String name = "Test.txt";
    byte[] bytes = _CONTENT.getBytes();

    FileEntry fileEntry =
        DLAppServiceUtil.addFileEntry(
            _group.getGroupId(),
            _folder.getFolderId(),
            name,
            ContentTypes.TEXT_PLAIN,
            name,
            "",
            "",
            bytes,
            ServiceContextTestUtil.getServiceContext(_group.getGroupId()));

    assertFileEntryType(fileEntry, _contractDLFileEntryType);

    // Add file to subfolder

    fileEntry =
        DLAppServiceUtil.addFileEntry(
            _group.getGroupId(),
            _subfolder.getFolderId(),
            name,
            ContentTypes.TEXT_PLAIN,
            name,
            "",
            "",
            bytes,
            ServiceContextTestUtil.getServiceContext(_group.getGroupId()));

    assertFileEntryType(fileEntry, _contractDLFileEntryType);

    // Configure subfolder

    DLFolderLocalServiceUtil.updateFolder(
        _subfolder.getFolderId(),
        _subfolder.getParentFolderId(),
        _subfolder.getName(),
        _subfolder.getDescription(),
        _basicDocumentDLFileEntryType.getPrimaryKey(),
        ListUtil.toList(new long[] {_basicDocumentDLFileEntryType.getPrimaryKey()}),
        true,
        ServiceContextTestUtil.getServiceContext(_group.getGroupId()));

    fileEntry = DLAppServiceUtil.getFileEntry(fileEntry.getFileEntryId());

    assertFileEntryType(fileEntry, _basicDocumentDLFileEntryType);
  }
 @After
 public void tearDown() throws Exception {
   if (parentFolder != null) {
     DLAppServiceUtil.deleteFolder(parentFolder.getFolderId());
   }
 }