[사이드프로젝트] 그저 그런 REST API로 괜찮은가? - 진정한 REST API 구현해보기 - Event & Event DTO

Event Model Test 구현


테스트 할 케이스는 다음과 같다.

  • Build가 제대로 이뤄지는가
  • Build가 됐을 때, 값이 제대로 들어가는가
  • DTO에서 Model로 변환됐을 때 제대로 만들어지는가
package com.carrykim.restapi.event;

import com.carrykim.restapi.event.model.Event;
import com.carrykim.restapi.event.model.dto.EventDto;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

class EventTest {
    @Test
    public void build(){
        Event event = Event.builder().build();
        assertNotNull(event);
    }

    @Test
    public void javaBean(){
        //Given
        String eventName = "My Event";
        String eventDescription = "This is my event";

        //When
        Event event = Event.builder()
                .name(eventName)
                .description(eventDescription)
                .build();

        //Then
        assertEquals(eventName, event.getName());
        assertEquals(eventDescription, event.getDescription());
    }

    @Test
    public void fromDto(){
        //Given
        String eventName = "My Event";
        String eventDescription = "This is my event";

        Event event = Event.builder()
                .name(eventName)
                .description(eventDescription)
                .build();

        EventDto eventDto = EventDto.builder()
                .name(eventName)
                .description(eventDescription)
                .build();

        //When
        Event fromDto = eventDto.toModel();

        //Then
        assertEquals(event.getName(), fromDto.getName());
        assertEquals(event.getDescription(), fromDto.getDescription());
    }
}

Event Model 구현


package com.carrykim.restapi.event.model;

import lombok.*;

import javax.persistence.*;

@Getter @Setter
@EqualsAndHashCode(of = "id") @Builder
@NoArgsConstructor @AllArgsConstructor
@Entity
public class Event {
    @Id @GeneratedValue
    private Integer id;
    private String name;
    private String description;
}

Event DTO 구현


package com.carrykim.restapi.event.model.dto;

import com.carrykim.restapi.event.model.Event;
import lombok.*;

@Getter @Setter @Builder
@NoArgsConstructor @AllArgsConstructor
public class EventDto {
    private String name;
    private String description;

    public Event toModel(){
        return Event.builder()
                .name(this.name)
                .description(this.description)
                .build();
    }
}

좋은 웹페이지 즐겨찾기