개발강의정리/Spring

[스프링 기반 REST API 개발] 4-2. Event 조회 API 구현

nineDeveloper 2020. 3. 29.
728x90

스프링 기반 REST API 개발

4. 이벤트 목록 조회 및 수정 REST API 개발

포스팅 참조 정보

GitHub

공부한 내용은 GitHub에 공부용 Organizations에 정리 하고 있습니다

해당 포스팅에 대한 내용의 GitHub 주소

실습 내용이나 자세한 소스코드는 GitHub에 있습니다
포스팅 내용은 간략하게 추린 핵심 내용만 포스팅되어 있습니다

https://github.com/freespringlecture/spring-rest-api-study/tree/chap04-02_event_get

해당 포스팅 참고 인프런 강의

https://www.inflearn.com/course/spring_rest-api/dashboard

실습 환경

  • Java Version: Java 11
  • SpringBoot Version: 2.1.2.RELEASE

2. Event 조회 API 구현

EventController 에 조회 이벤트 구현

@GetMapping("/{id}")
public ResponseEntity getEvent(@PathVariable Integer id) {
    Optional<Event> optionalEvent = this.eventRepository.findById(id);
    if (optionalEvent.isEmpty()) {
        return ResponseEntity.notFound().build();
    }

    Event event = optionalEvent.get();
    EventResource eventResource = new EventResource(event);
    eventResource.add(new Link("/docs/index.html#resources-events-get").withRel("profile"));
    return ResponseEntity.ok(eventResource);
}

EventControllerTests 에 조회 이벤트 테스트 코드 구현


...

// queryEvents 테스트코드 ResultActions 에 담아서 처리하도록 변경
@Test
@TestDescription("30개의 이벤트를 10개씩 두번째 페이지 조회하기")
public void queryEvents() throws Exception {
    //Given
    IntStream.range(0, 30).forEach(this::generateEvent);

    // When & Then
    ResultActions perform = this.mockMvc.perform(get("/api/events")
            .param("page", "1")
            .param("size", "10")
            .param("sort", "name,DESC")
    );

    // Then
    perform.andDo(print())
            .andExpect(status().isOk())
            .andExpect(jsonPath("page").exists())
            .andExpect(jsonPath("_embedded.eventList[0]._links.self").exists())
            .andExpect(jsonPath("_links.self").exists())
            .andExpect(jsonPath("_links.profile").exists())
            .andDo(document("query-events"))
    ;
}

...

@Test
@TestDescription("기존의 이벤트를 하나 조회하기")
public void getEvent() throws Exception {
    // Given
    Event event = this.generateEvent(100);

    // When & Then
    this.mockMvc.perform((get("/api/events/{id}", event.getId())))
            .andExpect(status().isOk())
            .andExpect(jsonPath("name").exists())
            .andExpect(jsonPath("id").exists())
            .andExpect(jsonPath("_links.self").exists())
            .andExpect(jsonPath("_links.profile").exists())
            .andDo(document("get-an-event"))
    ;
}

@Test
@TestDescription("없는 이벤트는 조회했을 때 404 응답받기")
public void getEvent404() throws Exception {
    // When & Then
    this.mockMvc.perform(get("/api/events/11883"))
            .andExpect(status().isNotFound());
}

private Event generateEvent(int index) {
    Event event = Event.builder()
            .name("event " + index)
            .description("test event")
            .build();

    return this.eventRepository.save(event);
}

테스트 할 것

조회하는 이벤트가 있는 경우 이벤트 리소스 확인

  • 링크
    • self
    • profile
    • (update)
  • 이벤트 데이터

조회하는 이벤트가 없는 경우 404 응답 확인

728x90

댓글

💲 추천 글