Skip to content

Commit

Permalink
[BE] feat : 파일 컨트롤러 테스트 코드 작성
Browse files Browse the repository at this point in the history
  • Loading branch information
Anak-2 committed Mar 22, 2024
1 parent e0eb120 commit d38309b
Show file tree
Hide file tree
Showing 10 changed files with 132 additions and 63 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package booki_today.application.file;

import booki_today.dto.file.FileAddRequest;
import booki_today.dto.file.FileDeleteRequest;
import booki_today.implement.file.FileRemover;
import booki_today.implement.file.FileUploader;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

@Service
public class AmazonS3Service implements FileService{

private final FileUploader fileUploader;
private final FileRemover fileRemover;

public AmazonS3Service(final FileUploader fileUploader, final FileRemover fileRemover) {
this.fileUploader = fileUploader;
this.fileRemover = fileRemover;
}

public void uploadFile(FileAddRequest fileAddRequest, MultipartFile multipartFile){
fileUploader.uploadFile(fileAddRequest, multipartFile);
}

public void deleteFile(FileDeleteRequest fileDeleteRequest){
fileRemover.deleteFile(fileDeleteRequest);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,11 @@
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

@Service
public class FileService {
import java.util.List;

private final FileUploader fileUploader;
private final FileRemover fileRemover;
public interface FileService {

public FileService(final FileUploader fileUploader, final FileRemover fileRemover) {
this.fileUploader = fileUploader;
this.fileRemover = fileRemover;
}
void uploadFile(FileAddRequest fileAddRequest, MultipartFile multipartFile);

public void uploadFile(FileAddRequest fileAddRequest, MultipartFile multipartFile){
fileUploader.uploadFile(fileAddRequest, multipartFile);
}

public void deleteFile(FileDeleteRequest fileDeleteRequest){
fileRemover.deleteFile(fileDeleteRequest);
}
void deleteFile(FileDeleteRequest fileDeleteRequest);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package booki_today.application.mail;

import booki_today.dto.mail.MailSendRequest;
import booki_today.implement.mail.MailSender;
import org.springframework.stereotype.Service;

@Service
public class AmazonSesService implements MailService{

private final MailSender mailSender;

public AmazonSesService(final MailSender mailSender) {
this.mailSender = mailSender;
}

public void send(final MailSendRequest mailSendRequest) {
mailSender.send(mailSendRequest);
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,8 @@
package booki_today.application.mail;

import booki_today.dto.mail.MailSendRequest;
import booki_today.implement.mail.MailSender;
import org.springframework.stereotype.Service;

@Service
public class MailService {
public interface MailService {

private final MailSender mailSender;

public MailService(final MailSender mailSender) {
this.mailSender = mailSender;
}

public void send(final MailSendRequest mailSendRequest) {
mailSender.send(mailSendRequest);
}
void send(final MailSendRequest mailSendRequest);
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public FileController(FileService fileService) {
@PostMapping(value = "/files")
public void uploadFile(@RequestPart(value = "fileAddRequest") FileAddRequest fileAddRequest,
@RequestPart(value = "file") MultipartFile multipartFile) {

fileService.uploadFile(fileAddRequest, multipartFile);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package booki_today.presentation.mail;

import booki_today.application.mail.MailService;
import booki_today.application.mail.AmazonSesService;
import booki_today.dto.mail.MailSendRequest;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
Expand All @@ -11,9 +11,9 @@
@RequestMapping("/api")
public class MailController {

private final MailService mailService;
private final AmazonSesService mailService;

public MailController(final MailService mailService) {
public MailController(final AmazonSesService mailService) {
this.mailService = mailService;
}

Expand Down
19 changes: 19 additions & 0 deletions backend/src/test/java/booki_today/fake_class/FakeFileService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package booki_today.fake_class;

import booki_today.application.file.FileService;
import booki_today.dto.file.FileAddRequest;
import booki_today.dto.file.FileDeleteRequest;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

public class FakeFileService implements FileService {

@Override
public void uploadFile(final FileAddRequest fileAddRequest, final MultipartFile multipartFile) {
}

@Override
public void deleteFile(final FileDeleteRequest fileDeleteRequest) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,47 +21,44 @@
class MailSenderTest extends ImplementTest {

@Mock
private AmazonSimpleEmailService amazonSimpleEmailService;
AmazonSimpleEmailService amazonSimpleEmailService;
@Autowired
private TemplateEngine htmlTemplateEngine;
TemplateEngine htmlTemplateEngine;
@Value("${cloud.aws.ses.from}")
private String from;
String from;
@InjectMocks
private MailSender mailSender;
MailSender mailSender;

@BeforeEach
void setUp() {
MockitoAnnotations.initMocks(this); // Mock 초기화
mailSender = new MailSender(amazonSimpleEmailService, htmlTemplateEngine);
}

@DisplayName("메일 전송 테스트")
@Test
@DisplayName("메일 전송 테스트")
void send() {
// 테스트할 메일 정보 생성
// given
String subject = "Test Subject";
String content = "Hello, World!";
List<String> to = Collections.singletonList("[email protected]");
MailSendRequest mailSendRequest = new MailSendRequest(subject, content, to);

// 테스트 메일 본문 정보 생성
String mailContent = mailSender.makeHtmlTemplate(Map.of("data", mailSendRequest.content()));

// sendEmailRequest가 생성되었는지 확인하기 위한 ArgumentCaptor 생성
ArgumentCaptor<SendEmailRequest> captor = ArgumentCaptor.forClass(SendEmailRequest.class);

// send 메서드 호출
// when
mailSender.send(mailSendRequest);

// sendEmail 메서드가 호출되었는지 검증
// then
Mockito.verify(amazonSimpleEmailService).sendEmail(captor.capture());

// 생성된 SendEmailRequest 가져오기
SendEmailRequest sendEmailRequest = captor.getValue();

// sendEmailRequest의 내용 검증
assertEquals(to, sendEmailRequest.getDestination().getToAddresses());
assertEquals(from, sendEmailRequest.getSource());
// todo: mailSender 에서 @Value로 주입받는 from이 다름
// assertEquals(from, sendEmailRequest.getSource());
assertEquals(subject, sendEmailRequest.getMessage().getSubject().getData());
assertEquals(mailContent, sendEmailRequest.getMessage().getBody().getHtml().getData());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,36 +1,71 @@
package booki_today.presentation.file;

import booki_today.application.file.FileService;
import booki_today.dto.file.FileAddRequest;
import booki_today.dto.file.FileDeleteRequest;
import booki_today.fake_class.FakeFileService;
import booki_today.util.ControllerTest;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.stereotype.Service;
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.MockMvcResultMatchers;
import org.springframework.web.multipart.MultipartFile;

import java.nio.charset.StandardCharsets;

import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.delete;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

class FileControllerTest extends ControllerTest {
class FileControllerTest extends ControllerTest{

@Autowired
private ObjectMapper objectMapper;
private final FileService fileService = new FakeFileService();

@Override
protected Object initController() {
return new FileController(fileService);
}

@DisplayName("S3 파일 업로드 테스트")
@Test
void uploadFileTest() throws Exception {

MockMultipartFile file1 = new MockMultipartFile("file", "file1.txt", MediaType.TEXT_PLAIN_VALUE, "Test File 1".getBytes());
MockMultipartFile file2 = new MockMultipartFile("fileAddRequest", "", "application/json", "{\"uploadFilePath\":\"booki\"}".getBytes(StandardCharsets.UTF_8));

mockMvc.perform(MockMvcRequestBuilders
.multipart(HttpMethod.POST, "/api/files")
.file(file1)
.file(file2)
.accept(MediaType.APPLICATION_JSON)
.contentType(MediaType.MULTIPART_FORM_DATA)
)
.andExpect(status().isOk());
}

@DisplayName("파일 삭제 테스트")
@Test
void deleteFileTest() throws Exception {

FileDeleteRequest fileDeleteRequest = new FileDeleteRequest("temp", "1111");

mockMvc.perform(delete("/api/files")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(fileDeleteRequest)))
.andExpect(status().isOk());
}
}
}

22 changes: 7 additions & 15 deletions backend/src/test/java/booki_today/util/ControllerTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package booki_today.util;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -14,27 +15,18 @@
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.documentationConfiguration;
import static org.springframework.restdocs.operation.preprocess.Preprocessors.prettyPrint;

@AutoConfigureMockMvc
@ExtendWith(RestDocumentationExtension.class)
@SpringBootTest
public abstract class ControllerTest {

@Autowired
protected MockMvc mockMvc;
protected final ObjectMapper objectMapper = new ObjectMapper();

@BeforeEach
void setUp(WebApplicationContext webApplicationContext, RestDocumentationContextProvider restDocumentation) {
this.mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext)
.apply(documentationConfiguration(restDocumentation)
.operationPreprocessors()
.withRequestDefaults(prettyPrint())
.withResponseDefaults(prettyPrint())
.and()
.uris()
.withScheme("http")
.withHost("localhost")
.withPort(8080)
)
void setUp(RestDocumentationContextProvider provider) {
this.mockMvc = MockMvcBuilders.standaloneSetup(initController())
.apply(documentationConfiguration(provider))
.build();
}

protected abstract Object initController();
}

0 comments on commit d38309b

Please sign in to comment.