aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/org/onap/portalng/e2e/steps/user_administration/UserAdministrationSteps.java
blob: 1460e3782945c0a46abbbfbd6c956c048f84a864 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/*
 *   Copyright (c) 2023. Deutsche Telekom AG
 *
 *   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.
 *
 *   SPDX-License-Identifier: Apache-2.0
 *
 */

package org.onap.portalng.e2e.steps.user_administration;

import com.codeborne.selenide.Condition;
import com.codeborne.selenide.Selenide;
import com.codeborne.selenide.SelenideElement;
import org.junit.Assert;
import org.onap.portalng.e2e.steps.main.MainSteps;
import org.onap.portalng.e2e.helpers.UiSessionVariables;
import org.onap.portalng.e2e.pages.PopUpDialogPage;
import org.onap.portalng.e2e.steps.common.CommonSteps;
import org.onap.portalng.e2e.steps.common.TableSteps;
import org.onap.portalng.e2e.types.Button;
import org.onap.portalng.e2e.types.UserRole;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.support.ui.ExpectedConditions;

import java.util.Optional;

import static com.codeborne.selenide.Condition.*;
import static com.codeborne.selenide.Selenide.$;
import static com.codeborne.selenide.Selenide.$$;
import static org.assertj.core.api.Assertions.assertThat;
import static org.onap.portalng.e2e.helpers.CommonUtils.checkPresenceOfElement;
import static org.onap.portalng.e2e.helpers.CommonUtils.checkTextOfElement;
import static org.onap.portalng.e2e.pages.PageWithTable.ROW_DELETE_BUTTON;
import static org.onap.portalng.e2e.pages.PopUpDialogPage.ALERT_SUCCESS;
import static org.onap.portalng.e2e.pages.PopUpDialogPage.*;
import static org.onap.portalng.e2e.pages.UserAdministrationPage.*;

public class UserAdministrationSteps {

    public static void userClicksOnCreateUserButton() {
        $(BUTTON_CREATE).shouldHave(Condition.text("Create User"));
        $(BUTTON_CREATE).click();
    }

    public static void userVerifiesCreateUserForm() {
        $(TITLE_H2).shouldHave(Condition.text("Create User"));

        $$(TITLE_H4).get(0).shouldHave(Condition.text("Set User Data"));
        $$(TITLE_H4).get(1).shouldHave(Condition.text("Set Roles"));

        $$(TITLE_H5).get(1).shouldHave(Condition.text("Available"));
        $$(TITLE_H5).get(2).shouldHave(Condition.text("Assigned"));

        $(LABEL_ID).shouldHave(Condition.text("ID"));
        $(LABEL_USERNAME).shouldHave(Condition.text("Username"));
        $(LABEL_EMAIL).shouldHave(Condition.text("Email"));
        $(LABEL_FIRST_NAME).shouldHave(Condition.text("First Name"));
        $(LABEL_LAST_NAME).shouldHave(Condition.text("Last Name"));

        $(INPUT_ID).shouldBe(Condition.disabled);
    }

    public static void userFillsDataNecessaryForCreatingTheUserInTheForm(String username) {
        $(INPUT_USERNAME).setValue(username);
        $(INPUT_EMAIL).setValue(username + "@test.user");
        $(INPUT_FIRST_NAME).setValue("E2E");
        $(INPUT_LAST_NAME).setValue("TestUser");
        UiSessionVariables.USERNAME.set(username);

        $$(FORM_CHECK).findBy(Condition.text("onap_admin")).$("input").click();
    }
    public static void userFillsSpecificValuesForUserCreation(String input, String value) {
        switch (input) {
            case "EMAIL":
                if (value.equals("clear")) {
                    $(INPUT_EMAIL).setValue(" ");
                    $(INPUT_EMAIL).sendKeys(Keys.BACK_SPACE);
                } else {
                    $(INPUT_EMAIL).setValue(" ");
                    $(INPUT_EMAIL).sendKeys(Keys.BACK_SPACE);
                    $(INPUT_EMAIL).setValue(value);
                }
                break;
            case "USERNAME":
                if (value.equals("clear")) {
                    $(INPUT_USERNAME).setValue(" ");
                    $(INPUT_USERNAME).sendKeys(Keys.BACK_SPACE);
                } else {
                    $(INPUT_USERNAME).setValue(value);
                }
                break;
            case "LAST_NAME":
                $(INPUT_LAST_NAME).setValue(value);
                break;
            case "FIRST_NAME":
                $(INPUT_FIRST_NAME).setValue(value);
                break;
            default:
                throw new IllegalStateException("Unexpected value: " + input);
        }
    }

