Refactor Code
- Runtime Exception을 IllegalStateException으로 변경 - apache.commons.lang3 의존성 추가 - 문자열 테스트에서 apache.commons.lang3.RandomStringUtils 를 사용하도록 변경
This commit is contained in:
@@ -65,6 +65,7 @@ dependencies {
|
||||
|
||||
testImplementation 'org.springframework.boot:spring-boot-starter-test'
|
||||
testImplementation 'org.springframework.restdocs:spring-restdocs-mockmvc'
|
||||
implementation 'org.apache.commons:commons-lang3:3.12.0'
|
||||
}
|
||||
|
||||
test {
|
||||
|
||||
@@ -4,6 +4,7 @@ import com.yam.app.comment.domain.CommentProcessor;
|
||||
import com.yam.app.comment.presentation.CreateCommentCommand;
|
||||
import com.yam.app.comment.presentation.UpdateCommentCommand;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
@Service
|
||||
public class CommentFacade {
|
||||
@@ -14,14 +15,17 @@ public class CommentFacade {
|
||||
this.commentProcessor = commentProcessor;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Long create(CreateCommentCommand request, Long memberId) {
|
||||
return commentProcessor.create(request.getContent(), request.getArticleId(), memberId);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void update(UpdateCommentCommand request, Long commentId, Long memberId) {
|
||||
commentProcessor.update(request.getContent(), commentId, memberId);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void delete(Long commentId, Long memberId) {
|
||||
commentProcessor.delete(commentId, memberId);
|
||||
}
|
||||
|
||||
@@ -22,8 +22,9 @@ public final class MybatisCommentRepository implements CommentReader, CommentRep
|
||||
public Long save(Comment entity) {
|
||||
int result = template.insert(SAVE_FQCN, entity);
|
||||
if (result != 1) {
|
||||
throw new RuntimeException(
|
||||
String.format("There was a problem saving the object : %s", entity));
|
||||
throw new IllegalStateException(
|
||||
String.format("Unintentionally, more records were saved than expected. : %s",
|
||||
entity));
|
||||
}
|
||||
|
||||
return entity.getId();
|
||||
@@ -33,8 +34,9 @@ public final class MybatisCommentRepository implements CommentReader, CommentRep
|
||||
public void update(Comment entity) {
|
||||
int result = template.update(UPDATE_FQCN, entity);
|
||||
if (result != 1) {
|
||||
throw new RuntimeException(
|
||||
String.format("There was a problem updating the object : %s", entity));
|
||||
throw new IllegalStateException(
|
||||
String.format("Unintentionally, more records were updated than expected. : %s",
|
||||
entity));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -42,8 +44,9 @@ public final class MybatisCommentRepository implements CommentReader, CommentRep
|
||||
public void delete(Comment entity) {
|
||||
int result = template.update(DELETE_FQCN, entity);
|
||||
if (result != 1) {
|
||||
throw new RuntimeException(
|
||||
String.format("There was a problem soft deleting the object : %s", entity));
|
||||
throw new IllegalStateException(
|
||||
String.format("Unintentionally, more records were soft-deleted than expected. : %s",
|
||||
entity));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -28,7 +28,7 @@ public final class CommentCommandApi {
|
||||
this.commentFacade = commentFacade;
|
||||
}
|
||||
|
||||
@PostMapping("/api/comments/create")
|
||||
@PostMapping("/api/comments/")
|
||||
public ResponseEntity<Void> createComment(
|
||||
@RequestBody @Valid CreateCommentCommand request,
|
||||
@AuthenticationPrincipal Authentication authentication) {
|
||||
|
||||
@@ -2,7 +2,7 @@ package com.yam.app.comment.presentation;
|
||||
|
||||
public final class CommentApiUri {
|
||||
|
||||
public static final String CREATE_COMMENT = "/api/comments/create";
|
||||
public static final String CREATE_COMMENT = "/api/comments/";
|
||||
public static final String UPDATE_COMMENT = "/api/comments/";
|
||||
public static final String DELETE_COMMENT = "/api/comments/";
|
||||
|
||||
|
||||
@@ -11,7 +11,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.yam.app.comment.application.CommentFacade;
|
||||
import java.util.Random;
|
||||
import org.apache.commons.lang3.RandomStringUtils;
|
||||
import org.javaunit.autoparams.AutoSource;
|
||||
import org.javaunit.autoparams.customization.Customization;
|
||||
import org.javaunit.autoparams.customization.SettablePropertyWriter;
|
||||
@@ -38,16 +38,6 @@ final class CommentCommandApiTest {
|
||||
@MockBean
|
||||
private CommentFacade commentFacade;
|
||||
|
||||
private String generatedRandomString(int length) {
|
||||
Random random = new Random();
|
||||
|
||||
return random.ints(48, 123)
|
||||
.filter(i -> (i <= 57 || i >= 65) && (i <= 90 || i >= 97))
|
||||
.limit(length)
|
||||
.collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append)
|
||||
.toString();
|
||||
}
|
||||
|
||||
@Nested
|
||||
@DisplayName("댓글 작성 HTTP API")
|
||||
class CreateCommentApi {
|
||||
@@ -135,7 +125,7 @@ final class CommentCommandApiTest {
|
||||
var maxContentLength = 120;
|
||||
var session = new MockHttpSession();
|
||||
var exceededLengthCommand = new CreateCommentCommand();
|
||||
exceededLengthCommand.setContent(generatedRandomString(maxContentLength + 1));
|
||||
exceededLengthCommand.setContent(RandomStringUtils.random(maxContentLength + 1));
|
||||
exceededLengthCommand.setArticleId(args);
|
||||
|
||||
//Act
|
||||
@@ -243,7 +233,7 @@ final class CommentCommandApiTest {
|
||||
var maxContentLength = 120;
|
||||
var session = new MockHttpSession();
|
||||
var exceededLengthCommand = new UpdateCommentCommand();
|
||||
exceededLengthCommand.setContent(generatedRandomString(maxContentLength + 1));
|
||||
exceededLengthCommand.setContent(RandomStringUtils.random(maxContentLength + 1));
|
||||
exceededLengthCommand.setCommentId(args);
|
||||
|
||||
//Act
|
||||
|
||||
Reference in New Issue
Block a user