Skip to content

Commit

Permalink
test(docs): CommandDocsService 테스트 코드 작성
Browse files Browse the repository at this point in the history
  • Loading branch information
NameIsUser06 committed Apr 21, 2024
1 parent b22ad27 commit ecc5400
Show file tree
Hide file tree
Showing 10 changed files with 368 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.OneToMany;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
Expand All @@ -28,12 +30,15 @@ public class Docs {
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@NotNull
@Size(max = 32)
@Column(length = 32, unique = true)
private String title;

@Column
private Integer enroll;

@NotNull
@Enumerated(EnumType.STRING)
private DocsType docsType;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import jakarta.persistence.IdClass;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Positive;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
Expand All @@ -24,14 +26,16 @@
public class VersionDocs {

@Id
@NotNull
private Integer version;

@Id
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "docs_id")
private Docs docs;

@Column(columnDefinition = "TEXT", nullable = false)
@Column(columnDefinition = "TEXT")
@NotNull
private String contents;

@CreatedDate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
@EqualsAndHashCode
public class VersionDocsPk implements Serializable {
private int version;
private Docs docs;
private Long docs;
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ public class DocsValidator {
private final DocsRepository docsRepository;
private final DocsReader docsReader;

public void checkTitleAlreadyExist(Docs docs) {
if (docsRepository.existsByTitle(docs.getTitle())) {
public void checkTitleAlreadyExist(String title) {
if (docsRepository.existsByTitle(title)) {
throw new BumawikiException(ErrorCode.DOCS_TITLE_ALREADY_EXIST);
}
}
Expand All @@ -42,7 +42,6 @@ public void checkUpdateOneSelf(User user, Docs docs) {
throw new BumawikiException(ErrorCode.CANNOT_CHANGE_YOUR_DOCS);
}
}

public void checkUpdatableDocsType(DocsType docsType) {
if (docsType.equals(DocsType.READONLY)) {
throw new BumawikiException(ErrorCode.NO_UPDATABLE_DOCS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ public VersionResponseDto getDocsVersion(Docs findDocs) {
public List<DocsPopularResponseDto> findByThumbsUpsDesc() {
return jpaQueryFactory
.select(
constructor(DocsPopularResponseDto.class, docs.title, docs.enroll, docs.docsType, thumbsUp.id.count()))
.from(docs)
.innerJoin(docs, thumbsUp.docs)
.groupBy(docs.title, docs.enroll, docs.docsType)
constructor(DocsPopularResponseDto.class, thumbsUp.docs.title, thumbsUp.docs.enroll,
thumbsUp.docs.docsType, thumbsUp.id.count()))
.from(thumbsUp)
.innerJoin(thumbsUp.docs)
.groupBy(thumbsUp.docs.title, thumbsUp.docs.enroll, thumbsUp.docs.docsType)
.orderBy(thumbsUp.id.count().desc())
.limit(25)
.fetch();

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ public class CommandDocsService {
private final ThumbsUpDeleter thumbsUpDeleter;

public void create(Docs docs, User user, String contents) {
docsValidator.checkTitleAlreadyExist(docs);
docsValidator.checkTitleAlreadyExist(docs.getTitle());
docsCreator.create(docs, user, contents);
}

public void delete(Long id) {
docsDeleter.delete(id);
thumbsUpDeleter.deleteAllByDocsId(id);
docsDeleter.delete(id);
}

public void update(User user, String title, String contents, Integer updatingVersion) {
Expand All @@ -53,8 +53,8 @@ public void update(User user, String title, String contents, Integer updatingVer
}

public void titleUpdate(String title, String changedTitle) {
docsValidator.checkTitleAlreadyExist(changedTitle);
Docs docs = docsReader.findByTitle(title);
docsValidator.checkTitleAlreadyExist(docs);

docsUpdater.updateTitle(docs, changedTitle);
}
Expand All @@ -72,7 +72,7 @@ public void solveConflict(String title, String contents, Integer version, User u
throw new BumawikiException(ErrorCode.DOCS_CONFLICTED);
}

docsCreator.create(
docsCreator.createVersionDocs(
docs,
user,
contents
Expand Down
12 changes: 8 additions & 4 deletions src/main/java/com/project/bumawiki/domain/user/domain/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import java.util.List;

import com.project.bumawiki.domain.thumbsup.domain.ThumbsUp;
import com.project.bumawiki.domain.thumbsup.exception.AlreadyThumbsUpException;
import com.project.bumawiki.domain.thumbsup.exception.YouDontThumbsUpThisDocs;
import com.project.bumawiki.domain.thumbsup.presentation.dto.ThumbsUpResponseDto;
import com.project.bumawiki.domain.user.domain.authority.Authority;

Expand All @@ -18,6 +20,8 @@
import jakarta.persistence.Id;
import jakarta.persistence.OneToMany;
import jakarta.validation.constraints.Max;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
Expand All @@ -42,24 +46,24 @@ public class User {
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Max(32)
@Size(max = 32)
@Column(unique = true, length = 32)
private String email;

@Max(16)
@Size(max = 16)
@NotNull
@Column(length = 16)
private String name;

@Max(8)
@Column(length = 8)
private Integer enroll;

@Max(20)
@Column(length = 20)
private String nickName;

@Enumerated(EnumType.STRING)
@Column(length = 16)
@Enumerated(EnumType.STRING)
private Authority authority;

public List<ThumbsUpResponseDto> getList() {
Expand Down
Loading

0 comments on commit ecc5400

Please sign in to comment.