Skip to content

Commit

Permalink
test: 회원 이름 정보 수정 Controller, Service 테스트코드 작성 (#94)
Browse files Browse the repository at this point in the history
  • Loading branch information
ywonchae1 committed Jul 30, 2024
1 parent 728a8e5 commit 52d6183
Show file tree
Hide file tree
Showing 10 changed files with 190 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,9 @@ public Member updateGoal(Integer goal) {
this.goal = goal;
return this;
}

public Member updateName(String name) {
this.name = name;
return this;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.depromeet.member.service;

import com.depromeet.exception.ForbiddenException;
import com.depromeet.exception.InternalServerException;
import com.depromeet.exception.NotFoundException;
import com.depromeet.member.domain.Member;
Expand Down Expand Up @@ -53,6 +54,9 @@ public Member updateGoal(Long memberId, Integer goal) {

@Override
public Member updateName(Long memberId, String name) {
if (name == null || name.isBlank()) {
throw new ForbiddenException(MemberErrorType.NAME_CANNOT_BE_BLANK);
}
return memberPersistencePort
.updateName(memberId, name)
.orElseThrow(() -> new InternalServerException(MemberErrorType.UPDATE_NAME_FAILED));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,15 @@ public Optional<Member> updateGoal(Long memberId, Integer goal) {
return member;
});
}

@Override
public Optional<Member> updateName(Long memberId, String name) {
return findById(memberId)
.map(
item -> {
Member member = item.updateName(name);
save(member);
return member;
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.depromeet.service;

import com.depromeet.member.domain.Member;
import com.depromeet.member.domain.MemberRole;
import com.depromeet.member.port.out.persistence.MemberPersistencePort;
import com.depromeet.member.service.MemberService;
import com.depromeet.mock.FakeMemberRepository;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

class MemberServiceTest {
private MemberPersistencePort fakeMemberRepository;

private MemberService memberService;

private Long userId = 1L;
private Member member;

@BeforeEach
void init() {
fakeMemberRepository = new FakeMemberRepository();

// Member create
member =
Member.builder()
.id(userId)
.name("member1")
.email("[email protected]")
.role(MemberRole.USER)
.build();
fakeMemberRepository.save(member);

memberService = new MemberService(fakeMemberRepository);
}

@Test
void 회원의_이름을_수정할__있다() {
// given
Long memberId = 1L;
String newName = "테스트";

// when
Member member1 = memberService.updateName(memberId, newName);

// then
Assertions.assertThat(member1.getName()).isEqualTo(newName);
}

@Test
void 회원의_이름은_공백을_허용하지_않는다() {
// given
Long memberId = 1L;
String newName = "";

// when

// then
Assertions.assertThatThrownBy(() -> memberService.updateName(memberId, newName))
.hasMessage("멤버의 이름은 공백이 허용되지 않습니다");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
public enum MemberErrorType implements ErrorType {
NOT_FOUND("MEMBER_1", "멤버가 존재하지 않습니다"),
UPDATE_GOAL_FAILED("MEMBER_2", "멤버의 목표 수정에 실패하였습니다"),
UPDATE_NAME_FAILED("MEMBER_3", "멤버의 이름 수정에 실패하였습니다");
UPDATE_NAME_FAILED("MEMBER_3", "멤버의 이름 수정에 실패하였습니다"),
NAME_CANNOT_BE_BLANK("MEMBER_4", "멤버의 이름은 공백이 허용되지 않습니다");

private final String code;
private final String message;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.depromeet.memory.config;
package com.depromeet.config;

import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.*;
import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.*;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package com.depromeet.member.controller;

import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import com.depromeet.config.ControllerTestConfig;
import com.depromeet.member.api.MemberController;
import com.depromeet.member.domain.Member;
import com.depromeet.member.domain.MemberRole;
import com.depromeet.member.facade.MemberFacade;
import com.depromeet.memory.mock.WithCustomMockMember;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.HashMap;
import java.util.Map;
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.http.MediaType;

@WebMvcTest(MemberController.class)
public class MemberControllerTest extends ControllerTestConfig {
@Autowired private ObjectMapper objectMapper;

@MockBean MemberFacade memberFacade;

@Test
@WithCustomMockMember
void 회원의_이름을_수정합니다() throws Exception {
Map<String, String> requestBody = new HashMap<>();
requestBody.put("name", "테스트");

Member member = new Member(1L, 3000, "테스트", "[email protected]", MemberRole.USER, "aa");
when(memberFacade.updateName(anyLong(), anyString())).thenReturn(member);

mockMvc.perform(
patch("/member")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(requestBody)))
.andExpect(status().isOk())
.andExpect(jsonPath("$.code").value("MEMBER_4"))
.andExpect(jsonPath("$.message").value("멤버 이름 수정에 성공하였습니다"))
.andExpect(jsonPath("$.data.name").value("테스트"))
.andDo(print());
}

@Test
@WithCustomMockMember
void 회원의_이름은_Null_허용하지_않습니다() throws Exception {
Map<String, String> requestBody = new HashMap<>();
requestBody.put("name", null);

mockMvc.perform(
patch("/member")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(requestBody)))
.andExpect(status().isBadRequest())
.andExpect(jsonPath("$.code").value("COMMON_1"))
.andExpect(jsonPath("$.message").value("입력 값 검증에 실패하였습니다"))
.andExpect(jsonPath("$.data.fieldErrors[0].reason").value("must not be blank"))
.andDo(print());
}

@Test
@WithCustomMockMember
void 회원의_이름은_공백을_허용하지_않습니다() throws Exception {
Map<String, String> requestBody = new HashMap<>();
requestBody.put("name", "");

mockMvc.perform(
patch("/member")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(requestBody)))
.andExpect(status().isBadRequest())
.andExpect(jsonPath("$.code").value("COMMON_1"))
.andExpect(jsonPath("$.message").value("입력 값 검증에 실패하였습니다"))
.andExpect(jsonPath("$.data.fieldErrors[0].reason").value("must not be blank"))
.andDo(print());
}

@Test
@WithCustomMockMember
void 회원의_이름은_빈문자열을_허용하지_않습니다() throws Exception {
Map<String, String> requestBody = new HashMap<>();
requestBody.put("name", " ");

mockMvc.perform(
patch("/member")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(requestBody)))
.andExpect(status().isBadRequest())
.andExpect(jsonPath("$.code").value("COMMON_1"))
.andExpect(jsonPath("$.message").value("입력 값 검증에 실패하였습니다"))
.andExpect(jsonPath("$.data.fieldErrors[0].reason").value("must not be blank"))
.andDo(print());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import com.depromeet.config.ControllerTestConfig;
import com.depromeet.image.api.ImageController;
import com.depromeet.image.dto.response.ImageResponse;
import com.depromeet.image.dto.response.ImageUploadResponse;
import com.depromeet.image.facade.ImageFacade;
import com.depromeet.memory.config.ControllerTestConfig;
import com.depromeet.memory.fixture.dto.ImageUploadResponseDtoFixture;
import com.depromeet.memory.fixture.dto.MemoryImagesDtoFixture;
import com.depromeet.memory.mock.WithCustomMockMember;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

import com.depromeet.config.ControllerTestConfig;
import com.depromeet.memory.api.MemoryController;
import com.depromeet.memory.config.ControllerTestConfig;
import com.depromeet.memory.facade.MemoryFacade;
import com.depromeet.memory.mock.WithCustomMockMember;
import com.fasterxml.jackson.databind.ObjectMapper;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

import com.depromeet.memory.config.ControllerTestConfig;
import com.depromeet.config.ControllerTestConfig;
import com.depromeet.memory.mock.WithCustomMockMember;
import com.depromeet.pool.api.PoolController;
import com.depromeet.pool.facade.PoolFacade;
Expand Down

0 comments on commit 52d6183

Please sign in to comment.