示例#1
0
  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 testGetFieldsFromContentWithDocumentLibraryElement() throws Exception {

    Fields expectedFields = new Fields();

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

    FileEntry fileEntry =
        DLAppLocalServiceUtil.addFileEntry(
            TestPropsValues.getUserId(),
            group.getGroupId(),
            DLFolderConstants.DEFAULT_PARENT_FOLDER_ID,
            "Test 1.txt",
            ContentTypes.TEXT_PLAIN,
            RandomTestUtil.randomBytes(),
            serviceContext);

    Field documentLibraryField = getDocumentLibraryField(fileEntry, _ddmStructure.getStructureId());

    expectedFields.put(documentLibraryField);

    Field fieldsDisplayField =
        getFieldsDisplayField(_ddmStructure.getStructureId(), "document_library_INSTANCE_4aGOvP3N");

    expectedFields.put(fieldsDisplayField);

    String content = read("test-journal-content-doc-library-field.xml");

    XPath xPathSelector = SAXReaderUtil.createXPath("//dynamic-content");

    Document document = SAXReaderUtil.read(content);

    Element element = (Element) xPathSelector.selectSingleNode(document);

    String[] previewURLs = new String[2];

    previewURLs[0] =
        DLUtil.getPreviewURL(
            fileEntry, fileEntry.getFileVersion(), null, StringPool.BLANK, true, true);
    previewURLs[1] =
        DLUtil.getPreviewURL(
            fileEntry, fileEntry.getFileVersion(), null, StringPool.BLANK, false, false);

    for (int i = 0; i < previewURLs.length; i++) {
      element.addCDATA(previewURLs[i]);

      Fields actualFields = JournalConverterUtil.getDDMFields(_ddmStructure, document.asXML());

      Assert.assertEquals(expectedFields, actualFields);
    }
  }
  protected FileEntry addFileEntry(long folderId, String sourceFileName) throws Exception {

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

    return DLAppLocalServiceUtil.addFileEntry(
        TestPropsValues.getUserId(),
        group.getGroupId(),
        folderId,
        sourceFileName,
        ContentTypes.TEXT_PLAIN,
        RandomTestUtil.randomBytes(TikaSafeRandomizerBumper.INSTANCE),
        serviceContext);
  }
  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 testRequestFileInTrash() throws Exception {
    ServiceContext serviceContext =
        ServiceContextTestUtil.getServiceContext(group.getGroupId(), TestPropsValues.getUserId());

    FileEntry fileEntry =
        DLAppLocalServiceUtil.addFileEntry(
            TestPropsValues.getUserId(),
            group.getGroupId(),
            parentFolder.getFolderId(),
            "Test Trash.txt",
            ContentTypes.TEXT_PLAIN,
            RandomTestUtil.randomBytes(),
            serviceContext);

    MockHttpServletResponse mockHttpServletResponse = testRequestFile(fileEntry, _user, false);

    Assert.assertEquals(MockHttpServletResponse.SC_OK, mockHttpServletResponse.getStatus());

    DLAppServiceUtil.moveFileEntryToTrash(fileEntry.getFileEntryId());

    mockHttpServletResponse = testRequestFile(fileEntry, _user, false);

    Assert.assertEquals(MockHttpServletResponse.SC_NOT_FOUND, mockHttpServletResponse.getStatus());

    mockHttpServletResponse = testRequestFile(fileEntry, _user, true);

    Assert.assertEquals(
        MockHttpServletResponse.SC_UNAUTHORIZED, mockHttpServletResponse.getStatus());

    RoleLocalServiceUtil.addUserRoles(_user.getUserId(), new long[] {_role.getRoleId()});

    mockHttpServletResponse = testRequestFile(fileEntry, _user, true);

    Assert.assertEquals(MockHttpServletResponse.SC_OK, mockHttpServletResponse.getStatus());
  }
  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};
  }
  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());
  }
  @Test
  public void testDocLibraryField() throws Exception {
    String definition = read("ddm-structure-doc-lib-field.xsd");

    DDMStructure structure =
        addStructure(
            _CLASS_NAME_ID,
            null,
            "Documents and Media Field Structure",
            definition,
            StorageType.JSON.getValue(),
            DDMStructureConstants.TYPE_DEFAULT);

    Fields fields = new Fields();

    Map<Locale, List<Serializable>> dataMap = new HashMap<>();

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

    FileEntry file1 =
        DLAppLocalServiceUtil.addFileEntry(
            TestPropsValues.getUserId(),
            TestPropsValues.getGroupId(),
            DLFolderConstants.DEFAULT_PARENT_FOLDER_ID,
            "Test 1.txt",
            ContentTypes.TEXT_PLAIN,
            RandomTestUtil.randomBytes(TikaSafeRandomizerBumper.INSTANCE),
            serviceContext);

    String file1Value = getDocLibraryFieldValue(file1);

    FileEntry file2 =
        DLAppLocalServiceUtil.addFileEntry(
            TestPropsValues.getUserId(),
            TestPropsValues.getGroupId(),
            DLFolderConstants.DEFAULT_PARENT_FOLDER_ID,
            "Test 2.txt",
            ContentTypes.TEXT_PLAIN,
            RandomTestUtil.randomBytes(TikaSafeRandomizerBumper.INSTANCE),
            serviceContext);

    String file2Value = getDocLibraryFieldValue(file2);

    List<Serializable> enValues = ListUtil.fromArray(new Serializable[] {file1Value, file2Value});

    dataMap.put(_enLocale, enValues);

    List<Serializable> ptValues = ListUtil.fromArray(new Serializable[] {file1Value, file2Value});

    dataMap.put(_ptLocale, ptValues);

    Field documentLibraryField =
        new Field(structure.getStructureId(), "doc_library", dataMap, _enLocale);

    fields.put(documentLibraryField);

    Field fieldsDisplayField =
        createFieldsDisplayField(
            structure.getStructureId(), "doc_library_INSTANCE_rztm,doc_library_INSTANCE_ovho");

    fields.put(fieldsDisplayField);

    validate(structure.getStructureId(), fields);
  }
  @Before
  public void setUp() throws Exception {
    _liveGroup = GroupTestUtil.addGroup();
    _stagingGroup = GroupTestUtil.addGroup();

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

    _fileEntry =
        DLAppLocalServiceUtil.addFileEntry(
            TestPropsValues.getUserId(),
            _stagingGroup.getGroupId(),
            DLFolderConstants.DEFAULT_PARENT_FOLDER_ID,
            RandomTestUtil.randomString() + ".txt",
            ContentTypes.TEXT_PLAIN,
            RandomTestUtil.randomBytes(TikaSafeRandomizerBumper.INSTANCE),
            serviceContext);

    ThumbnailCapability thumbnailCapability =
        _fileEntry.getRepositoryCapability(ThumbnailCapability.class);

    _fileEntry = thumbnailCapability.setLargeImageId(_fileEntry, _fileEntry.getFileEntryId());

    TestReaderWriter testReaderWriter = new TestReaderWriter();

    _portletDataContextExport =
        PortletDataContextFactoryUtil.createExportPortletDataContext(
            _stagingGroup.getCompanyId(),
            _stagingGroup.getGroupId(),
            new HashMap<String, String[]>(),
            new Date(System.currentTimeMillis() - Time.HOUR),
            new Date(),
            testReaderWriter);

    Document document = SAXReaderUtil.createDocument();

    Element manifestRootElement = document.addElement("root");

    manifestRootElement.addElement("header");

    testReaderWriter.addEntry("/manifest.xml", document.asXML());

    Element rootElement = SAXReaderUtil.createElement("root");

    _portletDataContextExport.setExportDataRootElement(rootElement);

    _stagingPrivateLayout = LayoutTestUtil.addLayout(_stagingGroup, true);
    _stagingPublicLayout = LayoutTestUtil.addLayout(_stagingGroup, false);

    _portletDataContextExport.setPlid(_stagingPublicLayout.getPlid());

    _portletDataContextImport =
        PortletDataContextFactoryUtil.createImportPortletDataContext(
            _liveGroup.getCompanyId(),
            _liveGroup.getGroupId(),
            new HashMap<String, String[]>(),
            new TestUserIdStrategy(),
            testReaderWriter);

    _portletDataContextImport.setImportDataRootElement(rootElement);

    Element missingReferencesElement = rootElement.addElement("missing-references");

    _portletDataContextImport.setMissingReferencesElement(missingReferencesElement);

    _livePublicLayout = LayoutTestUtil.addLayout(_liveGroup, false);

    _portletDataContextImport.setPlid(_livePublicLayout.getPlid());

    _portletDataContextImport.setSourceGroupId(_stagingGroup.getGroupId());

    rootElement.addElement("entry");

    _referrerStagedModel =
        JournalTestUtil.addArticle(
            _stagingGroup.getGroupId(),
            RandomTestUtil.randomString(),
            RandomTestUtil.randomString());
  }