org.onap migration
[vid.git] / vid-automation / src / main / java / vid / automation / test / test / VidBaseTestCase.java
1 package vid.automation.test.test;
2
3 import com.fasterxml.jackson.databind.ObjectMapper;
4 import org.junit.Assert;
5 import org.openecomp.sdc.ci.tests.datatypes.Configuration;
6 import org.openecomp.sdc.ci.tests.datatypes.UserCredentials;
7 import org.openecomp.sdc.ci.tests.execute.setup.SetupCDTest;
8 import org.openecomp.sdc.ci.tests.utilities.FileHandling;
9 import org.openecomp.sdc.ci.tests.utilities.GeneralUIUtils;
10 import org.openqa.selenium.By;
11 import org.openqa.selenium.WebElement;
12 import vid.automation.test.Constants;
13 import vid.automation.test.infra.Click;
14 import vid.automation.test.infra.SelectOption;
15 import vid.automation.test.model.*;
16 import vid.automation.test.sections.*;
17
18 import java.io.File;
19 import java.util.ArrayList;
20 import java.util.List;
21
22 import static org.testng.AssertJUnit.fail;
23
24
25 public class VidBaseTestCase extends SetupCDTest {
26
27     static String currentUserId = null;
28
29     @Override
30     protected UserCredentials getUserCredentials() {
31         ObjectMapper mapper = new ObjectMapper().enableDefaultTyping();
32         try {
33             File configFile = FileHandling.getConfigFile("credentials");
34             if(!configFile.exists()) {
35                 String basePath = System.getProperty("BASE_PATH");
36                 configFile = new File( basePath + File.separator + "conf" + File.separator + "credentials");
37             }
38             Credentials credentials = mapper.readValue(configFile, Credentials.class);
39             return new UserCredentials(credentials.userId, credentials.password, "", "", "");
40         } catch (Exception e) {
41             e.printStackTrace();
42             return null;
43         }
44     }
45
46     @Override
47     protected org.openecomp.sdc.ci.tests.datatypes.Configuration getEnvConfiguration() {
48
49         try {
50             String envUrl = System.getProperty("ENV_URL");
51             boolean isCustomLogin = Boolean.valueOf(System.getProperty("CUSTOM_LOGIN"));
52             Configuration configuration = new org.openecomp.sdc.ci.tests.datatypes.Configuration(envUrl, isCustomLogin);
53             //configuration.setBrowser("chorme");
54             return configuration;
55             //return new org.openecomp.sdc.ci.tests.datatypes.Configuration(envUrl, isCustomLogin);
56         } catch (Exception e) {
57             e.printStackTrace();
58             return null;
59         }
60     }
61
62     @Override
63     protected void loginToLocalSimulator(UserCredentials userCredentials) {
64         LoginExternalPage.performLoginExternal(userCredentials);
65     }
66
67     protected void relogin(Credentials credentials) throws Exception {
68         if (!credentials.userId.equals(currentUserId)) {
69             currentUserId = credentials.userId;
70             UserCredentials userCredentials = new UserCredentials(credentials.userId,
71                     credentials.password, "", "", "");
72             reloginWithNewRole(userCredentials);
73         }
74     }
75
76     /**
77      * Validates that permitted options are enabled and others are disabled.
78      *
79      * @param permittedItems           the list of permitted items.
80      * @param dropdownOptionsClassName the class name of the specific dropdown options.
81      * @return true, if all dropdown options disabled state is according to the permissions.
82      */
83     protected void assertDropdownPermittedItemsByValue(ArrayList<String> permittedItems, String dropdownOptionsClassName) {
84         GeneralUIUtils.ultimateWait();
85         List<WebElement> optionsList =
86                 GeneralUIUtils.getWebElementsListBy(By.className(dropdownOptionsClassName), 30);
87         for (WebElement option :
88                 optionsList) {
89             String optionValue = option.getAttribute("value");
90             if ((option.isEnabled() && !permittedItems.contains(optionValue)) ||
91                     !option.isEnabled() && permittedItems.contains(optionValue)) {
92                 fail(Constants.DROPDOWN_PERMITTED_ASSERT_FAIL_MESSAGE);
93             }
94         }
95     }
96
97     protected void assertAllIsPermitted(String dropdownOptionsClassName) {
98         GeneralUIUtils.ultimateWait();
99         List<WebElement> optionsList =
100                 GeneralUIUtils.getWebElementsListBy(By.className(dropdownOptionsClassName), 30);
101         for (WebElement option :
102                 optionsList) {
103             String optionValue = option.getAttribute("value");
104             if (!option.isEnabled()) {
105                 fail(Constants.DROPDOWN_PERMITTED_ASSERT_FAIL_MESSAGE);
106             }
107         }
108     }
109
110     protected void assertDropdownPermittedItemsByName(ArrayList<String> permittedItems, String dropdownOptionsClassName) {
111         GeneralUIUtils.ultimateWait();
112         List<WebElement> optionsList =
113                 GeneralUIUtils.getWebElementsListBy(By.className(dropdownOptionsClassName), 30);
114         for (WebElement option :
115                 optionsList) {
116             String optionText = option.getText();
117             if ((option.isEnabled() && !permittedItems.contains(optionText)) ||
118                     !option.isEnabled() && permittedItems.contains(optionText)) {
119                 fail(Constants.DROPDOWN_PERMITTED_ASSERT_FAIL_MESSAGE);
120             }
121         }
122     }
123
124     protected void assertViewEditButtonState(String expectedButtonText, String UUID) {
125         WebElement viewEditWebElement = GeneralUIUtils.getWebElementByTestID(Constants.VIEW_EDIT_TEST_ID_PREFIX + UUID, 100);
126         Assert.assertEquals(expectedButtonText, viewEditWebElement.getText());
127         GeneralUIUtils.ultimateWait();
128     }
129
130     protected void addVNF(String name, String lcpRegion, String tenant, String suppressRollback,
131                           String legacyRegion, String productFamily, ArrayList<String> permittedTenants) throws InterruptedException {
132         ViewEditPage viewEditPage = new ViewEditPage();
133
134         viewEditPage.selectNodeInstanceToAdd(name);
135         viewEditPage.generateAndSetInstanceName(Constants.ViewEdit.VNF_INSTANCE_NAME_PREFIX);
136         viewEditPage.selectProductFamily(productFamily);
137         viewEditPage.selectLCPRegion(lcpRegion);
138
139         assertDropdownPermittedItemsByValue(permittedTenants, Constants.ViewEdit.TENANT_OPTION_CLASS);
140         viewEditPage.selectTenant(tenant);
141
142         Click.onFirstSelectOptionById(Constants.OwningEntity.PLATFORM_SELECT_TEST_ID);
143         SelectOption.selectFirstTwoOptionsFromMultiselectById(Constants.OwningEntity.LOB_SELECT_TEST_ID);
144
145         viewEditPage.selectSuppressRollback(suppressRollback);
146
147         viewEditPage.setLegacyRegion(legacyRegion);
148
149         viewEditPage.clickConfirmButton();
150         assertSuccessfulVNFCreation();
151         viewEditPage.clickCloseButton();
152         GeneralUIUtils.ultimateWait();
153     }
154
155     protected void addVFModule(String name, String lcpRegion, String tenant, String suppressRollback,
156                                String legacyRegion, ArrayList<String> permittedTenants) {
157         ViewEditPage viewEditPage = new ViewEditPage();
158
159         viewEditPage.selectVolumeGroupToAdd(name);
160         viewEditPage.generateAndSetInstanceName(Constants.ViewEdit.VF_MODULE_INSTANCE_NAME_PREFIX);
161         viewEditPage.selectLCPRegion(lcpRegion);
162
163         assertDropdownPermittedItemsByValue(permittedTenants, Constants.ViewEdit.TENANT_OPTION_CLASS);
164         viewEditPage.selectTenant(tenant);
165
166         viewEditPage.selectSuppressRollback(suppressRollback);
167
168         viewEditPage.setLegacyRegion(legacyRegion);
169
170         viewEditPage.clickConfirmButton();
171         assertSuccessfulVFModuleCreation();
172         viewEditPage.clickCloseButton();
173         GeneralUIUtils.ultimateWait();
174     }
175
176     protected void addVolumeGroup(String name, String lcpRegion, String tenant, String suppressRollback,
177                                   String legacyRegion, ArrayList<String> permittedTenants) {
178         ViewEditPage viewEditPage = new ViewEditPage();
179
180         viewEditPage.selectVolumeGroupToAdd(name);
181         viewEditPage.generateAndSetInstanceName(Constants.ViewEdit.VOLUME_GROUP_INSTANCE_NAME_PREFIX);
182         viewEditPage.selectLCPRegion(lcpRegion);
183
184         assertDropdownPermittedItemsByValue(permittedTenants, Constants.ViewEdit.TENANT_OPTION_CLASS);
185         viewEditPage.selectTenant(tenant);
186
187         viewEditPage.selectSuppressRollback(suppressRollback);
188
189         viewEditPage.setLegacyRegion(legacyRegion);
190
191         viewEditPage.clickConfirmButton();
192         assertSuccessfulVolumeGroupCreation();
193         viewEditPage.clickCloseButton();
194         GeneralUIUtils.ultimateWait();
195     }
196
197     void assertSuccessfulVNFCreation() {
198         boolean byText = GeneralUIUtils.findAndWaitByText(Constants.ViewEdit.VNF_CREATED_SUCCESSFULLY_TEXT, 100);
199         Assert.assertTrue(Constants.ViewEdit.VNF_CREATION_FAILED_MESSAGE, byText);
200     }
201
202     void assertSuccessfulPNFAssociation() {
203         //TODO
204         boolean byText = GeneralUIUtils.findAndWaitByText(Constants.PnfAssociation.PNF_ASSOCIATED_SUCCESSFULLY_TEXT, 100);
205         Assert.assertTrue(Constants.PnfAssociation.PNF_ASSOCIATED_FAILED_MESSAGE, byText);
206     }
207     void assertSuccessfulVolumeGroupCreation() {
208         boolean byText = GeneralUIUtils.findAndWaitByText(Constants.ViewEdit.VOLUME_GROUP_CREATED_SUCCESSFULLY_TEXT, 100);
209         Assert.assertTrue(Constants.ViewEdit.VOLUME_GROUP_CREATION_FAILED_MESSAGE, byText);
210     }
211
212     void assertSuccessfulVFModuleCreation() {
213         boolean byText = GeneralUIUtils.findAndWaitByText(Constants.ViewEdit.VF_MODULE_CREATED_SUCCESSFULLY_TEXT, 100);
214         Assert.assertTrue(Constants.ViewEdit.VF_MODULE_CREATION_FAILED_MESSAGE, byText);
215     }
216
217     void goToExistingInstanceById(String instanceUUID) {
218         SearchExistingPage searchExistingPage = new SearchExistingPage();
219         SideMenu.navigateToSearchExistingPage();
220         searchExistingPage.searchForInstanceByUuid(instanceUUID);
221         assertViewEditButtonState( Constants.VIEW_EDIT_BUTTON_TEXT, instanceUUID);
222         searchExistingPage.clickEditViewByInstanceId(instanceUUID);
223     }
224
225     void goToExistingInstanceByName(String instanceName) {
226         SearchExistingPage searchExistingPage = new SearchExistingPage();
227         SideMenu.navigateToSearchExistingPage();
228         searchExistingPage.searchForInstanceByName(instanceName);
229         WebElement instanceIdRow = GeneralUIUtils.getWebElementByTestID(Constants.INSTANCE_ID_FOR_NAME_TEST_ID_PREFIX + instanceName, 30);
230         String instanceId = instanceIdRow.getText();
231         assertViewEditButtonState( Constants.VIEW_EDIT_BUTTON_TEXT, instanceId);
232         searchExistingPage.clickEditViewByInstanceId(instanceId);
233     }
234
235 }