Skip to content

Commit 1a3ceaa

Browse files
committed
Refactor integration tests to use dynamic base URI
This commit refactors the integration tests in the `RouteIntegrationTest` and `AirlineIntegrationTest` classes to use a dynamic base URI based on the value of the `spring.couchbase.bootstrap-hosts` property. This allows the tests to run against different environments without modifying the code.
1 parent d1c8190 commit 1a3ceaa

File tree

3 files changed

+74
-20
lines changed

3 files changed

+74
-20
lines changed

src/test/java/org/couchbase/quickstart/springdata/controllers/AirlineIntegrationTest.java

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ class AirlineIntegrationTest {
2626
@Value("${local.server.port}")
2727
private int port;
2828

29+
@Value("${spring.couchbase.bootstrap-hosts}")
30+
private String bootstrapHosts;
31+
2932
@Autowired
3033
private TestRestTemplate restTemplate;
3134

@@ -34,15 +37,22 @@ class AirlineIntegrationTest {
3437

3538
@BeforeEach
3639
void setUp() {
40+
String baseUri = "";
41+
if (bootstrapHosts.contains("localhost")) {
42+
baseUri = "http://localhost:" + port;
43+
} else {
44+
baseUri = bootstrapHosts;
45+
}
46+
System.out.println("baseUri: " + baseUri);
3747
try {
3848
if (airlineService.getAirlineById("airline_create").isPresent()) {
39-
restTemplate.delete("http://localhost:" + port + "/api/v1/airline/airline_create");
49+
restTemplate.delete(baseUri + "/api/v1/airline/airline_create");
4050
}
4151
if (airlineService.getAirlineById("airline_update").isPresent()) {
42-
restTemplate.delete("http://localhost:" + port + "/api/v1/airline/airline_update");
52+
restTemplate.delete(baseUri + "/api/v1/airline/airline_update");
4353
}
4454
if (airlineService.getAirlineById("airline_delete").isPresent()) {
45-
restTemplate.delete("http://localhost:" + port + "/api/v1/airline/airline_delete");
55+
restTemplate.delete(baseUri + "/api/v1/airline/airline_delete");
4656
}
4757
} catch (DocumentNotFoundException | DataRetrievalFailureException e) {
4858
System.out.println("Document not found during setup");
@@ -53,15 +63,22 @@ void setUp() {
5363

5464
@AfterEach
5565
void tearDown() {
66+
String baseUri = "";
67+
if (bootstrapHosts.contains("localhost")) {
68+
baseUri = "http://localhost:" + port;
69+
} else {
70+
baseUri = bootstrapHosts;
71+
}
72+
System.out.println("baseUri: " + baseUri);
5673
try {
5774
if (airlineService.getAirlineById("airline_create").isPresent()) {
58-
restTemplate.delete("http://localhost:" + port + "/api/v1/airline/airline_create");
75+
restTemplate.delete(baseUri + "/api/v1/airline/airline_create");
5976
}
6077
if (airlineService.getAirlineById("airline_update").isPresent()) {
61-
restTemplate.delete("http://localhost:" + port + "/api/v1/airline/airline_update");
78+
restTemplate.delete(baseUri + "/api/v1/airline/airline_update");
6279
}
6380
if (airlineService.getAirlineById("airline_delete").isPresent()) {
64-
restTemplate.delete("http://localhost:" + port + "/api/v1/airline/airline_delete");
81+
restTemplate.delete(baseUri + "/api/v1/airline/airline_delete");
6582
}
6683
} catch (DocumentNotFoundException | DataRetrievalFailureException e) {
6784
System.out.println("Document not found during teardown");

src/test/java/org/couchbase/quickstart/springdata/controllers/AirportIntegrationTest.java

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ class AirportIntegrationTest {
2727
@Value("${local.server.port}")
2828
private int port;
2929

30+
@Value("${spring.couchbase.bootstrap-hosts}")
31+
private String bootstrapHosts;
32+
3033
@Autowired
3134
private TestRestTemplate restTemplate;
3235

@@ -35,34 +38,49 @@ class AirportIntegrationTest {
3538

3639
@BeforeEach
3740
void setUp() {
41+
42+
String baseUri = "";
43+
if (bootstrapHosts.contains("localhost")) {
44+
baseUri = "http://localhost:" + port;
45+
} else {
46+
baseUri = bootstrapHosts;
47+
}
48+
3849
try {
39-
if (airportService.getAirportById("airport_create").isPresent()) {
40-
restTemplate.delete("http://localhost:" + port + "/api/v1/airport/airport_create");
50+
if (airportService.getAirportById("airport_create").isPresent()) {
51+
restTemplate.delete(baseUri + "/api/v1/airport/airport_create");
4152
}
4253
if (airportService.getAirportById("airport_update").isPresent()) {
43-
restTemplate.delete("http://localhost:" + port + "/api/v1/airport/airport_update");
54+
restTemplate.delete(baseUri + "/api/v1/airport/airport_update");
4455
}
4556
if (airportService.getAirportById("airport_delete").isPresent()) {
46-
restTemplate.delete("http://localhost:" + port + "/api/v1/airport/airport_delete");
57+
restTemplate.delete(baseUri + "/api/v1/airport/airport_delete");
4758
}
59+
4860
} catch (DocumentNotFoundException | DataRetrievalFailureException e) {
4961
System.out.println("Document not found during setup");
5062
} catch (Exception e) {
51-
System.out.println("Error deleting test data during setup");
63+
System.out.println("Error creating test data during setup");
5264
}
5365
}
5466

5567
@AfterEach
5668
void tearDown() {
69+
String baseUri = "";
70+
if (bootstrapHosts.contains("localhost")) {
71+
baseUri = "http://localhost:" + port;
72+
} else {
73+
baseUri = bootstrapHosts;
74+
}
5775
try {
5876
if (airportService.getAirportById("airport_create").isPresent()) {
59-
restTemplate.delete("http://localhost:" + port + "/api/v1/airport/airport_create");
77+
restTemplate.delete(baseUri + "/api/v1/airport/airport_create");
6078
}
6179
if (airportService.getAirportById("airport_update").isPresent()) {
62-
restTemplate.delete("http://localhost:" + port + "/api/v1/airport/airport_update");
80+
restTemplate.delete(baseUri + "/api/v1/airport/airport_update");
6381
}
6482
if (airportService.getAirportById("airport_delete").isPresent()) {
65-
restTemplate.delete("http://localhost:" + port + "/api/v1/airport/airport_delete");
83+
restTemplate.delete(baseUri + "/api/v1/airport/airport_delete");
6684
}
6785
} catch (DocumentNotFoundException | DataRetrievalFailureException e) {
6886
System.out.println("Document not found during setup");

src/test/java/org/couchbase/quickstart/springdata/controllers/RouteIntegrationTest.java

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ class RouteIntegrationTest {
2828
@Value("${local.server.port}")
2929
private int port;
3030

31+
@Value("${spring.couchbase.bootstrap-hosts}")
32+
private String bootstrapHosts;
33+
3134
@Autowired
3235
private TestRestTemplate restTemplate;
3336

@@ -36,15 +39,23 @@ class RouteIntegrationTest {
3639

3740
@BeforeEach
3841
void setUp() {
42+
43+
String baseUri = "";
44+
if (bootstrapHosts.contains("localhost")) {
45+
baseUri = "http://localhost:" + port;
46+
} else {
47+
baseUri = bootstrapHosts;
48+
}
49+
3950
try {
4051
if (routeService.getRouteById("route_create").isPresent()) {
41-
restTemplate.delete("http://localhost:" + port + "/api/v1/route/route_create");
52+
restTemplate.delete(baseUri + "/api/v1/route/route_create");
4253
}
4354
if (routeService.getRouteById("route_update").isPresent()) {
44-
restTemplate.delete("http://localhost:" + port + "/api/v1/route/route_update");
55+
restTemplate.delete(baseUri + "/api/v1/route/route_update");
4556
}
4657
if (routeService.getRouteById("route_delete").isPresent()) {
47-
restTemplate.delete("http://localhost:" + port + "/api/v1/route/route_delete");
58+
restTemplate.delete(baseUri + "/api/v1/route/route_delete");
4859
}
4960
} catch (DocumentNotFoundException | DataRetrievalFailureException e) {
5061
System.out.println("Document not found during setup");
@@ -55,15 +66,23 @@ void setUp() {
5566

5667
@AfterEach
5768
void tearDown() {
69+
70+
String baseUri = "";
71+
if (bootstrapHosts.contains("localhost")) {
72+
baseUri = "http://localhost:" + port;
73+
} else {
74+
baseUri = bootstrapHosts;
75+
}
76+
5877
try {
5978
if (routeService.getRouteById("route_create").isPresent()) {
60-
restTemplate.delete("http://localhost:" + port + "/api/v1/route/route_create");
79+
restTemplate.delete(baseUri + "/api/v1/route/route_create");
6180
}
6281
if (routeService.getRouteById("route_update").isPresent()) {
63-
restTemplate.delete("http://localhost:" + port + "/api/v1/route/route_update");
82+
restTemplate.delete(baseUri + "/api/v1/route/route_update");
6483
}
6584
if (routeService.getRouteById("route_delete").isPresent()) {
66-
restTemplate.delete("http://localhost:" + port + "/api/v1/route/route_delete");
85+
restTemplate.delete(baseUri + "/api/v1/route/route_delete");
6786
}
6887
} catch (DocumentNotFoundException | DataRetrievalFailureException e) {
6988
System.out.println("Document not found during setup");

0 commit comments

Comments
 (0)