From 808ccbc48058c018ccf43135f0c64119b0720eab Mon Sep 17 00:00:00 2001 From: unknown Date: Tue, 27 Feb 2024 16:01:33 +0100 Subject: [PATCH] manque tests --- .../controller/CollectController.java | 47 +++++++++++++------ .../controller/TeacherController.java | 7 ++- .../WebService/exception/CollectAdvice.java | 17 +++++++ .../exception/CollectException.java | 7 +++ .../SAE/ApiREST/WebService/model/Collect.java | 18 ++++--- .../SAE/ApiREST/WebService/model/Teacher.java | 2 +- .../service/ICollectionService.java | 8 ++-- .../service/StubCollectionService.java | 34 ++++++++------ 8 files changed, 96 insertions(+), 44 deletions(-) create mode 100644 WebService/src/main/java/SAE/ApiREST/WebService/exception/CollectAdvice.java create mode 100644 WebService/src/main/java/SAE/ApiREST/WebService/exception/CollectException.java diff --git a/WebService/src/main/java/SAE/ApiREST/WebService/controller/CollectController.java b/WebService/src/main/java/SAE/ApiREST/WebService/controller/CollectController.java index c42c182..6ffeac5 100644 --- a/WebService/src/main/java/SAE/ApiREST/WebService/controller/CollectController.java +++ b/WebService/src/main/java/SAE/ApiREST/WebService/controller/CollectController.java @@ -1,5 +1,7 @@ package SAE.ApiREST.WebService.controller; +import SAE.ApiREST.WebService.exception.ArticleException; +import SAE.ApiREST.WebService.exception.CollectException; import SAE.ApiREST.WebService.model.Article; import SAE.ApiREST.WebService.model.Collect; import SAE.ApiREST.WebService.service.ICollectionService; @@ -25,22 +27,34 @@ public class CollectController { // region GET @GetMapping(value = "/getAllCollection", produces = MediaType.APPLICATION_JSON_VALUE) public @ResponseBody List getAllCollection(){ - return collectionService.getAllCollections(); + List results = collectionService.getAllCollections(); + if(results.isEmpty()) { + throw new CollectException("No collections available"); + } + return results; } - @GetMapping(value = "/getCollectionById/{isbn}", produces = MediaType.APPLICATION_JSON_VALUE) - public @ResponseBody Collect getCollectionById(@PathVariable(value = "isbn") long isbn){ - return collectionService.getCollectionById(isbn); + @GetMapping(value = "/getCollectionById/{id}", produces = MediaType.APPLICATION_JSON_VALUE) + public @ResponseBody Collect getCollectionById(@PathVariable(value = "id") Integer id){ + Collect results = collectionService.getCollectionById(id); + if(results == null) { + throw new CollectException("No collections available"); + } + return results; } @GetMapping(value = "/getAllCollectionsByName/{name}", produces = MediaType.APPLICATION_JSON_VALUE) public @ResponseBody List getAllCollectionsByName(@PathVariable(value = "name") String name){ - return collectionService.getAllCollectionsByName(name); + List results = collectionService.getAllCollectionsByName(name); + if(results.isEmpty()) { + throw new CollectException("No collections available"); + } + return results; } // endregion // region DELETE - @DeleteMapping(value = "/deleteColletionById/{isbn}") - public @ResponseBody void deleteColletionById(@RequestParam("isbn") long isbn){ - collectionService.deleteColletionById(isbn); + @DeleteMapping(value = "/deleteColletionById/{id}") + public @ResponseBody void deleteColletionById(@RequestParam("id") Integer id){ + collectionService.deleteColletionById(id); } @DeleteMapping(value = "/deleteColletionByName/{name}") @@ -72,17 +86,22 @@ public class CollectController { collectionService.modifyCollectionName(collection,name); } - @PostMapping(value="/modifyCollectionNameById", produces = MediaType.APPLICATION_JSON_VALUE) - public @ResponseBody void modifyCollectionNameById(@RequestParam("isbn") long isbn, @RequestParam("name") String name){ - collectionService.modifyCollectionNameById(isbn,name); + @PostMapping(value="/modifyCollectionNameById/{id}", produces = MediaType.APPLICATION_JSON_VALUE) + public @ResponseBody void modifyCollectionNameById(@RequestParam("id") Integer id, @RequestParam("name") String name){ + collectionService.modifyCollectionNameById(id,name); } // endregion // endregion // region Article - @GetMapping(value = "/getAllArticles", produces = MediaType.APPLICATION_JSON_VALUE) - public @ResponseBody List
getAllArticles(@RequestParam("collection") Collect collection){ - return collectionService.getAllArticles(collection); + @GetMapping(value = "/getAllArticlesById/{id}", produces = MediaType.APPLICATION_JSON_VALUE) + public @ResponseBody List
getAllArticlesById(@PathVariable(value = "id") Integer id){ + List
results = collectionService.getAllArticlesById(id); + if(results == null) { + throw new ArticleException("No articles available"); + } + + return results; } @PutMapping(value = "/addArticle", produces = MediaType.APPLICATION_JSON_VALUE) diff --git a/WebService/src/main/java/SAE/ApiREST/WebService/controller/TeacherController.java b/WebService/src/main/java/SAE/ApiREST/WebService/controller/TeacherController.java index 5d44e1b..95ac917 100644 --- a/WebService/src/main/java/SAE/ApiREST/WebService/controller/TeacherController.java +++ b/WebService/src/main/java/SAE/ApiREST/WebService/controller/TeacherController.java @@ -19,19 +19,18 @@ public class TeacherController { private ITeacherService iTeacherServ; - public TeacherController(ITeacherService iserv) { - this.iTeacherServ = iserv; + public TeacherController() { } @GetMapping(value = "/all", produces = MediaType.APPLICATION_JSON_VALUE) @ResponseStatus(HttpStatus.OK) - public List getAllTeacher(){ + public @ResponseBody List getAllTeacher(){ return iTeacherServ.getAllTeacher(); } @PostMapping(produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE) @ResponseStatus(HttpStatus.CREATED) - public Teacher createTeacher( @RequestBody Teacher teach){ + public @ResponseBody Teacher createTeacher( @RequestBody Teacher teach){ return teach; } diff --git a/WebService/src/main/java/SAE/ApiREST/WebService/exception/CollectAdvice.java b/WebService/src/main/java/SAE/ApiREST/WebService/exception/CollectAdvice.java new file mode 100644 index 0000000..57b222d --- /dev/null +++ b/WebService/src/main/java/SAE/ApiREST/WebService/exception/CollectAdvice.java @@ -0,0 +1,17 @@ +package SAE.ApiREST.WebService.exception; + +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.ControllerAdvice; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.bind.annotation.ResponseStatus; + +@ControllerAdvice +public class CollectAdvice { + @ResponseBody + @ExceptionHandler(CollectException.class) + @ResponseStatus(HttpStatus.NOT_FOUND) + String collectHandler(CollectException ex) { + return ex.getMessage(); + } +} \ No newline at end of file diff --git a/WebService/src/main/java/SAE/ApiREST/WebService/exception/CollectException.java b/WebService/src/main/java/SAE/ApiREST/WebService/exception/CollectException.java new file mode 100644 index 0000000..d489715 --- /dev/null +++ b/WebService/src/main/java/SAE/ApiREST/WebService/exception/CollectException.java @@ -0,0 +1,7 @@ +package SAE.ApiREST.WebService.exception; + +public class CollectException extends RuntimeException { + public CollectException(String exception) { + super(exception); + } +} diff --git a/WebService/src/main/java/SAE/ApiREST/WebService/model/Collect.java b/WebService/src/main/java/SAE/ApiREST/WebService/model/Collect.java index 2c8e85e..161a4c2 100644 --- a/WebService/src/main/java/SAE/ApiREST/WebService/model/Collect.java +++ b/WebService/src/main/java/SAE/ApiREST/WebService/model/Collect.java @@ -7,8 +7,8 @@ import java.util.List; @Entity public class Collect { @Id - @GeneratedValue(strategy= GenerationType.AUTO) - private final long isbn = 0; + @GeneratedValue(strategy=GenerationType.AUTO) + private Integer id; @Column(name = "articles") private ArrayList
articles; @Column(name = "name") @@ -16,18 +16,24 @@ public class Collect { @Column(name = "teacher") private Teacher teacher; + public Collect() { + } + public Collect(String name, Teacher teacher){ this.name = name; this.teacher = teacher; this.articles = new ArrayList
(); } - - public Collect() { + public Collect(String name, Teacher teacher, Integer id){ + this.name = name; + this.teacher = teacher; + this.articles = new ArrayList
(); + this.id = id; } // region Article - public long getId(){ - return isbn; + public Integer getId(){ + return id; } // endregion diff --git a/WebService/src/main/java/SAE/ApiREST/WebService/model/Teacher.java b/WebService/src/main/java/SAE/ApiREST/WebService/model/Teacher.java index a121920..ce4cd66 100644 --- a/WebService/src/main/java/SAE/ApiREST/WebService/model/Teacher.java +++ b/WebService/src/main/java/SAE/ApiREST/WebService/model/Teacher.java @@ -21,7 +21,7 @@ public class Teacher { public Teacher(Integer id, String date, String mail, String username) { this.id = id; - this.date = LocalDate.parse(date, DateTimeFormatter.ISO_DATE); + this.date = LocalDate.parse(date, DateTimeFormatter.ofPattern("dd-MM-yyyy")); this.mail = mail; this.username = username; } diff --git a/WebService/src/main/java/SAE/ApiREST/WebService/service/ICollectionService.java b/WebService/src/main/java/SAE/ApiREST/WebService/service/ICollectionService.java index 8e3cec6..e2a0813 100644 --- a/WebService/src/main/java/SAE/ApiREST/WebService/service/ICollectionService.java +++ b/WebService/src/main/java/SAE/ApiREST/WebService/service/ICollectionService.java @@ -6,16 +6,16 @@ import java.util.List; public interface ICollectionService{ public List getAllCollections(); - public Collect getCollectionById(long isbn); + public Collect getCollectionById(Integer id); public List getAllCollectionsByName(String name); - public void deleteColletionById(long isbn); + public void deleteColletionById(Integer id); public void deleteColletionByName(String name); public void deleteAllColletionByName(String name); public void addCollection(Collect collection); public void addCollections(List collection); public void modifyCollectionName(Collect collection, String name); - public void modifyCollectionNameById(long isbn, String name); - public List
getAllArticles(Collect collection); + public void modifyCollectionNameById(Integer id, String name); + public List
getAllArticlesById(Integer id); public void addArticle(Collect collection, Article article); public void deleteArticle(Collect collection, Article article); } \ No newline at end of file diff --git a/WebService/src/main/java/SAE/ApiREST/WebService/service/StubCollectionService.java b/WebService/src/main/java/SAE/ApiREST/WebService/service/StubCollectionService.java index 42b6801..8c8feed 100644 --- a/WebService/src/main/java/SAE/ApiREST/WebService/service/StubCollectionService.java +++ b/WebService/src/main/java/SAE/ApiREST/WebService/service/StubCollectionService.java @@ -18,13 +18,13 @@ public class StubCollectionService implements ICollectionService { public List getAllCollections() { List collections = new ArrayList<>(); - collections.add(new Collect("collect1", new Teacher(1, "12-01-2023", "aline.alipres@gmail.com", "MsGarconManque"))); + collections.add(new Collect("collect1", new Teacher(1, "12-03-2023", "aline.alipres@gmail.com", "MsGarconManque"),0)); - Collect collection2 = new Collect("collect2", new Teacher(1, "12-01-2023", "aline.alipres@gmail.com", "MsGarconManque")); + Collect collection2 = new Collect("collect2", new Teacher(1, "12-03-2023", "aline.alipres@gmail.com", "MsGarconManque"),1); collection2.addArticle(new Article("toi","azezeaea", LocalDate.now().minusMonths(1),LocalDate.now().minusMonths(2),true,1)); collections.add(collection2); - Collect collection3 = new Collect("collect3", new Teacher(1, "12-01-2023", "aline.alipres@gmail.com", "MsGarconManque")); + Collect collection3 = new Collect("collect3", new Teacher(1, "12-03-2023", "aline.alipres@gmail.com", "MsGarconManque"),3); collection3.addArticle(new Article("toi","azezeaea",LocalDate.now().minusMonths(1),LocalDate.now().minusMonths(2),true,1)); collection3.addArticle(new Article("toi","azezeaea",LocalDate.now().minusMonths(1),LocalDate.now().minusMonths(2),true,1)); collections.add(collection3); @@ -36,14 +36,13 @@ public class StubCollectionService implements ICollectionService { // region GET @Override - public Collect getCollectionById(long isbn){ + public Collect getCollectionById(Integer id){ List collections = getAllCollections(); - for(Collect collection : collections){ - if(collection.getId() == isbn) { - return collection; - } + try { + return collections.get(id); + }catch (Exception e){ + return null; } - return null; } @Override @@ -61,9 +60,9 @@ public class StubCollectionService implements ICollectionService { // region DELETE @Override - public void deleteColletionById(long isbn){ + public void deleteColletionById(Integer id){ List collections = getAllCollections(); - Collect collection = getCollectionById(isbn); + Collect collection = getCollectionById(id); collections.remove(collection); } @@ -103,8 +102,8 @@ public class StubCollectionService implements ICollectionService { } @Override - public void modifyCollectionNameById(long isbn, String name){ - Collect collection = getCollectionById(isbn); + public void modifyCollectionNameById(Integer id, String name){ + Collect collection = getCollectionById(id); modifyCollectionName(collection,name); } // endregion @@ -112,8 +111,13 @@ public class StubCollectionService implements ICollectionService { // region Article @Override - public List
getAllArticles(Collect collect){ - return collect.getAllArticles(); + public List
getAllArticlesById(Integer id){ + List
result = new ArrayList<>(); + Collect collect = getCollectionById(id); + if (result.addAll(collect.getAllArticles())){ + return result; + } + return null; } @Override public void addArticle(Collect collect, Article article){