Skip to content

Commit 8c9b4e1

Browse files
authored
Merge pull request #30157 from KyleAure/ddlgen-doc-examples
2 parents 54f9acd + 594f78b commit 8c9b4e1

File tree

26 files changed

+1581
-429
lines changed

26 files changed

+1581
-429
lines changed

dev/fattest.simplicity/resources/init-sqlserver.sql

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,19 @@ CREATE DATABASE TEST;
44
-- Enable XA connections
55
EXEC sp_sqljdbc_xa_install;
66

7+
-- Enable all users to use distributed transactions
8+
GRANT EXECUTE ON xp_sqljdbc_xa_init to public
9+
GRANT EXECUTE ON xp_sqljdbc_xa_start to public
10+
GRANT EXECUTE ON xp_sqljdbc_xa_end to public
11+
GRANT EXECUTE ON xp_sqljdbc_xa_prepare to public
12+
GRANT EXECUTE ON xp_sqljdbc_xa_commit to public
13+
GRANT EXECUTE ON xp_sqljdbc_xa_rollback to public
14+
GRANT EXECUTE ON xp_sqljdbc_xa_recover to public
15+
GRANT EXECUTE ON xp_sqljdbc_xa_forget to public
16+
GRANT EXECUTE ON xp_sqljdbc_xa_rollback_ex to public
17+
GRANT EXECUTE ON xp_sqljdbc_xa_forget_ex to public
18+
GRANT EXECUTE ON xp_sqljdbc_xa_prepare_ex to public
19+
GRANT EXECUTE ON xp_sqljdbc_xa_init_ex to public
20+
721
-- Allow snapshot isolation
822
ALTER DATABASE TEST SET ALLOW_SNAPSHOT_ISOLATION ON

