space to tab
This commit is contained in:
1 parent
7583274fff
commit
a9c9542966
3 files changed
+113
-113
No files matched your search
@@ -28,68 +28,68 @@ import org.springframework.web.servlet.mvc.support.RedirectAttributes;
|
||||
@RestController
|
||||
public class ImageController {
|
||||
|
||||
@Autowired
|
||||
private ObjectMapper mapper;
|
||||
@Autowired
|
||||
private ObjectMapper mapper;
|
||||
|
||||
private final ImageDao imageDao;
|
||||
private final ImageDao imageDao;
|
||||
|
||||
public ImageController(ImageDao imageDao) {
|
||||
this.imageDao = imageDao;
|
||||
}
|
||||
public ImageController(ImageDao imageDao) {
|
||||
this.imageDao = imageDao;
|
||||
}
|
||||
|
||||
@GetMapping(value = "/images/{id}", produces = MediaType.IMAGE_JPEG_VALUE)
|
||||
public ResponseEntity<?> getImage(@PathVariable("id") long id) {
|
||||
@GetMapping(value = "/images/{id}", produces = MediaType.IMAGE_JPEG_VALUE)
|
||||
public ResponseEntity<?> getImage(@PathVariable("id") long id) {
|
||||
|
||||
Optional<Image> image = imageDao.retrieve(id);
|
||||
Optional<Image> image = imageDao.retrieve(id);
|
||||
|
||||
if (image.isPresent()) {
|
||||
InputStream inputStream = new ByteArrayInputStream(image.get().getData());
|
||||
return ResponseEntity.ok().contentType(MediaType.IMAGE_JPEG).body(new InputStreamResource(inputStream));
|
||||
}
|
||||
return new ResponseEntity<>("Image id=" + id + " not found.", HttpStatus.NOT_FOUND);
|
||||
}
|
||||
if (image.isPresent()) {
|
||||
InputStream inputStream = new ByteArrayInputStream(image.get().getData());
|
||||
return ResponseEntity.ok().contentType(MediaType.IMAGE_JPEG).body(new InputStreamResource(inputStream));
|
||||
}
|
||||
return new ResponseEntity<>("Image id=" + id + " not found.", HttpStatus.NOT_FOUND);
|
||||
}
|
||||
|
||||
@DeleteMapping(value = "/images/{id}")
|
||||
public ResponseEntity<?> deleteImage(@PathVariable("id") long id) {
|
||||
@DeleteMapping(value = "/images/{id}")
|
||||
public ResponseEntity<?> deleteImage(@PathVariable("id") long id) {
|
||||
|
||||
Optional<Image> image = imageDao.retrieve(id);
|
||||
Optional<Image> image = imageDao.retrieve(id);
|
||||
|
||||
if (image.isPresent()) {
|
||||
imageDao.delete(image.get());
|
||||
return new ResponseEntity<>("Image id=" + id + " deleted.", HttpStatus.OK);
|
||||
}
|
||||
return new ResponseEntity<>("Image id=" + id + " not found.", HttpStatus.NOT_FOUND);
|
||||
}
|
||||
if (image.isPresent()) {
|
||||
imageDao.delete(image.get());
|
||||
return new ResponseEntity<>("Image id=" + id + " deleted.", HttpStatus.OK);
|
||||
}
|
||||
return new ResponseEntity<>("Image id=" + id + " not found.", HttpStatus.NOT_FOUND);
|
||||
}
|
||||
|
||||
@PostMapping(value = "/images")
|
||||
public ResponseEntity<?> addImage(@RequestParam("file") MultipartFile file, RedirectAttributes redirectAttributes) {
|
||||
@PostMapping(value = "/images")
|
||||
public ResponseEntity<?> addImage(@RequestParam("file") MultipartFile file, RedirectAttributes redirectAttributes) {
|
||||
|
||||
String contentType = file.getContentType();
|
||||
if (contentType != null && !contentType.equals(MediaType.IMAGE_JPEG.toString())) {
|
||||
return new ResponseEntity<>("Only JPEG file format supported", HttpStatus.UNSUPPORTED_MEDIA_TYPE);
|
||||
}
|
||||
try {
|
||||
imageDao.create(new Image(file.getOriginalFilename(), file.getBytes()));
|
||||
} catch (IOException e) {
|
||||
return new ResponseEntity<>("Failure to read file", HttpStatus.NO_CONTENT);
|
||||
}
|
||||
return new ResponseEntity<>("Image uploaded", HttpStatus.CREATED);
|
||||
}
|
||||
String contentType = file.getContentType();
|
||||
if (contentType != null && !contentType.equals(MediaType.IMAGE_JPEG.toString())) {
|
||||
return new ResponseEntity<>("Only JPEG file format supported", HttpStatus.UNSUPPORTED_MEDIA_TYPE);
|
||||
}
|
||||
try {
|
||||
imageDao.create(new Image(file.getOriginalFilename(), file.getBytes()));
|
||||
} catch (IOException e) {
|
||||
return new ResponseEntity<>("Failure to read file", HttpStatus.NO_CONTENT);
|
||||
}
|
||||
return new ResponseEntity<>("Image uploaded", HttpStatus.CREATED);
|
||||
}
|
||||
|
||||
@GetMapping(value = "/images", produces = "application/json")
|
||||
@ResponseBody
|
||||
public ArrayNode getImageList() {
|
||||
List<Image> images = imageDao.retrieveAll();
|
||||
ArrayNode nodes = mapper.createArrayNode();
|
||||
for (Image image : images) {
|
||||
ObjectNode objectNode = mapper.createObjectNode();
|
||||
objectNode.put("id", image.getId());
|
||||
objectNode.put("name", image.getName());
|
||||
objectNode.put("size", image.getSize());
|
||||
objectNode.put("type", image.getMediaType().toString());
|
||||
nodes.add(objectNode);
|
||||
}
|
||||
return nodes;
|
||||
}
|
||||
@GetMapping(value = "/images", produces = "application/json")
|
||||
@ResponseBody
|
||||
public ArrayNode getImageList() {
|
||||
List<Image> images = imageDao.retrieveAll();
|
||||
ArrayNode nodes = mapper.createArrayNode();
|
||||
for (Image image : images) {
|
||||
ObjectNode objectNode = mapper.createObjectNode();
|
||||
objectNode.put("id", image.getId());
|
||||
objectNode.put("name", image.getName());
|
||||
objectNode.put("size", image.getSize());
|
||||
objectNode.put("type", image.getMediaType().toString());
|
||||
nodes.add(objectNode);
|
||||
}
|
||||
return nodes;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -7,13 +7,13 @@ import org.springframework.boot.test.context.SpringBootTest;
|
||||
@SpringBootTest
|
||||
class BackendApplicationTests {
|
||||
|
||||
@Test
|
||||
void contextLoads() {
|
||||
}
|
||||
@Test
|
||||
void contextLoads() {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(1)
|
||||
public void appLaunchShouldReturnError() throws Exception {
|
||||
@Test
|
||||
@Order(1)
|
||||
public void appLaunchShouldReturnError() throws Exception {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -26,68 +26,68 @@ import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
|
||||
@TestMethodOrder(OrderAnnotation.class)
|
||||
public class ImageControllerTests {
|
||||
|
||||
@Autowired
|
||||
private MockMvc mockMvc;
|
||||
@Autowired
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@BeforeAll
|
||||
public static void reset() {
|
||||
// reset Image class static counter
|
||||
ReflectionTestUtils.setField(Image.class, "count", Long.valueOf(0));
|
||||
}
|
||||
@BeforeAll
|
||||
public static void reset() {
|
||||
// reset Image class static counter
|
||||
ReflectionTestUtils.setField(Image.class, "count", Long.valueOf(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(1)
|
||||
public void getImageListShouldReturnSuccess() throws Exception {
|
||||
this.mockMvc.perform(get("/images")).andDo(print()).andExpect(status().isOk())
|
||||
.andExpect(content().contentType(MediaType.APPLICATION_JSON));
|
||||
}
|
||||
@Test
|
||||
@Order(1)
|
||||
public void getImageListShouldReturnSuccess() throws Exception {
|
||||
this.mockMvc.perform(get("/images")).andDo(print()).andExpect(status().isOk())
|
||||
.andExpect(content().contentType(MediaType.APPLICATION_JSON));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(2)
|
||||
public void getImageShouldReturnNotFound() throws Exception {
|
||||
this.mockMvc.perform(get("/images/1000")).andDo(print()).andExpect(status().isNotFound());
|
||||
}
|
||||
@Test
|
||||
@Order(2)
|
||||
public void getImageShouldReturnNotFound() throws Exception {
|
||||
this.mockMvc.perform(get("/images/1000")).andDo(print()).andExpect(status().isNotFound());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(3)
|
||||
public void getImageShouldReturnSuccess() throws Exception {
|
||||
this.mockMvc.perform(get("/images/0")).andExpect(status().isOk());
|
||||
}
|
||||
@Test
|
||||
@Order(3)
|
||||
public void getImageShouldReturnSuccess() throws Exception {
|
||||
this.mockMvc.perform(get("/images/0")).andExpect(status().isOk());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(4)
|
||||
public void deleteImagesShouldReturnMethodNotAllowed() throws Exception {
|
||||
this.mockMvc.perform(delete("/images")).andDo(print()).andExpect(status().isMethodNotAllowed());
|
||||
}
|
||||
@Test
|
||||
@Order(4)
|
||||
public void deleteImagesShouldReturnMethodNotAllowed() throws Exception {
|
||||
this.mockMvc.perform(delete("/images")).andDo(print()).andExpect(status().isMethodNotAllowed());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(5)
|
||||
public void deleteImageShouldReturnNotFound() throws Exception {
|
||||
this.mockMvc.perform(delete("/images/1000")).andDo(print()).andExpect(status().isNotFound());
|
||||
}
|
||||
@Test
|
||||
@Order(5)
|
||||
public void deleteImageShouldReturnNotFound() throws Exception {
|
||||
this.mockMvc.perform(delete("/images/1000")).andDo(print()).andExpect(status().isNotFound());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(6)
|
||||
public void deleteImageShouldReturnSuccess() throws Exception {
|
||||
this.mockMvc.perform(delete("/images/0")).andDo(print()).andExpect(status().isOk());
|
||||
}
|
||||
@Test
|
||||
@Order(6)
|
||||
public void deleteImageShouldReturnSuccess() throws Exception {
|
||||
this.mockMvc.perform(delete("/images/0")).andDo(print()).andExpect(status().isOk());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(7)
|
||||
public void createImageShouldReturnSuccess() throws Exception {
|
||||
final ClassPathResource imgFile = new ClassPathResource("images/test.jpg");
|
||||
MockMultipartFile multipartFile = new MockMultipartFile("file", "test.jpg", "image/jpeg",
|
||||
imgFile.getInputStream());
|
||||
this.mockMvc.perform(MockMvcRequestBuilders.multipart("/images").file(multipartFile)).andDo(print())
|
||||
.andExpect(status().isCreated());
|
||||
}
|
||||
@Test
|
||||
@Order(7)
|
||||
public void createImageShouldReturnSuccess() throws Exception {
|
||||
final ClassPathResource imgFile = new ClassPathResource("images/test.jpg");
|
||||
MockMultipartFile multipartFile = new MockMultipartFile("file", "test.jpg", "image/jpeg",
|
||||
imgFile.getInputStream());
|
||||
this.mockMvc.perform(MockMvcRequestBuilders.multipart("/images").file(multipartFile)).andDo(print())
|
||||
.andExpect(status().isCreated());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(8)
|
||||
public void createImageShouldReturnUnsupportedMediaType() throws Exception {
|
||||
MockMultipartFile multipartFile = new MockMultipartFile("file", "test.jpg", "text/plain", "Test".getBytes());
|
||||
this.mockMvc.perform(MockMvcRequestBuilders.multipart("/images").file(multipartFile)).andDo(print())
|
||||
.andExpect(status().isUnsupportedMediaType());
|
||||
}
|
||||
@Test
|
||||
@Order(8)
|
||||
public void createImageShouldReturnUnsupportedMediaType() throws Exception {
|
||||
MockMultipartFile multipartFile = new MockMultipartFile("file", "test.jpg", "text/plain", "Test".getBytes());
|
||||
this.mockMvc.perform(MockMvcRequestBuilders.multipart("/images").file(multipartFile)).andDo(print())
|
||||
.andExpect(status().isUnsupportedMediaType());
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user