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

[BE/Test] 운영 서버에 반영 #415

Merged
merged 14 commits into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
Expand Up @@ -6,13 +6,14 @@

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.gaebaljip.exceed.adapter.in.member.request.UpdateWeightRequest;
import com.gaebaljip.exceed.adapter.in.member.response.UpdateWeightResponse;
import com.gaebaljip.exceed.application.port.in.member.UpdateWeightCommand;
import com.gaebaljip.exceed.application.service.member.UpdateWeightService;
import com.gaebaljip.exceed.application.port.in.member.UpdateWeightUsecase;
import com.gaebaljip.exceed.common.ApiResponse;
import com.gaebaljip.exceed.common.ApiResponseGenerator;
import com.gaebaljip.exceed.common.annotation.AuthenticationMemberId;
Expand All @@ -30,16 +31,16 @@
@SecurityRequirement(name = "access-token")
@Tag(name = "[몸무게 수정]")
public class UpdateWeightController {
private final UpdateWeightService updateWeightService;
private final UpdateWeightUsecase updateWeightUsecase;

@Operation(summary = "회원 몸무게 및 목표 몸무게 수정", description = "회원 몸무게 및 목표 몸무게를 수정한다.")
@PatchMapping("/members/weight")
public ApiResponse<UpdateWeightResponse> updateWeight(
HttpServletRequest servletRequest,
UpdateWeightRequest request,
@RequestBody UpdateWeightRequest request,
@AuthenticationMemberId Long memberId) {
UpdateWeightResponse response =
updateWeightService.execute(
updateWeightUsecase.execute(
UpdateWeightCommand.of(request.weight(), request.targetWeight(), memberId));
Events.raise(
UpdateWeightEvent.from(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
import com.gaebaljip.exceed.common.ValidationMessage;
import com.gaebaljip.exceed.common.annotation.Enum;

import lombok.Builder;

@Builder
public record UpdateMemberRequest(
@NotNull(message = "키를 " + ValidationMessage.NOT_NULL)
@Min(value = 0, message = "키는 " + ValidationMessage.MIN_0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@
@Slf4j
public class LoggingFilter extends OncePerRequestFilter {
private final List<String> excludeUrl =
List.of("/actuator/health", "/actuator/prometheus", "/v1/health");
List.of(
"/actuator/health",
"/actuator/prometheus",
"/v1/health",
"/api-docs/swagger-config",
"/api-docs");

@Override
protected void doFilterInternal(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,240 @@
package com.gaebaljip.exceed.adapter.in.member;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.ResultActions;

import com.gaebaljip.exceed.adapter.in.member.request.UpdateMemberRequest;
import com.gaebaljip.exceed.application.port.in.member.UpdateMemberUsecase;
import com.gaebaljip.exceed.common.ControllerTest;
import com.gaebaljip.exceed.common.ValidationMessage;
import com.gaebaljip.exceed.common.WithMockUser;

@WebMvcTest(UpdateMemberController.class)
public class UpdateMemberControllerTest extends ControllerTest {
@MockBean private UpdateMemberUsecase updateMemberUsecase;

@Test
@DisplayName("회원 수정 성공")
@WithMockUser
void when_updateMember_expected_success() throws Exception {
// given
UpdateMemberRequest updateMemberRequest =
UpdateMemberRequest.builder()
.height(180.3)
.activity("VERY_ACTIVE")
.age(40)
.gender("MALE")
.etc("회원 수정")
.build();
// when
ResultActions resultActions =
mockMvc.perform(
put("/v1/members")
.content(om.writeValueAsString(updateMemberRequest))
.contentType(MediaType.APPLICATION_JSON));

// then
resultActions.andExpect(status().isOk());
}

@Test
@DisplayName("회원 수정 실패 - 키를 입력하지 않은 경우")
@WithMockUser
void when_updateMember_height_null_expected_exception() throws Exception {
// given
UpdateMemberRequest updateMemberRequest =
UpdateMemberRequest.builder()
.height(null)
.activity("VERY_ACTIVE")
.age(40)
.gender("MALE")
.etc("회원 수정")
.build();

// when
ResultActions resultActions =
mockMvc.perform(
put("/v1/members")
.content(om.writeValueAsString(updateMemberRequest))
.contentType(MediaType.APPLICATION_JSON));

// then
resultActions.andExpectAll(
status().isBadRequest(),
jsonPath("$.error.reason").value("키를 " + ValidationMessage.NOT_NULL));
}

@Test
@DisplayName("회원 수정 실패 - 키가 0보다 작은 경우")
@WithMockUser
void when_updateMember_height_negative_expected_exception() throws Exception {
// given
UpdateMemberRequest updateMemberRequest =
UpdateMemberRequest.builder()
.height(-1.0)
.activity("VERY_ACTIVE")
.age(40)
.gender("MALE")
.etc("회원 수정")
.build();

// when
ResultActions resultActions =
mockMvc.perform(
put("/v1/members")
.content(om.writeValueAsString(updateMemberRequest))
.contentType(MediaType.APPLICATION_JSON));

// then
resultActions.andExpectAll(
status().isBadRequest(),
jsonPath("$.error.reason").value("키는 " + ValidationMessage.MIN_0));
}

@Test
@DisplayName("회원 수정 실패 - 나이를 입력하지 않은 경우")
@WithMockUser
void when_updateMember_age_null_expected_exception() throws Exception {
// given
UpdateMemberRequest updateMemberRequest =
UpdateMemberRequest.builder()
.height(180.3)
.activity("VERY_ACTIVE")
.age(null)
.gender("MALE")
.etc("회원 수정")
.build();

// when
ResultActions resultActions =
mockMvc.perform(
put("/v1/members")
.content(om.writeValueAsString(updateMemberRequest))
.contentType(MediaType.APPLICATION_JSON));

// then
resultActions.andExpectAll(
status().isBadRequest(),
jsonPath("$.error.reason").value("나이를 " + ValidationMessage.NOT_NULL));
}

@Test
@DisplayName("회원 수정 실패 - 나이가 0보다 작은 경우")
@WithMockUser
void when_updateMember_age_negative_expected_exception() throws Exception {
// given
UpdateMemberRequest updateMemberRequest =
UpdateMemberRequest.builder()
.height(180.3)
.activity("VERY_ACTIVE")
.age(-1)
.gender("MALE")
.etc("회원 수정")
.build();

// when
ResultActions resultActions =
mockMvc.perform(
put("/v1/members")
.content(om.writeValueAsString(updateMemberRequest))
.contentType(MediaType.APPLICATION_JSON));

// then
resultActions.andExpectAll(
status().isBadRequest(),
jsonPath("$.error.reason").value("나이는 " + ValidationMessage.MIN_0));
}

@Test
@DisplayName("회원 수정 실패 - 성별이 올바르지 않은 경우")
@WithMockUser
void when_updateMember_gender_invalid_expected_exception() throws Exception {
// given
String gender = "MALLY";

UpdateMemberRequest updateMemberRequest =
UpdateMemberRequest.builder()
.height(180.3)
.activity("VERY_ACTIVE")
.age(40)
.gender(gender)
.etc("회원 수정")
.build();

// when
ResultActions resultActions =
mockMvc.perform(
put("/v1/members")
.content(om.writeValueAsString(updateMemberRequest))
.contentType(MediaType.APPLICATION_JSON));

// then
resultActions.andExpectAll(
status().isBadRequest(),
jsonPath("$.error.reason").value(gender + "는 올바르지 않은 값입니다."));
}

@Test
@DisplayName("회원 수정 실패 - 활동량이 올바르지 않은 경우")
@WithMockUser
void when_updateMember_activity_invalid_expected_exception() throws Exception {
// given
String activity = "INVALID";

UpdateMemberRequest updateMemberRequest =
UpdateMemberRequest.builder()
.height(180.3)
.activity(activity)
.age(40)
.gender("MALE")
.etc("회원 수정")
.build();

// when
ResultActions resultActions =
mockMvc.perform(
put("/v1/members")
.content(om.writeValueAsString(updateMemberRequest))
.contentType(MediaType.APPLICATION_JSON));

// then
resultActions.andExpectAll(
status().isBadRequest(),
jsonPath("$.error.reason").value(activity + "는 올바르지 않은 값입니다."));
}

@Test
@DisplayName("회원 수정 실패 - 기타사항을 입력하지 않은 경우")
@WithMockUser
void when_updateMember_etc_null_expected_exception() throws Exception {
// given
UpdateMemberRequest updateMemberRequest =
UpdateMemberRequest.builder()
.height(180.3)
.activity("VERY_ACTIVE")
.age(40)
.gender("MALE")
.etc(null)
.build();

// when
ResultActions resultActions =
mockMvc.perform(
put("/v1/members")
.content(om.writeValueAsString(updateMemberRequest))
.contentType(MediaType.APPLICATION_JSON));

// then
resultActions.andExpectAll(
status().isBadRequest(),
jsonPath("$.error.reason").value("기타사항을 " + ValidationMessage.NOT_BLANK));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.gaebaljip.exceed.adapter.in.member;

import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.http.MediaType;
import org.springframework.test.context.event.ApplicationEvents;
import org.springframework.test.context.event.RecordApplicationEvents;
import org.springframework.test.web.servlet.ResultActions;

import com.gaebaljip.exceed.adapter.in.member.request.UpdateWeightRequest;
import com.gaebaljip.exceed.application.port.in.member.UpdateWeightUsecase;
import com.gaebaljip.exceed.common.ControllerTest;
import com.gaebaljip.exceed.common.WithMockUser;
import com.gaebaljip.exceed.common.event.Events;
import com.gaebaljip.exceed.common.event.UpdateWeightEvent;

@RecordApplicationEvents
@WebMvcTest(UpdateWeightController.class)
public class UpdateWeightControllerTest extends ControllerTest {

@MockBean UpdateWeightUsecase updateWeightUsecase;
@Autowired ApplicationEvents events;
@Autowired ApplicationEventPublisher applicationEventPublisher;

@BeforeEach
void setUp() {
Events.setPublisher(applicationEventPublisher);
}

@AfterEach
void tearDown() {
Events.reset();
}

@Test
@WithMockUser(memberId = 1L)
@DisplayName("몸무게 수정 성공")
void when_updateWeight_expected_success() throws Exception {
// given
UpdateWeightRequest updateWeightRequest =
UpdateWeightRequest.builder().weight(50.0).targetWeight(70.5).build();

// when
ResultActions resultActions =
mockMvc.perform(
patch("/v1/members/weight")
.content(om.writeValueAsString(updateWeightRequest))
.contentType(MediaType.APPLICATION_JSON));

long count = events.stream(UpdateWeightEvent.class).count();

// then
resultActions.andExpectAll(status().isOk());
assertAll(() -> assertEquals(1, count));
}
}
Loading
Loading