View Javadoc
1   /*
2    *  Copyright 2022 Red Hat
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      https://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   */
16  package org.jboss.hal.testsuite.configuration.datasource;
17  
18  import java.util.function.Consumer;
19  
20  import org.jboss.arquillian.core.api.annotation.Inject;
21  import org.jboss.arquillian.drone.api.annotation.Drone;
22  import org.jboss.hal.meta.token.NameTokens;
23  import org.jboss.hal.resources.Ids;
24  import org.jboss.hal.resources.Names;
25  import org.jboss.hal.testsuite.Console;
26  import org.jboss.hal.testsuite.Random;
27  import org.jboss.hal.testsuite.container.WildFlyContainer;
28  import org.jboss.hal.testsuite.fragment.FormFragment;
29  import org.jboss.hal.testsuite.fragment.WizardFragment;
30  import org.jboss.hal.testsuite.fragment.finder.ColumnFragment;
31  import org.jboss.hal.testsuite.model.ResourceVerifier;
32  import org.jboss.hal.testsuite.test.Manatoko;
33  import org.junit.jupiter.api.Assertions;
34  import org.junit.jupiter.api.BeforeAll;
35  import org.junit.jupiter.api.BeforeEach;
36  import org.junit.jupiter.api.Test;
37  import org.openqa.selenium.By;
38  import org.openqa.selenium.InvalidElementStateException;
39  import org.openqa.selenium.TimeoutException;
40  import org.openqa.selenium.WebDriver;
41  import org.testcontainers.junit.jupiter.Container;
42  import org.testcontainers.junit.jupiter.Testcontainers;
43  import org.wildfly.extras.creaper.commands.datasources.AddDataSource;
44  import org.wildfly.extras.creaper.core.online.OnlineManagementClient;
45  import org.wildfly.extras.creaper.core.online.operations.admin.Administration;
46  
47  import static org.jboss.arquillian.graphene.Graphene.waitModel;
48  import static org.jboss.hal.dmr.ModelDescriptionConstants.CONNECTION_URL;
49  import static org.jboss.hal.dmr.ModelDescriptionConstants.DATASOURCES;
50  import static org.jboss.hal.dmr.ModelDescriptionConstants.DRIVER_NAME;
51  import static org.jboss.hal.dmr.ModelDescriptionConstants.JNDI_NAME;
52  import static org.jboss.hal.dmr.ModelDescriptionConstants.NAME;
53  import static org.jboss.hal.dmr.ModelDescriptionConstants.PASSWORD;
54  import static org.jboss.hal.dmr.ModelDescriptionConstants.USER_NAME;
55  import static org.jboss.hal.testsuite.container.WildFlyConfiguration.DEFAULT;
56  import static org.jboss.hal.testsuite.fixtures.DataSourceFixtures.DATA_SOURCE_CREATE_CUSTOM;
57  import static org.jboss.hal.testsuite.fixtures.DataSourceFixtures.DATA_SOURCE_CREATE_EXISTING;
58  import static org.jboss.hal.testsuite.fixtures.DataSourceFixtures.DATA_SOURCE_CREATE_H2;
59  import static org.jboss.hal.testsuite.fixtures.DataSourceFixtures.DATA_SOURCE_CREATE_TEST_CANCEL;
60  import static org.jboss.hal.testsuite.fixtures.DataSourceFixtures.DATA_SOURCE_CREATE_TEST_CHANGE;
61  import static org.jboss.hal.testsuite.fixtures.DataSourceFixtures.DATA_SOURCE_CREATE_TEST_FINISH;
62  import static org.jboss.hal.testsuite.fixtures.DataSourceFixtures.H2_CONNECTION_URL;
63  import static org.jboss.hal.testsuite.fixtures.DataSourceFixtures.H2_DRIVER;
64  import static org.jboss.hal.testsuite.fixtures.DataSourceFixtures.H2_JNDI_NAME;
65  import static org.jboss.hal.testsuite.fixtures.DataSourceFixtures.H2_NAME;
66  import static org.jboss.hal.testsuite.fixtures.DataSourceFixtures.H2_PASSWORD;
67  import static org.jboss.hal.testsuite.fixtures.DataSourceFixtures.H2_USER_NAME;
68  import static org.jboss.hal.testsuite.fixtures.DataSourceFixtures.dataSourceAddress;
69  import static org.jboss.hal.testsuite.fixtures.DataSourceFixtures.h2ConnectionUrl;
70  import static org.jboss.hal.testsuite.fragment.finder.FinderFragment.configurationSubsystemPath;
71  import static org.junit.jupiter.api.Assertions.assertEquals;
72  import static org.junit.jupiter.api.Assertions.assertTrue;
73  import static org.junit.jupiter.api.Assertions.fail;
74  
75  @Manatoko
76  @Testcontainers
77  class DataSourceCreateTest {
78  
79      @Container static WildFlyContainer wildFly = WildFlyContainer.standalone(DEFAULT);
80      static final String H2_CSS_SELECTOR = "input[type=radio][name=template][value=h2]";
81      static OnlineManagementClient client;
82  
83      @BeforeAll
84      static void setupModel() throws Exception {
85          client = wildFly.managementClient();
86          client.apply(new AddDataSource.Builder<>(DATA_SOURCE_CREATE_EXISTING)
87                  .driverName("h2")
88                  .jndiName(Random.jndiName(DATA_SOURCE_CREATE_EXISTING))
89                  .connectionUrl(h2ConnectionUrl(DATA_SOURCE_CREATE_EXISTING))
90                  .build());
91      }
92  
93      @Drone WebDriver browser;
94      @Inject Console console;
95      ColumnFragment column;
96  
97      @BeforeEach
98      void prepare() {
99          column = console.finder(NameTokens.CONFIGURATION, configurationSubsystemPath(DATASOURCES)
100                 .append(Ids.DATA_SOURCE_DRIVER, Ids.asId(Names.DATASOURCES)))
101                 .column(Ids.DATA_SOURCE_CONFIGURATION);
102     }
103 
104     /**
105      * Create a data source that already exists
106      */
107     @Test
108     void createExisting() {
109         column.dropdownAction(Ids.DATA_SOURCE_ADD_ACTIONS, Ids.DATA_SOURCE_ADD);
110         WizardFragment wizard = console.wizard();
111         wizard.getRoot().findElement(By.cssSelector(H2_CSS_SELECTOR)).click();
112         wizard.next(Ids.DATA_SOURCE_NAMES_FORM);
113         FormFragment namesForms = wizard.getForm(Ids.DATA_SOURCE_NAMES_FORM);
114         namesForms.clear(NAME);
115         namesForms.text(NAME, DATA_SOURCE_CREATE_EXISTING);
116         wizard.next();
117 
118         namesForms.expectError(NAME);
119         wizard.cancel();
120     }
121 
122     /**
123      * Create a custom H2 data source
124      */
125     @Test
126     void createCustom() throws Exception {
127         // choose template
128         column.dropdownAction(Ids.DATA_SOURCE_ADD_ACTIONS, Ids.DATA_SOURCE_ADD);
129         WizardFragment wizard = console.wizard();
130         wizard.getRoot().findElement(By.cssSelector("input[type=radio][name=template][value=custom]")).click();
131         wizard.next(Ids.DATA_SOURCE_NAMES_FORM);
132 
133         // attributes
134         FormFragment namesForms = wizard.getForm(Ids.DATA_SOURCE_NAMES_FORM);
135         namesForms.text(NAME, DATA_SOURCE_CREATE_CUSTOM);
136         String jndiName = Random.jndiName(DATA_SOURCE_CREATE_CUSTOM);
137         namesForms.text(JNDI_NAME, jndiName);
138         wizard.next(Ids.DATA_SOURCE_DRIVER_FORM);
139 
140         // JDBC driver
141         FormFragment driverForm = wizard.getForm(Ids.DATA_SOURCE_DRIVER_FORM);
142         driverForm.text(DRIVER_NAME, H2_DRIVER);
143         wizard.next(Ids.DATA_SOURCE_CONNECTION_FORM);
144 
145         // connection
146         FormFragment connectionForm = wizard.getForm(Ids.DATA_SOURCE_CONNECTION_FORM);
147         String connectionUrl = h2ConnectionUrl(DATA_SOURCE_CREATE_CUSTOM);
148         connectionForm.text(CONNECTION_URL, connectionUrl);
149         wizard.next(Ids.DATA_SOURCE_TEST_CONNECTION);
150 
151         // test connection
152         wizard.next(Ids.DATA_SOURCE_REVIEW_FORM); // do nothing here
153 
154         // review
155         FormFragment reviewForm = wizard.getForm(Ids.DATA_SOURCE_REVIEW_FORM);
156         assertEquals(DATA_SOURCE_CREATE_CUSTOM, reviewForm.value(NAME));
157         assertEquals(jndiName, reviewForm.value(JNDI_NAME));
158         assertEquals(connectionUrl, reviewForm.value(CONNECTION_URL));
159         assertEquals(H2_DRIVER, reviewForm.value(DRIVER_NAME));
160 
161         // do it
162         wizard.finishStayOpen();
163         wizard.verifySuccess();
164         wizard.close();
165 
166         String itemId = Ids.dataSourceConfiguration(DATA_SOURCE_CREATE_CUSTOM, false);
167         assertTrue(column.containsItem(itemId));
168         assertTrue(column.isSelected(itemId));
169         new ResourceVerifier(dataSourceAddress(DATA_SOURCE_CREATE_CUSTOM), client).verifyExists();
170     }
171 
172     /**
173      * Create a data source using the H2 template
174      */
175     @Test
176     void createH2() throws Exception {
177         column.dropdownAction(Ids.DATA_SOURCE_ADD_ACTIONS, Ids.DATA_SOURCE_ADD);
178         WizardFragment wizard = console.wizard();
179         wizard.getRoot().findElement(By.cssSelector(H2_CSS_SELECTOR)).click();
180         wizard.next(Ids.DATA_SOURCE_NAMES_FORM);
181         wizard.next(Ids.DATA_SOURCE_DRIVER_FORM);
182         wizard.next(Ids.DATA_SOURCE_CONNECTION_FORM);
183         wizard.next(Ids.DATA_SOURCE_TEST_CONNECTION);
184         wizard.next(Ids.DATA_SOURCE_REVIEW_FORM); // do nothing here
185 
186         FormFragment reviewForm = wizard.getForm(Ids.DATA_SOURCE_REVIEW_FORM);
187         reviewForm.showSensitive(USER_NAME);
188         reviewForm.showSensitive(PASSWORD);
189         assertEquals(H2_NAME, reviewForm.value(NAME));
190         assertEquals(H2_JNDI_NAME, reviewForm.value(JNDI_NAME));
191         assertEquals(H2_CONNECTION_URL, reviewForm.value(CONNECTION_URL));
192         assertEquals(H2_DRIVER, reviewForm.value(DRIVER_NAME));
193         assertEquals(H2_USER_NAME, reviewForm.value(USER_NAME));
194         assertEquals(H2_PASSWORD, reviewForm.value(PASSWORD));
195 
196         wizard.finishStayOpen();
197         wizard.verifySuccess();
198         wizard.close();
199 
200         String itemId = Ids.dataSourceConfiguration(DATA_SOURCE_CREATE_H2, false);
201         assertTrue(column.containsItem(itemId));
202         assertTrue(column.isSelected(itemId));
203         new ResourceVerifier(dataSourceAddress(DATA_SOURCE_CREATE_H2), client).verifyExists();
204     }
205 
206     /**
207      * Create a data source, test the connection and cancel the wizard
208      */
209     @Test
210     void createTestCancel() throws Exception {
211         Administration administration = new Administration(client);
212         administration.reloadIfRequired();
213 
214         column.dropdownAction(Ids.DATA_SOURCE_ADD_ACTIONS, Ids.DATA_SOURCE_ADD);
215         WizardFragment wizard = console.wizard();
216         wizard.getRoot().findElement(By.cssSelector(H2_CSS_SELECTOR)).click();
217         wizard.next(Ids.DATA_SOURCE_NAMES_FORM);
218         FormFragment namesForms = wizard.getForm(Ids.DATA_SOURCE_NAMES_FORM);
219         namesForms.clear(NAME);
220         namesForms.text(NAME, DATA_SOURCE_CREATE_TEST_CANCEL);
221         namesForms.clear(JNDI_NAME);
222         namesForms.text(JNDI_NAME, Random.jndiName(DATA_SOURCE_CREATE_TEST_CANCEL));
223         wizard.next(Ids.DATA_SOURCE_DRIVER_FORM);
224         wizard.next(Ids.DATA_SOURCE_CONNECTION_FORM);
225         wizard.next(Ids.DATA_SOURCE_TEST_CONNECTION);
226 
227         browser.findElement(By.id(Ids.DATA_SOURCE_TEST_CONNECTION)).click();
228         wizard.verifySuccess(waitModel());
229         handlePossibleException(wizard, WizardFragment::cancel, "not possible to click Cancel button in the wizard!");
230 
231         String itemId = Ids.dataSourceConfiguration(DATA_SOURCE_CREATE_TEST_CANCEL, false);
232         Assertions.assertFalse(column.containsItem(itemId));
233         new ResourceVerifier(dataSourceAddress(DATA_SOURCE_CREATE_TEST_CANCEL), client).verifyDoesNotExist();
234     }
235 
236     /**
237      * Create a data source, test the connection and finish the wizard
238      */
239     @Test
240     void createTestFinish() throws Exception {
241         column.dropdownAction(Ids.DATA_SOURCE_ADD_ACTIONS, Ids.DATA_SOURCE_ADD);
242         WizardFragment wizard = console.wizard();
243         wizard.getRoot().findElement(By.cssSelector(H2_CSS_SELECTOR)).click();
244         wizard.next(Ids.DATA_SOURCE_NAMES_FORM);
245         FormFragment namesForms = wizard.getForm(Ids.DATA_SOURCE_NAMES_FORM);
246         namesForms.clear(NAME);
247         namesForms.text(NAME, DATA_SOURCE_CREATE_TEST_FINISH);
248         namesForms.clear(JNDI_NAME);
249         String jndiName = Random.jndiName(DATA_SOURCE_CREATE_TEST_FINISH);
250         namesForms.text(JNDI_NAME, jndiName);
251         wizard.next(Ids.DATA_SOURCE_DRIVER_FORM);
252         wizard.next(Ids.DATA_SOURCE_CONNECTION_FORM);
253         wizard.next(Ids.DATA_SOURCE_TEST_CONNECTION);
254 
255         browser.findElement(By.id(Ids.DATA_SOURCE_TEST_CONNECTION)).click();
256         wizard.verifySuccess(waitModel());
257         try {
258             wizard.next(Ids.DATA_SOURCE_REVIEW_FORM);
259         } catch (TimeoutException e) {
260             fail("HAL-1440: the Review page of the wizard didn't appear!");
261             e.printStackTrace();
262         }
263 
264         FormFragment reviewForm = wizard.getForm(Ids.DATA_SOURCE_REVIEW_FORM);
265         reviewForm.showSensitive(USER_NAME);
266         reviewForm.showSensitive(PASSWORD);
267         assertEquals(DATA_SOURCE_CREATE_TEST_FINISH, reviewForm.value(NAME));
268         assertEquals(jndiName, reviewForm.value(JNDI_NAME));
269         assertEquals(H2_CONNECTION_URL, reviewForm.value(CONNECTION_URL));
270         assertEquals(H2_DRIVER, reviewForm.value(DRIVER_NAME));
271         assertEquals(H2_USER_NAME, reviewForm.value(USER_NAME));
272         assertEquals(H2_PASSWORD, reviewForm.value(PASSWORD));
273 
274         wizard.finishStayOpen();
275         wizard.verifySuccess();
276         wizard.close();
277 
278         String itemId = Ids.dataSourceConfiguration(DATA_SOURCE_CREATE_TEST_FINISH, false);
279         assertTrue(column.containsItem(itemId));
280         assertTrue(column.isSelected(itemId));
281         new ResourceVerifier(dataSourceAddress(DATA_SOURCE_CREATE_TEST_FINISH), client).verifyExists();
282     }
283 
284     /**
285      * Create a data source, test the connection, make changes and finish the wizard
286      */
287     @Test
288     void createTestChange() throws Exception {
289         column.dropdownAction(Ids.DATA_SOURCE_ADD_ACTIONS, Ids.DATA_SOURCE_ADD);
290         WizardFragment wizard = console.wizard();
291         wizard.getRoot().findElement(By.cssSelector(H2_CSS_SELECTOR)).click();
292         wizard.next(Ids.DATA_SOURCE_NAMES_FORM);
293         FormFragment namesForms = wizard.getForm(Ids.DATA_SOURCE_NAMES_FORM);
294         namesForms.clear(NAME);
295         namesForms.text(NAME, DATA_SOURCE_CREATE_TEST_CHANGE);
296         namesForms.clear(JNDI_NAME);
297         String jndiName = Random.jndiName(DATA_SOURCE_CREATE_TEST_CHANGE);
298         namesForms.text(JNDI_NAME, jndiName);
299         wizard.next(Ids.DATA_SOURCE_DRIVER_FORM);
300         wizard.next(Ids.DATA_SOURCE_CONNECTION_FORM);
301         wizard.next(Ids.DATA_SOURCE_TEST_CONNECTION);
302 
303         browser.findElement(By.id(Ids.DATA_SOURCE_TEST_CONNECTION)).click();
304         wizard.verifySuccess(waitModel());
305         handlePossibleException(wizard, wzd -> wzd.back(Ids.DATA_SOURCE_CONNECTION_FORM),
306                 "not possible to click Back button in the wizard!");
307 
308         FormFragment connectionForm = wizard.getForm(Ids.DATA_SOURCE_CONNECTION_FORM);
309         connectionForm.clear(USER_NAME);
310         connectionForm.text(USER_NAME, "changed");
311 
312         wizard.next(Ids.DATA_SOURCE_TEST_CONNECTION);
313         wizard.next(Ids.DATA_SOURCE_REVIEW_FORM);
314 
315         FormFragment reviewForm = wizard.getForm(Ids.DATA_SOURCE_REVIEW_FORM);
316         reviewForm.showSensitive(USER_NAME);
317         reviewForm.showSensitive(PASSWORD);
318         assertEquals(DATA_SOURCE_CREATE_TEST_CHANGE, reviewForm.value(NAME));
319         assertEquals(jndiName, reviewForm.value(JNDI_NAME));
320         assertEquals(H2_CONNECTION_URL, reviewForm.value(CONNECTION_URL));
321         assertEquals(H2_DRIVER, reviewForm.value(DRIVER_NAME));
322         assertEquals("changed", reviewForm.value(USER_NAME));
323         assertEquals(H2_PASSWORD, reviewForm.value(PASSWORD));
324 
325         wizard.finishStayOpen();
326         wizard.verifySuccess();
327         wizard.close();
328 
329         String itemId = Ids.dataSourceConfiguration(DATA_SOURCE_CREATE_TEST_CHANGE, false);
330         assertTrue(column.containsItem(itemId));
331         assertTrue(column.isSelected(itemId));
332         new ResourceVerifier(dataSourceAddress(DATA_SOURCE_CREATE_TEST_CHANGE), client)
333                 .verifyExists()
334                 .verifyAttribute(USER_NAME, "changed");
335     }
336 
337     private void handlePossibleException(WizardFragment wizard, Consumer<WizardFragment> action, String failMessage) {
338         try {
339             action.accept(wizard);
340         } catch (InvalidElementStateException e) {
341             // clean up opened wizard first
342             if (wizard.getRoot().isDisplayed()) {
343                 wizard.close();
344             }
345             fail("HAL-1440: " + failMessage);
346             e.printStackTrace();
347         }
348     }
349 }