Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@ repositories {

dependencies {
implementation 'org.springframework.boot:spring-boot-starter'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'io.rest-assured:rest-assured:5.3.1'

}

test {
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/roomescape/RoomescapeController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package roomescape;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class RoomescapeController {
@GetMapping("/")
public String home() {
return "home";
}
}
68 changes: 68 additions & 0 deletions src/main/java/roomescape/controller/ReservationController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// ReservationController.java
package roomescape.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicLong;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import roomescape.model.Reservation;
import roomescape.exception.BadRequestReservationException;

@RestController // RestController로 변경
@RequestMapping("/reservations")
public class ReservationController {
private List<Reservation> reservations = new ArrayList<>();
private AtomicLong index = new AtomicLong(0);

public void reset() {
reservations.clear();
index.set(0);
}

@GetMapping
public List<Reservation> getReservation() {
return reservations;
}

@PostMapping
public ResponseEntity<Reservation> addReservation(@RequestBody Reservation newReservation) {
// 예외 조건 추가
if (newReservation.getName() == null || newReservation.getName().isBlank() ||
newReservation.getDate() == null || newReservation.getDate().isBlank() ||
newReservation.getTime() == null || newReservation.getTime().isBlank()) {
throw new BadRequestReservationException("Required fields are missing.");
}

long id = index.incrementAndGet();
Reservation reservation = new Reservation(
id,
newReservation.getName(),
newReservation.getDate(),
newReservation.getTime()
);
reservations.add(reservation);
return ResponseEntity.status(HttpStatus.CREATED)
.header("Location", "/reservations/" + id)
.body(reservation);
}

@DeleteMapping("/{id}")
public ResponseEntity<Void> deleteReservation(@PathVariable Long id) {
boolean removed = reservations.removeIf(r -> r.getId().equals(id));
if (removed) {
return ResponseEntity.noContent().build();
} else {
throw new BadRequestReservationException("Reservation not found.");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package roomescape.exception;

public class BadRequestReservationException extends RuntimeException {
public BadRequestReservationException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package roomescape.exception;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class ReservationExceptionHandler {
@ExceptionHandler(BadRequestReservationException.class)
public ResponseEntity<Void> handleBadRequest(BadRequestReservationException e) {
return ResponseEntity.badRequest().build();
}
}
31 changes: 31 additions & 0 deletions src/main/java/roomescape/model/Reservation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package roomescape.model;

public class Reservation {
private Long id;
private String name;
private String date;
private String time;

public Reservation(Long id, String name, String date, String time) {
this.id = id;
this.name = name;
this.date = date;
this.time = time;
}

public Long getId() {
return id;
}

public String getName() {
return name;
}

public String getDate() {
return date;
}

public String getTime() {
return time;
}
}
78 changes: 77 additions & 1 deletion src/test/java/roomescape/MissionStepTest.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
package roomescape;

import io.restassured.RestAssured;
import java.util.HashMap;
import java.util.Map;

import static org.hamcrest.Matchers.is;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.annotation.DirtiesContext;

import io.restassured.RestAssured;
import io.restassured.http.ContentType;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
@DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_EACH_TEST_METHOD)
public class MissionStepTest {
Expand All @@ -16,4 +22,74 @@ public class MissionStepTest {
.then().log().all()
.statusCode(200);
}

@Test
void 이단계() {
RestAssured.given().log().all()
.when().get("/reservation")
.then().log().all()
.statusCode(200);

RestAssured.given().log().all()
.when().get("/reservations")
.then().log().all()
.statusCode(200)
.body("size()", is(2)); // 아직 생성 요청이 없으니 Controller에서 임의로 넣어준 Reservation 갯수 만큼 검증하거나 0개임을 확인하세요.
}

@Test
void 삼단계() {
Map<String, String> params = new HashMap<>();
params.put("name", "브라운");
params.put("date", "2023-08-05");
params.put("time", "15:40");

RestAssured.given().log().all()
.contentType(ContentType.JSON)
.body(params)
.when().post("/reservations")
.then().log().all()
.statusCode(201)
.header("Location", "/reservations/1")
.body("id", is(1));

RestAssured.given().log().all()
.when().get("/reservations")
.then().log().all()
.statusCode(200)
.body("size()", is(1));

RestAssured.given().log().all()
.when().delete("/reservations/1")
.then().log().all()
.statusCode(204);

RestAssured.given().log().all()
.when().get("/reservations")
.then().log().all()
.statusCode(200)
.body("size()", is(0));
}

@Test
void 사단계() {
Map<String, String> params = new HashMap<>();
params.put("name", "브라운");
params.put("date", "");
params.put("time", "");

// 필요한 인자가 없는 경우
RestAssured.given().log().all()
.contentType(ContentType.JSON)
.body(params)
.when().post("/reservations")
.then().log().all()
.statusCode(400);

// 삭제할 예약이 없는 경우
RestAssured.given().log().all()
.when().delete("/reservations/1")
.then().log().all()
.statusCode(400);
}
}