    public static void userClicksOnSaveButtonInTheForm() {
        $(BUTTON_SAVE).click();
    }

    public static void userClicksOnCancelButtonInTheForm() {
        $(BUTTON_CANCEL).click();
    }

    public static void userSeeFeedback(String invalidInput, String message) {
        switch (invalidInput) {
            case "REQUIRED_USERNAME":
                checkTextOfElement($(REQUIRED_USERNAME), message);
                checkPresenceOfElement($(INVALID_INPUT));
                break;
            case "REQUIRED_EMAIL":
                checkTextOfElement($(REQUIRED_EMAIL), message);
                checkPresenceOfElement($(INVALID_INPUT));
                break;
            case "INVALID_USERNAME":
                checkTextOfElement($(INVALID_USERNAME), message);
                checkPresenceOfElement($(INVALID_INPUT));
                break;
            case "INVALID_EMAIL":
                checkTextOfElement($(INVALID_EMAIL), message);
                checkPresenceOfElement($(INVALID_INPUT));
                break;
            case "WRONG_EMAIL_FORMAT":
                checkTextOfElement($(WRONG_EMAIL_FORMAT), message);
                checkPresenceOfElement($(INVALID_INPUT));
                break;
            default:
                throw new IllegalArgumentException(invalidInput + " is not implemented yet");

        }
    }


    public static void userVerifiesNewUserInUserList() throws InterruptedException {
        Selenide.Wait().until(ExpectedConditions.visibilityOf($$(TABLE_ROWS).get(0)));

        for (SelenideElement row : $$(TABLE_ROWS)) {
            if ($(row).findAll(By.id("td")).findBy(Condition.text("e2e-test-user")).exists()) {
                Assert.assertEquals("e2e-test-user", $(row).findAll(By.id("td")).get(0).getText());
                Assert.assertEquals("E2e", $(row).findAll(By.id("td")).get(1).getText());
                Assert.assertEquals("TestUser", $(row).findAll(By.id("td")).get(2).getText());
                Assert.assertEquals("e2e@test.user", $(row).findAll(By.id("td")).get(3).getText());
                Assert.assertEquals("onap_admin", $(row).findAll(By.id("td")).get(4).getText());
            }
        }
    }

    public static void deleteUser(String username) {
        MainSteps.userClicksOnUserMenuBtn();

        Optional<SelenideElement> userRow = TableSteps.findRowWithText(username, true);
        if (userRow.isPresent()) {
            assertThat(userRow.isPresent()).as("User cannot be deleted, user with " + username + " was not found.").isTrue();
            userRow.get().$(ROW_DELETE_BUTTON).click();
            $(DIALOG_HEADER).shouldHave(text("Delete User"));
            $(DIALOG_BODY).shouldHave(text("Are you sure, that you want to delete user: " + username + " ? "));
            $(DIALOG_APPLY_BUTTON).shouldHave(text("Delete"));
            $(DIALOG_APPLY_BUTTON).click();
            $$(ALERT_SUCCESS).last().shouldHave(text("User successfully deleted."));
            userRow.get().should(not(exist));
        }
    }

    public static void clickOnEditBtn(String username) {
        CommonSteps.userClicksOnButtonInMainMenu(Button.USERS);

        Optional<SelenideElement> userRow = TableSteps.findRowWithText(username, true);
        assertThat(userRow.isPresent()).as("User cannot be updated, user with " + username + " was not found.").isTrue();
        userRow.get().$(BUTTON_EDIT).click();
        userRow.get().should(not(exist));
    }

