diff --git a/gymboard-cdn/pom.xml b/gymboard-cdn/pom.xml index cc4ad41..c154ea6 100644 --- a/gymboard-cdn/pom.xml +++ b/gymboard-cdn/pom.xml @@ -31,11 +31,25 @@ postgresql runtime + org.springframework.boot spring-boot-starter-test test + + org.mockito + mockito-core + 5.1.1 + test + + + + com.h2database + h2 + 2.1.214 + test + diff --git a/gymboard-cdn/src/main/resources/application-development.properties b/gymboard-cdn/src/main/resources/application-development.properties index 1f85786..7b50361 100644 --- a/gymboard-cdn/src/main/resources/application-development.properties +++ b/gymboard-cdn/src/main/resources/application-development.properties @@ -2,6 +2,8 @@ spring.datasource.username=gymboard-cdn-dev spring.datasource.password=testpass spring.datasource.url=jdbc:postgresql://localhost:5433/gymboard-cdn-dev +spring.jpa.hibernate.ddl-auto=update + server.port=8082 app.web-origin=http://localhost:9000 diff --git a/gymboard-cdn/src/main/resources/application.properties b/gymboard-cdn/src/main/resources/application.properties index f65e572..45044f6 100644 --- a/gymboard-cdn/src/main/resources/application.properties +++ b/gymboard-cdn/src/main/resources/application.properties @@ -1,3 +1,5 @@ +spring.jpa.open-in-view=false + # TODO: Find a better way than dumping files into memory. spring.servlet.multipart.enabled=true spring.servlet.multipart.max-file-size=1GB diff --git a/gymboard-cdn/src/test/java/nl/andrewlalis/gymboardcdn/service/UploadServiceTest.java b/gymboard-cdn/src/test/java/nl/andrewlalis/gymboardcdn/service/UploadServiceTest.java new file mode 100644 index 0000000..b8eb101 --- /dev/null +++ b/gymboard-cdn/src/test/java/nl/andrewlalis/gymboardcdn/service/UploadServiceTest.java @@ -0,0 +1,51 @@ +package nl.andrewlalis.gymboardcdn.service; + +import nl.andrewlalis.gymboardcdn.api.FileUploadResponse; +import nl.andrewlalis.gymboardcdn.model.StoredFileRepository; +import nl.andrewlalis.gymboardcdn.model.VideoProcessingTask; +import nl.andrewlalis.gymboardcdn.model.VideoProcessingTaskRepository; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; +import org.springframework.mock.web.MockMultipartFile; +import org.springframework.web.multipart.MultipartFile; + +import java.io.IOException; +import java.nio.file.Path; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.AdditionalAnswers.returnsFirstArg; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.*; + +public class UploadServiceTest { + @Test + public void processableVideoUploadSuccess() throws IOException { + StoredFileRepository storedFileRepository = Mockito.mock(StoredFileRepository.class); + VideoProcessingTaskRepository videoTaskRepository = Mockito.mock(VideoProcessingTaskRepository.class); + when(videoTaskRepository.save(any(VideoProcessingTask.class))) + .then(returnsFirstArg()); + FileService fileService = Mockito.mock(FileService.class); + MultipartFile multipartFile = new MockMultipartFile( + "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")); + + when(fileService.createNewFileIdentifier()).thenReturn("abc"); + + UploadService uploadService = new UploadService( + storedFileRepository, + videoTaskRepository, + fileService + ); + var expectedResponse = new FileUploadResponse("abc"); + var response = uploadService.processableVideoUpload(multipartFile); + assertEquals(expectedResponse, response); + verify(fileService, times(1)).saveToTempFile(multipartFile); + verify(videoTaskRepository, times(1)).save(any()); + verify(fileService, times(1)).createNewFileIdentifier(); + } +} diff --git a/gymboard-cdn/src/test/resources/application.properties b/gymboard-cdn/src/test/resources/application.properties new file mode 100644 index 0000000..9195bbe --- /dev/null +++ b/gymboard-cdn/src/test/resources/application.properties @@ -0,0 +1,16 @@ +spring.datasource.url=jdbc:h2:mem:db;DB_CLOSE_DELAY=-1 +spring.datasource.driver-class-name=org.h2.Driver + +spring.jpa.open-in-view=false + +spring.servlet.multipart.enabled=true +spring.servlet.multipart.max-file-size=1GB +spring.servlet.multipart.max-request-size=2GB + +spring.jpa.hibernate.ddl-auto=update + +server.port=8082 + +app.web-origin=http://localhost:9000 +app.files.storage-dir=./test-cdn-files/ +app.files.temp-dir=./test-cdn-files/tmp/ diff --git a/gymboard-cdn/src/test/resources/sample_flex.mp4 b/gymboard-cdn/src/test/resources/sample_flex.mp4 new file mode 100644 index 0000000..41f9b7d Binary files /dev/null and b/gymboard-cdn/src/test/resources/sample_flex.mp4 differ