Skip to content

Commit 74784d8

Browse files
committed
Add exception handling for Couchbase connection and added more tests for airline
1 parent 9efd0da commit 74784d8

File tree

2 files changed

+138
-68
lines changed

2 files changed

+138
-68
lines changed

src/main/java/org/couchbase/quickstart/springboot/configs/CouchbaseConfig.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.couchbase.quickstart.springboot.configs;
22

3+
import com.couchbase.client.core.error.CouchbaseException;
34
import org.springframework.beans.factory.annotation.Autowired;
45
import org.springframework.context.annotation.Bean;
56
import org.springframework.context.annotation.Configuration;
@@ -30,9 +31,10 @@ public class CouchbaseConfig {
3031
*/
3132
@Bean(destroyMethod = "disconnect")
3233
public Cluster getCouchbaseCluster() {
33-
return Cluster.connect(dbProp.getHostName(), dbProp.getUsername(), dbProp.getPassword());
34+
try {
35+
return Cluster.connect(dbProp.getHostName(), dbProp.getUsername(), dbProp.getPassword());
3436

35-
// Here is an alternative version that enables TLS by configuring the cluster environment.
37+
// Here is an alternative version that enables TLS by configuring the cluster environment.
3638
/* return Cluster.connect(
3739
dbProp.getHostName(),
3840
ClusterOptions.clusterOptions(dbProp.getUsername(), dbProp.getPassword())
@@ -46,7 +48,17 @@ public Cluster getCouchbaseCluster() {
4648
);
4749
})
4850
);
51+
4952
*/
53+
} catch (CouchbaseException e) {
54+
System.err.println("Could not connect to Couchbase cluster at " + dbProp.getHostName());
55+
System.err.println("Please check the username (" + dbProp.getUsername() + ") and password (" + dbProp.getPassword() + ")");
56+
throw e;
57+
} catch (Exception e) {
58+
System.err.println("Could not connect to Couchbase cluster at " + dbProp.getHostName());
59+
throw e;
60+
}
61+
5062
}
5163

5264
@Bean

src/test/java/org/couchbase/quickstart/springboot/controllers/AirlineControllerTest.java

Lines changed: 124 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
import org.springframework.beans.factory.annotation.Value;
1111
import org.springframework.boot.test.context.SpringBootTest;
1212
import org.springframework.boot.test.web.client.TestRestTemplate;
13+
import org.springframework.core.ParameterizedTypeReference;
14+
import org.springframework.http.HttpMethod;
1315
import org.springframework.http.HttpStatus;
1416
import org.springframework.http.ResponseEntity;
1517

@@ -22,10 +24,13 @@ class AirlineControllerTest {
2224
@Autowired
2325
private TestRestTemplate restTemplate;
2426

25-
// {"id":10,"type":"airline","name":"40-Mile Air","iata":"Q5","icao":"MLA","callsign":"MILE-AIR","country":"United States"}
27+
// {"id":10,"type":"airline","name":"40-Mile
28+
// Air","iata":"Q5","icao":"MLA","callsign":"MILE-AIR","country":"United
29+
// States"}
2630
@Test
2731
void testGetAirline() {
28-
ResponseEntity<Airline> response = restTemplate.getForEntity("http://localhost:" + port + "/api/v1/airline/airline_10", Airline.class);
32+
ResponseEntity<Airline> response = restTemplate
33+
.getForEntity("http://localhost:" + port + "/api/v1/airline/airline_10", Airline.class);
2934
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
3035
Airline airline = response.getBody();
3136
assertThat(airline).isNotNull();
@@ -41,7 +46,8 @@ void testGetAirline() {
4146
@Test
4247
void testCreateAirline() {
4348
Airline airline = new Airline("airline_11", "airline", "Test Airline", "TA", "TST", "TEST", "United States");
44-
ResponseEntity<Airline> response = restTemplate.postForEntity("http://localhost:" + port + "/api/v1/airline/" + airline.getId(), airline, Airline.class);
49+
ResponseEntity<Airline> response = restTemplate.postForEntity(
50+
"http://localhost:" + port + "/api/v1/airline/" + airline.getId(), airline, Airline.class);
4551
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.CREATED);
4652
Airline createdAirline = response.getBody();
4753
assertThat(createdAirline).isNotNull();
@@ -51,14 +57,16 @@ void testCreateAirline() {
5157
assertThat(createdAirline.getIata()).isEqualTo("TA");
5258
assertThat(createdAirline.getIcao()).isEqualTo("TST");
5359
assertThat(createdAirline.getCallsign()).isEqualTo("TEST");
54-
assertThat(createdAirline.getCountry()).isEqualTo("United States");
60+
assertThat(createdAirline.getCountry()).isEqualTo("United States");
5561
}
5662

5763
@Test
5864
void testUpdateAirline() {
59-
Airline airline = new Airline("airline_11", "airline", "Updated Test Airline", "TA", "TST", "TEST", "United States");
65+
Airline airline = new Airline("airline_11", "airline", "Updated Test Airline", "TA", "TST", "TEST",
66+
"United States");
6067
restTemplate.put("http://localhost:" + port + "/api/v1/airline/" + airline.getId(), airline);
61-
ResponseEntity<Airline> response = restTemplate.getForEntity("http://localhost:" + port + "/api/v1/airline/" + airline.getId(), Airline.class);
68+
ResponseEntity<Airline> response = restTemplate
69+
.getForEntity("http://localhost:" + port + "/api/v1/airline/" + airline.getId(), Airline.class);
6270

6371
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
6472
Airline updatedAirline = response.getBody();
@@ -75,93 +83,143 @@ void testUpdateAirline() {
7583

7684
@Test
7785
void testDeleteAirline() {
78-
ResponseEntity<Airline> response = restTemplate.getForEntity("http://localhost:" + port + "/api/v1/airline/airline_11", Airline.class);
86+
ResponseEntity<Airline> response = restTemplate
87+
.getForEntity("http://localhost:" + port + "/api/v1/airline/airline_11", Airline.class);
7988
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
8089
Airline airline = response.getBody();
8190
assertThat(airline).isNotNull();
8291
restTemplate.delete("http://localhost:" + port + "/api/v1/airline/" + airline.getId());
83-
ResponseEntity<Airline> response2 = restTemplate.getForEntity("http://localhost:" + port + "/api/v1/airline/airline_11", Airline.class);
92+
ResponseEntity<Airline> response2 = restTemplate
93+
.getForEntity("http://localhost:" + port + "/api/v1/airline/airline_11", Airline.class);
8494
assertThat(response2.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
8595
}
8696

8797
@Test
8898
void testListAirlines() {
89-
ResponseEntity<List> response = restTemplate.getForEntity("http://localhost:" + port + "/api/v1/airline/list", List.class);
99+
ResponseEntity<List<Airline>> response = restTemplate.exchange(
100+
"http://localhost:" + port + "/api/v1/airline/list", HttpMethod.GET, null,
101+
new ParameterizedTypeReference<List<Airline>>() {
102+
});
90103
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
91-
104+
105+
List<Airline> airlines = response.getBody();
106+
assertThat(airlines).isNotNull();
107+
assertThat(airlines.size()).isGreaterThan(180);
108+
92109
}
93110

94111
@Test
95112
void testListAirlinesByCountry() {
96-
ResponseEntity<List> response = restTemplate.getForEntity("http://localhost:" + port + "/api/v1/airline/country/test", List.class);
113+
// Check that if it contains
114+
// airline_10226{"id":10226,"type":"airline","name":"Atifly","iata":"A1","icao":"A1F","callsign":"atifly","country":"United
115+
// States"}
116+
String country = "United States";
117+
ResponseEntity<List<Airline>> response = restTemplate.exchange(
118+
"http://localhost:" + port + "/api/v1/airline/country/" + country,
119+
HttpMethod.GET, null, new ParameterizedTypeReference<List<Airline>>() {
120+
});
97121
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
98-
// Add more assertions as needed
122+
123+
List<Airline> airlines = response.getBody();
124+
Airline airline = airlines.stream().filter(a -> a.getId().equals("10226")).findFirst().orElse(null);
125+
assertThat(airline).isNotNull();
126+
assertThat(airline.getId()).isEqualTo("10226");
127+
assertThat(airline.getType()).isEqualTo("airline");
128+
assertThat(airline.getName()).isEqualTo("Atifly");
129+
assertThat(airline.getIata()).isEqualTo("A1");
130+
assertThat(airline.getIcao()).isEqualTo("A1F");
131+
assertThat(airline.getCallsign()).isEqualTo("atifly");
132+
assertThat(airline.getCountry()).isEqualTo("United States");
133+
134+
// {"id":1191,"type":"airline","name":"Air
135+
// Austral","iata":"UU","icao":"REU","callsign":"REUNION","country":"France"}
136+
137+
country = "France";
138+
ResponseEntity<List<Airline>> response2 = restTemplate.exchange(
139+
"http://localhost:" + port + "/api/v1/airline/country/" + country,
140+
HttpMethod.GET, null, new ParameterizedTypeReference<List<Airline>>() {
141+
});
142+
assertThat(response2.getStatusCode()).isEqualTo(HttpStatus.OK);
143+
144+
List<Airline> airlines2 = response2.getBody();
145+
Airline airline2 = airlines2.stream().filter(a -> a.getId().equals("1191")).findFirst().orElse(null);
146+
assertThat(airline2).isNotNull();
147+
assertThat(airline2.getId()).isEqualTo("1191");
148+
assertThat(airline2.getType()).isEqualTo("airline");
149+
assertThat(airline2.getName()).isEqualTo("Air Austral");
150+
assertThat(airline2.getIata()).isEqualTo("UU");
151+
assertThat(airline2.getIcao()).isEqualTo("REU");
152+
assertThat(airline2.getCallsign()).isEqualTo("REUNION");
153+
assertThat(airline2.getCountry()).isEqualTo("France");
154+
99155
}
100156

101-
// @Test
102-
// void testListAirlinesByDestinationAirport() {
103-
// ResponseEntity<List> response = restTemplate.getForEntity("http://localhost:" + port + "/api/v1/airline/destination/test", List.class);
104-
// assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
105-
// // Add more assertions as needed
106-
// }
157+
// @Test
158+
// void testListAirlinesByDestinationAirport() {
159+
// ResponseEntity<List> response = restTemplate.getForEntity("http://localhost:"
160+
// + port + "/api/v1/airline/destination/test", List.class);
161+
// assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
162+
// // Add more assertions as needed
163+
// }
107164
}
108165

109-
110-
//package org.couchbase.quickstart.springboot.controllers;
166+
// package org.couchbase.quickstart.springboot.controllers;
111167
//
112-
//import org.couchbase.quickstart.springboot.configs.CouchbaseConfig;
113-
//import org.couchbase.quickstart.springboot.configs.DBProperties;
114-
//import org.couchbase.quickstart.springboot.configs.Swagger;
115-
//import org.junit.Before;
116-
//import org.junit.Test;
117-
//import org.junit.runner.RunWith;
118-
//import org.springframework.beans.factory.annotation.Autowired;
119-
//import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
120-
//import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
121-
//import org.springframework.boot.test.context.SpringBootTest;
122-
//import org.springframework.test.context.ContextConfiguration;
123-
//import org.springframework.test.context.junit4.SpringRunner;
124-
//import org.springframework.test.context.web.WebAppConfiguration;
125-
//import org.springframework.test.web.servlet.MockMvc;
126-
//import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
127-
//import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
128-
//import org.springframework.test.web.servlet.setup.MockMvcBuilders;
129-
//import org.springframework.web.context.WebApplicationContext;
168+
// import org.couchbase.quickstart.springboot.configs.CouchbaseConfig;
169+
// import org.couchbase.quickstart.springboot.configs.DBProperties;
170+
// import org.couchbase.quickstart.springboot.configs.Swagger;
171+
// import org.junit.Before;
172+
// import org.junit.Test;
173+
// import org.junit.runner.RunWith;
174+
// import org.springframework.beans.factory.annotation.Autowired;
175+
// import
176+
// org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
177+
// import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
178+
// import org.springframework.boot.test.context.SpringBootTest;
179+
// import org.springframework.test.context.ContextConfiguration;
180+
// import org.springframework.test.context.junit4.SpringRunner;
181+
// import org.springframework.test.context.web.WebAppConfiguration;
182+
// import org.springframework.test.web.servlet.MockMvc;
183+
// import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
184+
// import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
185+
// import org.springframework.test.web.servlet.setup.MockMvcBuilders;
186+
// import org.springframework.web.context.WebApplicationContext;
130187
//
131-
//import com.couchbase.client.java.Bucket;
132-
//import com.couchbase.client.java.Cluster;
133-
//import com.couchbase.client.java.Collection;
188+
// import com.couchbase.client.java.Bucket;
189+
// import com.couchbase.client.java.Cluster;
190+
// import com.couchbase.client.java.Collection;
134191
//
135192
//
136-
////@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
137-
////@AutoConfigureMockMvc
138-
//@ContextConfiguration(classes = {CouchbaseConfig.class, Swagger.class, DBProperties.class})
139-
////@RunWith(SpringRunner.class)
193+
//// @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
194+
//// @AutoConfigureMockMvc
195+
// @ContextConfiguration(classes = {CouchbaseConfig.class, Swagger.class,
196+
// DBProperties.class})
197+
//// @RunWith(SpringRunner.class)
140198
//
141-
//@RunWith(SpringRunner.class)
142-
//@WebMvcTest(AirlineController.class)
143-
//@WebAppConfiguration
144-
//public class AirlineControllerTest {
199+
// @RunWith(SpringRunner.class)
200+
// @WebMvcTest(AirlineController.class)
201+
// @WebAppConfiguration
202+
// public class AirlineControllerTest {
145203
//
146-
// @Autowired
147-
// private WebApplicationContext webApplicationContext;
204+
// @Autowired
205+
// private WebApplicationContext webApplicationContext;
148206
//
149-
// private MockMvc mockMvc;
207+
// private MockMvc mockMvc;
150208
//
151-
// // ... your other dependencies or setup if needed
209+
// // ... your other dependencies or setup if needed
152210
//
153-
// @Before
154-
// public void setup() {
155-
// mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
156-
// }
211+
// @Before
212+
// public void setup() {
213+
// mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
214+
// }
157215
//
158-
// @Test
159-
// public void testGetAirline() {
160-
// // Perform the GET request using mockMvc and assert the response
161-
// mockMvc.perform(MockMvcRequestBuilders.get("/api/v1/airline/test"))
162-
// .andExpect(MockMvcResultMatchers.status().isOk())
163-
// .andExpect(MockMvcResultMatchers.jsonPath("$.id").value("test"));
164-
// }
216+
// @Test
217+
// public void testGetAirline() {
218+
// // Perform the GET request using mockMvc and assert the response
219+
// mockMvc.perform(MockMvcRequestBuilders.get("/api/v1/airline/test"))
220+
// .andExpect(MockMvcResultMatchers.status().isOk())
221+
// .andExpect(MockMvcResultMatchers.jsonPath("$.id").value("test"));
222+
// }
165223
//
166-
// // ... other test methods
167-
//}
224+
// // ... other test methods
225+
// }

0 commit comments

Comments
 (0)