001/*
002 * (C) Copyright 2013 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl-2.1.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Antoine Taillefer <ataillefer@nuxeo.com>
016 */
017package org.nuxeo.drive.operations.test;
018
019import java.util.concurrent.TimeUnit;
020
021import org.apache.commons.logging.Log;
022import org.apache.commons.logging.LogFactory;
023import org.nuxeo.common.utils.IdUtils;
024import org.nuxeo.ecm.core.api.CoreSession;
025import org.nuxeo.ecm.core.api.DocumentModel;
026import org.nuxeo.ecm.core.api.DocumentModelList;
027import org.nuxeo.ecm.core.api.DocumentRef;
028import org.nuxeo.ecm.core.api.NuxeoException;
029import org.nuxeo.ecm.core.api.PathRef;
030import org.nuxeo.ecm.core.work.api.WorkManager;
031import org.nuxeo.ecm.platform.usermanager.UserManager;
032import org.nuxeo.ecm.user.center.profile.UserProfileService;
033import org.nuxeo.runtime.api.Framework;
034
035/**
036 * Helper for the Nuxeo Drive integration tests.
037 *
038 * @author Antoine Taillefer
039 * @see NuxeoDriveSetupIntegrationTests
040 * @see NuxeoDriveTearDownIntegrationTests
041 */
042public final class NuxeoDriveIntegrationTestsHelper {
043
044    static final Log log = LogFactory.getLog(NuxeoDriveIntegrationTestsHelper.class);
045
046    public static final String TEST_USER_NAME_PREFIX = "nuxeoDriveTestUser_";
047
048    public static final String TEST_WORKSPACE_PARENT_NAME = "workspaces";
049
050    public static final String TEST_WORKSPACE_NAME = "nuxeo-drive-test-workspace";
051
052    public static final String TEST_WORKSPACE_TITLE = "Nuxeo Drive Test Workspace";
053
054    public static final String USER_WORKSPACE_PARENT_NAME = "UserWorkspaces";
055
056    private NuxeoDriveIntegrationTestsHelper() {
057        // Helper class
058    }
059
060    public static void cleanUp(CoreSession session) {
061
062        // Delete test users and their personal workspace if exist
063        UserManager userManager = Framework.getLocalService(UserManager.class);
064        DocumentModelList testUsers = userManager.searchUsers(TEST_USER_NAME_PREFIX);
065        for (DocumentModel testUser : testUsers) {
066            String testUserName = (String) testUser.getPropertyValue(userManager.getUserSchemaName() + ":"
067                    + userManager.getUserIdField());
068            if (userManager.getPrincipal(testUserName) != null) {
069                userManager.deleteUser(testUserName);
070            }
071            String testUserWorkspaceName = IdUtils.generateId(testUserName, "-", false, 30);
072            String testUserWorkspacePath = getDefaultDomainPath(session) + "/" + USER_WORKSPACE_PARENT_NAME + "/"
073                    + testUserWorkspaceName;
074            DocumentRef testUserWorkspaceRef = new PathRef(testUserWorkspacePath);
075            if (session.exists(testUserWorkspaceRef)) {
076                session.removeDocument(testUserWorkspaceRef);
077                session.save();
078            }
079        }
080
081        // Delete test workspace if exists
082        String testWorkspacePath = getDefaultDomainPath(session) + "/" + TEST_WORKSPACE_PARENT_NAME + "/"
083                + TEST_WORKSPACE_NAME;
084        DocumentRef testWorkspaceDocRef = new PathRef(testWorkspacePath);
085        if (session.exists(testWorkspaceDocRef)) {
086            session.removeDocument(testWorkspaceDocRef);
087            session.save();
088        }
089
090        // Invalidate user profile cache
091        Framework.getLocalService(UserProfileService.class).clearCache();
092    }
093
094    public static String getDefaultDomainPath(CoreSession session) {
095        String query = "SELECT * FROM Document where ecm:primaryType = 'Domain'";
096        DocumentModelList results = session.query(query);
097        if (results.isEmpty()) {
098            throw new NuxeoException(String.format("Found no domains in repository %s", session.getRepositoryName()));
099        }
100        if (results.size() > 1) {
101            if (log.isDebugEnabled()) {
102                log.debug(String.format("Found more than one domain in repository %s, using first one.",
103                        session.getRepositoryName()));
104            }
105        }
106        DocumentModel defaultDomain = results.get(0);
107        String defaultDomainPath = defaultDomain.getPathAsString();
108        if (log.isDebugEnabled()) {
109            log.debug(String.format("Using default domain %s", defaultDomainPath));
110        }
111        return defaultDomainPath;
112    }
113
114    public static void checkOperationAllowed() {
115        if (!(Framework.isDevModeSet() || Framework.isTestModeSet() || Framework.getProperty("org.nuxeo.ecm.tester.name") != null)) {
116            throw new UnsupportedOperationException("This operation cannot be run unless test mode is set.");
117        }
118    }
119
120    public static void waitForAsyncCompletion() throws InterruptedException {
121        Framework.getService(WorkManager.class).awaitCompletion(20, TimeUnit.SECONDS);
122    }
123
124}