Skip to content

Commit 2df99f4

Browse files
authored
Merge pull request #34081 from TheoGkoumas/refactor-optim
Jakarta Security 4.0 FAT: Test classes optimization refactoring
2 parents cd7638b + 0c4038f commit 2df99f4

File tree

15 files changed

+976
-150
lines changed

15 files changed

+976
-150
lines changed

dev/io.openliberty.security.jakartasec.4.0.internal_fat/fat/src/io/openliberty/security/jakartasec/fat/FATSuite.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,16 @@
1818

1919
import componenttest.custom.junit.runner.AlwaysPassesTest;
2020
import io.openliberty.security.jakartasec.fat.tests.InMemoryIdentityStoreTests;
21+
import io.openliberty.security.jakartasec.fat.tests.MultipleHAMCustomTests;
2122
import io.openliberty.security.jakartasec.fat.tests.MultipleHAMDuplicateTests;
22-
import io.openliberty.security.jakartasec.fat.tests.MultipleHAMTests;
2323

2424
@RunWith(Suite.class)
2525
@SuiteClasses({
2626
AlwaysPassesTest.class,
27-
MultipleHAMTests.class,
27+
MultipleHAMCustomTests.class,
2828
MultipleHAMDuplicateTests.class,
29+
// MultipleHAMInbuiltTests.class,
30+
// MultipleHAMInbuiltQualifiersTests.class,
2931
InMemoryIdentityStoreTests.class
3032
})
3133
public class FATSuite {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,317 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2026 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+
* Contributors:
11+
* IBM Corporation - initial API and implementation
12+
*******************************************************************************/
13+
package io.openliberty.security.jakartasec.fat.tests;
14+
15+
import static org.junit.Assert.assertEquals;
16+
import static org.junit.Assert.assertNotNull;
17+
18+
import java.io.BufferedReader;
19+
import java.io.IOException;
20+
import java.io.InputStreamReader;
21+
import java.net.HttpURLConnection;
22+
import java.net.URL;
23+
import java.util.Base64;
24+
25+
import com.ibm.websphere.simplicity.log.Log;
26+
27+
import componenttest.topology.impl.LibertyServer;
28+
29+
/**
30+
* Abstract base test class for Jakarta Security 4.0 tests.
31+
* Contains common reusable methods for server setup, HTTP requests, and assertions.
32+
*/
33+
public abstract class BaseJakartaSecurity40Test {
34+
35+
protected static final String SERVER_NAME = "jakartaSec40Server";
36+
37+
/**
38+
* Get the test class for logging purposes.
39+
* Subclasses should override this to return their own class.
40+
*/
41+
protected abstract Class<?> getTestClass();
42+
43+
/**
44+
* Get the Liberty server instance.
45+
* Subclasses must implement this to provide their server instance.
46+
*/
47+
protected abstract LibertyServer getServer();
48+
49+
/**
50+
* Build the URL for the application endpoint.
51+
*
52+
* @param contextRoot the context root of the application
53+
* @param resourcePath the resource path
54+
* @return the complete URL string
55+
*/
56+
protected String buildUrl(String contextRoot, String resourcePath) {
57+
return "http://localhost:" + getServer().getHttpDefaultPort() + contextRoot + resourcePath;
58+
}
59+
60+
/**
61+
* Execute a GET request and verify the expected response code.
62+
*
63+
* @param url the URL to request
64+
* @param expectedStatusCode the expected HTTP status code
65+
* @return the HTTP connection
66+
* @throws Exception if an error occurs
67+
*/
68+
protected HttpURLConnection executeGetRequest(String url, int expectedStatusCode) throws Exception {
69+
Log.info(getTestClass(), "executeGetRequest", "Executing GET request to: " + url);
70+
71+
URL urlObj = new URL(url);
72+
HttpURLConnection conn = (HttpURLConnection) urlObj.openConnection();
73+
74+
conn.setRequestMethod("GET");
75+
conn.setDoInput(true);
76+
int responseCode = conn.getResponseCode();
77+
78+
assertEquals("Expected status code " + expectedStatusCode + " but got " + responseCode,
79+
expectedStatusCode, responseCode);
80+
81+
return conn;
82+
}
83+
84+
/**
85+
* Execute a GET request with Basic Authentication and verify the expected response code.
86+
*
87+
* @param url the URL to request
88+
* @param username
89+
* @param password
90+
* @param expectedStatusCode the expected HTTP status code
91+
* @return the HTTP connection
92+
* @throws Exception if an error occurs
93+
*/
94+
protected HttpURLConnection executeGetRequest(String url, String username, String password, int expectedStatusCode) throws Exception {
95+
Log.info(getTestClass(), "executeGetRequest", "Executing GET request to: " + url);
96+
97+
URL urlObj = new URL(url);
98+
HttpURLConnection conn = (HttpURLConnection) urlObj.openConnection();
99+
100+
// Set Basic Authentication header
101+
String auth = username + ":" + password;
102+
String encodedAuth = Base64.getEncoder().encodeToString(auth.getBytes());
103+
conn.setRequestProperty("Authorization", "Basic " + encodedAuth);
104+
105+
conn.setRequestMethod("GET");
106+
conn.setDoInput(true);
107+
int responseCode = conn.getResponseCode();
108+
109+
assertEquals("Expected status code " + expectedStatusCode + " but got " + responseCode,
110+
expectedStatusCode, responseCode);
111+
112+
return conn;
113+
}
114+
115+
/**
116+
* Get the response from a GET request with Basic Authentication
117+
*
118+
* @param url
119+
* @param username
120+
* @param password
121+
* @param expectedStatusCode
122+
* @return the response code as String
123+
*/
124+
protected String getResponseFromGetRequest(String url, String username, String password, int expectedStatusCode) {
125+
HttpURLConnection conn = null;
126+
int responseCode = -1;
127+
try {
128+
conn = executeGetRequest(url, username, password, expectedStatusCode);
129+
130+
responseCode = conn.getResponseCode();
131+
assertEquals("Expected status code " + expectedStatusCode + " but got " + responseCode,
132+
expectedStatusCode, responseCode);
133+
} catch (Exception e) {
134+
Log.error(getTestClass(), "getResponseFromGetRequest", e);
135+
conn.disconnect();
136+
}
137+
138+
// Read response
139+
StringBuilder response = new StringBuilder();
140+
if (responseCode == 200) {
141+
try {
142+
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
143+
String inputLine;
144+
145+
while ((inputLine = in.readLine()) != null) {
146+
response.append(inputLine);
147+
}
148+
149+
in.close();
150+
151+
} catch (IOException e) {
152+
Log.error(getTestClass(), "getResponseFromGetRequest", e);
153+
conn.disconnect();
154+
}
155+
}
156+
157+
conn.disconnect();
158+
159+
Log.info(getTestClass(), "getResponseFromGetRequest",
160+
"Request to " + url + " with user " + username + " returned status " + responseCode);
161+
162+
return response.toString();
163+
}
164+
165+
/**
166+
* Mark the server trace log and execute a GET request.
167+
* Useful for tests that need to check trace output after a request.
168+
*
169+
* @param url the URL to request
170+
* @param expectedStatusCode the expected HTTP status code
171+
* @return the HTTP connection
172+
* @throws Exception if an error occurs
173+
*/
174+
protected HttpURLConnection executeGetRequestWithTraceMark(String url, int expectedStatusCode) throws Exception {
175+
getServer().setTraceMarkToEndOfDefaultTrace();
176+
return executeGetRequest(url, expectedStatusCode);
177+
}
178+
179+
/**
180+
* Mark the server log and execute a GET request.
181+
* Useful for tests that need to check log output after a request.
182+
*
183+
* @param url the URL to request
184+
* @param expectedStatusCode the expected HTTP status code
185+
* @return the HTTP connection
186+
* @throws Exception if an error occurs
187+
*/
188+
protected HttpURLConnection executeGetRequestWithLogMark(String url, int expectedStatusCode) throws Exception {
189+
getServer().setMarkToEndOfLog();
190+
return executeGetRequest(url, expectedStatusCode);
191+
}
192+
193+
/**
194+
* Wait for a string in the trace log using the current mark.
195+
*
196+
* @param searchString the string to search for
197+
* @return the found string or null if not found
198+
* @throws Exception if an error occurs
199+
*/
200+
protected String waitForStringInTrace(String searchString) throws Exception {
201+
return getServer().waitForStringInTraceUsingMark(searchString);
202+
}
203+
204+
/**
205+
* Wait for a string in the trace log using the current mark with a timeout.
206+
*
207+
* @param searchString the string to search for
208+
* @param timeout the timeout in milliseconds
209+
* @return the found string or null if not found
210+
* @throws Exception if an error occurs
211+
*/
212+
protected String waitForStringInTrace(String searchString, long timeout) throws Exception {
213+
return getServer().waitForStringInTraceUsingMark(searchString, timeout);
214+
}
215+
216+
/**
217+
* Wait for a string in the server log using the current mark.
218+
*
219+
* @param searchString the string to search for
220+
* @return the found string or null if not found
221+
* @throws Exception if an error occurs
222+
*/
223+
protected String waitForStringInLog(String searchString) throws Exception {
224+
return getServer().waitForStringInLogUsingMark(searchString);
225+
}
226+
227+
/**
228+
* Wait for a string in the server log using the current mark with a timeout.
229+
*
230+
* @param searchString the string to search for
231+
* @param timeout the timeout in milliseconds
232+
* @return the found string or null if not found
233+
* @throws Exception if an error occurs
234+
*/
235+
protected String waitForStringInLog(String searchString, long timeout) throws Exception {
236+
return getServer().waitForStringInLogUsingMark(searchString, timeout);
237+
}
238+
239+
/**
240+
* Assert that a string appears in the trace log.
241+
*
242+
* @param message the assertion message
243+
* @param searchString the string to search for
244+
* @throws Exception if an error occurs
245+
*/
246+
protected void assertStringInTrace(String message, String searchString) throws Exception {
247+
assertNotNull(message, waitForStringInTrace(searchString));
248+
}
249+
250+
/**
251+
* Assert that a string appears in the trace log with a timeout.
252+
*
253+
* @param message the assertion message
254+
* @param searchString the string to search for
255+
* @param timeout the timeout in milliseconds
256+
* @throws Exception if an error occurs
257+
*/
258+
protected void assertStringInTrace(String message, String searchString, long timeout) throws Exception {
259+
assertNotNull(message, waitForStringInTrace(searchString, timeout));
260+
}
261+
262+
/**
263+
* Assert that a string appears in the server log.
264+
*
265+
* @param message the assertion message
266+
* @param searchString the string to search for
267+
* @throws Exception if an error occurs
268+
*/
269+
protected void assertStringInLog(String message, String searchString) throws Exception {
270+
assertNotNull(message, waitForStringInLog(searchString));
271+
}
272+
273+
/**
274+
* Assert that a string appears in the server log with a timeout.
275+
*
276+
* @param message the assertion message
277+
* @param searchString the string to search for
278+
* @param timeout the timeout in milliseconds
279+
* @throws Exception if an error occurs
280+
*/
281+
protected void assertStringInLog(String message, String searchString, long timeout) throws Exception {
282+
assertNotNull(message, waitForStringInLog(searchString, timeout));
283+
}
284+
285+
/**
286+
* Log test information.
287+
*
288+
* @param methodName the test method name
289+
* @param message the message to log
290+
*/
291+
protected void logInfo(String methodName, String message) {
292+
Log.info(getTestClass(), methodName, message);
293+
}
294+
295+
/**
296+
* Start the server and log the action.
297+
*
298+
* @throws Exception if an error occurs
299+
*/
300+
protected void startServer() throws Exception {
301+
logInfo("startServer", "Starting server...");
302+
getServer().startServer();
303+
logInfo("startServer", "Server started successfully");
304+
}
305+
306+
/**
307+
* Stop the server with expected error/warning messages.
308+
*
309+
* @param expectedMessages the expected error/warning message IDs
310+
* @throws Exception if an error occurs
311+
*/
312+
protected void stopServer(String... expectedMessages) throws Exception {
313+
logInfo("stopServer", "Stopping server...");
314+
getServer().stopServer(expectedMessages);
315+
logInfo("stopServer", "Server stopped successfully");
316+
}
317+
}

0 commit comments

Comments
 (0)