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 org.jboss.arquillian.core.api.annotation.Inject;
19  import org.jboss.hal.meta.token.NameTokens;
20  import org.jboss.hal.resources.Ids;
21  import org.jboss.hal.resources.Names;
22  import org.jboss.hal.testsuite.Console;
23  import org.jboss.hal.testsuite.Random;
24  import org.jboss.hal.testsuite.command.AddCredentialStore;
25  import org.jboss.hal.testsuite.container.WildFlyContainer;
26  import org.jboss.hal.testsuite.fragment.FormFragment;
27  import org.jboss.hal.testsuite.fragment.WizardFragment;
28  import org.jboss.hal.testsuite.fragment.finder.ColumnFragment;
29  import org.jboss.hal.testsuite.model.ResourceVerifier;
30  import org.jboss.hal.testsuite.test.Manatoko;
31  import org.junit.jupiter.api.BeforeAll;
32  import org.junit.jupiter.api.BeforeEach;
33  import org.junit.jupiter.api.Test;
34  import org.openqa.selenium.By;
35  import org.testcontainers.junit.jupiter.Container;
36  import org.testcontainers.junit.jupiter.Testcontainers;
37  import org.wildfly.extras.creaper.core.online.ModelNodeResult;
38  import org.wildfly.extras.creaper.core.online.OnlineManagementClient;
39  import org.wildfly.extras.creaper.core.online.operations.Operations;
40  
41  import static org.jboss.hal.dmr.ModelDescriptionConstants.ALIAS;
42  import static org.jboss.hal.dmr.ModelDescriptionConstants.CLEAR_TEXT;
43  import static org.jboss.hal.dmr.ModelDescriptionConstants.DATASOURCES;
44  import static org.jboss.hal.dmr.ModelDescriptionConstants.NAME;
45  import static org.jboss.hal.dmr.ModelDescriptionConstants.PASSWORD;
46  import static org.jboss.hal.dmr.ModelDescriptionConstants.READ_ALIASES_OPERATION;
47  import static org.jboss.hal.dmr.ModelDescriptionConstants.STORE;
48  import static org.jboss.hal.testsuite.container.WildFlyConfiguration.DEFAULT;
49  import static org.jboss.hal.testsuite.fixtures.DataSourceFixtures.DATA_SOURCE_CREATE_H2_UNIQUE;
50  import static org.jboss.hal.testsuite.fixtures.DataSourceFixtures.H2_PASSWORD;
51  import static org.jboss.hal.testsuite.fixtures.DataSourceFixtures.dataSourceAddress;
52  import static org.jboss.hal.testsuite.fixtures.SecurityFixtures.CREDENTIAL_STORE_CREATE;
53  import static org.jboss.hal.testsuite.fixtures.SecurityFixtures.credentialStoreAddress;
54  import static org.jboss.hal.testsuite.fragment.finder.FinderFragment.configurationSubsystemPath;
55  import static org.junit.jupiter.api.Assertions.assertTrue;
56  
57  /**
58   * Tests the creation of a new H2 database with a reference to a new alias in an existing credential store. The alias specified
59   * during the creation of the database must be added to the existing credential store.
60   */
61  @Manatoko
62  @Testcontainers
63  class DataSourceCreateCredRefTest {
64  
65      @Container static WildFlyContainer wildFly = WildFlyContainer.standalone(DEFAULT);
66      static final String H2_CSS_SELECTOR = "input[type=radio][name=template][value=h2]";
67      static OnlineManagementClient client;
68  
69      @BeforeAll
70      static void setupModel() throws Exception {
71          client = wildFly.managementClient();
72          client.apply(new AddCredentialStore(CREDENTIAL_STORE_CREATE));
73      }
74  
75      @Inject Console console;
76      ColumnFragment column;
77  
78      @BeforeEach
79      void prepare() {
80          column = console.finder(NameTokens.CONFIGURATION, configurationSubsystemPath(DATASOURCES)
81                  .append(Ids.DATA_SOURCE_DRIVER, Ids.asId(Names.DATASOURCES)))
82                  .column(Ids.DATA_SOURCE_CONFIGURATION);
83      }
84  
85      /**
86       * Creates a new H2 database with a credential reference to an existing credential store. Verifies that the database has
87       * been created and the alias is part of the credential store.
88       */
89      @Test
90      void createH2() throws Exception {
91          column.dropdownAction(Ids.DATA_SOURCE_ADD_ACTIONS, Ids.DATA_SOURCE_ADD);
92          WizardFragment wizard = console.wizard();
93          wizard.getRoot().findElement(By.cssSelector(H2_CSS_SELECTOR)).click();
94          wizard.next(Ids.DATA_SOURCE_NAMES_FORM);
95          FormFragment namesForms = wizard.getForm(Ids.DATA_SOURCE_NAMES_FORM);
96          namesForms.text(NAME, DATA_SOURCE_CREATE_H2_UNIQUE);
97  
98          wizard.next(Ids.DATA_SOURCE_DRIVER_FORM);
99          wizard.next(Ids.DATA_SOURCE_CONNECTION_FORM);
100         String alias = Random.name();
101         FormFragment connectionForm = wizard.getForm(Ids.DATA_SOURCE_CONNECTION_FORM);
102         connectionForm.clear(PASSWORD);
103         connectionForm.text(STORE, CREDENTIAL_STORE_CREATE);
104         connectionForm.text(ALIAS, alias);
105         connectionForm.text(CLEAR_TEXT, H2_PASSWORD);
106 
107         wizard.next(Ids.DATA_SOURCE_TEST_CONNECTION);
108         wizard.next(Ids.DATA_SOURCE_REVIEW_FORM); // do nothing here
109         wizard.finishStayOpen();
110         wizard.verifySuccess();
111         wizard.close();
112 
113         String itemId = Ids.dataSourceConfiguration(DATA_SOURCE_CREATE_H2_UNIQUE, false);
114         assertTrue(column.containsItem(itemId));
115         assertTrue(column.isSelected(itemId));
116         Operations operations = new Operations(client);
117         new ResourceVerifier(dataSourceAddress(DATA_SOURCE_CREATE_H2_UNIQUE), client).verifyExists();
118         new ResourceVerifier(credentialStoreAddress(CREDENTIAL_STORE_CREATE), client).verifyTrue(
119                 "Alias not present in credential store",
120                 () -> {
121                     ModelNodeResult result = operations.invoke(READ_ALIASES_OPERATION,
122                             credentialStoreAddress(CREDENTIAL_STORE_CREATE));
123                     if (result.isSuccess() && !result.value().asList().isEmpty()) {
124                         String actualAlias = result.value().asList().get(0).asString();
125                         return alias.equals(actualAlias);
126                     }
127                     return false;
128                 });
129     }
130 }