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/main/java/pdl/backend/ImageDao.java b/backend/src/main/java/pdl/backend/ImageDao.java index e825245..32acdc9 100644 --- a/backend/src/main/java/pdl/backend/ImageDao.java +++ b/backend/src/main/java/pdl/backend/ImageDao.java @@ -16,68 +16,70 @@ import org.springframework.stereotype.Repository; @Repository public class ImageDao implements Dao { - private final Map images = new HashMap<>(); + private final Map images = new HashMap<>(); - public ImageDao() { - try { - final var folderPath = new ClassPathResource("images"); - final var folder = folderPath.getFile(); - getAllImages(folder); - } catch (final Exception e) { - e.printStackTrace(); - } - } + public ImageDao() { + try { + final var folderPath = new ClassPathResource("images"); + if (!folderPath.exists()) + throw new ImageResourceMissingException("folder images doesn't not exsit"); + final var folder = folderPath.getFile(); + getAllImages(folder); + } catch (final Exception e) { + e.printStackTrace(); + } + } - public void getAllImages(final File folder) { - for (final File file : folder.listFiles()) { - if (file.isDirectory()) { - getAllImages(file); - } else { - String extension = ""; + public void getAllImages(final File folder) { + for (final File file : folder.listFiles()) { + if (file.isDirectory()) { + getAllImages(file); + } else { + String extension = ""; - int i = file.getName().lastIndexOf('.'); - if (i > 0) { - extension = file.getName().substring(i + 1); - } - if (!extension.equals("jpg") && !extension.equals("png")) { - continue; - } - byte[] fileContent; - try { - fileContent = Files.readAllBytes(file.toPath()); - Image img = new Image(file.getName(), fileContent); - images.put(img.getId(), img); - } catch (final IOException e) { - e.printStackTrace(); - } - } - } - } + int i = file.getName().lastIndexOf('.'); + if (i > 0) { + extension = file.getName().substring(i + 1); + } + if (!extension.equals("jpg") && !extension.equals("png")) { + continue; + } + byte[] fileContent; + try { + fileContent = Files.readAllBytes(file.toPath()); + Image img = new Image(file.getName(), fileContent); + images.put(img.getId(), img); + } catch (final IOException e) { + e.printStackTrace(); + } + } + } + } - @Override - public Optional retrieve(final long id) { - return Optional.ofNullable(images.get(id)); - } + @Override + public Optional retrieve(final long id) { + return Optional.ofNullable(images.get(id)); + } - @Override - public List retrieveAll() { - return new ArrayList(images.values()); - } + @Override + public List retrieveAll() { + return new ArrayList(images.values()); + } - @Override - public void create(final Image img) { - images.put(img.getId(), img); - } + @Override + public void create(final Image img) { + images.put(img.getId(), img); + } - @Override - public void update(final Image img, final String[] params) { - img.setName(Objects.requireNonNull(params[0], "Name cannot be null")); + @Override + public void update(final Image img, final String[] params) { + img.setName(Objects.requireNonNull(params[0], "Name cannot be null")); - images.put(img.getId(), img); - } + images.put(img.getId(), img); + } - @Override - public void delete(final Image img) { - images.remove(img.getId()); - } + @Override + public void delete(final Image img) { + images.remove(img.getId()); + } } diff --git a/backend/src/main/java/pdl/backend/ImageResourceMissingException.java b/backend/src/main/java/pdl/backend/ImageResourceMissingException.java new file mode 100644 index 0000000..fe78b22 --- /dev/null +++ b/backend/src/main/java/pdl/backend/ImageResourceMissingException.java @@ -0,0 +1,9 @@ +package pdl.backend; + +import java.io.FileNotFoundException; + +public class ImageResourceMissingException extends FileNotFoundException { + public ImageResourceMissingException(String message) { + super(message); + } +} 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()); + } }