|
| 1 | +/* |
| 2 | + * Copyright 2026 JBoss by 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 | + * http://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.as.quickstarts.cmt.jts; |
| 17 | + |
| 18 | +import static org.junit.Assert.assertEquals; |
| 19 | +import static org.junit.Assert.assertTrue; |
| 20 | + |
| 21 | +import java.io.IOException; |
| 22 | +import java.net.CookieManager; |
| 23 | +import java.net.CookiePolicy; |
| 24 | +import java.net.URI; |
| 25 | +import java.net.URISyntaxException; |
| 26 | +import java.net.URLEncoder; |
| 27 | +import java.net.http.HttpClient; |
| 28 | +import java.net.http.HttpRequest; |
| 29 | +import java.net.http.HttpRequest.BodyPublisher; |
| 30 | +import java.net.http.HttpRequest.BodyPublishers; |
| 31 | +import java.net.http.HttpResponse; |
| 32 | +import java.nio.charset.StandardCharsets; |
| 33 | +import java.time.Duration; |
| 34 | +import java.util.Map; |
| 35 | +import java.util.regex.Matcher; |
| 36 | +import java.util.regex.Pattern; |
| 37 | + |
| 38 | +import org.junit.Test; |
| 39 | + |
| 40 | +public class BasicRuntimeIT { |
| 41 | + |
| 42 | + private static final String DEFAULT_SERVER_HOST = "http://localhost"; |
| 43 | + private static final int DEFAULT_SERVER_PORT = 8080; |
| 44 | + private static final String APP_CONTEXT = "/jts-application-component-1"; |
| 45 | + |
| 46 | + @Test |
| 47 | + public void testHTTPEndpointIsAvailable() throws IOException, InterruptedException, URISyntaxException { |
| 48 | + String serverHost = getServerHost(0); |
| 49 | + final HttpRequest request = HttpRequest.newBuilder() |
| 50 | + .uri(new URI(serverHost + APP_CONTEXT + "/")) |
| 51 | + .GET() |
| 52 | + .build(); |
| 53 | + final HttpClient client = HttpClient.newBuilder() |
| 54 | + .followRedirects(HttpClient.Redirect.ALWAYS) |
| 55 | + .connectTimeout(Duration.ofMinutes(1)) |
| 56 | + .build(); |
| 57 | + final HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString()); |
| 58 | + assertEquals(200, response.statusCode()); |
| 59 | + assertTrue(response.body().contains( |
| 60 | + "<meta http-equiv=\"Refresh\" content=\"0; URL=addCustomer.jsf\">")); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + public void testAddCustomer() throws Exception { |
| 65 | + HttpClient client = createHttpClient(); |
| 66 | + String uniqueName = "TestCustomer_" + System.currentTimeMillis(); |
| 67 | + long baselineMessages = getMessagesReceived(); |
| 68 | + |
| 69 | + HttpResponse<String> response = submitCustomerForm(client, uniqueName); |
| 70 | + assertEquals(200, response.statusCode()); |
| 71 | + assertTrue(response.body().contains(uniqueName)); |
| 72 | + assertTrue(response.uri().toString().contains("customers.jsf")); |
| 73 | + |
| 74 | + waitForMessagesReceived(baselineMessages + 1, 30_000); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + public void testDuplicateCustomerRollback() throws Exception { |
| 79 | + HttpClient client = createHttpClient(); |
| 80 | + String uniqueName = "DuplicateTest_" + System.currentTimeMillis(); |
| 81 | + long baselineMessages = getMessagesReceived(); |
| 82 | + |
| 83 | + HttpResponse<String> response = submitCustomerForm(client, uniqueName); |
| 84 | + assertEquals(200, response.statusCode()); |
| 85 | + assertTrue(response.body().contains(uniqueName)); |
| 86 | + assertTrue(response.uri().toString().contains("customers.jsf")); |
| 87 | + |
| 88 | + waitForMessagesReceived(baselineMessages + 1, 30_000); |
| 89 | + |
| 90 | + response = submitCustomerForm(client, uniqueName); |
| 91 | + assertEquals(200, response.statusCode()); |
| 92 | + assertTrue(response.uri().toString().contains("duplicate.jsf")); |
| 93 | + |
| 94 | + assertMessagesStayAt(baselineMessages + 1, 10_000); |
| 95 | + } |
| 96 | + |
| 97 | + // SERVER_HOST is expected to include the port (e.g. http://localhost:8080). |
| 98 | + // When portOffset is non-zero, the port is parsed and the offset is added |
| 99 | + // (defaulting to 80 for http or 443 for https if no port is present). |
| 100 | + private String getServerHost(int portOffset) { |
| 101 | + String host = System.getenv("SERVER_HOST"); |
| 102 | + if (host == null) { |
| 103 | + host = System.getProperty("server.host"); |
| 104 | + } |
| 105 | + if (host == null) { |
| 106 | + host = DEFAULT_SERVER_HOST + ":" + (DEFAULT_SERVER_PORT + portOffset); |
| 107 | + } else if (portOffset != 0) { |
| 108 | + try { |
| 109 | + URI uri = new URI(host); |
| 110 | + int defaultPort = "https".equals(uri.getScheme()) ? 443 : 80; |
| 111 | + int port = (uri.getPort() != -1 ? uri.getPort() : defaultPort) + portOffset; |
| 112 | + host = uri.getScheme() + "://" + uri.getHost() + ":" + port; |
| 113 | + } catch (URISyntaxException e) { |
| 114 | + throw new RuntimeException(e); |
| 115 | + } |
| 116 | + } |
| 117 | + return host; |
| 118 | + } |
| 119 | + |
| 120 | + private HttpClient createHttpClient() { |
| 121 | + return HttpClient.newBuilder() |
| 122 | + .followRedirects(HttpClient.Redirect.ALWAYS) |
| 123 | + .cookieHandler(new CookieManager(null, CookiePolicy.ACCEPT_ALL)) |
| 124 | + .version(HttpClient.Version.HTTP_1_1) |
| 125 | + .build(); |
| 126 | + } |
| 127 | + |
| 128 | + private HttpResponse<String> submitCustomerForm(HttpClient client, String name) throws IOException, InterruptedException, URISyntaxException { |
| 129 | + String serverHost = getServerHost(0); |
| 130 | + URI formUri = new URI(serverHost + APP_CONTEXT + "/addCustomer.jsf"); |
| 131 | + |
| 132 | + HttpResponse<String> getResponse = client.send( |
| 133 | + HttpRequest.newBuilder(formUri).GET().build(), |
| 134 | + HttpResponse.BodyHandlers.ofString()); |
| 135 | + assertEquals(200, getResponse.statusCode()); |
| 136 | + String body = getResponse.body(); |
| 137 | + // JSF requires the ViewState token from the hidden form field to accept a POST submission |
| 138 | + Matcher viewStateMatcher = Pattern.compile("name=\"jakarta\\.faces\\.ViewState\"[^>]*value=\"([^\"]+)\"").matcher(body); |
| 139 | + assertTrue("ViewState not found in response", viewStateMatcher.find()); |
| 140 | + String viewState = viewStateMatcher.group(1); |
| 141 | + |
| 142 | + HttpRequest postRequest = HttpRequest.newBuilder(formUri) |
| 143 | + .header("Content-Type", "application/x-www-form-urlencoded") |
| 144 | + .POST(ofFormData(Map.of( |
| 145 | + "addCustomerForm", "addCustomerForm", |
| 146 | + "addCustomerForm:name", name, |
| 147 | + "addCustomerForm:add", "Add", |
| 148 | + "jakarta.faces.ViewState", viewState))) |
| 149 | + .build(); |
| 150 | + return client.send(postRequest, HttpResponse.BodyHandlers.ofString()); |
| 151 | + } |
| 152 | + |
| 153 | + private void waitForMessagesReceived(long expected, long timeoutMs) throws IOException, InterruptedException { |
| 154 | + long deadline = System.currentTimeMillis() + timeoutMs; |
| 155 | + while (System.currentTimeMillis() < deadline) { |
| 156 | + if (getMessagesReceived() == expected) return; |
| 157 | + Thread.sleep(500); |
| 158 | + } |
| 159 | + assertEquals("Timed out waiting for messages received to reach " + expected, |
| 160 | + expected, getMessagesReceived()); |
| 161 | + } |
| 162 | + |
| 163 | + private void assertMessagesStayAt(long expected, long durationMs) throws IOException, InterruptedException { |
| 164 | + long deadline = System.currentTimeMillis() + durationMs; |
| 165 | + while (System.currentTimeMillis() < deadline) { |
| 166 | + assertEquals(expected, getMessagesReceived()); |
| 167 | + Thread.sleep(500); |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + private long getMessagesReceived() throws IOException, InterruptedException { |
| 172 | + String server2Host = getServerHost(100); |
| 173 | + HttpClient client = HttpClient.newBuilder() |
| 174 | + .connectTimeout(Duration.ofMinutes(1)) |
| 175 | + .build(); |
| 176 | + HttpRequest request = HttpRequest.newBuilder() |
| 177 | + .uri(URI.create(server2Host + "/jts-application-component-2/messages/count")) |
| 178 | + .GET() |
| 179 | + .build(); |
| 180 | + HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString()); |
| 181 | + assertEquals("Message count request failed with status " + response.statusCode(), |
| 182 | + 200, response.statusCode()); |
| 183 | + return Long.parseLong(response.body().trim()); |
| 184 | + } |
| 185 | + |
| 186 | + private static BodyPublisher ofFormData(Map<String, String> data) { |
| 187 | + StringBuilder builder = new StringBuilder(); |
| 188 | + for (Map.Entry<String, String> entry : data.entrySet()) { |
| 189 | + if (builder.length() > 0) { |
| 190 | + builder.append("&"); |
| 191 | + } |
| 192 | + builder.append(URLEncoder.encode(entry.getKey(), StandardCharsets.UTF_8)); |
| 193 | + builder.append("="); |
| 194 | + builder.append(URLEncoder.encode(entry.getValue(), StandardCharsets.UTF_8)); |
| 195 | + } |
| 196 | + return BodyPublishers.ofString(builder.toString()); |
| 197 | + } |
| 198 | +} |
0 commit comments