33import static org .assertj .core .api .Java6Assertions .assertThat ;
44
55import com .palantir .docker .compose .DockerComposeRule ;
6+ import com .palantir .docker .compose .connection .DockerPort ;
67import com .palantir .docker .compose .connection .waiting .HealthChecks ;
78
89import java .util .*;
910
1011import org .json .JSONException ;
1112import org .json .JSONObject ;
1213import org .junit .Before ;
14+ import org .junit .BeforeClass ;
1315import org .junit .ClassRule ;
1416import org .junit .Test ;
1517import org .junit .runner .RunWith ;
1618
19+ import org .slf4j .Logger ;
20+ import org .slf4j .LoggerFactory ;
1721import org .springframework .boot .test .context .SpringBootTest ;
1822import org .springframework .boot .test .web .client .TestRestTemplate ;
1923import org .springframework .http .*;
2327@ RunWith (SpringJUnit4ClassRunner .class )
2428@ SpringBootTest (classes = EdgeServiceApplication .class , webEnvironment = SpringBootTest .WebEnvironment .DEFINED_PORT )
2529public class EdgeServiceIntegrationTests {
30+ protected static final Logger LOG = LoggerFactory .getLogger (EdgeServiceIntegrationTests .class );
2631
32+ private static String tripCommandURL ;
33+ private static String tripQueryURL ;
34+ private static String gmapsAdapterURL ;
35+ private static String calculationServiceURL ;
36+ private static String userServiceURL ;
37+
38+ //Wait for all services to have ports open
2739 @ ClassRule
2840 public static DockerComposeRule docker = DockerComposeRule .builder ().pullOnStartup (true )
2941 .file ("src/test/resources/docker-compose.yml" )
30- .waitingForService ("microservice--user-service" , HealthChecks .toHaveAllPortsOpen ())
31- .waitingForService ("microservice--user-service" , HealthChecks .toRespondOverHttp (8091 ,
32- (port ) -> port .inFormat ("http://localhost:8091" )))
33- .waitingForService ("trip-management-cmd" , HealthChecks .toHaveAllPortsOpen ())
34- .waitingForService ("trip-management-cmd" , HealthChecks .toRespondOverHttp (8080 ,
35- (port ) -> port .inFormat ("http://localhost:8092" )))
36- .waitingForService ("trip-management-query" , HealthChecks .toHaveAllPortsOpen ())
37- .waitingForService ("trip-management-query" , HealthChecks .toRespondOverHttp (8080 ,
38- (port ) -> port .inFormat ("http://localhost:8093" )))
42+ .waitingForService ("userservice" , HealthChecks .toHaveAllPortsOpen ())
43+ .waitingForService ("mongo" , HealthChecks .toHaveAllPortsOpen ())
44+ .waitingForService ("rabbitmq" , HealthChecks .toHaveAllPortsOpen ())
45+ .waitingForService ("tripmanagementcmd" , HealthChecks .toHaveAllPortsOpen ())
46+ .waitingForService ("tripmanagementquery" , HealthChecks .toHaveAllPortsOpen ())
47+ .waitingForService ("gmapsadapter" , HealthChecks .toHaveAllPortsOpen ())
48+ .waitingForService ("calculationservice" , HealthChecks .toHaveAllPortsOpen ())
3949 .waitingForService ("discovery-service" , HealthChecks .toHaveAllPortsOpen ())
4050 .waitingForService ("discovery-service" , HealthChecks .toRespondOverHttp (8761 ,
4151 (port ) -> port .inFormat ("http://localhost:8761" )))
4252 .build ();
4353
54+ //Get IP addresses and ports to run tests on
55+ @ BeforeClass
56+ public static void initialize () {
57+ LOG .info ("Initializing ports from Docker" );
58+ DockerPort tripManagementCommand = docker .containers ().container ("tripmanagementcmd" )
59+ .port (8080 );
60+ tripCommandURL = String .format ("http://%s:%s" , tripManagementCommand .getIp (),
61+ tripManagementCommand .getExternalPort ());
62+ LOG .info ("Trip Command url found: " + tripCommandURL );
63+
64+ DockerPort tripManagementQuery = docker .containers ().container ("tripmanagementquery" )
65+ .port (8080 );
66+ tripQueryURL = String .format ("http://%s:%s" , tripManagementQuery .getIp (),
67+ tripManagementQuery .getExternalPort ());
68+ LOG .info ("Trip Query url found: " + tripQueryURL );
69+
70+ DockerPort gmapsAdapter = docker .containers ().container ("gmapsadapter" )
71+ .port (8080 );
72+ gmapsAdapterURL = String .format ("http://%s:%s" , gmapsAdapter .getIp (),
73+ gmapsAdapter .getExternalPort ());
74+ LOG .info ("Gmaps Adapter url found: " + gmapsAdapterURL );
75+
76+ DockerPort calculationService = docker .containers ().container ("calculationservice" )
77+ .port (8080 );
78+ calculationServiceURL = String .format ("http://%s:%s" , calculationService .getIp (),
79+ calculationService .getExternalPort ());
80+ LOG .info ("Calculation Service url found: " + calculationServiceURL );
81+
82+ DockerPort userService = docker .containers ().container ("userservice" )
83+ .port (8080 );
84+ userServiceURL = String .format ("http://%s:%s" , userService .getIp (),
85+ userService .getExternalPort ());
86+ while (!docker .containers ().container ("userservice" ).portIsListeningOnHttp (8080 ,
87+ (port ) -> port .inFormat (userServiceURL )).succeeded ()) {
88+ LOG .info ("Waiting for user service to respond over HTTP" );
89+ }
90+ LOG .info ("User Service url found: " + userServiceURL );
91+ }
92+
4493 private TestRestTemplate restTemplate = new TestRestTemplate ();
4594
4695 private String token ;
96+ private String tripId ;
4797
4898 @ Before
4999 public void setUp () throws JSONException {
@@ -63,40 +113,67 @@ public void setUp() throws JSONException {
63113 HttpEntity <String > request = new HttpEntity <>(body , headers );
64114
65115 //when:
66- ResponseEntity <String > response = restTemplate .postForEntity ("http://localhost:8091/auth/oauth/token" , request , String .class , parameters );
116+ ResponseEntity <String > response = restTemplate .postForEntity (userServiceURL + "/auth/oauth/token" ,
117+ request , String .class , parameters );
67118
68119 //then:
69120 assertThat (response .getStatusCodeValue ()).isEqualTo (200 );
70121 JSONObject json = new JSONObject (response .getBody ());
71122 token = json .getString ("access_token" );
72123 }
73124
125+
74126 @ Test
75127 public void tripCommandPOSTRequestSuccess () {
76128 //given:
77129 HttpHeaders headers = new HttpHeaders ();
78130 headers .add ("Authorization" , "Bearer " + token );
79131 headers .add ("Content-Type" , "application/json" );
80132
81- String body = "{ \" originAddress\" : \" Somewhere of the origin \" , \" destinationAddress\" : "
82- + "\" Somewhere destination \" , \" userId\" : \" 123e4567-e89b-12d3-a456-426655440000\" }" ;
133+ String body = "{ \" originAddress\" : \" Weston, FL \" , \" destinationAddress\" : "
134+ + "\" Miami, FL \" , \" userId\" : \" 123e4567-e89b-12d3-a456-426655440000\" }" ;
83135 HttpEntity <String > request = new HttpEntity <>(body , headers );
84136
85137 //when:
86- ResponseEntity <String > response = restTemplate .postForEntity ("http://localhost:8092/api/trip" , request , String .class );
138+ ResponseEntity <String > response = restTemplate .postForEntity (tripCommandURL + "/api/v1/trip" , request ,
139+ String .class );
140+
141+ //then:
142+ assertThat (response .getStatusCodeValue ()).isEqualTo (201 );
143+ }
144+
145+ @ Test
146+ public void tripQueryGETSpecificTripRequestSuccess () throws JSONException {
147+ //given:
148+ HttpHeaders headers = new HttpHeaders ();
149+ headers .add ("Authorization" , "Bearer " + token );
150+ headers .add ("Content-Type" , "application/json" );
151+
152+ String body = "{ \" originAddress\" : \" Weston, FL\" , \" destinationAddress\" : "
153+ + "\" Miami, FL\" , \" userId\" : \" 123e4567-e89b-12d3-a456-426655440000\" }" ;
154+ HttpEntity <String > request = new HttpEntity <>(body , headers );
155+ ResponseEntity <String > postResponse = restTemplate .postForEntity (tripCommandURL + "/api/v1/trip" , request , String .class );
156+ assertThat (postResponse .getStatusCodeValue ()).isEqualTo (201 );
157+
158+ JSONObject json = new JSONObject (postResponse .getBody ());
159+ tripId = json .getString ("id" );
160+
161+ //when:
162+ ResponseEntity <String > response = restTemplate .getForEntity (tripQueryURL + "/api/v1/trip/" + tripId , String .class );
87163
88164 //then:
89165 assertThat (response .getStatusCodeValue ()).isEqualTo (200 );
90166 }
91167
92168 @ Test
93- public void tripQueryGETRequestSuccess () {
169+ public void tripQueryGETAllTripsRequestSuccess () {
94170 //given:
95171 HttpHeaders headers = new HttpHeaders ();
96172 headers .add ("Authorization" , "Bearer " + token );
173+ headers .add ("Content-Type" , "application/json" );
97174
98175 //when:
99- ResponseEntity <String > response = restTemplate .getForEntity ("http://localhost:8093/ api/trips" , String .class );
176+ ResponseEntity <String > response = restTemplate .getForEntity (tripQueryURL + "/ api/v1 /trips" , String .class );
100177
101178 //then:
102179 assertThat (response .getStatusCodeValue ()).isEqualTo (200 );
0 commit comments