Fix FileUploadTests.java
This commit is contained in:
1 parent
cb9790fdd6
commit
c13f926a56
2 files changed
+53
-15
No files matched your search
@@ -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"));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user