From a9c9542966b250bad14bd9d097e660400df88f95 Mon Sep 17 00:00:00 2001 From: Theophyl Hoguet Date: Mon, 13 Mar 2023 22:52:30 +0100 Subject: [PATCH] space to tab --- .../java/pdl/backend/ImageController.java | 104 ++++++++--------- .../pdl/backend/BackendApplicationTests.java | 14 +-- .../pdl/backend/ImageControllerTests.java | 108 +++++++++--------- 3 files changed, 113 insertions(+), 113 deletions(-) diff --git a/backend/src/main/java/pdl/backend/ImageController.java b/backend/src/main/java/pdl/backend/ImageController.java index 8087761..9ee4318 100644 --- a/backend/src/main/java/pdl/backend/ImageController.java +++ b/backend/src/main/java/pdl/backend/ImageController.java @@ -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 = imageDao.retrieve(id); + Optional 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 = imageDao.retrieve(id); + Optional 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 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 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; + } } diff --git a/backend/src/test/java/pdl/backend/BackendApplicationTests.java b/backend/src/test/java/pdl/backend/BackendApplicationTests.java index cb692e0..0984f6e 100644 --- a/backend/src/test/java/pdl/backend/BackendApplicationTests.java +++ b/backend/src/test/java/pdl/backend/BackendApplicationTests.java @@ -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 { - } + } } diff --git a/backend/src/test/java/pdl/backend/ImageControllerTests.java b/backend/src/test/java/pdl/backend/ImageControllerTests.java index c9b2ed0..5540a97 100644 --- a/backend/src/test/java/pdl/backend/ImageControllerTests.java +++ b/backend/src/test/java/pdl/backend/ImageControllerTests.java @@ -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()); + } }