Storing files indexed by a database (#10174)
Creating the FileLocationService to link the FileSystemRepository to the ImageDbRepository. Removing test order Changing to BDDMockito Changing Long wrapper for @id. Changing the test names to given-when-then pattern Co-authored-by: Gilvan Ornelas Fernandes Filho <gilvan.fernandes@bairesdev.com>
This commit is contained in:
+69
@@ -0,0 +1,69 @@
|
||||
package com.baeldung.db.indexing;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||
import org.springframework.core.io.FileSystemResource;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.mock.web.MockMultipartFile;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.MvcResult;
|
||||
import org.springframework.test.web.servlet.request.MockMultipartHttpServletRequestBuilder;
|
||||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
|
||||
|
||||
@SpringBootTest(classes = ImageArchiveApplication.class)
|
||||
@AutoConfigureMockMvc
|
||||
class FileSystemImageIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
MockMvc mockMvc;
|
||||
|
||||
@MockBean
|
||||
FileLocationService fileLocationService;
|
||||
|
||||
@Test
|
||||
void givenJpegImage_whenUploadIt_thenReturnItsId() throws Exception {
|
||||
ClassLoader classLoader = ClassLoader.getSystemClassLoader();
|
||||
InputStream image = classLoader.getResourceAsStream("baeldung.jpeg");
|
||||
|
||||
MockMultipartHttpServletRequestBuilder multipartRequest = MockMvcRequestBuilders.multipart("/file-system/image")
|
||||
.file(new MockMultipartFile("image", "baeldung", MediaType.TEXT_PLAIN_VALUE, image));
|
||||
|
||||
MvcResult result = mockMvc.perform(multipartRequest)
|
||||
.andExpect(status().isOk())
|
||||
.andReturn();
|
||||
|
||||
assertThat(result.getResponse()
|
||||
.getContentAsString())
|
||||
.isEqualTo("1");
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenBaeldungImage_whenDownloadIt_thenReturnTheImage() throws Exception {
|
||||
given(fileLocationService.find(1L))
|
||||
.willReturn(baeldungJpegResource());
|
||||
|
||||
mockMvc.perform(MockMvcRequestBuilders
|
||||
.get("/file-system/image/1")
|
||||
.contentType(MediaType.IMAGE_JPEG_VALUE))
|
||||
.andExpect(status().isOk());
|
||||
}
|
||||
|
||||
private FileSystemResource baeldungJpegResource() {
|
||||
ClassLoader classLoader = ClassLoader.getSystemClassLoader();
|
||||
String imagePath = classLoader.getResource("baeldung.jpeg")
|
||||
.getFile();
|
||||
|
||||
return new FileSystemResource(Paths.get(imagePath));
|
||||
}
|
||||
|
||||
}
|
||||
+72
@@ -0,0 +1,72 @@
|
||||
package com.baeldung.db.indexing;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.mock.web.MockMultipartFile;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.MvcResult;
|
||||
import org.springframework.test.web.servlet.request.MockMultipartHttpServletRequestBuilder;
|
||||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
|
||||
|
||||
@SpringBootTest(classes = ImageArchiveApplication.class)
|
||||
@AutoConfigureMockMvc
|
||||
class ImageIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
MockMvc mockMvc;
|
||||
|
||||
@MockBean
|
||||
ImageDbRepository imageRepository;
|
||||
|
||||
@Test
|
||||
void givenBaeldungJpegImage_whenUploadIt_thenReturnItsId() throws Exception {
|
||||
ClassLoader classLoader = ClassLoader.getSystemClassLoader();
|
||||
InputStream image = classLoader.getResourceAsStream("baeldung.jpeg");
|
||||
|
||||
MockMultipartHttpServletRequestBuilder multipartRequest = MockMvcRequestBuilders.multipart("/image")
|
||||
.file(new MockMultipartFile("image", "baeldung", MediaType.TEXT_PLAIN_VALUE, image));
|
||||
|
||||
MvcResult result = mockMvc.perform(multipartRequest)
|
||||
.andExpect(status().isOk())
|
||||
.andReturn();
|
||||
|
||||
assertThat(result.getResponse()
|
||||
.getContentAsString())
|
||||
.isEqualTo("1");
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenExistingImage_whenDownloadIt_thenReturnHttpStatusOk() throws Exception {
|
||||
given(imageRepository.findById(1L))
|
||||
.willReturn(Optional.of(baeldungImage()));
|
||||
|
||||
mockMvc.perform(MockMvcRequestBuilders
|
||||
.get("/image/1")
|
||||
.contentType(MediaType.IMAGE_JPEG_VALUE))
|
||||
.andExpect(status().isOk());
|
||||
}
|
||||
|
||||
private Image baeldungImage() throws IOException {
|
||||
ClassLoader classLoader = ClassLoader.getSystemClassLoader();
|
||||
|
||||
Image mockImage = new Image();
|
||||
mockImage.setContent(Files.readAllBytes(Paths.get(classLoader.getResource("baeldung.jpeg")
|
||||
.getFile())));
|
||||
return mockImage;
|
||||
}
|
||||
|
||||
}
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 9.1 KiB |
Reference in New Issue
Block a user