Skip to content

Commit

Permalink
3주차 코드
Browse files Browse the repository at this point in the history
  • Loading branch information
unanchoi committed Mar 7, 2024
1 parent 3e2f017 commit 675e67b
Show file tree
Hide file tree
Showing 20 changed files with 367 additions and 34 deletions.
3 changes: 3 additions & 0 deletions spring-code-for-deploy/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ dependencies {
//h2
runtimeOnly 'com.h2database:h2'

// restdocs
testImplementation 'org.springframework.restdocs:spring-restdocs-mockmvc'

}
tasks.named('test') {
useJUnitPlatform()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,40 @@

import com.example.seminar.dto.request.post.PostCreateRequest;
import com.example.seminar.dto.request.post.PostUpdateRequest;
import com.example.seminar.dto.response.post.PostGetResponse;
import com.example.seminar.service.post.PostService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.net.URI;
import java.util.List;

@RestController
@RequestMapping("/api/posts")
@RequiredArgsConstructor
public class PostController {
private static final String CUSTOM_AUTH_ID = "X-Auth-Id";

private final PostService postService;

@PostMapping
public ResponseEntity<Void> createPost(
@RequestHeader(CUSTOM_AUTH_ID) Long memberId,
@RequestBody PostCreateRequest request) {
URI location = URI.create("/api/post/" + postService.create(request, memberId));
return ResponseEntity.created(location).build();
}

@PatchMapping("{postId}")
public ResponseEntity<Void> updatePost(
@PathVariable Long postId,
@RequestBody PostUpdateRequest request) {
postService.editContent(postId, request);
return ResponseEntity.noContent().build();
}

@DeleteMapping("{postId}")
public ResponseEntity<Void> deletePost(@PathVariable Long postId) {
postService.deleteById(postId);
return ResponseEntity.noContent().build();
}

@RestController
@RequestMapping("/api/posts")
@RequiredArgsConstructor
public class PostController {
private static final String CUSTOM_AUTH_ID = "X-Auth-Id";

private final PostService postService;

@PostMapping
public ResponseEntity<Void> createPost(
@RequestHeader(CUSTOM_AUTH_ID) Long memberId,
@RequestBody PostCreateRequest request) {
URI location = URI.create("/api/posts/" + postService.create(request, memberId));
return ResponseEntity.created(location).build();
}

@PatchMapping("{postId}")
public ResponseEntity<Void> updatePost(
@PathVariable Long postId,
@RequestBody PostUpdateRequest request) {
postService.editContent(postId, request);
return ResponseEntity.noContent().build();
}

@DeleteMapping("{postId}")
public ResponseEntity<Void> deletePost(@PathVariable Long postId) {
postService.deleteById(postId);
return ResponseEntity.noContent().build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public class Member extends BaseTimeEntity {
private String name;
private String nickname;
private int age;
private boolean isDeleted = false;

@Embedded
private SOPT sopt;
Expand All @@ -48,6 +49,7 @@ private Member(String name,
this.nickname = nickname;
this.age = age;
this.sopt = sopt;
this.isDeleted = false;
}

private void validateAge(final int age) {
Expand All @@ -73,6 +75,11 @@ private void validateNickname(final String nickname) {
}
}

public void remove() {

this.isDeleted = true;
}

public void updateSOPT(SOPT sopt) {
this.sopt = sopt;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,25 @@


import com.example.seminar.domain.SOPT;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;


public record MemberCreateRequest(
String name,
String nickname,
int age,
SOPT sopt
) {
<<<<<<< Updated upstream
public MemberCreateRequest(String name, String nickname, int age, SOPT sopt) {
this.name = name;
this.nickname = nickname;
this.age = age;
this.sopt = sopt;
}
=======

>>>>>>> Stashed changes
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.example.seminar.dto.request.member;

import com.example.seminar.domain.Part;
import lombok.AllArgsConstructor;
import lombok.Data;

@Data
@AllArgsConstructor
public class MemberProfileUpdateRequest {
private short generation;
private Part part;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@

import com.example.seminar.domain.Member;
import jakarta.persistence.EntityManager;
import jakarta.persistence.EntityManagerFactory;
import jakarta.persistence.PersistenceContext;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

@Repository
@RequiredArgsConstructor
Expand All @@ -14,6 +16,7 @@ public class MemberRepository {
@PersistenceContext
private final EntityManager em;

@Transactional
public void remove(Member member) {
em.remove(member);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ public class MemberRemover {
private final MemberRepository memberRepository;

public void remove(Member member) {
memberRepository.remove(member);
member.remove();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.example.seminar.config;

import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.restdocs.RestDocumentationExtension;
import org.springframework.test.web.servlet.MockMvc;

@ExtendWith(RestDocumentationExtension.class)
public abstract class RestDocsSupport {

protected MockMvc mockMvc;


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.example.seminar.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.web.servlet.MockMvc;


@WebMvcTest
@ActiveProfiles("test")
public class ControllerTestManager {

@Autowired
MockMvc mockMvc;

@Autowired
ObjectMapper objectMapper;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package com.example.seminar.controller;

import com.example.seminar.domain.Part;
import com.example.seminar.domain.SOPT;
import com.example.seminar.dto.request.member.MemberCreateRequest;
import com.example.seminar.dto.request.member.MemberProfileUpdateRequest;
import com.example.seminar.dto.response.MemberGetResponse;
import com.example.seminar.service.member.MemberService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.mockito.BDDMockito;
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;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;

import java.net.URI;
import java.util.List;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;

@WebMvcTest(controllers = MemberController.class)
@ActiveProfiles("test")
public class MemberControllerTest extends ControllerTestManager {

@MockBean
private MemberService memberService;
@Test
@DisplayName("신규 회원을 등록한다.")
void createMember() throws Exception {

// given
when(memberService.create(any(MemberCreateRequest.class)))
.thenReturn("/api/member/1");

MemberCreateRequest request = new MemberCreateRequest(
"오해영",
"5hae0",
28,
SOPT.builder()
.part(Part.DESIGN)
.build()
);

// when, then
mockMvc.perform(MockMvcRequestBuilders.post("/api/member")
.content(objectMapper.writeValueAsString(request))
.contentType(MediaType.APPLICATION_JSON_VALUE))
.andDo(MockMvcResultHandlers.print())
.andExpect(MockMvcResultMatchers.status().isCreated())
.andExpect(MockMvcResultMatchers.header().string("Location", "/api/member/1"))
;
}

@Test
@DisplayName("회원 정보 목록을 조회한다.")
void getMembersProfile() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.get("/api/member"))
.andDo(MockMvcResultHandlers.print())
.andExpect(MockMvcResultMatchers.status().isOk());
}

@Test
@DisplayName("회원 정보를 수정한다.")
void updateMemberSoptInfo() throws Exception {

MemberProfileUpdateRequest request = new MemberProfileUpdateRequest((short) 6, Part.SERVER);

mockMvc.perform(MockMvcRequestBuilders.patch("/api/member/1")
.content(objectMapper.writeValueAsString(request))
.contentType(MediaType.APPLICATION_JSON_VALUE))
.andDo(MockMvcResultHandlers.print())
.andExpect(MockMvcResultMatchers.status().isNoContent());
}

@Test
@DisplayName("회원 정보를 삭제한다.")
void deleteMember() throws Exception {
// given // when // then
mockMvc.perform(MockMvcRequestBuilders.delete("/api/member/1"))
.andDo(MockMvcResultHandlers.print())
.andExpect(MockMvcResultMatchers.status().isNoContent());

}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.example.seminar.controller;

import com.example.seminar.dto.request.post.PostCreateRequest;
import com.example.seminar.service.post.PostService;
import com.fasterxml.jackson.databind.ObjectMapper;
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.http.MediaType;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;

@WebMvcTest(controllers = PostController.class)
@ActiveProfiles("test")
public class PostControllerTest extends ControllerTestManager {

private static final String POST_API_ENDPOINT = "/api/posts";

@MockBean
PostService postService;

@Test
@DisplayName("게시글을 생성할 수 있다.")
void createPost() throws Exception {
// given
when(postService.create(any(PostCreateRequest.class), any(Long.class)))
.thenReturn("1");

PostCreateRequest request = new PostCreateRequest(
"제목",
"내용"
);

String CUSTOM_USER_ID = "X-Auth-Id";

// when then
mockMvc.perform(
MockMvcRequestBuilders.post(POST_API_ENDPOINT)
.content(objectMapper.writeValueAsString(request))
.contentType(MediaType.APPLICATION_JSON_VALUE)
.header(CUSTOM_USER_ID, 1L))
.andDo(MockMvcResultHandlers.print())
.andExpect(MockMvcResultMatchers.status().isCreated())
.andExpect(MockMvcResultMatchers.header().string("Location", "/api/posts/1"));
}
}
Loading

0 comments on commit 675e67b

Please sign in to comment.