|
| 1 | +package roomescape; |
| 2 | + |
| 3 | +import io.restassured.RestAssured; |
| 4 | +import io.restassured.http.ContentType; |
| 5 | +import org.junit.jupiter.api.Test; |
| 6 | +import org.springframework.beans.factory.annotation.Autowired; |
| 7 | +import org.springframework.boot.test.context.SpringBootTest; |
| 8 | +import org.springframework.jdbc.core.JdbcTemplate; |
| 9 | +import org.springframework.test.annotation.DirtiesContext; |
| 10 | +import roomescape.dto.Reservation; |
| 11 | + |
| 12 | +import java.sql.Connection; |
| 13 | +import java.sql.SQLException; |
| 14 | +import java.util.HashMap; |
| 15 | +import java.util.List; |
| 16 | +import java.util.Map; |
| 17 | + |
| 18 | +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; |
| 19 | +import static org.hamcrest.Matchers.is; |
| 20 | + |
| 21 | +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT) |
| 22 | +@DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_EACH_TEST_METHOD) |
| 23 | +public class MissionStepTest2 { |
| 24 | + |
| 25 | + @Autowired |
| 26 | + private JdbcTemplate jdbcTemplate; |
| 27 | + |
| 28 | + @Test |
| 29 | + void 오단계() { |
| 30 | + try (Connection connection = jdbcTemplate.getDataSource().getConnection()) { |
| 31 | + assertThat(connection).isNotNull(); |
| 32 | + assertThat(connection.getCatalog()).isEqualTo("DATABASE"); |
| 33 | + assertThat(connection.getMetaData().getTables(null, null, "RESERVATION", null).next()).isTrue(); |
| 34 | + } catch (SQLException e) { |
| 35 | + throw new RuntimeException(e); |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + void 육단계() { |
| 41 | + jdbcTemplate.update("INSERT INTO reservation (name, date, time) VALUES (?, ?, ?)", "브라운", "2023-08-05", "15:40"); |
| 42 | + |
| 43 | + List<Reservation> reservations = RestAssured.given().log().all() |
| 44 | + .when().get("/reservations") |
| 45 | + .then().log().all() |
| 46 | + .statusCode(200).extract() |
| 47 | + .jsonPath().getList(".", Reservation.class); |
| 48 | + |
| 49 | + Integer count = jdbcTemplate.queryForObject("SELECT count(1) from reservation", Integer.class); |
| 50 | + |
| 51 | + assertThat(reservations.size()).isEqualTo(count); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + void 칠단계() { |
| 56 | + Map<String, String> params = new HashMap<>(); |
| 57 | + params.put("name", "브라운"); |
| 58 | + params.put("date", "2023-08-05"); |
| 59 | + params.put("time", "10:00"); |
| 60 | + |
| 61 | + RestAssured.given().log().all() |
| 62 | + .contentType(ContentType.JSON) |
| 63 | + .body(params) |
| 64 | + .when().post("/reservations") |
| 65 | + .then().log().all() |
| 66 | + .statusCode(201) |
| 67 | + .header("Location", "/reservations/1"); |
| 68 | + |
| 69 | + Integer count = jdbcTemplate.queryForObject("SELECT count(1) from reservation", Integer.class); |
| 70 | + assertThat(count).isEqualTo(1); |
| 71 | + |
| 72 | + RestAssured.given().log().all() |
| 73 | + .when().delete("/reservations/1") |
| 74 | + .then().log().all() |
| 75 | + .statusCode(204); |
| 76 | + |
| 77 | + Integer countAfterDelete = jdbcTemplate.queryForObject("SELECT count(1) from reservation", Integer.class); |
| 78 | + assertThat(countAfterDelete).isEqualTo(0); |
| 79 | + } |
| 80 | +} |
0 commit comments