Fixed test.

This commit is contained in:
Andrew Lalis 2023-02-03 21:07:39 +01:00
parent 4c94a346c3
commit 67504d0883
2 changed files with 16 additions and 12 deletions

View File

@ -14,7 +14,6 @@ import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.server.ResponseStatusException; import org.springframework.web.server.ResponseStatusException;
import java.io.IOException; import java.io.IOException;

View File

@ -1,15 +1,16 @@
package nl.andrewlalis.gymboardcdn.service; package nl.andrewlalis.gymboardcdn.service;
import jakarta.servlet.ServletInputStream;
import jakarta.servlet.http.HttpServletRequest;
import nl.andrewlalis.gymboardcdn.api.FileUploadResponse; import nl.andrewlalis.gymboardcdn.api.FileUploadResponse;
import nl.andrewlalis.gymboardcdn.model.StoredFileRepository; import nl.andrewlalis.gymboardcdn.model.StoredFileRepository;
import nl.andrewlalis.gymboardcdn.model.VideoProcessingTask; import nl.andrewlalis.gymboardcdn.model.VideoProcessingTask;
import nl.andrewlalis.gymboardcdn.model.VideoProcessingTaskRepository; import nl.andrewlalis.gymboardcdn.model.VideoProcessingTaskRepository;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.mockito.Mockito; import org.mockito.Mockito;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Path; import java.nio.file.Path;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
@ -18,6 +19,12 @@ import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*; import static org.mockito.Mockito.*;
public class UploadServiceTest { public class UploadServiceTest {
/**
* Tests that when a processable video is uploaded, that it's saved to a
* temporary file, a new video processing task is created, and a new file
* identifier is generated for the file that may result.
* @throws IOException If an error occurs.
*/
@Test @Test
public void processableVideoUploadSuccess() throws IOException { public void processableVideoUploadSuccess() throws IOException {
StoredFileRepository storedFileRepository = Mockito.mock(StoredFileRepository.class); StoredFileRepository storedFileRepository = Mockito.mock(StoredFileRepository.class);
@ -25,13 +32,7 @@ public class UploadServiceTest {
when(videoTaskRepository.save(any(VideoProcessingTask.class))) when(videoTaskRepository.save(any(VideoProcessingTask.class)))
.then(returnsFirstArg()); .then(returnsFirstArg());
FileService fileService = Mockito.mock(FileService.class); FileService fileService = Mockito.mock(FileService.class);
MultipartFile multipartFile = new MockMultipartFile( when(fileService.saveToTempFile(any(InputStream.class), any(String.class)))
"file",
"testing.mp4",
"video/mp4",
new byte[]{1, 2, 3}
);
when(fileService.saveToTempFile(any(MultipartFile.class)))
.thenReturn(Path.of("test-cdn-files", "tmp", "bleh.mp4")); .thenReturn(Path.of("test-cdn-files", "tmp", "bleh.mp4"));
when(fileService.createNewFileIdentifier()).thenReturn("abc"); when(fileService.createNewFileIdentifier()).thenReturn("abc");
@ -41,10 +42,14 @@ public class UploadServiceTest {
videoTaskRepository, videoTaskRepository,
fileService fileService
); );
HttpServletRequest mockRequest = mock(HttpServletRequest.class);
when(mockRequest.getHeader("X-Filename")).thenReturn("testing.mp4");
ServletInputStream mockRequestInputStream = mock(ServletInputStream.class);
when(mockRequest.getInputStream()).thenReturn(mockRequestInputStream);
var expectedResponse = new FileUploadResponse("abc"); var expectedResponse = new FileUploadResponse("abc");
var response = uploadService.processableVideoUpload(multipartFile); var response = uploadService.processableVideoUpload(mockRequest);
assertEquals(expectedResponse, response); assertEquals(expectedResponse, response);
verify(fileService, times(1)).saveToTempFile(multipartFile); verify(fileService, times(1)).saveToTempFile(any(), any());
verify(videoTaskRepository, times(1)).save(any()); verify(videoTaskRepository, times(1)).save(any());
verify(fileService, times(1)).createNewFileIdentifier(); verify(fileService, times(1)).createNewFileIdentifier();
} }