Skip to content

Commit

Permalink
test : 변경 사항에 따른 테스트 코드 추가
Browse files Browse the repository at this point in the history
- FileDataHandlerService getFileList, hashNext

- FileService getFiles

- FolderDataHandlerService getFolderList hasNext

- FolderService getFolders

- StorageFacadeService getFolderContents

- PagingUtil calculateSize createPageable
  • Loading branch information
kochungcheon committed Dec 27, 2023
1 parent 15ad8be commit a0fce68
Show file tree
Hide file tree
Showing 21 changed files with 337 additions and 107 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.List;

import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.c4cometrue.mystorage.exception.ErrorCode;
Expand Down Expand Up @@ -54,4 +55,13 @@ private void validateFileOwnershipBy(Long folderId, Long userId) {
throw ErrorCode.UNAUTHORIZED_FILE_ACCESS.serviceException();
}
}

public List<FileMetadata> getFileList(Long parentId, Long cursorId, Long userId, Pageable page) {
return cursorId == null ? fileRepository.findAllByParentIdAndUploaderIdOrderByIdDesc(parentId, userId, page)
: fileRepository.findByParentIdAndUploaderIdAndIdLessThanOrderByIdDesc(parentId, cursorId, userId, page);
}

public Boolean hashNext(Long parentId, Long userId, Long lastIdOfList) {
return fileRepository.existsByParentIdAndUploaderIdAndIdLessThan(parentId, userId, lastIdOfList);
}
}
20 changes: 7 additions & 13 deletions src/main/java/com/c4cometrue/mystorage/file/FileService.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.c4cometrue.mystorage.file.dto.CursorFileResponse;
import com.c4cometrue.mystorage.file.dto.FileContent;
import com.c4cometrue.mystorage.folder.FolderService;
import com.c4cometrue.mystorage.util.FileUtil;

