ajout correction tp1-2
This commit is contained in:
1 parent
902d02f871
commit
9cce1dd794
34 files changed
+1373
No files matched your search
@@ -0,0 +1,13 @@
|
||||
package pdl.backend;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class BackendApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(BackendApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package pdl.backend;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.List;
|
||||
|
||||
public interface Dao<T> {
|
||||
|
||||
void create(final T t);
|
||||
|
||||
Optional<T> retrieve(final long id);
|
||||
|
||||
List<T> retrieveAll();
|
||||
|
||||
void update(final T t, final String[] params);
|
||||
|
||||
void delete(final T t);
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package pdl.backend;
|
||||
|
||||
public class Image {
|
||||
private static Long count = Long.valueOf(0);
|
||||
private Long id;
|
||||
private String name;
|
||||
private byte[] data;
|
||||
|
||||
public Image(final String name, final byte[] data) {
|
||||
id = count++;
|
||||
this.name = name;
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(final String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public byte[] getData() {
|
||||
return data;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
package pdl.backend;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ArrayNode;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.core.io.InputStreamResource;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
|
||||
|
||||
@RestController
|
||||
public class ImageController {
|
||||
|
||||
@Autowired
|
||||
private ObjectMapper mapper;
|
||||
|
||||
private final ImageDao imageDao;
|
||||
|
||||
@Autowired
|
||||
public ImageController(ImageDao imageDao) {
|
||||
this.imageDao = imageDao;
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/images/{id}", method = RequestMethod.GET, produces = MediaType.IMAGE_JPEG_VALUE)
|
||||
public ResponseEntity<?> getImage(@PathVariable("id") long 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);
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/images/{id}", method = RequestMethod.DELETE)
|
||||
public ResponseEntity<?> deleteImage(@PathVariable("id") long 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);
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/images", method = RequestMethod.POST)
|
||||
public ResponseEntity<?> addImage(@RequestParam("file") MultipartFile file, RedirectAttributes redirectAttributes) {
|
||||
|
||||
String contentType = file.getContentType();
|
||||
if (!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.OK);
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/images", method = RequestMethod.GET, 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());
|
||||
nodes.add(objectNode);
|
||||
}
|
||||
return nodes;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
package pdl.backend;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public class ImageDao implements Dao<Image> {
|
||||
|
||||
private final Map<Long, Image> images = new HashMap<>();
|
||||
|
||||
public ImageDao() {
|
||||
final ClassPathResource imgFile = new ClassPathResource("test.jpg");
|
||||
byte[] fileContent;
|
||||
try {
|
||||
fileContent = Files.readAllBytes(imgFile.getFile().toPath());
|
||||
Image img = new Image("test.jpg", fileContent);
|
||||
images.put(img.getId(), img);
|
||||
} catch (final IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<Image> retrieve(final long id) {
|
||||
return Optional.ofNullable(images.get(id));
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Image> retrieveAll() {
|
||||
return new ArrayList<Image>(images.values());
|
||||
}
|
||||
|
||||
@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"));
|
||||
|
||||
images.put(img.getId(), img);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(final Image img) {
|
||||
images.remove(img.getId());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 13 KiB |
@@ -0,0 +1,13 @@
|
||||
package pdl.backend;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
@SpringBootTest
|
||||
class BackendApplicationTests {
|
||||
|
||||
@Test
|
||||
void contextLoads() {
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
package pdl.backend;
|
||||
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
|
||||
|
||||
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Order;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.TestMethodOrder;
|
||||
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.core.io.ClassPathResource;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.mock.web.MockMultipartFile;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
|
||||
|
||||
@SpringBootTest
|
||||
@AutoConfigureMockMvc
|
||||
@TestMethodOrder(OrderAnnotation.class)
|
||||
public class ImageControllerTests {
|
||||
|
||||
@Autowired
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@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(2)
|
||||
public void getImageShouldReturnNotFound() throws Exception {
|
||||
this.mockMvc.perform(get("/images/1")).andDo(print()).andExpect(status().isNotFound());
|
||||
}
|
||||
|
||||
@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(5)
|
||||
public void deleteImageShouldReturnNotFound() throws Exception {
|
||||
this.mockMvc.perform(delete("/images/1")).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(7)
|
||||
public void createImageShouldReturnSuccess() throws Exception {
|
||||
final ClassPathResource imgFile = new ClassPathResource("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().isOk());
|
||||
}
|
||||
|
||||
@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