From 889044a846fb201aa036b99948ae53fff02a0551 Mon Sep 17 00:00:00 2001 From: aplantier Date: Fri, 11 Feb 2022 11:39:53 +0100 Subject: [PATCH] Create question functionality --- .../Model/Controllers/QuestionController.java | 32 +++++++++++--- src/main/java/Model/Documents/Question.java | 39 ++++++++--------- .../request/CreateQuestionRequest.java | 43 ++++++++++++++++--- .../request/CreateTextQuestionRequest.java | 17 -------- .../Repositories/QuestionRepository.java | 2 + src/test/java/QuestionnaireTest.java | 28 +++++++++--- src/test/java/SpringIntegration.java | 16 +++++++ .../resources/features/Questionnaire.feature | 5 +-- 8 files changed, 120 insertions(+), 62 deletions(-) delete mode 100644 src/main/java/Model/Payload/request/CreateTextQuestionRequest.java diff --git a/src/main/java/Model/Controllers/QuestionController.java b/src/main/java/Model/Controllers/QuestionController.java index 700b26e..0f060e4 100644 --- a/src/main/java/Model/Controllers/QuestionController.java +++ b/src/main/java/Model/Controllers/QuestionController.java @@ -3,8 +3,6 @@ import Model.Documents.*; import Model.Payload.request.CreateQuestionRequest; -import Model.Payload.request.CreateTextQuestionRequest; -import Model.Payload.request.SignupRequest; import Model.Payload.response.MessageResponse; import Model.Repositories.QuestionRepository; import Model.Repositories.ReponsesRepository; @@ -16,7 +14,6 @@ import javax.validation.Valid; import java.security.Principal; -import java.util.List; import java.util.Optional; import java.util.Set; @@ -40,16 +37,37 @@ public ResponseEntity addRessource(String reponse, @PathVariable String name, .badRequest() .body(new MessageResponse("Error: No such ressource!")); } - @PutMapping("/create") + + /** + * Method for a teacher to create a question + * @param createQuestionRequest_a + * @return + */ + @PostMapping("/create") + @PreAuthorize("hasRole('TEACHER')") public ResponseEntity createQuestion(@Valid @RequestBody CreateQuestionRequest createQuestionRequest_a){ - Question questionToAdd ; + Question questionToAdd = null ; + + Optional oquestion = questionRepository.findByEnonce(createQuestionRequest_a.getEnonce()); + if (oquestion.isPresent()) { + return ResponseEntity + .badRequest() + .body(new MessageResponse("Question Already exist")); + } switch (createQuestionRequest_a.getQuestionType()){ case "text": - questionToAdd= new Question(createQuestionRequest_a.getEnonce(),((CreateTextQuestionRequest) createQuestionRequest_a).getReponse()); + questionToAdd= new Question(createQuestionRequest_a.getEnonce(),createQuestionRequest_a.getReponse()); break; + default: + } - return ResponseEntity + if (questionToAdd != null ) { + questionRepository.save(questionToAdd); + return ResponseEntity.ok(new MessageResponse("Question successfully ")); + } + else + return ResponseEntity .badRequest() .body(new MessageResponse("Error: No such ressource!")); diff --git a/src/main/java/Model/Documents/Question.java b/src/main/java/Model/Documents/Question.java index 6f716b2..52f8b2c 100644 --- a/src/main/java/Model/Documents/Question.java +++ b/src/main/java/Model/Documents/Question.java @@ -1,10 +1,10 @@ package Model.Documents; +import org.hibernate.validator.constraints.Range; + import javax.persistence.*; import javax.validation.constraints.NotBlank; import javax.validation.constraints.Size; -import java.lang.reflect.Array; -import java.util.ArrayList; import java.util.HashSet; import java.util.Set; @@ -16,26 +16,21 @@ @Table( name = "Question") public class Question { @NotBlank - public String enonce_; + public String enonce=""; @Id @GeneratedValue(strategy = GenerationType.IDENTITY) public Long id; - @NotBlank @Size(max = 120) - public String listeEnonces_; + public String listeEnonces_=""; - @NotBlank - @Size(max = 120) - public String typeQuestion; - @NotBlank - @Size(max = 120) - public int [] reponsesMultiples; - @NotBlank @Size(max = 120) - public int reponseQcm; - @NotBlank + public String typeQuestion=""; +/* @Range(min=1, max=20) + public int [] reponsesMultiples={};*/ + @Range(min=0, max=20) + public int reponseQcm=0; @Size(max = 120) - public String reponseText; + public String reponseText=""; public Question() {} @@ -53,8 +48,8 @@ public Question() {} public Question(String enonce_, String listrReponses_a, int reponse_a) { this.listeEnonces_ = listrReponses_a; this.typeQuestion="qcm"; - this.enonce_ = enonce_; - this.reponseQcm = reponseQcm; + this.enonce = enonce_; + this.reponseQcm =reponse_a; } public boolean reponse(Reponse reponse){ @@ -63,13 +58,13 @@ public boolean reponse(Reponse reponse){ return reponse.reponseQcm == this.reponseQcm; case "choixmult": - if ( reponse.reponsesMultiples.length != reponse.reponsesMultiples.length) + /* if ( reponse.reponsesMultiples.length != reponse.reponsesMultiples.length) return false ; for (int i = 0 ; i < reponse.reponsesMultiples.length; i++){ if(reponse.reponsesMultiples[i] == this.reponsesMultiples[i]) { return false; } - } + }*/ return true; case "text": if(reponse.reponseText.equals(this.reponseText)) @@ -87,8 +82,8 @@ public boolean reponse(Reponse reponse){ public Question(String enonce_a, String listrReponses_a, int [] reponse_a) { this.typeQuestion="choixmult"; this.listeEnonces_ = listrReponses_a; - this.enonce_ = enonce_a; - this.reponsesMultiples = reponse_a; + this.enonce = enonce_a; + //this.reponsesMultiples = reponse_a; } @@ -99,7 +94,7 @@ public Question(String enonce_a, String listrReponses_a, int [] reponse_a) { */ public Question(String enonce_a, String reponse_a){ this.typeQuestion="text"; - this.enonce_=enonce_a; + this.enonce =enonce_a; this.reponseText=reponse_a; } } diff --git a/src/main/java/Model/Payload/request/CreateQuestionRequest.java b/src/main/java/Model/Payload/request/CreateQuestionRequest.java index 3a9c4a4..092a07d 100644 --- a/src/main/java/Model/Payload/request/CreateQuestionRequest.java +++ b/src/main/java/Model/Payload/request/CreateQuestionRequest.java @@ -1,25 +1,56 @@ package Model.Payload.request; -import javax.validation.constraints.Email; +import org.hibernate.validator.constraints.Range; + import javax.validation.constraints.NotBlank; import javax.validation.constraints.Size; -import java.util.Set; public class CreateQuestionRequest { @NotBlank @Size(min = 3, max = 20) - private String enonce_; + private String enonce; @NotBlank + @Size(min = 0, max = 20) + String questionType; + @Size(min = 3, max = 20) - private String questionType_; + private String reponse; + + @Size(max = 120) + public String listeEnonces_=""; + + @Size(max = 120) + public String typeQuestion=""; + /* @Range(min=1, max=20) + public int [] reponsesMultiples={};*/ + @Range(min=0, max=20) + public int reponseQcm=0; + @Size(max = 120) + public String reponseText=""; + + + public String getReponse(){ + return this.reponse; + } + public void setReponse(String reponseText){this.reponse=reponseText;} + public CreateQuestionRequest(){ + this.enonce="[DEFAULT ENONCE]"; + this.questionType="[DEFAULT QUESTION TYPE]"; + } public String getQuestionType(){ - return this.questionType_; + return this.questionType; } + public void setQuestionType(String questionType_a){ + this.questionType = questionType_a; + } public String getEnonce(){ - return this.enonce_; + return this.enonce; + } + public void setEnonce(String enonce_a){ + this.enonce=enonce_a; } diff --git a/src/main/java/Model/Payload/request/CreateTextQuestionRequest.java b/src/main/java/Model/Payload/request/CreateTextQuestionRequest.java deleted file mode 100644 index db9dc6b..0000000 --- a/src/main/java/Model/Payload/request/CreateTextQuestionRequest.java +++ /dev/null @@ -1,17 +0,0 @@ -package Model.Payload.request; - -import javax.validation.constraints.NotBlank; -import javax.validation.constraints.Size; - -public class CreateTextQuestionRequest extends CreateQuestionRequest{ - - @NotBlank - @Size(min = 3, max = 20) - private String reponse_; - - public String getReponse(){ - return this.reponse_; - } - - -} diff --git a/src/main/java/Model/Repositories/QuestionRepository.java b/src/main/java/Model/Repositories/QuestionRepository.java index 26442f0..f47592d 100644 --- a/src/main/java/Model/Repositories/QuestionRepository.java +++ b/src/main/java/Model/Repositories/QuestionRepository.java @@ -11,4 +11,6 @@ public interface QuestionRepository extends JpaRepository { Optional findById(Long name); + + Optional findByEnonce(String enonce_); } diff --git a/src/test/java/QuestionnaireTest.java b/src/test/java/QuestionnaireTest.java index 7d88b5c..55d8cc4 100644 --- a/src/test/java/QuestionnaireTest.java +++ b/src/test/java/QuestionnaireTest.java @@ -1,20 +1,18 @@ import Model.Controllers.AuthController; -import Model.Documents.Cours; import Model.Documents.Module; import Model.Documents.Questionnaire; -import Model.Repositories.ModuleRepository; -import Model.Repositories.QuestionnaireRepository; -import Model.Repositories.RessourcesRepository; -import Model.Repositories.UserRepository; +import Model.Payload.request.CreateQuestionRequest; +import Model.Repositories.*; import io.cucumber.java.en.And; import io.cucumber.java.en.Given; import io.cucumber.java.en.Then; import io.cucumber.java.en.When; +import io.cucumber.messages.internal.com.google.gson.Gson; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.crypto.password.PasswordEncoder; -import org.springframework.transaction.annotation.Transactional; import java.io.IOException; +import java.io.UnsupportedEncodingException; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -33,6 +31,9 @@ public class QuestionnaireTest extends SpringIntegration { @Autowired QuestionnaireRepository questionnaireRepository; + @Autowired + QuestionRepository questionRepository; + @Autowired AuthController authController; @@ -86,7 +87,20 @@ public void questionnairExist(String courseName){ assertTrue(questionnaireRepository.findByName(courseName).isPresent()); } @When("user {string} create {string} question with enonce {string} and with answer {string}") - public void userCreateTextQuestion(String user_a, String questionType, String enonce_a , String answer_a){ + public void userCreateTextQuestion(String user_a, String questionType, String enonce_a , String answer_a) throws UnsupportedEncodingException { + CreateQuestionRequest textQuestionRequest = new CreateQuestionRequest(); + textQuestionRequest.setEnonce(enonce_a); + textQuestionRequest.setReponse(answer_a); + textQuestionRequest.setQuestionType("text"); + Gson g = new Gson(); + String s = g.toJson(textQuestionRequest); + String jwt = authController.generateJwt(user_a, PASSWORD); + executePostObj("http://localhost:8080/api/module/question/create",s,jwt); + System.out.println(latestHttpResponse.getStatusLine().getStatusCode()); } + @Then("Question with enonce {string} exist") + public void questionExistByEnone(String enonce){ + assertTrue(questionRepository.findByEnonce(enonce).isPresent()); + } } diff --git a/src/test/java/SpringIntegration.java b/src/test/java/SpringIntegration.java index ca4babc..08b6bab 100644 --- a/src/test/java/SpringIntegration.java +++ b/src/test/java/SpringIntegration.java @@ -88,6 +88,22 @@ boolean executePostObj(String url, String obj) throws UnsupportedEncodingExcepti System.out.println(latestHttpResponse); return true; } + boolean executePostObj(String url, String obj, String jwt) throws UnsupportedEncodingException { + HttpPost request = new HttpPost(url); + request.addHeader("content-type", "application/json"); + request.setEntity(new StringEntity(obj)); + if (jwt != null) { + request.addHeader("Authorization", "Bearer " + jwt); + } + try{ + latestHttpResponse = httpClient.execute(request); + }catch(Throwable t){ + System.out.println(t.getLocalizedMessage()); + return false; + } + System.out.println(latestHttpResponse); + return true; + } boolean executePost(String url, String jwt) throws IOException { HttpPost request = new HttpPost(url); diff --git a/src/test/resources/features/Questionnaire.feature b/src/test/resources/features/Questionnaire.feature index 5367bdf..03da8ba 100644 --- a/src/test/resources/features/Questionnaire.feature +++ b/src/test/resources/features/Questionnaire.feature @@ -11,6 +11,5 @@ Feature: Questionnaire And Questionnaire "Quest1" has been added Scenario: Teacher add text question to Questionnaire - When "steve" create "text" question with enonce "Enonce1" and with possible answers "answ1" - And "steve" add "text" question to questionnaire "Quest1" - Then Questionnaire "Quest1" contain the question with Enonce : "Enonce1" \ No newline at end of file + When user "steve" create "text" question with enonce "Enonce1" and with answer "answ1" + Then Question with enonce "Enonce1" exist \ No newline at end of file