<commit_before>/*
 * File   : $Source$
 * Date   : $Date$
 * Version: $Revision$
 *
 * This library is part of OpenCms -
 * the Open Source Content Management System
 *
 * Copyright (C) 2002 - 2011 Alkacon Software (http://www.alkacon.com)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * For further information about Alkacon Software, please see the
 * company website: http://www.alkacon.com
 *
 * For further information about OpenCms, please see the
 * project website: http://www.opencms.org
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package org.opencms.ade.configuration;

import org.opencms.ade.detailpage.CmsDetailPageInfo;
import org.opencms.file.CmsObject;
import org.opencms.file.CmsResource;
import org.opencms.file.CmsResourceFilter;
import org.opencms.file.CmsUser;
import org.opencms.file.CmsVfsResourceAlreadyExistsException;
import org.opencms.file.types.CmsResourceTypeFolder;
import org.opencms.file.types.I_CmsResourceType;
import org.opencms.main.CmsException;
import org.opencms.main.OpenCms;
import org.opencms.security.I_CmsPrincipal;
import org.opencms.test.OpenCmsTestCase;
import org.opencms.test.OpenCmsTestProperties;
import org.opencms.util.CmsStringUtil;
import org.opencms.util.CmsUUID;
import org.opencms.xml.containerpage.CmsFormatterBean;
import org.opencms.xml.containerpage.CmsFormatterConfiguration;
import org.opencms.xml.content.CmsXmlContentProperty;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import junit.framework.ComparisonFailure;
import junit.framework.Test;

import com.google.common.collect.Lists;

/**
 * Lightweight tests for the ADE configuration mechanism which mostly do not read the configuration data from the VFS.<p>
 */
public class TestConfig extends OpenCmsTestCase {

    protected static final List<CmsPropertyConfig> NO_PROPERTIES = Collections.<CmsPropertyConfig> emptyList();

    protected static final List<CmsDetailPageInfo> NO_DETAILPAGES = Collections.<CmsDetailPageInfo> emptyList();

    protected static final List<CmsModelPageConfig> NO_MODEL_PAGES = Collections.<CmsModelPageConfig> emptyList();

    protected static final List<CmsResourceTypeConfig> NO_TYPES = Collections.<CmsResourceTypeConfig> emptyList();

    /**
     * Test constructor.<p>
     * 
     * @param arg0
     */
    public TestConfig(String arg0) {

        super(arg0);
    }

    /**
     * Returns the test suite.<p>
     * 
     * @return the test suite 
     */
    public static Test suite() {

        OpenCmsTestProperties.initialize(org.opencms.test.AllTests.TEST_PROPERTIES_PATH);
        return generateSetupTestWrapper(TestConfig.class, "ade-config", "/");
    }

    public void createFolder(String rootPath, boolean deep) throws CmsException {

        CmsObject cms = getCmsObject();
        cms.getRequestContext().setSiteRoot("");
        if (!deep) {
            cms.createResource(rootPath, CmsResourceTypeFolder.getStaticTypeId());
        } else {
            List<String> parents = new ArrayList<String>();
            String currentPath = rootPath;
            while (currentPath != null) {
                parents.add(currentPath);
                currentPath = CmsResource.getParentFolder(currentPath);
            }
            parents = Lists.reverse(parents);
            System.out.println("Creating folders up to " + rootPath + "(" + parents.size() + ")");

            for (String parent : parents) {
                System.out.println("Creating folder: " + parent);
                try {
                    cms.createResource(parent, CmsResourceTypeFolder.getStaticTypeId());
                } catch (CmsVfsResourceAlreadyExistsException e) {
                    // nop 
                }

            }
        }
    }

