3
3
import com .typesafe .config .Config ;
4
4
import fi .hsl .common .config .ConfigParser ;
5
5
import fi .hsl .common .config .ConfigUtils ;
6
- import fi .hsl .common .health .HealthServer ;
7
6
import org .apache .pulsar .client .api .Consumer ;
8
7
import org .apache .pulsar .client .api .Message ;
9
8
import org .apache .pulsar .client .api .Producer ;
10
- import org .apache .pulsar .shade .org .apache .http .HttpResponse ;
11
- import org .apache .pulsar .shade .org .apache .http .HttpStatus ;
12
- import org .apache .pulsar .shade .org .apache .http .client .HttpClient ;
13
- import org .apache .pulsar .shade .org .apache .http .client .methods .*;
14
- import org .apache .pulsar .shade .org .apache .http .impl .client .HttpClientBuilder ;
15
- import org .junit .BeforeClass ;
16
9
import org .junit .ClassRule ;
17
10
import org .junit .Test ;
18
11
import org .slf4j .Logger ;
21
14
import org .testcontainers .containers .PulsarContainer ;
22
15
import redis .clients .jedis .Jedis ;
23
16
24
- import java .io .BufferedReader ;
25
- import java .io .IOException ;
26
- import java .io .InputStreamReader ;
27
17
import java .nio .charset .Charset ;
28
18
import java .util .*;
29
19
import java .util .concurrent .TimeUnit ;
30
- import java .util .function .BooleanSupplier ;
31
20
32
21
import static org .junit .Assert .*;
33
22
@@ -47,16 +36,7 @@ public class ITPulsarApplication {
47
36
48
37
@ ClassRule
49
38
public static PulsarContainer pulsar = MockContainers .newPulsarContainer ();
50
-
51
- @ BeforeClass
52
- public static void setUp () throws Exception {
53
- MockContainers .configurePulsarContainer (pulsar , TENANT , NAMESPACE );
54
-
55
- if (PRINT_PULSAR_LOG ) {
56
- MockContainers .tail (pulsar , logger );
57
- }
58
- }
59
-
39
+
60
40
@ Test
61
41
public void testRedisContainer () {
62
42
Jedis jedis = MockContainers .newMockJedisConnection (redis );
@@ -244,119 +224,4 @@ public void testInitFailure(Config config) {
244
224
logger .debug ("Exception as expected" );
245
225
}
246
226
}
247
-
248
- @ Test
249
- public void testHttpServer () throws Exception {
250
- Config base = PulsarMockApplication .readConfig (CONFIG_FILE );
251
-
252
- PulsarApplication app = PulsarMockApplication .newInstance (base , redis , pulsar );
253
- assertNotNull (app );
254
-
255
- logger .info ("Pulsar Application created, testing HealthServer" );
256
-
257
- final Producer <byte []> producer = app .getContext ().getSingleProducer ();
258
- final Consumer <byte []> consumer = app .getContext ().getConsumer ();
259
- final Jedis jedis = app .getContext ().getJedis ();
260
- final HealthServer healthServer = app .getContext ().getHealthServer ();
261
-
262
- assertTrue (consumer .isConnected ());
263
- assertTrue (producer .isConnected ());
264
- assertTrue (jedis .isConnected ());
265
-
266
- logger .info ("Creating health check function" );
267
- final BooleanSupplier healthCheck = () -> {
268
- boolean status = true ;
269
- if (producer != null ) status &= producer .isConnected ();
270
- if (consumer != null ) status &= consumer .isConnected ();
271
- if (jedis != null ) status &= jedis .isConnected ();
272
- return status ;
273
- };
274
- healthServer .addCheck (healthCheck );
275
-
276
- String url = "http://localhost:" + healthServer .port + healthServer .endpoint ;
277
-
278
- logger .info ("Checking health" );
279
- HttpResponse response = makeGetRequest (url );
280
- assertEquals (HttpStatus .SC_OK , response .getStatusLine ().getStatusCode ());
281
- assertEquals ("OK" , getContent (response ));
282
-
283
- logger .info ("Disconnecting Jedis and checking health" );
284
- jedis .disconnect ();
285
- assertFalse (jedis .isConnected ());
286
-
287
- response = makeGetRequest (url );
288
- assertEquals (HttpStatus .SC_SERVICE_UNAVAILABLE , response .getStatusLine ().getStatusCode ());
289
- assertEquals ("FAIL" , getContent (response ));
290
-
291
- logger .info ("Reconnecting Jedis and checking health" );
292
- jedis .connect ();
293
- assertTrue (jedis .isConnected ());
294
-
295
- response = makeGetRequest (url );
296
- assertEquals (HttpStatus .SC_OK , response .getStatusLine ().getStatusCode ());
297
- assertEquals ("OK" , getContent (response ));
298
-
299
- logger .info ("Closing Pulsar consumer and checking health" );
300
- consumer .close ();
301
- assertFalse (consumer .isConnected ());
302
-
303
- response = makeGetRequest (url );
304
- assertEquals (HttpStatus .SC_SERVICE_UNAVAILABLE , response .getStatusLine ().getStatusCode ());
305
- assertEquals ("FAIL" , getContent (response ));
306
-
307
- response = makePostRequest (url );
308
- assertEquals (HttpStatus .SC_METHOD_NOT_ALLOWED , response .getStatusLine ().getStatusCode ());
309
- assertEquals ("Method Not Allowed" , getContent (response ));
310
-
311
- url = "http://localhost:" + healthServer .port + "/foo" ;
312
- response = makeGetRequest (url );
313
- assertEquals (HttpStatus .SC_NOT_FOUND , response .getStatusLine ().getStatusCode ());
314
- assertEquals ("Not Found" , getContent (response ));
315
-
316
- url = "http://localhost:" + healthServer .port + healthServer .endpoint + "foo" ;
317
- response = makeGetRequest (url );
318
- assertEquals (HttpStatus .SC_NOT_FOUND , response .getStatusLine ().getStatusCode ());
319
- assertEquals ("Not Found" , getContent (response ));
320
-
321
- app .close ();
322
- assertFalse (consumer .isConnected ());
323
- assertFalse (producer .isConnected ());
324
- assertFalse (jedis .isConnected ());
325
- }
326
-
327
- private HttpResponse makeGetRequest (final String url ) throws IOException {
328
- return makeRequest ("GET" , url );
329
- }
330
-
331
- private HttpResponse makePostRequest (final String url ) throws IOException {
332
- return makeRequest ("POST" , url );
333
- }
334
-
335
- private HttpResponse makeRequest (final String method , final String url ) throws IOException {
336
- HttpClient client = HttpClientBuilder .create ().build ();
337
- HttpUriRequest request ;
338
- switch (method .toLowerCase ()) {
339
- case "get" :
340
- request = new HttpGet (url );
341
- break ;
342
- case "post" :
343
- request = new HttpPost (url );
344
- break ;
345
- default :
346
- request = new HttpGet (url );
347
- break ;
348
- }
349
- HttpResponse response = client .execute (request );
350
- return response ;
351
- }
352
-
353
- private String getContent (final HttpResponse response ) throws IOException {
354
- BufferedReader reader = new BufferedReader (new InputStreamReader (response .getEntity ().getContent ()));
355
- StringBuffer content = new StringBuffer ();
356
- String line ;
357
- while ((line = reader .readLine ()) != null ) {
358
- content .append (line );
359
- }
360
- return content .toString ();
361
- }
362
227
}
0 commit comments