dev/fattest.simplicity/src/componenttest/topology/database/container/DatabaseContainerType.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ public <T> T cast(Object instance) {
177177
*/
178178
public static DatabaseContainerType valueOf(JdbcDatabaseContainer cont) {
179179
for (DatabaseContainerType elem : values())
180-
if (elem.getContainerClass() == cont.getClass())
180+
if (elem.getContainerClass().isInstance(cont))
181181
return elem;
182182
throw new IllegalArgumentException("Unrecognized JdbcDatabaseContainer class: " + cont.getClass().getCanonicalName());
183183
}

dev/fattest.simplicity/src/componenttest/topology/database/container/DatabaseContainerUtil.java

Lines changed: 366 additions & 338 deletions
Large diffs are not rendered by default.

dev/fattest.simplicity/src/componenttest/topology/utils/DDLGenScript.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
package componenttest.topology.utils;
1111

1212
import static org.junit.Assert.assertEquals;
13+
import static org.junit.Assert.assertFalse;
1314
import static org.junit.Assert.assertTrue;
1415

1516
import java.io.File;
@@ -277,6 +278,21 @@ public DDLGenScriptResult assertDDLFiles(List<String> expectedFileNames) {
277278
return this;
278279
}
279280

281+
/**
282+
* Asserts that none of the generated DDL files in the DDL directory contains the provided substring
283+
*
284+
* @param unexpectedSubstring a substring you do not expect to find in any DDL file names
285+
* @return this
286+
*/
287+
public DDLGenScriptResult assertNoDDLFileLike(String unexpectedSubstring) {
288+
for (String observedFileName : getFileNames()) {
289+
assertFalse("The generated DDL file " + observedFileName + " contained the unexpected substring " + unexpectedSubstring,
290+
observedFileName.contains(unexpectedSubstring));
291+
}
292+
293+
return this;
294+
}
295+
280296
///// Terminations /////
281297

282298
/**
Lines changed: 326 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,326 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2024 IBM Corporation and others.
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License 2.0
5+
* which accompanies this distribution, and is available at
6+
* http://www.eclipse.org/legal/epl-2.0/
7+
*
8+
* SPDX-License-Identifier: EPL-2.0
9+
*******************************************************************************/
10+
package componenttest.topology.database.container;
11+
12+
import static org.junit.Assert.assertEquals;
13+
import static org.junit.Assert.assertNotNull;
14+
import static org.junit.Assert.assertNull;
15+
import static org.junit.Assert.assertTrue;
16+
import static org.junit.Assert.fail;
17+
import static org.junit.Assume.assumeTrue;
18+
import static org.mockito.Mockito.doNothing;
19+
import static org.mockito.Mockito.when;
20+
21+
import java.io.InputStream;
22+
import java.lang.reflect.Method;
23+
import java.util.Set;
24+
25+
import org.junit.Before;
26+
import org.junit.Test;
27+
import org.mockito.ArgumentCaptor;
28+
import org.mockito.Mock;
29+
import org.mockito.MockitoAnnotations;
30+
31+
import com.ibm.websphere.simplicity.config.AuthData;
32+
import com.ibm.websphere.simplicity.config.DataSource;
33+
import com.ibm.websphere.simplicity.config.DatabaseStore;
34+
import com.ibm.websphere.simplicity.config.Fileset;
35+
import com.ibm.websphere.simplicity.config.JavaPermission;
36+
import com.ibm.websphere.simplicity.config.Library;
37+
import com.ibm.websphere.simplicity.config.ServerConfiguration;
38+
import com.ibm.websphere.simplicity.config.ServerConfigurationFactory;
39+
import com.ibm.websphere.simplicity.config.dsprops.Properties;
40+
import com.ibm.websphere.simplicity.config.dsprops.Properties_postgresql;
41+
42+
import componenttest.topology.impl.LibertyServer;
43+
44+
/**
45+
* Tests the DatabaseContainerUtil (soon to be renamed DatabaseContainerProperties) class
46+
*/
47+
public class DatabaseContainerPropertiesTest {
48+
49+
@Mock
50+
LibertyServer server;
51+
52+
@Mock
53+
PostgreSQLContainer cont;
54+
55+
@Before
56+
public void init() {
57+
MockitoAnnotations.initMocks(this);
58+
59+
when(cont.getHost()).thenReturn("localhost");
60+
when(cont.getFirstMappedPort()).thenReturn(9999);
61+
when(cont.getDatabaseName()).thenReturn("TEST");
62+
when(cont.getUsername()).thenReturn("TEST_USER");
63+
when(cont.getPassword()).thenReturn("TEST_PASS");
64+
}
65+
66+
@Test
67+
public void findAuthDataLocationsTest() throws Exception {
68+
when(server.getServerConfiguration()).thenReturn(getServerConfigFor("authDataServer.xml"));
69+
70+
DatabaseContainerUtil inst = DatabaseContainerUtil.build(server, cont);
71+
72+
Set<AuthData> ads = (Set<AuthData>) getFindAuthDataLocations().invoke(inst, server.getServerConfiguration().getDataSources().getById("DefaultDataSource"));
73+
74+
assertEquals(4, ads.size());
75+
76+
for(AuthData ad : ads) {
77+
if(ad.getId() == null) {
78+
assertEquals("myUser3", ad.getUser());
79+
assertEquals("myPass3", ad.getPassword());
80+
continue;
81+
}
82+
83+
if(ad.getId().equals("recoveryAuth")) {
84+
assertEquals("myUser1", ad.getUser());
85+
assertEquals("myPass1", ad.getPassword());
86+
continue;
87+
}
88+
89+
if(ad.getId().equals("userAuth")) {
90+
assertEquals("myUser2", ad.getUser());
91+
assertEquals("myPass2", ad.getPassword());
92+
continue;
93+
}
94+
95+
if(ad.getId().equals("contAuth")) {
96+
assertEquals("myUser4", ad.getUser());
97+
assertEquals("myPass4", ad.getPassword());
98+
continue;
99+
}
100+
101+
fail("Found auth data that should not have been present in findAuthDataLocations: " + ad.toString());
102+
}
103+
}
104+
105+
@Test
106+
public void withDriverReplacementTest() throws Exception {
107+
assumeTrue(!System.getProperty("os.name").equalsIgnoreCase("OS/400"));
108+
109+
when(server.getServerConfiguration()).thenReturn(getServerConfigFor("libraryServer.xml"));
110+
111+
DatabaseContainerUtil inst = DatabaseContainerUtil.build(server, cont)
112+
.withDriverReplacement();
113+
114+
ArgumentCaptor<ServerConfiguration> capturedConfig = ArgumentCaptor.forClass(ServerConfiguration.class);
115+
doNothing().when(server).updateServerConfiguration(capturedConfig.capture());
116+
117+
inst.modify();
118+
119+
ServerConfiguration result = capturedConfig.getValue();
120+
121+
assertNotNull(result.getLibraries().getById("JDBCLibrary"));
122+
123+
Library jdbcLib = result.getLibraries().getById("JDBCLibrary");
124+
assertEquals(1, jdbcLib.getFilesets().size());
125+
126+
Fileset jdbcFs = jdbcLib.getFilesets().get(0);
127+
assertEquals("${shared.resource.dir}/jdbc", jdbcFs.getDir());
128+
assertEquals(DatabaseContainerType.Postgres.getDriverName(), jdbcFs.getIncludes());
129+
130+
assertEquals(1, result.getJavaPermissions().size());
131+
132+
JavaPermission permission = result.getJavaPermissions().get(0);
133+
assertEquals("java.security.AllPermission", permission.getClassName());
134+
assertEquals("${shared.resource.dir}/jdbc/" + DatabaseContainerType.Postgres.getDriverName(), permission.getCodeBase());
135+
}
136+
137+
@Test
138+
public void withDriverVariableTest() throws Exception {
139+
assumeTrue(!System.getProperty("os.name").equalsIgnoreCase("OS/400"));
140+
141+
when(server.getServerConfiguration()).thenReturn(getServerConfigFor("libraryServer.xml"));
142+
143+
DatabaseContainerUtil inst = DatabaseContainerUtil.build(server, cont)
144+
.withDriverVariable();
145+
146+
ArgumentCaptor<ServerConfiguration> capturedConfig = ArgumentCaptor.forClass(ServerConfiguration.class);
147+
doNothing().when(server).updateServerConfiguration(capturedConfig.capture());
148+
149+
inst.modify();
150+
151+
ServerConfiguration result = capturedConfig.getValue();
152+
153+
assertNotNull(result.getLibraries().getById("JDBCLibrary"));
154+
155+
Library jdbcLib = result.getLibraries().getById("JDBCLibrary");
156+
assertEquals(1, jdbcLib.getFilesets().size());
157+
158+
Fileset jdbcFs = jdbcLib.getFilesets().get(0);
159+
assertEquals("${shared.resource.dir}/jdbc", jdbcFs.getDir());
160+
assertEquals("${env.DB_DRIVER}", jdbcFs.getIncludes());
161+
162+
assertEquals(1, result.getJavaPermissions().size());
163+
164+
JavaPermission permission = result.getJavaPermissions().get(0);
165+
assertEquals("java.security.AllPermission", permission.getClassName());
166+
assertEquals("${shared.resource.dir}/jdbc/${env.DB_DRIVER}", permission.getCodeBase());
167+
}
168+
169+
@Test
170+
public void withAuthVariablesTest() throws Exception {
171+
assumeTrue(!System.getProperty("os.name").equalsIgnoreCase("OS/400"));
172+
173+
when(server.getServerConfiguration()).thenReturn(getServerConfigFor("authDataServer.xml"));
174+
175+
DatabaseContainerUtil inst = DatabaseContainerUtil.build(server, cont)
176+
.withAuthVariables("myUserCustom", "myPassCustom");
177+
178+
ArgumentCaptor<ServerConfiguration> capturedConfig = ArgumentCaptor.forClass(ServerConfiguration.class);
179+
doNothing().when(server).updateServerConfiguration(capturedConfig.capture());
180+
181+
inst.modify();
182+
183+
ServerConfiguration result = capturedConfig.getValue();
184+
185+
assertEquals(1, result.getDataSources().size());
186+
187+
DataSource defaultDataSource = result.getDataSources().get(0);
188+
assertTrue(Boolean.valueOf(defaultDataSource.getFatModify()));
189+
assertNotNull(defaultDataSource.getProperties());
190+
assertEquals(1, defaultDataSource.getProperties().size());
191+
192+
//Note: generic properties not properties.postgres because we did not specify .withDatabaseProperties()
193+
assertEquals(1, defaultDataSource.getProperties().size());
194+
Properties dsProps = defaultDataSource.getProperties().get(0);
195+
assertEquals("${env.DB_USER}", dsProps.getUser());
196+
assertEquals("${env.DB_PASS}", dsProps.getPassword());
197+
}
198+
199+
@Test
200+
public void withGenericPropertiesTest() throws Exception {
201+
assumeTrue(!System.getProperty("os.name").equalsIgnoreCase("OS/400"));
202+
203+
when(server.getServerConfiguration()).thenReturn(getServerConfigFor("dataSourceServer.xml"));
204+
205+
DatabaseContainerUtil inst = DatabaseContainerUtil.build(server, cont)
206+
.withGenericProperties();
207+
208+
ArgumentCaptor<ServerConfiguration> capturedConfig = ArgumentCaptor.forClass(ServerConfiguration.class);
209+
doNothing().when(server).updateServerConfiguration(capturedConfig.capture());
210+
211+
inst.modify();
212+
213+
ServerConfiguration result = capturedConfig.getValue();
214+
215+
AuthData recoveryAuth = result.getAuthDataElements().getById("recoveryAuth");
216+
assertNotNull(recoveryAuth);
217+
assertEquals("false", recoveryAuth.getFatModify());
218+
assertEquals("myUser1", recoveryAuth.getUser());
219+
assertEquals("myPass1", recoveryAuth.getPassword());
220+
221+
AuthData userAuth = result.getAuthDataElements().getById("userAuth");
222+
assertNotNull(userAuth);
223+
assertEquals("true", userAuth.getFatModify());
224+
assertEquals(cont.getUsername(), userAuth.getUser());
225+
assertEquals(cont.getPassword(), userAuth.getPassword());
226+
227+
DataSource defaultDataSource = result.getDataSources().getById("DefaultDataSource");
228+
assertNotNull(defaultDataSource);
229+
230+
assertEquals(1, defaultDataSource.getContainerAuthDatas().size());
231+
AuthData containerAuthData = defaultDataSource.getContainerAuthDatas().get(0);
232+
assertNull(containerAuthData.getFatModify());
233+
assertEquals("myUser3", containerAuthData.getUser());
234+
assertEquals("myPass3", containerAuthData.getPassword());
235+
236+
//Note: generic properties because we specified .withGenericProperties();
237+
assertEquals(1, defaultDataSource.getProperties().size());
238+
Properties defaultDataSourceProperties = defaultDataSource.getProperties().get(0);
239+
assertEquals(cont.getHost(), defaultDataSourceProperties.getServerName());
240+
assertEquals(cont.getFirstMappedPort().toString(), defaultDataSourceProperties.getPortNumber());
241+
assertEquals(cont.getDatabaseName(), defaultDataSourceProperties.getDatabaseName());
242+
assertNull("Expected null but was " + defaultDataSourceProperties.getUser(), defaultDataSourceProperties.getUser());
243+
assertNull("Expected null but was " + defaultDataSourceProperties.getPassword(), defaultDataSourceProperties.getPassword());
244+
245+
assertEquals(1, result.getDatabaseStores().size());
246+
DatabaseStore dbStore = result.getDatabaseStores().get(0);
247+
248+
DataSource testDataSource = dbStore.getDataSources().getById("TestDataSource");
249+
assertNotNull(testDataSource);
250+
251+
//Note: generic properties because we specified .withGenericProperties();
252+
assertEquals(1, testDataSource.getProperties().size());
253+
Properties testDataSourceProperties = testDataSource.getProperties().get(0);
254+
assertEquals(cont.getHost(), testDataSourceProperties.getServerName());
255+
assertEquals(cont.getFirstMappedPort().toString(), testDataSourceProperties.getPortNumber());
256+
assertEquals(cont.getDatabaseName(), testDataSourceProperties.getDatabaseName());
257+
assertEquals(cont.getUsername(), testDataSourceProperties.getUser());
258+
assertEquals(cont.getPassword(), testDataSourceProperties.getPassword());
259+
}
260+
261+
@Test
262+
public void withDatabaseProperties() throws Exception {
263+
when(server.getServerConfiguration()).thenReturn(getServerConfigFor("dataSourceServer.xml"));
264+
265+
DatabaseContainerUtil inst = DatabaseContainerUtil.build(server, cont)
266+
.withDatabaseProperties();
267+
268+
ArgumentCaptor<ServerConfiguration> capturedConfig = ArgumentCaptor.forClass(ServerConfiguration.class);
269+
doNothing().when(server).updateServerConfiguration(capturedConfig.capture());
270+
271+
inst.modify();
272+
273+
ServerConfiguration result = capturedConfig.getValue();
274+
275+
DataSource defaultDataSource = result.getDataSources().getById("DefaultDataSource");
276+
assertNotNull(defaultDataSource);
277+
278+
assertEquals(1, defaultDataSource.getContainerAuthDatas().size());
279+
AuthData containerAuthData = defaultDataSource.getContainerAuthDatas().get(0);
280+
assertNull(containerAuthData.getFatModify());
281+
assertEquals("myUser3", containerAuthData.getUser());
282+
assertEquals("myPass3", containerAuthData.getPassword());
283+
284+
//Note: specific properties because we specified .withDatabaseProperties()
285+
assertEquals(1, defaultDataSource.getProperties_postgresql().size());
286+
Properties_postgresql defaultDataSourceProperties = defaultDataSource.getProperties_postgresql().get(0);
287+
assertEquals(cont.getHost(), defaultDataSourceProperties.getServerName());
288+
assertEquals(cont.getFirstMappedPort().toString(), defaultDataSourceProperties.getPortNumber());
289+
assertEquals(cont.getDatabaseName(), defaultDataSourceProperties.getDatabaseName());
290+
assertNull("Expected null but was " + defaultDataSourceProperties.getUser(), defaultDataSourceProperties.getUser());
291+
assertNull("Expected null but was " + defaultDataSourceProperties.getPassword(), defaultDataSourceProperties.getPassword());
292+
293+
assertEquals(1, result.getDatabaseStores().size());
294+
DatabaseStore dbStore = result.getDatabaseStores().get(0);
295+
296+
DataSource testDataSource = dbStore.getDataSources().getById("TestDataSource");
297+
assertNotNull(testDataSource);
298+
299+
//Note: specific properties because we specified .withDatabaseProperties()
300+
assertEquals(1, testDataSource.getProperties_postgresql().size());
301+
Properties_postgresql testDataSourceProperties = testDataSource.getProperties_postgresql().get(0);
302+
assertEquals(cont.getHost(), testDataSourceProperties.getServerName());
303+
assertEquals(cont.getFirstMappedPort().toString(), testDataSourceProperties.getPortNumber());
304+
assertEquals(cont.getDatabaseName(), testDataSourceProperties.getDatabaseName());
305+
assertEquals(cont.getUsername(), testDataSourceProperties.getUser());
306+
assertEquals(cont.getPassword(), testDataSourceProperties.getPassword());
307+
}
308+
309+
// Helper methods to use test server.xml files
310+
311+
private static ServerConfiguration getServerConfigFor(String fileName) throws Exception {
312+
return ServerConfigurationFactory.getInstance().unmarshal(getServerStream(fileName));
313+
}
314+
315+
private static InputStream getServerStream(String fileName) {
316+
return DatabaseContainerPropertiesTest.class.getResourceAsStream(fileName);
317+
}
318+
319+
// Helper methods to get private methods
320+
321+
private static Method getFindAuthDataLocations() throws Exception {
322+
Method m = DatabaseContainerUtil.class.getDeclaredMethod("findAuthDataLocations", DataSource.class);
323+
m.setAccessible(true);
324+
return m;
325+
}
326+
}

0 commit comments

Comments
 (0)