    public void testCreatable() throws Exception {

        String baseDirectory = "/sites/default/testCreatable";
        String contentDirectory = baseDirectory + "/.content";
        String plain = "plain";
        String binary = "binary";
        String plainDir = contentDirectory + "/" + plain;
        String binaryDir = contentDirectory + "/" + binary;

        createFolder(plainDir, true);
        createFolder(binaryDir, true);

        CmsObject cms = rootCms();
        String username = "User_testCreatable";
        CmsUser dummyUser = cms.createUser(username, "password", "description", Collections.<String, Object> emptyMap());
        cms.chacc(plainDir, I_CmsPrincipal.PRINCIPAL_USER, username, "+r+v-w");
        cms.chacc(binaryDir, I_CmsPrincipal.PRINCIPAL_USER, username, "+r+v+w");

        CmsObject dummyUserCms = rootCms();
        cms.addUserToGroup(username, OpenCms.getDefaultUsers().getGroupUsers());
        dummyUserCms.loginUser(username, "password");
        dummyUserCms.getRequestContext().setCurrentProject(cms.readProject("Offline"));

        CmsFolderOrName folder = new CmsFolderOrName(baseDirectory + "/.content", "plain");
        CmsResourceTypeConfig typeConf1 = new CmsResourceTypeConfig("plain", false, folder, "pattern_%(number)", null);
        CmsResourceTypeConfig typeConf2 = new CmsResourceTypeConfig("binary", false, new CmsFolderOrName(baseDirectory
            + "/.content", "binary"), "binary_%(number).html", null);

        CmsTestConfigData config1 = new CmsTestConfigData(
            baseDirectory,
            list(typeConf1, typeConf2),
            NO_PROPERTIES,
            NO_DETAILPAGES,
            NO_MODEL_PAGES);
        config1.initialize(rootCms());
        List<CmsResourceTypeConfig> creatableTypes = config1.getCreatableTypes(dummyUserCms);
        assertEquals(1, creatableTypes.size());
        assertEquals(binary, creatableTypes.get(0).getTypeName());

        creatableTypes = config1.getCreatableTypes(cms);
        assertEquals(2, creatableTypes.size());
        assertEquals(
            set("plain", "binary"),
            set(creatableTypes.get(0).getTypeName(), creatableTypes.get(1).getTypeName()));

    }

