<commit_before>/**
 * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.wso2.ds.ui.integration.test.dashboard;

import ds.integration.tests.common.domain.DSIntegrationTestConstants;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebElement;
import org.testng.annotations.*;
import org.wso2.carbon.automation.engine.context.TestUserMode;
import org.wso2.ds.ui.integration.util.DSUIIntegrationTest;

import javax.xml.xpath.XPathExpressionException;
import java.net.MalformedURLException;

import static org.testng.Assert.*;

/**
 * Tests per user dashboard personalization.
 */
public class CreateCustomDashboardPerUser extends DSUIIntegrationTest {
    private static final String USERNAME_EDITOR = "editor";
    private static final String PASSWORD_EDITOR = "<PASSWORD>";
    private static final String RETYPE_PASSWORD_EDITOR = "<PASSWORD>";
    private static final String USERNAME_VIEWER = "viewer";
    private static final String PASSWORD_VIEWER = "<PASSWORD>";
    private static final String RETYPE_PASSWORD_VIEWER = "<PASSWORD>";
    private static final String EDITOR_ROLE = "dashboardEditorRole";
    private static final String VIEWER_ROLE = "dashboardViewerRole";
    private static final String DASHBOARD_TITLE = "perUserCustomDashboard";
    private static final String DASHBOARD_DESCRIPTION = "This is sample description for dashboard";
    private static final String DASHBOARD_PAGE_NAME = "PersonalizeDashBoardTitle";
    private String dashboardTitle;

    /**
     * Initialize the class.
     *
     * @param userMode       user mode
     * @param dashboardTitle title of the dashboard
     */
    @Factory(dataProvider = "userMode")
    public CreateCustomDashboardPerUser(TestUserMode userMode, String dashboardTitle) {
        super(userMode);
        this.dashboardTitle = dashboardTitle;
    }

    /**
     * Provides user modes to initialize the class.
     *
     * @return
     */
    @DataProvider(name = "userMode")
    private static Object[][] userModeProvider() {
        return new Object[][]{{TestUserMode.SUPER_TENANT_ADMIN, DASHBOARD_TITLE}};
    }

    /**
     * Setting up the testing environment.
     *
     * @throws Exception
     */
    @BeforeClass(alwaysRun = true)
    public void setUp() throws XPathExpressionException, MalformedURLException {
        resourcePath = DSIntegrationTestConstants.DASHBOARD_REGISTRY_BASE_PATH + dashboardTitle.toLowerCase();
        loginToAdminConsole(getCurrentUsername(), getCurrentPassword());
    }

    /**
     * Tests adding users and assign roles in the management console.
     *
     * @throws MalformedURLException
     * @throws XPathExpressionException
     */
    @Test(groups = "wso2.ds.dashboard", description = "Adding user to admin console and assign editor or viewer roles" +
            " to newly added users")
    public void testAddUserAssignRoles() throws MalformedURLException, XPathExpressionException {
        addUser(USERNAME_EDITOR, PASSWORD_EDITOR, RETYPE_PASSWORD_EDITOR);
        addRole(EDITOR_ROLE);
        assignRoleToUser(new String[]{USERNAME_EDITOR});
        addUser(USERNAME_VIEWER, PASSWORD_VIEWER, RETYPE_PASSWORD_VIEWER);
        addRole(VIEWER_ROLE);
        assignRoleToUser(new String[]{USERNAME_VIEWER});
        logoutFromAdminConsole();
    }

    /**
     * Assign dashboard viewers and editors.
     *
     * @throws XPathExpressionException
     * @throws MalformedURLException
     */
    @Test(groups = "wso2.ds.dashboard", description = "Assigning dashboard view and edit permission", dependsOnMethods =
            "testAddUserAssignRoles")
    public void testAddDashboardAndAssignRolesBySetting() throws XPathExpressionException, MalformedURLException {
        login(USERNAME_EDITOR, PASSWORD_EDITOR);
        addDashBoard(dashboardTitle, DASHBOARD_DESCRIPTION);
        WebElement dashboardItem = getDriver().findElement(By.id(dashboardTitle.toLowerCase()));
        dashboardItem.findElement(By.cssSelector(".ues-edit")).click();
        getDriver().findElement(By.id("dashboard-settings")).click();
        getDriver().executeScript("scroll(0, 200);");
        // Add viewer role
        WebElement viewerTextbox = getDriver().findElement(By.id("ues-share-view"));
        viewerTextbox.sendKeys("dashboardViewer");
        viewerTextbox.sendKeys(Keys.TAB);
        // Add editor role
        WebElement editorTextbox = getDriver().findElement(By.id("ues-share-edit"));
        editorTextbox.sendKeys("dashboardEditor");
        editorTextbox.sendKeys(Keys.TAB);
        // Remove all other roles
        getDriver().findElement(By.cssSelector(".ues-shared-view > .ues-shared-role > .remove-button")).click();
        getDriver().findElement(By.cssSelector(".ues-shared-edit > .ues-shared-role > .remove-button")).click();
        getDriver().findElement(By.id("ues-dashboard-saveBtn")).click();
    }

