|
| 1 | +package org.couchbase.quickstart.springboot.controllers; |
| 2 | + |
| 3 | +import static org.assertj.core.api.Assertions.assertThat; |
| 4 | + |
| 5 | +import java.util.List; |
| 6 | + |
| 7 | +import org.couchbase.quickstart.springboot.models.Airline; |
| 8 | +import org.junit.jupiter.api.Test; |
| 9 | +import org.springframework.beans.factory.annotation.Autowired; |
| 10 | +import org.springframework.beans.factory.annotation.Value; |
| 11 | +import org.springframework.boot.test.context.SpringBootTest; |
| 12 | +import org.springframework.boot.test.web.client.TestRestTemplate; |
| 13 | +import org.springframework.http.HttpStatus; |
| 14 | +import org.springframework.http.ResponseEntity; |
| 15 | + |
| 16 | +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) |
| 17 | +class AirlineControllerTest { |
| 18 | + |
| 19 | + @Value("${local.server.port}") |
| 20 | + private int port; |
| 21 | + |
| 22 | + @Autowired |
| 23 | + private TestRestTemplate restTemplate; |
| 24 | + |
| 25 | +// {"id":10,"type":"airline","name":"40-Mile Air","iata":"Q5","icao":"MLA","callsign":"MILE-AIR","country":"United States"} |
| 26 | + @Test |
| 27 | + void testGetAirline() { |
| 28 | + ResponseEntity<Airline> response = restTemplate.getForEntity("http://localhost:" + port + "/api/v1/airline/airline_10", Airline.class); |
| 29 | + assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); |
| 30 | + Airline airline = response.getBody(); |
| 31 | + assertThat(airline).isNotNull(); |
| 32 | + assertThat(airline.getId()).isEqualTo("10"); |
| 33 | + assertThat(airline.getType()).isEqualTo("airline"); |
| 34 | + assertThat(airline.getName()).isEqualTo("40-Mile Air"); |
| 35 | + assertThat(airline.getIata()).isEqualTo("Q5"); |
| 36 | + assertThat(airline.getIcao()).isEqualTo("MLA"); |
| 37 | + assertThat(airline.getCallsign()).isEqualTo("MILE-AIR"); |
| 38 | + assertThat(airline.getCountry()).isEqualTo("United States"); |
| 39 | + } |
| 40 | + |
| 41 | + @Test |
| 42 | + void testCreateAirline() { |
| 43 | + 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); |
| 45 | + assertThat(response.getStatusCode()).isEqualTo(HttpStatus.CREATED); |
| 46 | + Airline createdAirline = response.getBody(); |
| 47 | + assertThat(createdAirline).isNotNull(); |
| 48 | + assertThat(createdAirline.getId()).isEqualTo("airline_11"); |
| 49 | + assertThat(createdAirline.getType()).isEqualTo("airline"); |
| 50 | + assertThat(createdAirline.getName()).isEqualTo("Test Airline"); |
| 51 | + assertThat(createdAirline.getIata()).isEqualTo("TA"); |
| 52 | + assertThat(createdAirline.getIcao()).isEqualTo("TST"); |
| 53 | + assertThat(createdAirline.getCallsign()).isEqualTo("TEST"); |
| 54 | + assertThat(createdAirline.getCountry()).isEqualTo("United States"); |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + void testUpdateAirline() { |
| 59 | + Airline airline = new Airline("airline_11", "airline", "Updated Test Airline", "TA", "TST", "TEST", "United States"); |
| 60 | + 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); |
| 62 | + |
| 63 | + assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); |
| 64 | + Airline updatedAirline = response.getBody(); |
| 65 | + assertThat(updatedAirline).isNotNull(); |
| 66 | + assertThat(updatedAirline.getId()).isEqualTo("airline_11"); |
| 67 | + assertThat(updatedAirline.getType()).isEqualTo("airline"); |
| 68 | + assertThat(updatedAirline.getName()).isEqualTo("Updated Test Airline"); |
| 69 | + assertThat(updatedAirline.getIata()).isEqualTo("TA"); |
| 70 | + assertThat(updatedAirline.getIcao()).isEqualTo("TST"); |
| 71 | + assertThat(updatedAirline.getCallsign()).isEqualTo("TEST"); |
| 72 | + assertThat(updatedAirline.getCountry()).isEqualTo("United States"); |
| 73 | + |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + void testDeleteAirline() { |
| 78 | + ResponseEntity<Airline> response = restTemplate.getForEntity("http://localhost:" + port + "/api/v1/airline/airline_11", Airline.class); |
| 79 | + assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); |
| 80 | + Airline airline = response.getBody(); |
| 81 | + assertThat(airline).isNotNull(); |
| 82 | + 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); |
| 84 | + assertThat(response2.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + void testListAirlines() { |
| 89 | + ResponseEntity<List> response = restTemplate.getForEntity("http://localhost:" + port + "/api/v1/airline/list", List.class); |
| 90 | + assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); |
| 91 | + |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + void testListAirlinesByCountry() { |
| 96 | + ResponseEntity<List> response = restTemplate.getForEntity("http://localhost:" + port + "/api/v1/airline/country/test", List.class); |
| 97 | + assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); |
| 98 | + // Add more assertions as needed |
| 99 | + } |
| 100 | + |
| 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 | +// } |
| 107 | +} |
| 108 | + |
| 109 | + |
| 110 | +//package org.couchbase.quickstart.springboot.controllers; |
| 111 | +// |
| 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; |
| 130 | +// |
| 131 | +//import com.couchbase.client.java.Bucket; |
| 132 | +//import com.couchbase.client.java.Cluster; |
| 133 | +//import com.couchbase.client.java.Collection; |
| 134 | +// |
| 135 | +// |
| 136 | +////@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) |
| 137 | +////@AutoConfigureMockMvc |
| 138 | +//@ContextConfiguration(classes = {CouchbaseConfig.class, Swagger.class, DBProperties.class}) |
| 139 | +////@RunWith(SpringRunner.class) |
| 140 | +// |
| 141 | +//@RunWith(SpringRunner.class) |
| 142 | +//@WebMvcTest(AirlineController.class) |
| 143 | +//@WebAppConfiguration |
| 144 | +//public class AirlineControllerTest { |
| 145 | +// |
| 146 | +// @Autowired |
| 147 | +// private WebApplicationContext webApplicationContext; |
| 148 | +// |
| 149 | +// private MockMvc mockMvc; |
| 150 | +// |
| 151 | +// // ... your other dependencies or setup if needed |
| 152 | +// |
| 153 | +// @Before |
| 154 | +// public void setup() { |
| 155 | +// mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build(); |
| 156 | +// } |
| 157 | +// |
| 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 | +// } |
| 165 | +// |
| 166 | +// // ... other test methods |
| 167 | +//} |
0 commit comments