1717package de .codecentric .boot .admin .client ;
1818
1919import java .net .InetAddress ;
20- import java .net .UnknownHostException ;
2120import java .time .Duration ;
2221import java .util .Arrays ;
2322import java .util .concurrent .CountDownLatch ;
23+ import java .util .concurrent .TimeUnit ;
2424import java .util .stream .Stream ;
2525
26+ import com .fasterxml .jackson .databind .PropertyNamingStrategies ;
2627import com .github .tomakehurst .wiremock .WireMockServer ;
2728import com .github .tomakehurst .wiremock .client .ResponseDefinitionBuilder ;
2829import com .github .tomakehurst .wiremock .common .ConsoleNotifier ;
2930import com .github .tomakehurst .wiremock .matching .RequestPatternBuilder ;
31+ import lombok .extern .slf4j .Slf4j ;
3032import org .junit .jupiter .api .AfterEach ;
3133import org .junit .jupiter .api .Test ;
3234import org .springframework .beans .factory .annotation .Autowired ;
3537import org .springframework .boot .WebApplicationType ;
3638import org .springframework .boot .autoconfigure .EnableAutoConfiguration ;
3739import org .springframework .boot .context .event .ApplicationReadyEvent ;
40+ import org .springframework .boot .jackson .autoconfigure .JacksonProperties ;
3841import org .springframework .context .ConfigurableApplicationContext ;
3942import org .springframework .context .event .EventListener ;
4043
5053import static com .github .tomakehurst .wiremock .core .WireMockConfiguration .options ;
5154import static org .awaitility .Awaitility .await ;
5255
56+ @ Slf4j
5357public abstract class AbstractClientApplicationTest {
5458
5559 private final WireMockServer wireMock = new WireMockServer (
@@ -67,39 +71,43 @@ protected void setUp(WebApplicationType type) {
6771 }
6872
6973 private void setUpWiremock () {
70- wireMock .start ();
74+ this . wireMock .start ();
7175 ResponseDefinitionBuilder response = created ().withHeader ("Content-Type" , "application/json" )
7276 .withHeader ("Connection" , "close" )
73- .withHeader ("Location" , wireMock .url ("/instances/abcdef" ))
77+ .withHeader ("Location" , this . wireMock .url ("/instances/abcdef" ))
7478 .withBody ("{ \" id\" : \" abcdef\" }" );
75- wireMock .stubFor (post (urlEqualTo ("/instances" )).willReturn (response ));
79+ this . wireMock .stubFor (post (urlEqualTo ("/instances" )).willReturn (response ));
7680 }
7781
7882 private void setUpApplication (WebApplicationType type ) {
79- application = new SpringApplication (TestClientApplication .class );
80- application .setWebApplicationType (type );
83+ this . application = new SpringApplication (TestClientApplication .class );
84+ this . application .setWebApplicationType (type );
8185 }
8286
8387 private void setUpApplicationContext (String ... additionalArgs ) {
8488 Stream <String > defaultArgs = Stream .of ("--spring.application.name=Test-Client" , "--server.port=0" ,
8589 "--management.endpoints.web.base-path=/mgmt" , "--endpoints.health.enabled=true" ,
86- "--spring.boot.admin.client.url=" + wireMock .url ("/" ));
90+ "--spring.boot.admin.client.url=" + this . wireMock .url ("/" ));
8791
8892 String [] args = Stream .concat (defaultArgs , Arrays .stream (additionalArgs )).toArray (String []::new );
8993
90- this .instance = application .run (args );
94+ this .instance = this . application .run (args );
9195 }
9296
9397 @ AfterEach
9498 void tearDown () {
95- wireMock .stop ();
96- if (instance != null ) {
97- instance .close ();
99+ this . wireMock .stop ();
100+ if (this . instance != null ) {
101+ this . instance .close ();
98102 }
99103 }
100104
105+ /**
106+ * @see JacksonProperties
107+ * @see PropertyNamingStrategies#LOWER_CAMEL_CASE
108+ */
101109 @ Test
102- public void test_context () throws InterruptedException , UnknownHostException {
110+ public void test_context () throws Exception {
103111 setUpApplicationContext ();
104112
105113 String hostName = InetAddress .getLocalHost ().getCanonicalHostName ();
@@ -113,12 +121,17 @@ public void test_context() throws InterruptedException, UnknownHostException {
113121 .withRequestBody (matchingJsonPath ("$.serviceUrl" , equalTo (serviceHost + "/" )))
114122 .withRequestBody (matchingJsonPath ("$.metadata.startup" , matching (".+" )));
115123
116- cdl .await ();
117- await ().atMost (Duration .ofMillis (2500 )).untilAsserted (() -> wireMock .verify (request ));
124+ log .info ("Waiting for registration at mocked sba-server for '{}' ..." , this .instance );
125+ cdl .await (5_000 , TimeUnit .MILLISECONDS );
126+ await ().atMost (Duration .ofMillis (2500 )).untilAsserted (() -> this .wireMock .verify (request ));
118127 }
119128
129+ /**
130+ * @see JacksonProperties
131+ * @see PropertyNamingStrategies#SNAKE_CASE
132+ */
120133 @ Test
121- public void test_context_with_snake_case () throws InterruptedException , UnknownHostException {
134+ public void test_context_with_snake_case () throws Exception {
122135 setUpApplicationContext ("--spring.jackson.property-naming-strategy=SNAKE_CASE" );
123136
124137 String hostName = InetAddress .getLocalHost ().getCanonicalHostName ();
@@ -132,16 +145,17 @@ public void test_context_with_snake_case() throws InterruptedException, UnknownH
132145 .withRequestBody (matchingJsonPath ("$.service_url" , equalTo (serviceHost + "/" )))
133146 .withRequestBody (matchingJsonPath ("$.metadata.startup" , matching (".+" )));
134147
135- cdl .await ();
136- await ().atMost (Duration .ofMillis (2500 )).untilAsserted (() -> wireMock .verify (request ));
148+ log .info ("Waiting for registration at mocked sba-server for '{}' ..." , this .instance );
149+ cdl .await (5_000 , TimeUnit .MILLISECONDS );
150+ await ().atMost (Duration .ofMillis (2500 )).untilAsserted (() -> this .wireMock .verify (request ));
137151 }
138152
139153 private int getServerPort () {
140- return instance .getEnvironment ().getProperty ("local.server.port" , Integer .class , 0 );
154+ return this . instance .getEnvironment ().getProperty ("local.server.port" , Integer .class , 0 );
141155 }
142156
143157 private int getManagementPort () {
144- return instance .getEnvironment ().getProperty ("local.management.port" , Integer .class , 0 );
158+ return this . instance .getEnvironment ().getProperty ("local.management.port" , Integer .class , 0 );
145159 }
146160
147161 @ SpringBootConfiguration
@@ -154,7 +168,9 @@ public static class TestClientApplication {
154168 @ EventListener
155169 public void ping (ApplicationReadyEvent ev ) {
156170 new Thread (() -> {
157- await ().atMost (Duration .ofMillis (500 )).until (() -> registrator .getRegisteredId () != null );
171+ log .info ("Waiting for registration at mocked sba-server for '{}' ..." , this );
172+ await ().atMost (Duration .ofMillis (3_500 )).until (() -> this .registrator .getRegisteredId () != null );
173+ log .info ("Found registration id '{}' for '{}'" , this .registrator .getRegisteredId (), this );
158174 cdl .countDown ();
159175 }).start ();
160176 }
0 commit comments