    /**
     * Tests for the editor role in the dashboards page and the dashboard view page.
     *
     * @throws MalformedURLException
     * @throws XPathExpressionException
     */
    @Test(groups = "wso2.ds.dashboard", description = "test for editor role", dependsOnMethods =
            "testAddDashboardAndAssignRolesBySetting")
    public void testForEditorRole() throws MalformedURLException, XPathExpressionException {

            String dashboardId = dashboardTitle.toLowerCase();
            redirectToLocation(DS_HOME_CONTEXT, DS_DASHBOARDS_CONTEXT);
            WebElement dashboard = getDriver().findElement(By.id(dashboardId));
            assertEquals(DASHBOARD_TITLE, dashboard.findElement(By.id("ues-dashboard-title")).getText());
            assertEquals(DASHBOARD_DESCRIPTION, dashboard.findElement(By.id("ues-dashboard-description")).getText());
            assertTrue(getDriver().isElementPresent(By.cssSelector("#" + dashboardId + " .ues-view")),
                    "view element is present in the current UI");
            assertTrue(getDriver().isElementPresent(By.cssSelector("#" + dashboardId + " .ues-edit")),
                    "design element is present in the current UI");
            assertTrue(getDriver().isElementPresent(By.cssSelector("#" + dashboardId + " .ues-settings")),
                    "settings element is present in the current UI");
            dashboard.findElement(By.cssSelector(".ues-view")).click();
            // Switch the driver to the new window and click on the edit/personalize link
            pushWindow();
            String bodyText = getDriver().findElement(By.tagName("body")).getText();
            assertTrue(bodyText.contains(USERNAME_EDITOR), "Expected Username is not matched");
            getDriver().findElement(By.linkText(USERNAME_EDITOR)).click();
            assertEquals("Edit", getDriver().findElement(By.cssSelector("i.ues-copy")).getAttribute("title"),
                    "Unable to find the edit button");
            getDriver().close();
            popWindow();
            logout();
    }

    /**
     * Tests for the viewer role in the dashboards page and the dashboard view page.
     *
     * @throws XPathExpressionException
     * @throws MalformedURLException
     */<commit_msg>Correcting indentation<commit_after>/**
 * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.wso2.ds.ui.integration.test.dashboard;

import ds.integration.tests.common.domain.DSIntegrationTestConstants;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebElement;
import org.testng.annotations.*;
import org.wso2.carbon.automation.engine.context.TestUserMode;
import org.wso2.ds.ui.integration.util.DSUIIntegrationTest;

import javax.xml.xpath.XPathExpressionException;
import java.net.MalformedURLException;

import static org.testng.Assert.*;

/**
 * Tests per user dashboard personalization.
 */
public class CreateCustomDashboardPerUser extends DSUIIntegrationTest {
    private static final String USERNAME_EDITOR = "editor";
    private static final String PASSWORD_EDITOR = "<PASSWORD>";
    private static final String RETYPE_PASSWORD_EDITOR = "<PASSWORD>";
    private static final String USERNAME_VIEWER = "viewer";
    private static final String PASSWORD_VIEWER = "<PASSWORD>";
    private static final String RETYPE_PASSWORD_VIEWER = "<PASSWORD>";
    private static final String EDITOR_ROLE = "dashboardEditorRole";
    private static final String VIEWER_ROLE = "dashboardViewerRole";
    private static final String DASHBOARD_TITLE = "perUserCustomDashboard";
    private static final String DASHBOARD_DESCRIPTION = "This is sample description for dashboard";
    private static final String DASHBOARD_PAGE_NAME = "PersonalizeDashBoardTitle";
    private String dashboardTitle;

    /**
     * Initialize the class.
     *
     * @param userMode       user mode
     * @param dashboardTitle title of the dashboard
     */
    @Factory(dataProvider = "userMode")
    public CreateCustomDashboardPerUser(TestUserMode userMode, String dashboardTitle) {
        super(userMode);
        this.dashboardTitle = dashboardTitle;
    }

    /**
     * Provides user modes to initialize the class.
     *
     * @return
     */
    @DataProvider(name = "userMode")
    private static Object[][] userModeProvider() {
        return new Object[][]{{TestUserMode.SUPER_TENANT_ADMIN, DASHBOARD_TITLE}};
    }

    /**
     * Setting up the testing environment.
     *
     * @throws Exception
     */
    @BeforeClass(alwaysRun = true)
    public void setUp() throws XPathExpressionException, MalformedURLException {
        resourcePath = DSIntegrationTestConstants.DASHBOARD_REGISTRY_BASE_PATH + dashboardTitle.toLowerCase();
        loginToAdminConsole(getCurrentUsername(), getCurrentPassword());
    }

