Skip to content

Commit e3918e0

Browse files
committed
test ports bound to localhost, improved jetty log messages, code cleanup
1 parent 41f8709 commit e3918e0

File tree

3 files changed

+35
-23
lines changed

3 files changed

+35
-23
lines changed

dsf-bpe/dsf-bpe-server/src/test/java/dev/dsf/bpe/integration/AbstractIntegrationTest.java

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -130,12 +130,8 @@ public abstract class AbstractIntegrationTest extends AbstractDbTest
130130

131131
private static final ReferenceCleaner referenceCleaner = new ReferenceCleanerImpl(new ReferenceExtractorImpl());
132132

133-
private static String fhirBaseUrl;
134133
private static JettyServer fhirServer;
135-
136134
private static FhirWebserviceClient webserviceClient;
137-
138-
private static String bpeBaseUrl;
139135
private static JettyServer bpeServer;
140136

141137
@BeforeClass
@@ -145,18 +141,18 @@ public static void beforeClass() throws Exception
145141
fhirLiquibaseRule.getMappedPort(5432), fhirLiquibaseRule.getDatabaseName());
146142
fhirDefaultDataSource.unwrap(BasicDataSource.class).start();
147143

148-
ServerSocketChannel fhirStatusConnectorChannel = JettyServer.serverSocketChannel();
149-
ServerSocketChannel fhirApiConnectorChannel = JettyServer.serverSocketChannel();
144+
ServerSocketChannel fhirStatusConnectorChannel = JettyServer.serverSocketChannel("127.0.0.1");
145+
ServerSocketChannel fhirApiConnectorChannel = JettyServer.serverSocketChannel("127.0.0.1");
150146

151147
logger.info("Creating FHIR Bundle ...");
152148
createTestBundle(certificates.getClientCertificate(), certificates.getExternalClientCertificate(),
153149
fhirApiConnectorChannel.socket().getLocalPort());
154150

155-
fhirBaseUrl = "https://localhost:" + fhirApiConnectorChannel.socket().getLocalPort() + FHIR_CONTEXT_PATH;
151+
String fhirBaseUrl = "https://localhost:" + fhirApiConnectorChannel.socket().getLocalPort() + FHIR_CONTEXT_PATH;
156152

157153
logger.info("Creating webservice client ...");
158-
webserviceClient = createWebserviceClient(fhirApiConnectorChannel.socket().getLocalPort(),
159-
certificates.getClientCertificate().getTrustStore(), certificates.getClientCertificate().getKeyStore(),
154+
webserviceClient = createWebserviceClient(fhirBaseUrl, certificates.getClientCertificate().getTrustStore(),
155+
certificates.getClientCertificate().getKeyStore(),
160156
certificates.getClientCertificate().getKeyStorePassword(), fhirContext, referenceCleaner);
161157

162158
logger.info("Starting FHIR Server ...");
@@ -180,15 +176,15 @@ public static void beforeClass() throws Exception
180176
bpeLiquibaseRule.getMappedPort(5432), bpeLiquibaseRule.getDatabaseName());
181177
bpeDefaultDataSource.unwrap(BasicDataSource.class).start();
182178

183-
ServerSocketChannel bpeStatusConnectorChannel = JettyServer.serverSocketChannel();
184-
ServerSocketChannel bpeApiConnectorChannel = JettyServer.serverSocketChannel();
179+
ServerSocketChannel bpeStatusConnectorChannel = JettyServer.serverSocketChannel("127.0.0.1");
180+
ServerSocketChannel bpeApiConnectorChannel = JettyServer.serverSocketChannel("127.0.0.1");
185181

186-
bpeBaseUrl = "https://localhost:" + bpeApiConnectorChannel.socket().getLocalPort() + BPE_CONTEXT_PATH;
182+
String bpeBaseUrl = "https://localhost:" + bpeApiConnectorChannel.socket().getLocalPort() + BPE_CONTEXT_PATH;
187183

188184
Files.createDirectories(EMPTY_PROCESS_DIRECTORY);
189185

190186
logger.info("Starting BPE Server ...");
191-
bpeServer = startBpeServer(bpeStatusConnectorChannel, bpeApiConnectorChannel, bpeBaseUrl);
187+
bpeServer = startBpeServer(bpeStatusConnectorChannel, bpeApiConnectorChannel, bpeBaseUrl, fhirBaseUrl);
192188

193189
logger.info("Creating FHIR template database ...");
194190
fhirLiquibaseRule.createTemplateDatabase();
@@ -200,12 +196,11 @@ public static void beforeClass() throws Exception
200196
Thread.sleep(Duration.ofSeconds(1));
201197
}
202198

203-
private static FhirWebserviceClient createWebserviceClient(int fhirApiPort, KeyStore trustStore, KeyStore keyStore,
204-
char[] keyStorePassword, FhirContext fhirContext, ReferenceCleaner referenceCleaner)
199+
private static FhirWebserviceClient createWebserviceClient(String fhirBaseUrl, KeyStore trustStore,
200+
KeyStore keyStore, char[] keyStorePassword, FhirContext fhirContext, ReferenceCleaner referenceCleaner)
205201
{
206-
return new FhirWebserviceClientJersey("https://localhost:" + fhirApiPort + FHIR_CONTEXT_PATH, trustStore,
207-
keyStore, keyStorePassword, null, null, null, null, 0, 0, false, "DSF Integration Test Client",
208-
fhirContext, referenceCleaner);
202+
return new FhirWebserviceClientJersey(fhirBaseUrl, trustStore, keyStore, keyStorePassword, null, null, null,
203+
null, 0, 0, false, "DSF Integration Test Client", fhirContext, referenceCleaner);
209204
}
210205

