1818
1919import java .net .InetAddress ;
2020import java .net .UnknownHostException ;
21+ import java .util .Arrays ;
2122import java .util .concurrent .CountDownLatch ;
23+ import java .util .stream .Stream ;
2224
2325import com .github .tomakehurst .wiremock .WireMockServer ;
2426import com .github .tomakehurst .wiremock .client .ResponseDefinitionBuilder ;
2729import org .junit .jupiter .api .AfterEach ;
2830import org .junit .jupiter .api .Test ;
2931import org .springframework .beans .factory .annotation .Autowired ;
32+ import org .springframework .boot .SpringApplication ;
3033import org .springframework .boot .SpringBootConfiguration ;
34+ import org .springframework .boot .WebApplicationType ;
3135import org .springframework .boot .autoconfigure .EnableAutoConfiguration ;
3236import org .springframework .boot .context .event .ApplicationReadyEvent ;
37+ import org .springframework .context .ConfigurableApplicationContext ;
3338import org .springframework .context .event .EventListener ;
3439
3540import de .codecentric .boot .admin .client .registration .ApplicationRegistrator ;
4550
4651public abstract class AbstractClientApplicationTest {
4752
48- public WireMockServer wireMock = new WireMockServer (options ().dynamicPort ().notifier (new ConsoleNotifier (true )));
53+ private final WireMockServer wireMock = new WireMockServer (
54+ options ().dynamicPort ().notifier (new ConsoleNotifier (true )));
55+
56+ private SpringApplication application ;
57+
58+ private ConfigurableApplicationContext instance ;
4959
5060 private static final CountDownLatch cdl = new CountDownLatch (1 );
5161
52- public void setUp () throws Exception {
62+ protected void setUp (WebApplicationType type ) throws Exception {
63+ setUpWiremock ();
64+ setUpApplication (type );
65+ }
66+
67+ private void setUpWiremock () {
5368 wireMock .start ();
5469 ResponseDefinitionBuilder response = created ().withHeader ("Content-Type" , "application/json" )
5570 .withHeader ("Connection" , "close" )
@@ -58,13 +73,33 @@ public void setUp() throws Exception {
5873 wireMock .stubFor (post (urlEqualTo ("/instances" )).willReturn (response ));
5974 }
6075
76+ private void setUpApplication (WebApplicationType type ) {
77+ application = new SpringApplication (TestClientApplication .class );
78+ application .setWebApplicationType (type );
79+ }
80+
81+ private void setUpApplicationContext (String ... additionalArgs ) {
82+ Stream <String > defaultArgs = Stream .of ("--spring.application.name=Test-Client" , "--server.port=0" ,
83+ "--management.endpoints.web.base-path=/mgmt" , "--endpoints.health.enabled=true" ,
84+ "--spring.boot.admin.client.url=" + wireMock .url ("/" ));
85+
86+ String [] args = Stream .concat (defaultArgs , Arrays .stream (additionalArgs )).toArray (String []::new );
87+
88+ this .instance = application .run (args );
89+ }
90+
6191 @ AfterEach
6292 void tearDown () {
6393 wireMock .stop ();
94+ if (instance != null ) {
95+ instance .close ();
96+ }
6497 }
6598
6699 @ Test
67100 public void test_context () throws InterruptedException , UnknownHostException {
101+ setUpApplicationContext ();
102+
68103 cdl .await ();
69104 Thread .sleep (2500 );
70105 String hostName = InetAddress .getLocalHost ().getCanonicalHostName ();
@@ -81,9 +116,33 @@ public void test_context() throws InterruptedException, UnknownHostException {
81116 wireMock .verify (request );
82117 }
83118
84- protected abstract int getServerPort ();
119+ @ Test
120+ public void test_context_with_snake_case () throws InterruptedException , UnknownHostException {
121+ setUpApplicationContext ("--spring.jackson.property-naming-strategy=SNAKE_CASE" );
85122
86- protected abstract int getManagementPort ();
123+ cdl .await ();
124+ Thread .sleep (2500 );
125+ String hostName = InetAddress .getLocalHost ().getCanonicalHostName ();
126+ String serviceHost = "http://" + hostName + ":" + getServerPort ();
127+ String managementHost = "http://" + hostName + ":" + getManagementPort ();
128+ RequestPatternBuilder request = postRequestedFor (urlEqualTo ("/instances" ));
129+ request .withHeader ("Content-Type" , equalTo ("application/json" ))
130+ .withRequestBody (matchingJsonPath ("$.name" , equalTo ("Test-Client" )))
131+ .withRequestBody (matchingJsonPath ("$.health_url" , equalTo (managementHost + "/mgmt/health" )))
132+ .withRequestBody (matchingJsonPath ("$.management_url" , equalTo (managementHost + "/mgmt" )))
133+ .withRequestBody (matchingJsonPath ("$.service_url" , equalTo (serviceHost + "/" )))
134+ .withRequestBody (matchingJsonPath ("$.metadata.startup" , matching (".+" )));
135+
136+ wireMock .verify (request );
137+ }
138+
139+ private int getServerPort () {
140+ return instance .getEnvironment ().getProperty ("local.server.port" , Integer .class , 0 );
141+ }
142+
143+ private int getManagementPort () {
144+ return instance .getEnvironment ().getProperty ("local.management.port" , Integer .class , 0 );
145+ }
87146
88147 @ SpringBootConfiguration
89148 @ EnableAutoConfiguration
0 commit comments