    /**
     * Tests adding users and assign roles in the management console.
     *
     * @throws MalformedURLException
     * @throws XPathExpressionException
     */
    @Test(groups = "wso2.ds.dashboard", description = "Adding user to admin console and assign editor or viewer roles" +
            " to newly added users")
    public void testAddUserAssignRoles() throws MalformedURLException, XPathExpressionException {
        addUser(USERNAME_EDITOR, PASSWORD_EDITOR, RETYPE_PASSWORD_EDITOR);
        addRole(EDITOR_ROLE);
        assignRoleToUser(new String[]{USERNAME_EDITOR});
        addUser(USERNAME_VIEWER, PASSWORD_VIEWER, RETYPE_PASSWORD_VIEWER);
        addRole(VIEWER_ROLE);
        assignRoleToUser(new String[]{USERNAME_VIEWER});
        logoutFromAdminConsole();
    }

    /**
     * Assign dashboard viewers and editors.
     *
     * @throws XPathExpressionException
     * @throws MalformedURLException
     */
    @Test(groups = "wso2.ds.dashboard", description = "Assigning dashboard view and edit permission", dependsOnMethods =
            "testAddUserAssignRoles")
    public void testAddDashboardAndAssignRolesBySetting() throws XPathExpressionException, MalformedURLException {
        login(USERNAME_EDITOR, <PASSWORD> <PASSWORD>);
        addDashBoard(dashboardTitle, DASHBOARD_DESCRIPTION);
        WebElement dashboardItem = getDriver().findElement(By.id(dashboardTitle.toLowerCase()));
        dashboardItem.findElement(By.cssSelector(".ues-edit")).click();
        getDriver().findElement(By.id("dashboard-settings")).click();
        getDriver().executeScript("scroll(0, 200);");
        // Add viewer role
        WebElement viewerTextbox = getDriver().findElement(By.id("ues-share-view"));
        viewerTextbox.sendKeys("dashboardViewer");
        viewerTextbox.sendKeys(Keys.TAB);
        // Add editor role
        WebElement editorTextbox = getDriver().findElement(By.id("ues-share-edit"));
        editorTextbox.sendKeys("dashboardEditor");
        editorTextbox.sendKeys(Keys.TAB);
        // Remove all other roles
        getDriver().findElement(By.cssSelector(".ues-shared-view > .ues-shared-role > .remove-button")).click();
        getDriver().findElement(By.cssSelector(".ues-shared-edit > .ues-shared-role > .remove-button")).click();
        getDriver().findElement(By.id("ues-dashboard-saveBtn")).click();
    }

    /**
     * Tests for the editor role in the dashboards page and the dashboard view page.
     *
     * @throws MalformedURLException
     * @throws XPathExpressionException
     */
    @Test(groups = "wso2.ds.dashboard", description = "test for editor role", dependsOnMethods =
            "testAddDashboardAndAssignRolesBySetting")
    public void testForEditorRole() throws MalformedURLException, XPathExpressionException {
        String dashboardId = dashboardTitle.toLowerCase();
        redirectToLocation(DS_HOME_CONTEXT, DS_DASHBOARDS_CONTEXT);
        WebElement dashboard = getDriver().findElement(By.id(dashboardId));
        assertEquals(DASHBOARD_TITLE, dashboard.findElement(By.id("ues-dashboard-title")).getText());
        assertEquals(DASHBOARD_DESCRIPTION, dashboard.findElement(By.id("ues-dashboard-description")).getText());
        assertTrue(getDriver().isElementPresent(By.cssSelector("#" + dashboardId + " .ues-view")),
                "view element is present in the current UI");
        assertTrue(getDriver().isElementPresent(By.cssSelector("#" + dashboardId + " .ues-edit")),
                "design element is present in the current UI");
        assertTrue(getDriver().isElementPresent(By.cssSelector("#" + dashboardId + " .ues-settings")),
                "settings element is present in the current UI");
        dashboard.findElement(By.cssSelector(".ues-view")).click();
        // Switch the driver to the new window and click on the edit/personalize link
        pushWindow();
        String bodyText = getDriver().findElement(By.tagName("body")).getText();
        assertTrue(bodyText.contains(USERNAME_EDITOR), "Expected Username is not matched");
        getDriver().findElement(By.linkText(USERNAME_EDITOR)).click();
        assertEquals("Edit", getDriver().findElement(By.cssSelector("i.ues-copy")).getAttribute("title"),
                "Unable to find the edit button");
        getDriver().close();
        popWindow();
        logout();
    }

    /**
     * Tests for the viewer role in the dashboards page and the dashboard view page.
     *
     * @throws XPathExpressionException
     * @throws MalformedURLException
     */
{'fraction_non_alphanumeric': 0.10546066252587992, 'fraction_numerical': 0.002587991718426501, 'mean_word_length': 3.543503821281599, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 2}, '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': '4830154', 'n_tokens_mistral': 4256, 'n_tokens_neox': 4185, 'n_words': 1008}