Event 생성 API 구현 : 입력값 제한하기

2024. 8. 22. 17:29Spring기반 REST API개발

 

EventRepository

package com.example.inflearnrestapi.events;


import org.springframework.data.jpa.repository.JpaRepository;

public interface EventRepository extends JpaRepository<Event, Integer> {

}

 

Event

package com.example.inflearnrestapi.events;

import jakarta.persistence.*;
import lombok.*;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDateTime;

@Builder @AllArgsConstructor @NoArgsConstructor
@Getter @Setter @EqualsAndHashCode(of = "id")
@Entity
public class Event {

    @Id @GeneratedValue
    private Integer id;
    private String name;
    private String description;
    private LocalDateTime beginEnrollmentDateTime;
    private LocalDateTime closeEnrollmentDateTime;
    private LocalDateTime beginEventDateTime;
    private LocalDateTime endEventDateTime;
    private String location; // (optional) 이게 없으면 온라인 모임
    private int basePrice; // (optional)
    private int maxPrice; // (optional)
    private int limitOfEnrollment;
    private boolean offline;
    private boolean free;
    @Enumerated(EnumType.STRING)
    private EventStatus eventStatus;

}

 

 

 

EventControllerTest

package com.example.inflearnrestapi.events;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.hateoas.MediaTypes;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;

import java.time.LocalDateTime;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

@SpringBootTest
@AutoConfigureMockMvc
class EventControllerTest {

    @Autowired
    MockMvc mockMvc;
    @Autowired
    ObjectMapper objectMapper;
    @MockBean
    EventRepository eventRepository;

    @Test
    public void createEvent() throws Exception {
        Event event = Event.builder()
                .name("Spring")
                .description("REST API Development with Spring")
                .beginEnrollmentDateTime(LocalDateTime.of(2024,8,21,13,50))
                .closeEnrollmentDateTime(LocalDateTime.of(2024,8,22,13,50))
                .beginEventDateTime(LocalDateTime.of(2024,8,23,13,50))
                .endEventDateTime(LocalDateTime.of(2024,8,24,13,50))
                .basePrice(100)
                .maxPrice(200)
                .limitOfEnrollment(200)
                .location("강남역 D2 스사텁 팩토리")
                .free(true)
                .offline(false)
                .build();



        mockMvc.perform(post("/api/events")
                .contentType(MediaType.APPLICATION_JSON)
                .accept(MediaTypes.HAL_JSON)
                .content(objectMapper.writeValueAsString(event)))  //perform 안에 주는게 요청
                .andDo(print())
                .andExpect(status().isCreated())
                .andExpect(jsonPath("id").exists())
                .andExpect(header().exists(HttpHeaders.LOCATION))
                .andExpect(header().string(HttpHeaders.CONTENT_TYPE,MediaTypes.HAL_JSON_VALUE))
                .andExpect(jsonPath("id").value(Matchers.not(100)))
                .andExpect(jsonPath("free").value(Matchers.not(true)));

    }
    @Test
    public void bad_RequestcreateEvent() throws Exception {
        Event event = Event.builder()
                .name("Spring")
                .description("REST API Development with Spring")
                .beginEnrollmentDateTime(LocalDateTime.of(2024,8,21,13,50))
                .closeEnrollmentDateTime(LocalDateTime.of(2024,8,22,13,50))
                .beginEventDateTime(LocalDateTime.of(2024,8,23,13,50))
                .endEventDateTime(LocalDateTime.of(2024,8,24,13,50))
                .basePrice(100)
                .maxPrice(200)
                .limitOfEnrollment(200)
                .location("강남역 D2 스사텁 팩토리")
                .free(true)
                .offline(false)
                .build();



        mockMvc.perform(post("/api/events")
                        .contentType(MediaType.APPLICATION_JSON)
                        .accept(MediaTypes.HAL_JSON)
                        .content(objectMapper.writeValueAsString(event)))  //perform 안에 주는게 요청
                .andDo(print())
                .andExpect(status().isBadRequest());

    }



}

 

application.properties

spring.jackson.deserialization.fail-on-unknown-properties=true

'Spring기반 REST API개발' 카테고리의 다른 글

REST API - HATEOAS  (0) 2024.08.24
REST API - 201 응답 받기  (0) 2024.08.21
REST API - 테스트 만들기  (0) 2024.08.21
Event 생성 API 구현 - 비즈니스 로직  (0) 2024.08.20
Event 도메인 구현  (0) 2024.08.20