33import org .apache .commons .lang3 .StringUtils ;
44import org .junit .jupiter .api .Test ;
55import org .junit .jupiter .api .extension .ExtendWith ;
6- import org .slf4j .Logger ;
7- import org .slf4j .LoggerFactory ;
86import org .springframework .boot .test .context .SpringBootTest ;
97import org .springframework .boot .test .system .CapturedOutput ;
108import org .springframework .boot .test .system .OutputCaptureExtension ;
9+ import org .springframework .boot .test .web .server .LocalServerPort ;
1110import org .springframework .http .HttpStatus ;
1211import org .springframework .http .ResponseEntity ;
1312import org .springframework .web .client .RestTemplate ;
1413
1514import java .io .IOException ;
1615import java .net .URISyntaxException ;
1716import java .net .URL ;
18- import java .util .regex .Matcher ;
19- import java .util .regex .Pattern ;
17+ import java .util .Arrays ;
2018
2119import static org .assertj .core .api .Assertions .assertThat ;
20+ import static org .assertj .core .api .Assertions .fail ;
2221
2322@ SpringBootTest (webEnvironment = SpringBootTest .WebEnvironment .DEFINED_PORT )
2423@ ExtendWith (OutputCaptureExtension .class )
2524class AppTests {
2625
27- private final static Logger log = LoggerFactory .getLogger (AppTests .class );
28- private static final int WAIT_FOR_STARTUP_SECONDS = 90 ;
29- private static final String HTTPS_NGROK_TUNNEL_REGEX = "(https:\\ /\\ /)?(([^.]+)\\ .)?ngrok\\ .io" ;
26+ static final int WAIT_FOR_STARTUP_SECONDS = 90 ;
27+
28+ @ LocalServerPort
29+ int serverPort ;
3030
3131 @ Test
32- public void shouldStartNgrok (CapturedOutput output ) throws IOException , URISyntaxException , InterruptedException {
33- log .info ("[ TEST ] Waiting for ngrok..." );
32+ void should_start_ngrok_and_log_tunnel_details (CapturedOutput output ) throws IOException , URISyntaxException , InterruptedException {
33+ System .out .println ("[automation-test] Waiting for ngrok startup confirmation in output logs..." );
34+ waitForNgrokStartConfirmationInLogs (output );
35+ final String ngrokHttpsTunnelUrl = extractNgrokHttpsTunnelUrlFromLogs (output );
36+ System .out .println ("[automation-test] Ngrok tunnel is running between ::" + serverPort + " <-> " + ngrokHttpsTunnelUrl );
37+
38+ System .out .println ("[automation-test] Executing GET request..." );
39+ long timerStart = System .currentTimeMillis ();
40+ final ResponseEntity <String > responseFromTunnel = new RestTemplate ()
41+ .getForEntity (
42+ new URL (ngrokHttpsTunnelUrl ).toURI (),
43+ String .class
44+ );
45+ long timerStop = System .currentTimeMillis ();
46+ System .out .println ("[automation-test] " + ngrokHttpsTunnelUrl + " responded in "
47+ + (timerStop - timerStart ) + "ms with\n \n " + responseFromTunnel + "\n " );
48+
49+ assertThat (responseFromTunnel .getStatusCode ()).isEqualTo (HttpStatus .OK );
50+ assertThat (responseFromTunnel .getBody ()).isEqualTo ("<h1>Hello World!</h1>" );
51+ }
3452
35- boolean ngrokStarted = false ;
53+ private String extractNgrokHttpsTunnelUrlFromLogs (CapturedOutput output ) {
54+ return Arrays .stream (StringUtils .split (output .toString (), " " ))
55+ .filter (s -> s .startsWith ("https://" ) && s .contains (".ngrok.io" )).findFirst ().get ();
56+ }
3657
37- Thread . sleep ( 1000 );
58+ private void waitForNgrokStartConfirmationInLogs ( CapturedOutput output ) throws InterruptedException {
3859 for (int i = WAIT_FOR_STARTUP_SECONDS ; i > 0 ; i --) {
3960 Thread .sleep (1000 );
40- if (output .toString ().contains ("Ngrok started successfully!" )) {
41- ngrokStarted = true ;
42- log .info ("[ TEST ] Ngrok start detected!" );
43- break ;
61+ if (output .toString ().contains ("Ngrok started successfully!" ) || output .toString ().contains ("Ngrok was already running!" )) {
62+ return ;
4463 }
4564 }
46-
47- assertThat (ngrokStarted ).isTrue ();
48-
49- Pattern pattern = Pattern .compile (HTTPS_NGROK_TUNNEL_REGEX );
50- Matcher matcher = pattern .matcher (output .getOut ());
51-
52- assertThat (matcher .find ()).isTrue ();
53- final String ngrokHttpsRemoteUrl = StringUtils .split (matcher .group (), "[" )[1 ];
54- log .info ("[ TEST ] Captured ngrok tunnel url = [ {} ]" , ngrokHttpsRemoteUrl );
55-
56- ResponseEntity <String > responseFromTunnel = new RestTemplate ().getForEntity (new URL (ngrokHttpsRemoteUrl ).toURI (), String .class );
57-
58- log .info ("Response from [ {} ] = \n \n \n {}\n \n " , ngrokHttpsRemoteUrl , responseFromTunnel .toString ());
59-
60- assertThat (responseFromTunnel .getStatusCode ()).isEqualTo (HttpStatus .OK );
61- assertThat (responseFromTunnel .getBody ()).isEqualTo ("<h1>Hello World!</h1>" );
65+ fail ("Ngrok not started!" );
6266 }
63- }
67+ }
0 commit comments