Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEAT] 스포츠 전체 조회 기능 구현 #105

Merged
merged 13 commits into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from 9 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.sports.server.query.application;

import com.sports.server.query.dto.response.SportResponse;
import com.sports.server.query.repository.SportQueryRepository;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class SportQueryService {

private final SportQueryRepository sportQueryRepository;

public List<SportResponse> findAll() {
return sportQueryRepository.findAll().stream().map(SportResponse::new).toList();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.sports.server.query.dto.response;

import com.sports.server.command.sport.domain.Sport;

public record SportResponse(
Long id,
String name

) {
public SportResponse(Sport sport) {
this(
sport.getId(),
sport.getName()
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.sports.server.query.presentation;

import com.sports.server.query.application.SportQueryService;
import com.sports.server.query.dto.response.SportResponse;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequiredArgsConstructor
@RequestMapping("/sports" )
public class SportQueryController {

private final SportQueryService sportQueryService;

@GetMapping
public ResponseEntity<List<SportResponse>> findAll() {
return ResponseEntity.ok(sportQueryService.findAll());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.sports.server.query.repository;

import com.sports.server.command.sport.domain.Sport;
import java.util.List;
import org.springframework.data.repository.Repository;

public interface SportQueryRepository extends Repository<Sport, Long> {
List<Sport> findAll();
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class CheerTalkControllerTest extends DocumentationTest {
);

result.andExpect((status().isOk()))
.andDo(RESULT_HANDLER.document(
.andDo(restDocsHandler.document(
requestFields(
fieldWithPath("content").type(JsonFieldType.STRING).description("응원톡의 내용"),
fieldWithPath("gameTeamId").type(JsonFieldType.NUMBER).description("응원하는 게임팀")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class GameControllerTest extends DocumentationTest {

//then
result.andExpect((status().isOk()))
.andDo(RESULT_HANDLER.document(
.andDo(restDocsHandler.document(
pathParameters(
parameterWithName("gameId").description("게임의 ID")
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class ReportControllerTest extends DocumentationTest {

//then
result.andExpect((status().isNoContent()))
.andDo(RESULT_HANDLER.document(
.andDo(restDocsHandler.document(
requestFields(
fieldWithPath("cheerTalkId").type(JsonFieldType.NUMBER).description("신고하는 응원톡의 ID")
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.sports.server.query.acceptance;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertAll;

import com.sports.server.query.dto.response.SportResponse;
import com.sports.server.support.AcceptanceTest;
import io.restassured.RestAssured;
import io.restassured.response.ExtractableResponse;
import io.restassured.response.Response;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.test.context.jdbc.Sql;

@Sql("/game-fixture.sql")
public class SportQueryAcceptanceTest extends AcceptanceTest {

@Test
void 모든_종목을_조회한다() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

스포츠, 종목에 대한 코멘트는 여기다가 남길게요. 확실히 영어이름은 Sport인데 한글로는 스포츠 또는 종목으로 혼용하고 있네요. 축구, 농구 등을 생각했을 땐 스포츠가 적절해보이지만 루미큐브 같은 경우는 스포츠라 볼 수 있는지 애매하긴 하네요.

종목이 더 포괄적인 의미를 담고 있는 것 같긴한데 용어 통일을 해야한다면 현재로서는 스포츠가 더 적절한것 같긴해요.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

넵 확인했습니당
스포츠로 용어 통일하도록 할게요!


// when
ExtractableResponse<Response> response = RestAssured.given().log().all()
.when()
.contentType(MediaType.APPLICATION_JSON_VALUE)
.get("/sports")
.then().log().all()
.extract();

// then
List<SportResponse> actual = toResponses(response, SportResponse.class);
assertAll(
() -> assertThat(response.statusCode()).isEqualTo(HttpStatus.OK.value()),
() -> assertThat(actual)
.map(SportResponse::id)
.containsExactly(1L, 2L),
() -> assertThat(actual)
.map(SportResponse::name)
.containsExactly("농구", "루미큐브"));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public class CheerTalkQueryControllerTest extends DocumentationTest {
);

result.andExpect((status().isOk()))
.andDo(RESULT_HANDLER.document(
.andDo(restDocsHandler.document(
queryParameters(
parameterWithName("cursor").description("마지막 응원톡의 ID"),
parameterWithName("size").description("조회하고자 하는 응원톡의 개수")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class GameQueryControllerTest extends DocumentationTest {

// then
result.andExpect((status().isOk()))
.andDo(RESULT_HANDLER.document(
.andDo(restDocsHandler.document(
pathParameters(
parameterWithName("gameId").description("게임의 ID")
),
Expand Down Expand Up @@ -78,7 +78,7 @@ class GameQueryControllerTest extends DocumentationTest {

// then
result.andExpect((status().isOk()))
.andDo(RESULT_HANDLER.document(
.andDo(restDocsHandler.document(
pathParameters(
parameterWithName("gameId").description("게임의 ID")
),
Expand Down Expand Up @@ -121,7 +121,7 @@ class GameQueryControllerTest extends DocumentationTest {

// then
result.andExpect((status().isOk()))
.andDo(RESULT_HANDLER.document(
.andDo(restDocsHandler.document(
queryParameters(
parameterWithName("league_id").description("대회의 ID"),
parameterWithName("status").description("게임의 상태"),
Expand Down Expand Up @@ -161,7 +161,7 @@ class GameQueryControllerTest extends DocumentationTest {

// then
result.andExpect((status().isOk()))
.andDo(RESULT_HANDLER.document(
.andDo(restDocsHandler.document(
pathParameters(
parameterWithName("gameId").description("게임의 ID")
),
Expand Down Expand Up @@ -205,7 +205,7 @@ class GameQueryControllerTest extends DocumentationTest {

// then
result.andExpect((status().isOk()))
.andDo(RESULT_HANDLER.document(
.andDo(restDocsHandler.document(
pathParameters(
parameterWithName("gameId").description("게임의 ID")
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public class LeagueQueryControllerTest extends DocumentationTest {

// then
result.andExpect((status().isOk()))
.andDo(RESULT_HANDLER.document(
.andDo(restDocsHandler.document(
responseFields(
fieldWithPath("[].leagueId").type(JsonFieldType.NUMBER).description("리그의 ID"),
fieldWithPath("[].name").type(JsonFieldType.STRING).description("리그의 이름")
Expand Down Expand Up @@ -67,7 +67,7 @@ public class LeagueQueryControllerTest extends DocumentationTest {

// then
result.andExpect((status().isOk()))
.andDo(RESULT_HANDLER.document(
.andDo(restDocsHandler.document(
pathParameters(
parameterWithName("leagueId").description("리그의 ID")
),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.sports.server.query.presentation;

import static org.mockito.BDDMockito.given;
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.get;
import static org.springframework.restdocs.payload.PayloadDocumentation.fieldWithPath;
import static org.springframework.restdocs.payload.PayloadDocumentation.responseFields;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import com.sports.server.query.dto.response.SportResponse;
import com.sports.server.support.DocumentationTest;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.springframework.http.MediaType;
import org.springframework.restdocs.payload.JsonFieldType;
import org.springframework.test.web.servlet.ResultActions;

public class SportQueryControllerTest extends DocumentationTest {

@Test
void 종목을_전체_조회한다() throws Exception {

// given
List<SportResponse> responses = List.of(
new SportResponse(1L, "농구"),
new SportResponse(2L, "루미큐브")
);

given(sportQueryService.findAll())
.willReturn(responses);

// when
ResultActions result = mockMvc.perform(get("/sports")
.contentType(MediaType.APPLICATION_JSON)
);

// then
result.andExpect((status().isOk()))
.andDo(restDocsHandler.document(
responseFields(
fieldWithPath("[].id").type(JsonFieldType.NUMBER).description("종목의 ID"),
fieldWithPath("[].name").type(JsonFieldType.STRING).description("종목의 이름")
)
));

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public class TimelineQueryControllerTest extends DocumentationTest {

// then
result.andExpect((status().isOk()))
.andDo(RESULT_HANDLER.document(
.andDo(restDocsHandler.document(
pathParameters(
parameterWithName("gameId").description("게임의 ID")
),
Expand Down
41 changes: 11 additions & 30 deletions src/test/java/com/sports/server/support/DocumentationTest.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
package com.sports.server.support;

import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document;
import static org.springframework.restdocs.operation.preprocess.Preprocessors.modifyUris;
import static org.springframework.restdocs.operation.preprocess.Preprocessors.preprocessRequest;
import static org.springframework.restdocs.operation.preprocess.Preprocessors.preprocessResponse;
import static org.springframework.restdocs.operation.preprocess.Preprocessors.prettyPrint;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.sports.server.command.cheertalk.application.CheerTalkService;
import com.sports.server.command.cheertalk.presentation.CheerTalkController;
Expand All @@ -14,23 +8,14 @@
import com.sports.server.command.report.application.ReportService;
import com.sports.server.command.report.presentation.ReportController;
import com.sports.server.common.log.TimeLogTemplate;
import com.sports.server.query.application.CheerTalkQueryService;
import com.sports.server.query.application.GameQueryService;
import com.sports.server.query.application.GameTeamQueryService;
import com.sports.server.query.application.LeagueQueryService;
import com.sports.server.query.application.LineupPlayerQueryService;
import com.sports.server.query.application.TimelineQueryService;
import com.sports.server.query.presentation.CheerTalkQueryController;
import com.sports.server.query.presentation.GameQueryController;
import com.sports.server.query.presentation.LeagueQueryController;
import com.sports.server.query.presentation.TimelineQueryController;
import com.sports.server.query.application.*;
import com.sports.server.query.presentation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.restdocs.AutoConfigureRestDocs;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.Import;
import org.springframework.restdocs.mockmvc.RestDocumentationResultHandler;
import org.springframework.restdocs.operation.preprocess.OperationRequestPreprocessor;
import org.springframework.test.web.servlet.MockMvc;

@WebMvcTest(controllers = {
Expand All @@ -40,25 +25,18 @@
CheerTalkQueryController.class,
GameQueryController.class,
LeagueQueryController.class,
TimelineQueryController.class
TimelineQueryController.class,
SportQueryController.class
})
@Import({
TimeLogTemplate.class
TimeLogTemplate.class,
RestDocsConfig.class
})
@AutoConfigureRestDocs
public class DocumentationTest {

private static final OperationRequestPreprocessor HOST_INFO = preprocessRequest(modifyUris()
.scheme("https")
.host("www.api.hufstreaming.site")
.removePort(), prettyPrint()
);

protected static final RestDocumentationResultHandler RESULT_HANDLER = document(
"{class-name}/{method-name}",
HOST_INFO,
preprocessResponse(prettyPrint())
);
@Autowired
protected RestDocumentationResultHandler restDocsHandler;

@Autowired
protected MockMvc mockMvc;
Expand Down Expand Up @@ -92,4 +70,7 @@ public class DocumentationTest {

@MockBean
protected TimelineQueryService timelineQueryService;

@MockBean
protected SportQueryService sportQueryService;
}
28 changes: 28 additions & 0 deletions src/test/java/com/sports/server/support/RestDocsConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.sports.server.support;

import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.restdocs.mockmvc.MockMvcRestDocumentation;
import org.springframework.restdocs.mockmvc.RestDocumentationResultHandler;
import org.springframework.restdocs.operation.preprocess.OperationRequestPreprocessor;

import static org.springframework.restdocs.operation.preprocess.Preprocessors.*;

@TestConfiguration
public class RestDocsConfig {

private static final OperationRequestPreprocessor HOST_INFO = preprocessRequest(modifyUris()
.scheme("https" )
.host("www.api.hufstreaming.site" )
.removePort(), prettyPrint()
);

@Bean
public RestDocumentationResultHandler restDocsMockMvcConfigurationCustomizer() {
return MockMvcRestDocumentation.document(
"{class-name}/{method-name}",
HOST_INFO,
preprocessResponse(prettyPrint())
);
}
}
Comment on lines +11 to +28
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

위처럼 RestDocumentationResultHandler를 빈으로 등록하고 사용할 곳에서 주입 받아서 사용하면 문서들이 생겨나네요. 저도 정확한 오류 원인은 잘 모르겠지만 빈으로 등록해야 완전하게 동작하나봅니다

    @Autowired
    protected RestDocumentationResultHandler restDocsHandler;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

그때는 되고 지금은 안된다니 . . 당혹스럽네요
해결 감사합니닷 👍🏻🫡

Loading