<commit_before>/*
 * (C) Copyright 2012 Nuxeo SA (http://nuxeo.com/) and contributors.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser General Public License
 * (LGPL) version 2.1 which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl.html
 *
 * 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.
 *
 * Contributors:
 *     <NAME> <<EMAIL>>
 */
package org.nuxeo.drive.operations;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import static org.nuxeo.drive.operations.test.NuxeoDriveIntegrationTestsHelper.TEST_WORKSPACE_PATH;
import static org.nuxeo.drive.operations.test.NuxeoDriveIntegrationTestsHelper.USER_WORKSPACE_PARENT_PATH;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.codehaus.jackson.map.ObjectMapper;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.nuxeo.drive.operations.test.NuxeoDriveSetupIntegrationTests;
import org.nuxeo.drive.operations.test.NuxeoDriveTearDownIntegrationTests;
import org.nuxeo.ecm.automation.client.Session;
import org.nuxeo.ecm.automation.client.jaxrs.impl.HttpAutomationClient;
import org.nuxeo.ecm.automation.client.model.Blob;
import org.nuxeo.ecm.automation.test.RestFeature;
import org.nuxeo.ecm.core.api.ClientException;
import org.nuxeo.ecm.core.api.CoreSession;
import org.nuxeo.ecm.core.api.DocumentModel;
import org.nuxeo.ecm.core.api.DocumentRef;
import org.nuxeo.ecm.core.api.NuxeoPrincipal;
import org.nuxeo.ecm.core.api.PathRef;
import org.nuxeo.ecm.core.api.security.SecurityConstants;
import org.nuxeo.ecm.core.test.annotations.Granularity;
import org.nuxeo.ecm.core.test.annotations.RepositoryConfig;
import org.nuxeo.ecm.platform.usermanager.UserManager;
import org.nuxeo.ecm.platform.userworkspace.api.UserWorkspaceService;
import org.nuxeo.runtime.test.runner.Deploy;
import org.nuxeo.runtime.test.runner.Features;
import org.nuxeo.runtime.test.runner.FeaturesRunner;
import org.nuxeo.runtime.test.runner.Jetty;

import com.google.inject.Inject;

/**
 * Tests the Nuxeo Drive integration tests operations.
 *
 * @author <NAME>
 */
@RunWith(FeaturesRunner.class)
@Features(RestFeature.class)
@Deploy({ "org.nuxeo.ecm.platform.userworkspace.types",
        "org.nuxeo.ecm.platform.userworkspace.api",
        "org.nuxeo.ecm.platform.userworkspace.core",
        "org.nuxeo.drive.operations" })
@RepositoryConfig(cleanup = Granularity.METHOD)
@Jetty(port = 18080)
public class TestIntegrationTestOperations {

    @Inject
    protected CoreSession session;

    @Inject
    protected UserManager userManager;

    @Inject
    protected UserWorkspaceService userWorkspaceService;

    @Inject
    protected HttpAutomationClient automationClient;

    protected Session clientSession;

    protected ObjectMapper mapper;

    @Before
    public void init() throws Exception {

        // Get an Automation client session as Administrator
        clientSession = automationClient.getSession("Administrator",
                "Administrator");
        mapper = new ObjectMapper();
    }

