Fix FileUploadTests.java
This commit is contained in:
1 parent
cb9790fdd6
commit
c13f926a56
2 files changed
+53
-15
No files matched your search
@@ -26,6 +26,7 @@ dependencies {
|
|||||||
implementation 'org.springframework.boot:spring-boot-starter-web'
|
implementation 'org.springframework.boot:spring-boot-starter-web'
|
||||||
implementation 'org.thymeleaf.extras:thymeleaf-extras-springsecurity5'
|
implementation 'org.thymeleaf.extras:thymeleaf-extras-springsecurity5'
|
||||||
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
|
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
|
||||||
|
implementation 'org.projectlombok:lombok:1.18.22'
|
||||||
compileOnly 'org.projectlombok:lombok'
|
compileOnly 'org.projectlombok:lombok'
|
||||||
developmentOnly 'org.springframework.boot:spring-boot-devtools'
|
developmentOnly 'org.springframework.boot:spring-boot-devtools'
|
||||||
runtimeOnly 'org.postgresql:postgresql'
|
runtimeOnly 'org.postgresql:postgresql'
|
||||||
|
|||||||
@@ -1,20 +1,42 @@
|
|||||||
package site.easystartup.easystartupcore;
|
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.nio.file.Paths;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
|
import lombok.SneakyThrows;
|
||||||
|
import org.aspectj.lang.annotation.After;
|
||||||
import org.hamcrest.Matchers;
|
import org.hamcrest.Matchers;
|
||||||
|
import org.junit.jupiter.api.AfterAll;
|
||||||
|
import org.junit.jupiter.api.BeforeAll;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
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.autoconfigure.web.servlet.AutoConfigureMockMvc;
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
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.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.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 org.springframework.test.web.servlet.MockMvc;
|
||||||
|
import site.easystartup.easystartupcore.config.WebSecurityConfig;
|
||||||
import site.easystartup.easystartupcore.storage.StorageFileNotFoundException;
|
import site.easystartup.easystartupcore.storage.StorageFileNotFoundException;
|
||||||
import site.easystartup.easystartupcore.storage.StorageService;
|
import site.easystartup.easystartupcore.storage.StorageService;
|
||||||
|
|
||||||
|
import javax.sql.DataSource;
|
||||||
|
|
||||||
import static org.mockito.BDDMockito.given;
|
import static org.mockito.BDDMockito.given;
|
||||||
import static org.mockito.BDDMockito.then;
|
import static org.mockito.BDDMockito.then;
|
||||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
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;
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||||
|
|
||||||
|
|
||||||
@AutoConfigureMockMvc
|
@EnableAutoConfiguration(exclude = { SecurityAutoConfiguration.class })
|
||||||
|
@AutoConfigureMockMvc(addFilters = false)
|
||||||
@SpringBootTest
|
@SpringBootTest
|
||||||
public class FileUploadTests {
|
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
|
@Autowired
|
||||||
private MockMvc mvc;
|
private MockMvc mvc;
|
||||||
|
|
||||||
@@ -35,28 +66,27 @@ public class FileUploadTests {
|
|||||||
private StorageService storageService;
|
private StorageService storageService;
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldListAllFiles() throws Exception {
|
public void shouldListAllFiles() {
|
||||||
given(this.storageService.loadAll())
|
given(this.storageService.loadAll())
|
||||||
.willReturn(Stream.of(Paths.get("first.txt"), Paths.get("second.txt")));
|
.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
|
@Test
|
||||||
public void shouldSaveUploadedFile() throws Exception {
|
public void shouldSaveUploadedFile() {
|
||||||
MockMultipartFile multipartFile = new MockMultipartFile("file", "test.txt",
|
MockMultipartFile file
|
||||||
"text/plain", "Spring Framework".getBytes());
|
= new MockMultipartFile(
|
||||||
this.mvc.perform(multipart("/").file(multipartFile))
|
"file",
|
||||||
.andExpect(status().isFound())
|
"hello.txt",
|
||||||
.andExpect(header().string("Location", "/"));
|
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
|
@Test
|
||||||
public void should404WhenMissingFile() throws Exception {
|
public void should404WhenMissingFile() throws Exception {
|
||||||
given(this.storageService.load("test.txt"))
|
given(this.storageService.load("test.txt"))
|
||||||
@@ -65,4 +95,11 @@ public class FileUploadTests {
|
|||||||
this.mvc.perform(get("/files/test.txt")).andExpect(status().isNotFound());
|
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