Skip to content

Commit

Permalink
feat: 공연장 dto, service validation 추가 (#28)
Browse files Browse the repository at this point in the history
* feat : add validation in EventHallCreate, Update Dtos

* feat: #25 공연장 생성 테스트 추가

* feat: #25 add 공연장 삭제, 수정, 목록 조회 성공 테스트

* fix : 테스트 코드 수정

---------

Co-authored-by: zerozae <[email protected]>
  • Loading branch information
KarmaPol and park0jae authored Dec 26, 2023
1 parent 18242ad commit f1d7c8c
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.pgms.apievent.eventHall.dto.response.EventHallResponse;
import com.pgms.apievent.eventHall.service.EventHallService;
import com.pgms.coredomain.response.ApiResponse;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
Expand All @@ -21,7 +22,7 @@ public class EventHallController {
private final EventHallService eventHallService;

@PostMapping
public ResponseEntity<ApiResponse> createEventHall(@RequestBody EventHallCreateRequest request){
public ResponseEntity<ApiResponse> createEventHall(@RequestBody @Valid EventHallCreateRequest request){
EventHallResponse eventHallResponse = eventHallService.createEventHall(request);

URI location = ServletUriComponentsBuilder.fromCurrentRequest()
Expand All @@ -40,7 +41,7 @@ public ResponseEntity<Void> deleteEventHall(@PathVariable Long id){
@PutMapping("/{id}")
public ResponseEntity<ApiResponse> updateEventHall(
@PathVariable Long id,
@RequestBody EventHallUpdateRequest request) {
@RequestBody @Valid EventHallUpdateRequest request) {
EventHallResponse response = eventHallService.updateEventHall(id, request);
return ResponseEntity.ok(ApiResponse.ok(response));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package com.pgms.apievent.eventHall.dto.request;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Size;

import java.util.List;

public record EventHallCreateRequest(String name,
public record EventHallCreateRequest(@NotBlank(message = "공연장 이름은 빈칸을 지정할 수 없습니다.")
@Size(max = 25, message = "공연장 이름은 10자 이내로 입력해주세요.") String name,
String address,
List<EventHallSeatCreateRequest> eventHallSeatCreateRequests) {
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
package com.pgms.apievent.eventHall.dto.request;

public record EventHallSeatCreateRequest(String name) {
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Size;

public record EventHallSeatCreateRequest(@NotBlank(message = "공연장 좌석은 빈칸을 지정할 수 없습니다.")
@Size(max = 10, message = "공연장 좌석은 10자 이내로 입력해주세요.") String name) {
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
package com.pgms.apievent.eventHall.dto.request;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Size;

import java.util.List;

public record EventHallUpdateRequest(String name, String address, List<EventHallSeatCreateRequest> eventHallSeatCreateRequests) {
public record EventHallUpdateRequest(@NotBlank(message = "공연장 이름은 빈칸을 지정할 수 없습니다.")
@Size(max = 25, message = "공연장 이름은 10자 이내로 입력해주세요.") String name,
String address,
List<EventHallSeatCreateRequest> eventHallSeatCreateRequests) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package com.pgms.apievent.eventHall.service;

import com.pgms.apievent.eventHall.dto.request.EventHallCreateRequest;
import com.pgms.apievent.eventHall.dto.request.EventHallSeatCreateRequest;
import com.pgms.apievent.eventHall.dto.request.EventHallUpdateRequest;
import com.pgms.apievent.eventHall.dto.response.EventHallResponse;
import com.pgms.coredomain.domain.event.EventHall;
import com.pgms.coredomain.domain.event.EventHallSeat;
import com.pgms.coredomain.domain.event.repository.EventHallRepository;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.IntStream;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;

@SpringBootTest
@Transactional
class EventHallServiceTest {

@Autowired
private EventHallRepository eventHallRepository;
@Autowired
private EventHallService eventHallService;

@BeforeEach
void clean(){
eventHallRepository.deleteAll();
}

@Test
public void 공연장_생성_성공(){
// given
List<EventHallSeatCreateRequest> eventHallSeatCreateRequests = IntStream.range(0, 10)
.mapToObj(i -> new EventHallSeatCreateRequest("T" + String.valueOf(i)))
.toList();

EventHallCreateRequest eventHallCreateRequest = new EventHallCreateRequest("test", "test", eventHallSeatCreateRequests);

// when
EventHallResponse eventHallResponse = eventHallService.createEventHall(eventHallCreateRequest);

// then
assertThat(eventHallResponse.name(), is("test"));
assertThat(eventHallResponse.eventHallSeatResponses().size(), is(10));
}

@Test
public void 공연장_삭제_성공(){
// given
List<EventHallSeat> eventHallSeats = IntStream.range(0, 10)
.mapToObj(i -> new EventHallSeat("T" + String.valueOf(i)))
.toList();

EventHall eventHall = EventHall.builder()
.name("test")
.address("test")
.eventHallSeats(eventHallSeats)
.build();

EventHall savedEventHall = eventHallRepository.save(eventHall);

// when
eventHallRepository.delete(savedEventHall);

// then
assertThat(eventHallRepository.count(), is(0L));
}

@Test
public void 공연장_수정_성공(){
// given
List<EventHallSeat> eventHallSeats = IntStream.range(0, 10)
.mapToObj(i -> new EventHallSeat("T" + String.valueOf(i)))
.toList();

EventHall eventHall = EventHall.builder()
.name("test")
.address("test")
.eventHallSeats(eventHallSeats)
.build();

EventHall savedEventHall = eventHallRepository.save(eventHall);

List<EventHallSeatCreateRequest> eventHallSeatCreateRequests = IntStream.range(0, 10)
.mapToObj(i -> new EventHallSeatCreateRequest("T" + String.valueOf(i)))
.toList();

EventHallUpdateRequest eventHallUpdateRequest = new EventHallUpdateRequest("update", "test", eventHallSeatCreateRequests);

// when
EventHallResponse eventHallResponse = eventHallService.updateEventHall(savedEventHall.getId(), eventHallUpdateRequest);

// then
assertThat(eventHallResponse.name(), is("update"));
assertThat(savedEventHall.getEventHallSeats().size(), is(10));
}

@Test
public void 공연장_목록_조회_성공(){
// given

List<EventHallSeat> eventHallSeats = IntStream.range(0, 10)
.mapToObj(i -> new EventHallSeat("T" + String.valueOf(i)))
.toList();

List<EventHall> eventHalls = IntStream.range(0, 10)
.mapToObj(i -> {
return EventHall.builder()
.name("test")
.address("test")
.eventHallSeats(eventHallSeats)
.build();
})
.toList();

// when
List<EventHall> savedEventHalls = eventHallRepository.saveAll(eventHalls);

// then
assertThat(savedEventHalls.size(), is(10));
}
}
4 changes: 3 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@ subprojects {
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-web'
compileOnly 'org.projectlombok:lombok'
implementation 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
testAnnotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

Expand Down

0 comments on commit f1d7c8c

Please sign in to comment.