Expand All @@ -21,8 +23,6 @@ public class FileService {
private final FileDataHandlerService fileDataHandlerService;
private final FolderService folderService;

private final FileRepository fileRepository;

@Value("${file.buffer}")
private int bufferSize;

Expand Down Expand Up @@ -65,17 +65,11 @@ public List<FileMetadata> findChildBy(Long parentId, Long userId) {
}

public CursorFileResponse getFiles(Long parentId, Long cursorId, Long userId, Pageable page) {
List<FileMetadata> files = getFileList(parentId, cursorId, userId, page);
List<FileMetadata> files = fileDataHandlerService.getFileList(parentId, cursorId, userId, page);
List<FileContent> fileContents = files.stream()
.map(file -> FileContent.of(file.getId(), file.getOriginalFileName()))
.collect(Collectors.toList());
Long lastIdOfList = files.isEmpty() ? null : files.get(files.size() - 1).getId();
return CursorFileResponse.of(files, hashNext(parentId, userId, lastIdOfList));
}

private List<FileMetadata> getFileList(Long parentId, Long cursorId, Long userId, Pageable page) {
return cursorId == null ? fileRepository.findAllByParentIdAndUploaderIdOrderByIdDesc(parentId, userId, page)
: fileRepository.findByParentIdAndUploaderIdAndIdLessThanOrderByIdDesc(parentId, cursorId, userId, page);
}

private Boolean hashNext(Long parentId, Long userId, Long lastIdOfList) {
return fileRepository.existsByParentIdAndUploaderIdAndIdLessThan(parentId, userId, lastIdOfList);
return CursorFileResponse.of(fileContents, fileDataHandlerService.hashNext(parentId, userId, lastIdOfList));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@

import java.util.List;

import com.c4cometrue.mystorage.file.FileMetadata;

public record CursorFileResponse(List<FileMetadata> fileMetadata, Boolean fileHasNext) {
public static CursorFileResponse of(List<FileMetadata> fileMetadata, Boolean fileHasNext) {
return new CursorFileResponse(fileMetadata, fileHasNext);
public record CursorFileResponse(List<FileContent> fileContents, Boolean fileHasNext) {
public static CursorFileResponse of(List<FileContent> fileContents, Boolean fileHasNext) {
return new CursorFileResponse(fileContents, fileHasNext);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.c4cometrue.mystorage.file.dto;

public record FileContent(Long fileId, String fileName) {
public static FileContent of (Long fileId, String fileName) {
return new FileContent(fileId, fileName);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.List;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.c4cometrue.mystorage.exception.ErrorCode;
Expand Down Expand Up @@ -70,4 +71,12 @@ private FolderMetadata findBy(Long folderId) {
return folderRepository.findById(folderId)
.orElseThrow(() -> ErrorCode.CANNOT_FOUND_FOLDER.serviceException("folderId { }", folderId));
}
public List<FolderMetadata> getFolderList(Long parentId, Long cursorId, Long userId, Pageable page) {
return cursorId == null ? folderRepository.findAllByParentIdAndUploaderIdOrderByIdDesc(parentId, userId, page) :
folderRepository.findByParentIdAndUploaderIdAndIdLessThanOrderByIdDesc(parentId, userId, cursorId, page);
}

public Boolean hasNext(Long parentId, Long userId, Long id) {
return folderRepository.existsByParentIdAndUploaderIdAndIdLessThan(parentId, userId, id);
}
}
20 changes: 7 additions & 13 deletions src/main/java/com/c4cometrue/mystorage/folder/FolderService.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.c4cometrue.mystorage.folder.dto.CursorFolderResponse;
import com.c4cometrue.mystorage.folder.dto.FolderContent;
import com.c4cometrue.mystorage.util.FolderUtil;

import lombok.RequiredArgsConstructor;
Expand All @@ -16,7 +18,6 @@
@RequiredArgsConstructor
public class FolderService {
private final FolderDataHandlerService folderDataHandlerService;
private final FolderRepository folderRepository;

// 부모 폴더는 null 이 될 수 있다
public void createBy(Long userId, String userFolderName, Long parentId) {
Expand All @@ -42,18 +43,11 @@ public List<FolderMetadata> findChildBy(Long parentId, Long userId) {
}

public CursorFolderResponse getFolders(Long parentId, Long cursorId, Long userId, Pageable page) {
List<FolderMetadata> folders = getFolderList(parentId, cursorId, userId, page);
List<FolderMetadata> folders = folderDataHandlerService.getFolderList(parentId, cursorId, userId, page);
List<FolderContent> folderContents = folders.stream()
.map(folder -> FolderContent.of(folder.getId(), folder.getOriginalFolderName()))
.collect(Collectors.toList());
Long lastIdOfList = folders.isEmpty() ? null : folders.get(folders.size() - 1).getId();
return CursorFolderResponse.of(folders, hasNext(parentId, userId, lastIdOfList));
}


private List<FolderMetadata> getFolderList(Long parentId, Long cursorId, Long userId, Pageable page) {
return cursorId == null ? folderRepository.findAllByParentIdAndUploaderIdOrderByIdDesc(parentId, userId, page) :
folderRepository.findByParentIdAndUploaderIdAndIdLessThanOrderByIdDesc(parentId, userId, cursorId, page);
}

private Boolean hasNext(Long parentId, Long userId, Long id) {
return folderRepository.existsByParentIdAndUploaderIdAndIdLessThan(parentId, userId, id);
return CursorFolderResponse.of(folderContents, folderDataHandlerService.hasNext(parentId, userId, lastIdOfList));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@

import java.util.List;

import com.c4cometrue.mystorage.folder.FolderMetadata;

public record CursorFolderResponse(List<FolderMetadata> folderMetadata, Boolean folderHasNext) {
public static CursorFolderResponse of(List<FolderMetadata> folderMetadata, Boolean folderHasNext) {
return new CursorFolderResponse(folderMetadata, folderHasNext);
public record CursorFolderResponse(List<FolderContent> folderMetadata, Boolean folderHasNext) {
public static CursorFolderResponse of(List<FolderContent> folderContents, Boolean folderHasNext) {
return new CursorFolderResponse(folderContents, folderHasNext);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.c4cometrue.mystorage.folder.dto;

public record FolderContent(Long folderId, String fileName) {
public static FolderContent of(Long folderId, String fileName) {
return new FolderContent(folderId, fileName);
}
}

This file was deleted.

16 changes: 2 additions & 14 deletions src/main/java/com/c4cometrue/mystorage/meta/MetadataType.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,7 @@
package com.c4cometrue.mystorage.meta;

import lombok.Getter;

@Getter
public enum MetadataType {
FILE("File"),
FOLDER("Folder");

private String type;

private MetadataType(String type) {
this.type = type;
}

public String getType() {
return type;
}
FILE,
FOLDER;
}
18 changes: 0 additions & 18 deletions src/main/java/com/c4cometrue/mystorage/meta/PagingHelper.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.c4cometrue.mystorage.folder.FolderService;
import com.c4cometrue.mystorage.folder.dto.CursorFolderResponse;
import com.c4cometrue.mystorage.meta.dto.CursorMetaResponse;
import com.c4cometrue.mystorage.util.PagingUtil;

import lombok.RequiredArgsConstructor;

Expand All @@ -16,26 +17,23 @@
public class StorageFacadeService {
private final FolderService folderService;
private final FileService fileService;
private final PagingHelper pagingHelper;

public CursorMetaResponse getFolderContents(Long parentId, Long cursorId, Long userId, Integer size,
boolean cursorFlag) {
Integer contentsSize = pagingHelper.calculateSize(size);
Integer contentsSize = PagingUtil.calculateSize(size);

if (cursorFlag) {
return handleFolderFirstStrategy(parentId, cursorId, userId, contentsSize);
} else {
return handleFileFirstStrategy(parentId, cursorId, userId, contentsSize);
}
return cursorFlag
? handleFolderFirstStrategy(parentId, cursorId, userId, contentsSize)
: handleFileFirstStrategy(parentId, cursorId, userId, contentsSize);
}

private CursorMetaResponse handleFolderFirstStrategy(Long parentId, Long cursorId, Long userId,
Integer contentsSize) {
Pageable page = pagingHelper.createPageable(contentsSize);
Pageable page = PagingUtil.createPageable(contentsSize);
CursorFolderResponse cursorFolderResponse = folderService.getFolders(parentId, cursorId, userId, page);

if (Boolean.FALSE.equals(cursorFolderResponse.folderHasNext())) {
Pageable remainPage = pagingHelper.createPageable(
Pageable remainPage = PagingUtil.createPageable(
contentsSize - cursorFolderResponse.folderMetadata().size());
CursorFileResponse cursorFileResponse = fileService.getFiles(parentId, null, userId, remainPage);
return CursorMetaResponse.of(cursorFolderResponse, cursorFileResponse);
Expand All @@ -45,7 +43,7 @@ private CursorMetaResponse handleFolderFirstStrategy(Long parentId, Long cursorI

private CursorMetaResponse handleFileFirstStrategy(Long parentId, Long cursorId, Long userId,
Integer contentsSize) {
Pageable page = pagingHelper.createPageable(contentsSize);
Pageable page = PagingUtil.createPageable(contentsSize);
CursorFileResponse cursorFileResponse = fileService.getFiles(parentId, cursorId, userId, page);
return CursorMetaResponse.of(CursorFolderResponse.of(null, false), cursorFileResponse);
}
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/com/c4cometrue/mystorage/util/PagingUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.c4cometrue.mystorage.util;

import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;

public class PagingUtil {
private PagingUtil() {
throw new AssertionError("should not be invoke");
}
private static final int DEFAULT_SIZE = 10;

public static Integer calculateSize(Integer size) {
return size == null ? DEFAULT_SIZE : size;
}

public static Pageable createPageable(int size) {
return PageRequest.of(0, size);
}
}
Loading

0 comments on commit a0fce68

Please sign in to comment.