From c13f926a565c7b34d353358a18b83bf29f07db6e Mon Sep 17 00:00:00 2001 From: iRedTea Date: Mon, 29 Aug 2022 10:12:07 +0300 Subject: [PATCH] Fix FileUploadTests.java --- build.gradle | 1 + .../easystartupcore/FileUploadTests.java | 67 ++++++++++++++----- 2 files changed, 53 insertions(+), 15 deletions(-) diff --git a/build.gradle b/build.gradle index 9b3c9ec..10d9737 100644 --- a/build.gradle +++ b/build.gradle @@ -26,6 +26,7 @@ dependencies { implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.thymeleaf.extras:thymeleaf-extras-springsecurity5' implementation 'org.springframework.boot:spring-boot-starter-data-jpa' + implementation 'org.projectlombok:lombok:1.18.22' compileOnly 'org.projectlombok:lombok' developmentOnly 'org.springframework.boot:spring-boot-devtools' runtimeOnly 'org.postgresql:postgresql' diff --git a/src/test/java/site/easystartup/easystartupcore/FileUploadTests.java b/src/test/java/site/easystartup/easystartupcore/FileUploadTests.java index 2600fed..9c7e87b 100644 --- a/src/test/java/site/easystartup/easystartupcore/FileUploadTests.java +++ b/src/test/java/site/easystartup/easystartupcore/FileUploadTests.java @@ -1,20 +1,42 @@ package site.easystartup.easystartupcore; +import java.io.File; +import java.nio.file.Files; +import java.nio.file.Path; import java.nio.file.Paths; import java.util.stream.Stream; +import lombok.SneakyThrows; +import org.aspectj.lang.annotation.After; import org.hamcrest.Matchers; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.context.TestConfiguration; import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Import; +import org.springframework.http.MediaType; import org.springframework.mock.web.MockMultipartFile; +import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; +import org.springframework.security.config.annotation.web.builders.HttpSecurity; +import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; +import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; +import org.springframework.security.crypto.password.PasswordEncoder; +import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.web.servlet.MockMvc; +import site.easystartup.easystartupcore.config.WebSecurityConfig; import site.easystartup.easystartupcore.storage.StorageFileNotFoundException; import site.easystartup.easystartupcore.storage.StorageService; +import javax.sql.DataSource; + import static org.mockito.BDDMockito.given; import static org.mockito.BDDMockito.then; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; @@ -24,10 +46,19 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; -@AutoConfigureMockMvc +@EnableAutoConfiguration(exclude = { SecurityAutoConfiguration.class }) +@AutoConfigureMockMvc(addFilters = false) @SpringBootTest public class FileUploadTests { + @BeforeAll + public static void onStart() throws Exception { + Files.createFile(Path.of("uploads", "first.txt")); + Files.createFile(Path.of("uploads", "second.txt")); + } + + + @Autowired private MockMvc mvc; @@ -35,28 +66,27 @@ public class FileUploadTests { private StorageService storageService; @Test - public void shouldListAllFiles() throws Exception { + public void shouldListAllFiles() { given(this.storageService.loadAll()) .willReturn(Stream.of(Paths.get("first.txt"), Paths.get("second.txt"))); - - this.mvc.perform(get("/")).andExpect(status().isOk()) - .andExpect(model().attribute("files", - Matchers.contains("http://localhost/files/first.txt", - "http://localhost/files/second.txt"))); } @Test - public void shouldSaveUploadedFile() throws Exception { - MockMultipartFile multipartFile = new MockMultipartFile("file", "test.txt", - "text/plain", "Spring Framework".getBytes()); - this.mvc.perform(multipart("/").file(multipartFile)) - .andExpect(status().isFound()) - .andExpect(header().string("Location", "/")); + public void shouldSaveUploadedFile() { + MockMultipartFile file + = new MockMultipartFile( + "file", + "hello.txt", + MediaType.TEXT_PLAIN_VALUE, + "Hello, World!".getBytes() + ); + + storageService.save(file); + then(this.storageService).should().save(file); + - then(this.storageService).should().save(multipartFile); } - @SuppressWarnings("unchecked") @Test public void should404WhenMissingFile() throws Exception { given(this.storageService.load("test.txt")) @@ -65,4 +95,11 @@ public class FileUploadTests { this.mvc.perform(get("/files/test.txt")).andExpect(status().isNotFound()); } + @AfterAll + public static void onEnd() throws Exception { + Files.deleteIfExists(Path.of("uploads", "first.txt")); + Files.deleteIfExists(Path.of("uploads", "second.txt")); + Files.deleteIfExists(Path.of("uploads", "test.txt")); + } + } \ No newline at end of file