211206
protected static FhirWebserviceClient getWebserviceClient()
@@ -394,7 +389,7 @@ private static JettyServer startFhirServer(ServerSocketChannel statusConnectorCh
394389
}
395390

396391
private static JettyServer startBpeServer(ServerSocketChannel statusConnectorChannel,
397-
ServerSocketChannel apiConnectorChannel, String baseUrl) throws Exception
392+
ServerSocketChannel apiConnectorChannel, String bpeBaseUrl, String fhirBaseUrl) throws Exception
398393
{
399394
Map<String, String> initParameters = new HashMap<>();
400395
initParameters.put("dev.dsf.server.status.port",
@@ -416,6 +411,7 @@ private static JettyServer startBpeServer(ServerSocketChannel statusConnectorCha
416411
initParameters.put("dev.dsf.bpe.fhir.client.trust.server.certificate.cas",
417412
certificates.getCaCertificateFile().toString());
418413

414+
initParameters.put("dev.dsf.bpe.server.base.url", bpeBaseUrl);
419415
initParameters.put("dev.dsf.bpe.fhir.server.base.url", fhirBaseUrl);
420416

421417
initParameters.put("dev.dsf.bpe.process.api.directroy", "../dsf-bpe-server-jetty/docker/api");

dsf-common/dsf-common-jetty/src/main/java/dev/dsf/common/jetty/JettyServer.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.io.IOException;
44
import java.net.InetSocketAddress;
5+
import java.net.SocketAddress;
56
import java.net.StandardSocketOptions;
67
import java.nio.channels.ServerSocketChannel;
78
import java.security.KeyStore;
@@ -59,9 +60,10 @@ private static HttpConnectionFactory httpConnectionFactory(Customizer... customi
5960
return new HttpConnectionFactory(httpConfiguration);
6061
}
6162

62-
public static ServerSocketChannel serverSocketChannel()
63+
public static ServerSocketChannel serverSocketChannel(String hostname)
6364
{
64-
InetSocketAddress bindAddress = new InetSocketAddress(0);
65+
InetSocketAddress bindAddress = hostname == null ? new InetSocketAddress(0)
66+
: new InetSocketAddress(hostname, 0);
6567
ServerSocketChannel serverChannel = null;
6668

6769
try
@@ -108,6 +110,7 @@ public static Function<Server, ServerConnector> statusConnector(ServerSocketChan
108110
{
109111
ServerConnector connector = new ServerConnector(server, httpConnectionFactory());
110112
connector.open(channel);
113+
setHostAndPort(channel, connector);
111114

112115
return connector;
113116
}
@@ -147,6 +150,7 @@ public static Function<Server, ServerConnector> httpConnector(ServerSocketChanne
147150
httpConnectionFactory(new ForwardedRequestCustomizer(),
148151
new ForwardedSecureRequestCustomizer(clientCertificateHeaderName)));
149152
connector.open(channel);
153+
setHostAndPort(channel, connector);
150154

151155
return connector;
152156
}
@@ -173,6 +177,7 @@ server, sslConnectionFactory(clientCertificateTrustStore, serverCertificateKeySt
173177
keyStorePassword, needClientAuth),
174178
httpConnectionFactory(new SecureRequestCustomizer()));
175179
connector.open(channel);
180+
setHostAndPort(channel, connector);
176181

177182
return connector;
178183
}
@@ -202,6 +207,17 @@ public static Function<Server, ServerConnector> httpsConnector(String host, int
202207
};
203208
}
204209

210+
private static void setHostAndPort(ServerSocketChannel channel, ServerConnector connector) throws IOException
211+
{
212+
SocketAddress address = channel.getLocalAddress();
213+
if (address != null && address instanceof InetSocketAddress i && i.getAddress() != null
214+
&& i.getAddress().getHostAddress() != null)
215+
{
216+
connector.setHost(i.getAddress().getHostAddress());
217+
connector.setPort(i.getPort());
218+
}
219+
}
220+
205221
private static SslConnectionFactory sslConnectionFactory(KeyStore clientCertificateTrustStore, KeyStore keyStore,
206222
char[] keyStorePassword, boolean needClientAuth)
207223
{

dsf-fhir/dsf-fhir-server/src/test/java/dev/dsf/fhir/integration/AbstractIntegrationTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,8 @@ public static void beforeClass() throws Exception
131131
logger.info("Creating Bundle ...");
132132
createTestBundle(certificates.getClientCertificate(), certificates.getExternalClientCertificate());
133133

134-
ServerSocketChannel statusConnectorChannel = JettyServer.serverSocketChannel();
135-
ServerSocketChannel apiConnectorChannel = JettyServer.serverSocketChannel();
134+
ServerSocketChannel statusConnectorChannel = JettyServer.serverSocketChannel("127.0.0.1");
135+
ServerSocketChannel apiConnectorChannel = JettyServer.serverSocketChannel("127.0.0.1");
136136

137137
baseUrl = "https://localhost:" + apiConnectorChannel.socket().getLocalPort() + CONTEXT_PATH;
138138

0 commit comments

Comments
 (0)