    public static void userCheckAllElementsOnEditPage() {
        checkTextOfElement($(TITLE_LBL), "Edit User");
        checkPresenceOfElement($(USER_DATA_HEADER));
        checkPresenceOfElement($(INPUT_ID));
        checkPresenceOfElement($(INPUT_USERNAME));
        checkPresenceOfElement($(INPUT_EMAIL));
        checkPresenceOfElement($(INPUT_FIRST_NAME));
        checkPresenceOfElement($(INPUT_LAST_NAME));
        checkTextOfElement($(LABEL_ID), "ID");
        checkTextOfElement($(LABEL_USERNAME), "Username");
        checkTextOfElement($(LABEL_EMAIL), "Email");
        checkTextOfElement($(LABEL_FIRST_NAME), "First Name");
        checkTextOfElement($(LABEL_LAST_NAME), "Last Name");
        checkPresenceOfElement($(ROLES_HEADER));
        checkTextOfElement($(AVAILABLE_ROLES), "Available");
        checkTextOfElement($(ASSIGNED_ROLES), "Assigned");
        checkTextOfElement($(ONAP_DESIGNER_ROLE), "onap_designer");
        checkTextOfElement($(ONAP_OPERATOR_ROLE), "onap_operator");
        checkTextOfElement($(ONAP_ADMIN_ROLE), "onap_admin");
        checkTextOfElement($(BUTTON_CANCEL), "Cancel");
        checkTextOfElement($(BUTTON_SAVE), "Save");
    }

    public static void userFillsValuesToEditFormPage() {
        $(INPUT_FIRST_NAME).setValue("firstName");
        $(INPUT_LAST_NAME).setValue("lastName");
        $(INPUT_EMAIL).setValue("e2e" + "@test.com");
    }

    public static void userClickOnBtnOnEditPage(String button) {
        switch (button) {
            case "Edit":
                $(BUTTON_SAVE).click();
                break;
            case "Cancel":
                $(BUTTON_CANCEL).click();
                break;
            default:
                throw new IllegalArgumentException("Button was not implemented");
        }
    }

    public static void userFillsEmailValuesForUserCreation(String email) {
        $(INPUT_EMAIL).clear();
        $(INPUT_EMAIL).setValue(email);
    }

    public static void userChecksPopupOnUserPage(String popUpType, String text) {
        if (popUpType.equals("SUCCESS")) {
            checkTextOfElement($(PopUpDialogPage.ALERT_SUCCESS), text);
        } else if (popUpType.equals("ERROR")) {
            checkTextOfElement($(PopUpDialogPage.ALERT_ERROR), text);
        }
    }

    public static void assignAvailableRolesToUser(UserRole userRole) {
        SelenideElement availableRolesBlock = $(AVAILABLE_ROLES_HEADER).parent();
        SelenideElement availableRole = availableRolesBlock.find(userRoleCheckBox(userRole));
        availableRole.click();
        availableRole.should(not(exist));
    }

    public static void unAssignRoleFromUser(UserRole userRole) {
        SelenideElement assignedRolesBlock = $(ASSIGNED_ROLES_HEADER).parent();
        SelenideElement assignedRole = assignedRolesBlock.find(userRoleCheckBox(userRole));
        assignedRole.click();
        assignedRole.should(not(exist));
    }

    public static void verifyThatUserRoleIsAvailable(UserRole userRole) {
        SelenideElement availableRolesBlock = $(AVAILABLE_ROLES_HEADER).parent();
        availableRolesBlock.find(userRoleCheckBox(userRole)).should(exist);
    }

    public static void verifyThatUserRoleIsAssigned(UserRole userRole) {
        SelenideElement assignedRolesBlock = $(ASSIGNED_ROLES_HEADER).parent();
        assignedRolesBlock.find(userRoleCheckBox(userRole)).should(exist);
    }

    public static By userRoleCheckBox(UserRole userRole) {
        switch (userRole) {
            case ONAP_DESIGNER:
                return roleCheckbox("onap_designer");
            case ONAP_OPERATOR:
                return roleCheckbox("onap_operator");
            case ONAP_ADMIN:
                return roleCheckbox("onap_admin");
            default:
                throw new IllegalArgumentException("Not recognized role");
        }
    }
}