    @Test
    public void testIntegrationTestsSetupAndTearDown() throws Exception {

        // ---------------------------------------------------------
        // Setup the integration tests environment as Administrator
        // ---------------------------------------------------------
        Blob testUserCredentialsBlob = (Blob) clientSession.newRequest(
                NuxeoDriveSetupIntegrationTests.ID).set("userNames", "joe,jack").execute();
        assertNotNull(testUserCredentialsBlob);

        // Check test users
        String testUserCredentials = IOUtils.toString(
                testUserCredentialsBlob.getStream(), "UTF-8");
        assertNotNull(testUserCredentials);
        String[] testUserCrendentialsArray = StringUtils.split(
                testUserCredentials, ",");
        assertEquals(2, testUserCrendentialsArray.length);
        assertTrue(testUserCrendentialsArray[0].startsWith("nuxeoDriveTestUser_joe:"));
        assertTrue(testUserCrendentialsArray[1].startsWith("nuxeoDriveTestUser_jack:"));

        NuxeoPrincipal joePrincipal = userManager.getPrincipal("nuxeoDriveTestUser_joe");
        assertNotNull(joePrincipal);
        assertTrue(joePrincipal.getGroups().contains("members"));
        NuxeoPrincipal jackPrincipal = userManager.getPrincipal("nuxeoDriveTestUser_jack");
        assertNotNull(jackPrincipal);
        assertTrue(jackPrincipal.getGroups().contains("members"));

        // Check test workspace
        DocumentRef testWorkspaceRef = new PathRef(TEST_WORKSPACE_PATH);
        DocumentModel testWorkspace = session.getDocument(testWorkspaceRef);
        assertEquals("Workspace", testWorkspace.getType());
        assertEquals("Nuxeo Drive Test Workspace", testWorkspace.getTitle());
        assertTrue(session.hasPermission(joePrincipal, testWorkspaceRef,
                SecurityConstants.WRITE));
        assertTrue(session.hasPermission(jackPrincipal, testWorkspaceRef,
                SecurityConstants.WRITE));

        // Create test users' personal workspaces for cleanup check
        userWorkspaceService.getUserPersonalWorkspace("nuxeoDriveTestUser_joe",
                session.getRootDocument());
        userWorkspaceService.getUserPersonalWorkspace(
                "nuxeoDriveTestUser_jack", session.getRootDocument());
        assertNotNull(session.getDocument(new PathRef(
                USER_WORKSPACE_PARENT_PATH + "/nuxeoDriveTestUser-joe")));
        assertNotNull(session.getDocument(new PathRef(
                USER_WORKSPACE_PARENT_PATH + "/nuxeoDriveTestUser-jack")));

        // ----------------------------------------------------------------------
        // Setup the integration tests environment with other user names without
        // having teared it down previously => should start by cleaning it up
        // ----------------------------------------------------------------------
        testUserCredentialsBlob = (Blob) clientSession.newRequest(
                NuxeoDriveSetupIntegrationTests.ID).set("userNames", "sarah").execute();
        assertNotNull(testUserCredentialsBlob);

        // Check cleanup
        assertNull(userManager.getPrincipal("nuxeoDriveTestUser_joe"));
        assertNull(userManager.getPrincipal("nuxeoDriveTestUser_jack"));
        // Invalid VCS cache
        session.save();
        try {
            session.getDocument(new PathRef(USER_WORKSPACE_PARENT_PATH
                    + "/nuxeoDriveTestUser-joe"));
            fail("User workspace should not exist.");
        } catch (ClientException e) {
            assertEquals("Failed to get document " + USER_WORKSPACE_PARENT_PATH
                    + "/nuxeoDriveTestUser-joe", e.getMessage());
        }
        try {
            session.getDocument(new PathRef(USER_WORKSPACE_PARENT_PATH
                    + "/nuxeoDriveTestUser-jack"));
            fail("User workspace should not exist.");
        } catch (ClientException e) {<commit_msg>NXP-11127: update java operations tests<commit_after>/*
 * (C) Copyright 2012 Nuxeo SA (http://nuxeo.com/) and contributors.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser General Public License
 * (LGPL) version 2.1 which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl.html
 *
 * 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.
 *
 * Contributors:
 *     <NAME> <<EMAIL>>
 */
package org.nuxeo.drive.operations;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.nuxeo.drive.operations.test.NuxeoDriveIntegrationTestsHelper.TEST_WORKSPACE_PATH;
import static org.nuxeo.drive.operations.test.NuxeoDriveIntegrationTestsHelper.USER_WORKSPACE_PARENT_PATH;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.codehaus.jackson.map.ObjectMapper;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.nuxeo.drive.operations.test.NuxeoDriveSetupIntegrationTests;
import org.nuxeo.drive.operations.test.NuxeoDriveTearDownIntegrationTests;
import org.nuxeo.ecm.automation.client.Session;
import org.nuxeo.ecm.automation.client.jaxrs.impl.HttpAutomationClient;
import org.nuxeo.ecm.automation.client.model.Blob;
import org.nuxeo.ecm.automation.test.RestFeature;
import org.nuxeo.ecm.core.api.ClientException;
import org.nuxeo.ecm.core.api.CoreSession;
import org.nuxeo.ecm.core.api.DocumentModel;
import org.nuxeo.ecm.core.api.DocumentRef;
import org.nuxeo.ecm.core.api.NuxeoPrincipal;
import org.nuxeo.ecm.core.api.PathRef;
import org.nuxeo.ecm.core.api.security.SecurityConstants;
import org.nuxeo.ecm.core.test.annotations.Granularity;
import org.nuxeo.ecm.core.test.annotations.RepositoryConfig;
import org.nuxeo.ecm.platform.usermanager.UserManager;
import org.nuxeo.ecm.platform.userworkspace.api.UserWorkspaceService;
import org.nuxeo.runtime.test.runner.Deploy;
import org.nuxeo.runtime.test.runner.Features;
import org.nuxeo.runtime.test.runner.FeaturesRunner;
import org.nuxeo.runtime.test.runner.Jetty;

import com.google.inject.Inject;

/**
 * Tests the Nuxeo Drive integration tests operations.
 *
 * @author <NAME>
 */
@RunWith(FeaturesRunner.class)
@Features(RestFeature.class)
@Deploy({ "org.nuxeo.ecm.platform.userworkspace.types",
        "org.nuxeo.ecm.platform.userworkspace.api",
        "org.nuxeo.ecm.platform.userworkspace.core",
        "org.nuxeo.drive.operations" })
@RepositoryConfig(cleanup = Granularity.METHOD)
@Jetty(port = 18080)
public class TestIntegrationTestOperations {

    @Inject
    protected CoreSession session;

    @Inject
    protected UserManager userManager;

    @Inject
    protected UserWorkspaceService userWorkspaceService;

    @Inject
    protected HttpAutomationClient automationClient;

    protected Session clientSession;

    protected ObjectMapper mapper;

    @Before
    public void init() throws Exception {

        // Get an Automation client session as Administrator
        clientSession = automationClient.getSession("Administrator",
                "Administrator");
        mapper = new ObjectMapper();
    }

    @Test
    public void testIntegrationTestsSetupAndTearDown() throws Exception {

        // ---------------------------------------------------------
        // Setup the integration tests environment as Administrator
        // ---------------------------------------------------------
        Blob testUserCredentialsBlob = (Blob) clientSession.newRequest(
                NuxeoDriveSetupIntegrationTests.ID).set("userNames", "joe,jack").execute();
        assertNotNull(testUserCredentialsBlob);

        // Check test users
        String testUserCredentials = IOUtils.toString(
                testUserCredentialsBlob.getStream(), "UTF-8");
        assertNotNull(testUserCredentials);
        String[] testUserCrendentialsArray = StringUtils.split(
                testUserCredentials, ",");
        assertEquals(2, testUserCrendentialsArray.length);
        assertTrue(testUserCrendentialsArray[0].startsWith("nuxeoDriveTestUser_joe:"));
        assertTrue(testUserCrendentialsArray[1].startsWith("nuxeoDriveTestUser_jack:"));

        // useMembersGroup is false by default
        NuxeoPrincipal joePrincipal = userManager.getPrincipal("nuxeoDriveTestUser_joe");
        assertNotNull(joePrincipal);
        assertFalse(joePrincipal.getGroups().contains("members"));
        NuxeoPrincipal jackPrincipal = userManager.getPrincipal("nuxeoDriveTestUser_jack");
        assertNotNull(jackPrincipal);
        assertFalse(jackPrincipal.getGroups().contains("members"));

        // Check test workspace
        DocumentRef testWorkspaceRef = new PathRef(TEST_WORKSPACE_PATH);
        DocumentModel testWorkspace = session.getDocument(testWorkspaceRef);
        assertEquals("Workspace", testWorkspace.getType());
        assertEquals("Nuxeo Drive Test Workspace", testWorkspace.getTitle());
        assertTrue(session.hasPermission(joePrincipal, testWorkspaceRef,
                SecurityConstants.WRITE));
        assertTrue(session.hasPermission(jackPrincipal, testWorkspaceRef,
                SecurityConstants.WRITE));

        // Create test users' personal workspaces for cleanup check
        userWorkspaceService.getUserPersonalWorkspace("nuxeoDriveTestUser_joe",
                session.getRootDocument());
        userWorkspaceService.getUserPersonalWorkspace(
                "nuxeoDriveTestUser_jack", session.getRootDocument());
        assertNotNull(session.getDocument(new PathRef(
                USER_WORKSPACE_PARENT_PATH + "/nuxeoDriveTestUser-joe")));
        assertNotNull(session.getDocument(new PathRef(
                USER_WORKSPACE_PARENT_PATH + "/nuxeoDriveTestUser-jack")));

        // ----------------------------------------------------------------------
        // Setup the integration tests environment with other user names without
        // having teared it down previously => should start by cleaning it up
        // ----------------------------------------------------------------------
        testUserCredentialsBlob = (Blob) clientSession.newRequest(
                NuxeoDriveSetupIntegrationTests.ID).set("userNames", "sarah").set(
                        "useMembersGroup", true).execute();
        assertNotNull(testUserCredentialsBlob);

        // Check cleanup
        assertNull(userManager.getPrincipal("nuxeoDriveTestUser_joe"));
        assertNull(userManager.getPrincipal("nuxeoDriveTestUser_jack"));
        // Invalid VCS cache
        session.save();
        try {
            session.getDocument(new PathRef(USER_WORKSPACE_PARENT_PATH
                    + "/nuxeoDriveTestUser-joe"));
            fail("User workspace should not exist.");
        } catch (ClientException e) {
            assertEquals("Failed to get document " + USER_WORKSPACE_PARENT_PATH
                    + "/nuxeoDriveTestUser-joe", e.getMessage());
        }
        try {
            session.getDocument(new PathRef(USER_WORKSPACE_PARENT_PATH
                    + "/nuxeoDriveTestUser-jack"));
            fail("User workspace should not exist.");
        } catch (ClientException e) {
{'fraction_non_alphanumeric': 0.12627264313103265, 'fraction_numerical': 0.002313896601877562, 'mean_word_length': 3.1128330614464383, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, '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': '6547218', 'n_tokens_mistral': 4275, 'n_tokens_neox': 4075, 'n_words': 787}