    public void testCreateContentsLocally() throws Exception {

        String typename = "plain";
        String baseDirectory = "/sites/default";
        String contentDirectory = baseDirectory + "/.content";

        String baseDirectory2 = "/sites/default/foo";
        String contentDirectory2 = baseDirectory2 + "/.content";

        CmsFolderOrName folder = new CmsFolderOrName(contentDirectory, typename);<commit_msg>Improved configuration tests.<commit_after>/*
 * File   : $Source$
 * Date   : $Date$
 * Version: $Revision$
 *
 * This library is part of OpenCms -
 * the Open Source Content Management System
 *
 * Copyright (C) 2002 - 2011 Alkacon Software (http://www.alkacon.com)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * For further information about Alkacon Software, please see the
 * company website: http://www.alkacon.com
 *
 * For further information about OpenCms, please see the
 * project website: http://www.opencms.org
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package org.opencms.ade.configuration;

import org.opencms.ade.detailpage.CmsDetailPageInfo;
import org.opencms.file.CmsObject;
import org.opencms.file.CmsResource;
import org.opencms.file.CmsResourceFilter;
import org.opencms.file.CmsUser;
import org.opencms.file.CmsVfsResourceAlreadyExistsException;
import org.opencms.file.types.CmsResourceTypeFolder;
import org.opencms.file.types.I_CmsResourceType;
import org.opencms.main.CmsException;
import org.opencms.main.OpenCms;
import org.opencms.security.I_CmsPrincipal;
import org.opencms.test.OpenCmsTestCase;
import org.opencms.test.OpenCmsTestProperties;
import org.opencms.util.CmsStringUtil;
import org.opencms.util.CmsUUID;
import org.opencms.xml.containerpage.CmsFormatterBean;
import org.opencms.xml.containerpage.CmsFormatterConfiguration;
import org.opencms.xml.content.CmsXmlContentProperty;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import junit.framework.ComparisonFailure;
import junit.framework.Test;

import com.google.common.collect.Lists;

/**
 * Lightweight tests for the ADE configuration mechanism which mostly do not read the configuration data from the VFS.<p>
 */
public class TestConfig extends OpenCmsTestCase {

    protected static final List<CmsPropertyConfig> NO_PROPERTIES = Collections.<CmsPropertyConfig> emptyList();

    protected static final List<CmsDetailPageInfo> NO_DETAILPAGES = Collections.<CmsDetailPageInfo> emptyList();

    protected static final List<CmsModelPageConfig> NO_MODEL_PAGES = Collections.<CmsModelPageConfig> emptyList();

    protected static final List<CmsResourceTypeConfig> NO_TYPES = Collections.<CmsResourceTypeConfig> emptyList();

    /**
     * Test constructor.<p>
     * 
     * @param arg0
     */
    public TestConfig(String arg0) {

        super(arg0);
    }

    /**
     * Returns the test suite.<p>
     * 
     * @return the test suite 
     */
    public static Test suite() {

        OpenCmsTestProperties.initialize(org.opencms.test.AllTests.TEST_PROPERTIES_PATH);
        return generateSetupTestWrapper(TestConfig.class, "ade-config", "/");
    }

    public void createFolder(String rootPath, boolean deep) throws CmsException {

        CmsObject cms = getCmsObject();
        cms.getRequestContext().setSiteRoot("");
        if (!deep) {
            cms.createResource(rootPath, CmsResourceTypeFolder.getStaticTypeId());
        } else {
            List<String> parents = new ArrayList<String>();
            String currentPath = rootPath;
            while (currentPath != null) {
                parents.add(currentPath);
                currentPath = CmsResource.getParentFolder(currentPath);
            }
            parents = Lists.reverse(parents);
            System.out.println("Creating folders up to " + rootPath + "(" + parents.size() + ")");

            for (String parent : parents) {
                System.out.println("Creating folder: " + parent);
                try {
                    cms.createResource(parent, CmsResourceTypeFolder.getStaticTypeId());
                } catch (CmsVfsResourceAlreadyExistsException e) {
                    // nop 
                }

            }
        }
    }

    public void testCreatable() throws Exception {

        String baseDirectory = "/sites/default/testCreatable";
        String contentDirectory = baseDirectory + "/.content";
        String plain = "plain";
        String binary = "binary";
        String plainDir = contentDirectory + "/" + plain;
        String binaryDir = contentDirectory + "/" + binary;

        createFolder(plainDir, true);
        createFolder(binaryDir, true);

        CmsObject cms = rootCms();
        String username = "User_testCreatable";
        CmsUser dummyUser = cms.createUser(username, "password", "description", Collections.<String, Object> emptyMap());
        cms.chacc(plainDir, I_CmsPrincipal.PRINCIPAL_USER, username, "+r+v-w");
        cms.chacc(binaryDir, I_CmsPrincipal.PRINCIPAL_USER, username, "+r+v+w");

        CmsObject dummyUserCms = rootCms();
        dummyUserCms.getRequestContext().setSiteRoot("/sites/default");
        cms.addUserToGroup(username, OpenCms.getDefaultUsers().getGroupUsers());
        dummyUserCms.loginUser(username, "password");
        dummyUserCms.getRequestContext().setCurrentProject(cms.readProject("Offline"));

        CmsFolderOrName folder = new CmsFolderOrName(baseDirectory + "/.content", "plain");
        CmsResourceTypeConfig typeConf1 = new CmsResourceTypeConfig("plain", false, folder, "pattern_%(number)", null);
        CmsResourceTypeConfig typeConf2 = new CmsResourceTypeConfig("binary", false, new CmsFolderOrName(baseDirectory
            + "/.content", "binary"), "binary_%(number).html", null);

        CmsTestConfigData config1 = new CmsTestConfigData(
            baseDirectory,
            list(typeConf1, typeConf2),
            NO_PROPERTIES,
            NO_DETAILPAGES,
            NO_MODEL_PAGES);
        config1.initialize(rootCms());
        List<CmsResourceTypeConfig> creatableTypes = config1.getCreatableTypes(dummyUserCms);
        assertEquals(1, creatableTypes.size());
        assertEquals(binary, creatableTypes.get(0).getTypeName());

        creatableTypes = config1.getCreatableTypes(cms);
        // because we're in the root site 
        assertEquals(0, creatableTypes.size());

        cms.getRequestContext().setSiteRoot("/sites/default");
        creatableTypes = config1.getCreatableTypes(cms);

        assertEquals(
            set("plain", "binary"),
            set(creatableTypes.get(0).getTypeName(), creatableTypes.get(1).getTypeName()));

    }

    public void testCreateContentsLocally() throws Exception {

        String typename = "plain";
        String baseDirectory = "/sites/default";
        String contentDirectory = baseDirectory + "/.content";

        String baseDirectory2 = "/sites/default/foo";
        String contentDirectory2 = baseDirectory2 + "/.content";

        CmsFolderOrName folder = new CmsFolderOrName(contentDirectory, typename);
{'fraction_non_alphanumeric': 0.10183454482519903, 'fraction_numerical': 0.0060228452751817235, 'mean_word_length': 3.3045292014302743, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 6, 'xml': 6}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6714274', 'n_tokens_mistral': 4125, 'n_tokens_neox': 3968, 'n_words': 985}