diff --git a/src/main/java/com/opinionowl/opinionowl/models/Answer.java b/src/main/java/com/opinionowl/opinionowl/models/Answer.java index 2aff86e..871346c 100644 --- a/src/main/java/com/opinionowl/opinionowl/models/Answer.java +++ b/src/main/java/com/opinionowl/opinionowl/models/Answer.java @@ -32,7 +32,7 @@ public class Answer { /** * The constructor for answer. - * @param response the response for which the answe belongs to. + * @param response the response for which the answer belongs to. * @param question the question id. * @param content the content of the reply. */ diff --git a/src/main/java/com/opinionowl/opinionowl/models/LongAnswerQuestion.java b/src/main/java/com/opinionowl/opinionowl/models/LongAnswerQuestion.java index a5621df..defa4bf 100644 --- a/src/main/java/com/opinionowl/opinionowl/models/LongAnswerQuestion.java +++ b/src/main/java/com/opinionowl/opinionowl/models/LongAnswerQuestion.java @@ -51,7 +51,7 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; LongAnswerQuestion that = (LongAnswerQuestion) o; - return this.getId() == that.getId(); + return Objects.equals(this.getId(), that.getId()); } } diff --git a/src/main/java/com/opinionowl/opinionowl/models/Question.java b/src/main/java/com/opinionowl/opinionowl/models/Question.java index 55cb680..11801ae 100644 --- a/src/main/java/com/opinionowl/opinionowl/models/Question.java +++ b/src/main/java/com/opinionowl/opinionowl/models/Question.java @@ -5,6 +5,8 @@ import lombok.Setter; import lombok.NoArgsConstructor; +import java.util.Objects; + /** * The Question class which keeps track of the survey questions. */ @@ -59,4 +61,16 @@ public boolean equals(Question comparedQuestion){ } return true; } + + /** + * @param o object that is being compared with + * @return boolean value saying whether objects are equal + */ + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Question question = (Question) o; + return Objects.equals(id, question.id) && Objects.equals(prompt, question.prompt) && type == question.type && Objects.equals(survey, question.survey); + } } diff --git a/src/main/java/com/opinionowl/opinionowl/models/RadioChoiceQuestion.java b/src/main/java/com/opinionowl/opinionowl/models/RadioChoiceQuestion.java index 7fb88ae..b2e82e9 100644 --- a/src/main/java/com/opinionowl/opinionowl/models/RadioChoiceQuestion.java +++ b/src/main/java/com/opinionowl/opinionowl/models/RadioChoiceQuestion.java @@ -4,7 +4,7 @@ import lombok.Getter; import lombok.Setter; -import java.util.Arrays; +import java.util.Objects; /** * The class used to describe the multiple choice questions. @@ -50,7 +50,6 @@ public String toString(){ } /** - * * @param o object that is being compared with * @return boolean value saying whether objects are equal */ @@ -59,7 +58,7 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; RadioChoiceQuestion that = (RadioChoiceQuestion) o; - return this.getId() == that.getId(); + return Objects.equals(this.getId(), that.getId()); } } diff --git a/src/main/java/com/opinionowl/opinionowl/models/RangeQuestion.java b/src/main/java/com/opinionowl/opinionowl/models/RangeQuestion.java index 6e8806f..809ffe7 100644 --- a/src/main/java/com/opinionowl/opinionowl/models/RangeQuestion.java +++ b/src/main/java/com/opinionowl/opinionowl/models/RangeQuestion.java @@ -71,7 +71,7 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; RangeQuestion that = (RangeQuestion) o; - return this.getId() == that.getId(); + return Objects.equals(this.getId(), that.getId()); } } diff --git a/src/main/java/com/opinionowl/opinionowl/models/Survey.java b/src/main/java/com/opinionowl/opinionowl/models/Survey.java index e9c500e..9507763 100644 --- a/src/main/java/com/opinionowl/opinionowl/models/Survey.java +++ b/src/main/java/com/opinionowl/opinionowl/models/Survey.java @@ -71,7 +71,7 @@ public boolean addQuestion(Question question){ */ public boolean removeQuestion(Long questionId){ if (!closed) { - return this.questions.removeIf(q -> q.getId() == questionId); + return this.questions.removeIf(q -> Objects.equals(q.getId(), questionId)); } return false; } @@ -94,7 +94,7 @@ public boolean addResponse(Response response){ * @return true if successfully remove, false otherwise. */ public boolean removeResponse(Long responseId){ - return this.responses.removeIf(r -> r.getId() == responseId); + return this.responses.removeIf(r -> Objects.equals(r.getId(), responseId)); } /** @@ -106,7 +106,7 @@ public List getResponsesForQuestion(Long questionId){ List result = new ArrayList<>(); for (Response r: responses){ for (Answer a: r.getAnswers()){ - if (a.getQuestion() == questionId){ + if (Objects.equals(a.getQuestion(), questionId)){ result.add(a.getContent()); } } diff --git a/src/test/java/com/opinionowl/opinionowl/IntegrationTest.java b/src/test/java/com/opinionowl/opinionowl/IntegrationTest.java deleted file mode 100644 index 98c8b67..0000000 --- a/src/test/java/com/opinionowl/opinionowl/IntegrationTest.java +++ /dev/null @@ -1,65 +0,0 @@ -package com.opinionowl.opinionowl; - -import com.opinionowl.opinionowl.models.*; -import com.opinionowl.opinionowl.repos.SurveyRepository; -import com.opinionowl.opinionowl.repos.UserRepository; -import org.junit.jupiter.api.Test; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; - -import java.util.List; - -@SpringBootTest -public class IntegrationTest { - @Autowired - private SurveyRepository surveyRepository; - - @Autowired - private UserRepository userRepository; - - /** - * Test method for the Survey class. - */ - @Test - public void testDatabaseIntegration() { - AppUser u1 = new AppUser("Test", "123"); - Survey survey = new Survey(u1, "TEST_SURVEY"); - u1.addSurvey(survey); - userRepository.save(u1); - - LongAnswerQuestion q1 = new LongAnswerQuestion(survey, "test1", 2); - RadioChoiceQuestion q2 = new RadioChoiceQuestion(survey, "test2", new String[]{"a", "c", "d"}); - RangeQuestion q3 = new RangeQuestion(survey, "test3", 1, 10, 1); - - survey.addQuestion(q1); - survey.addQuestion(q2); - survey.addQuestion(q3); - - surveyRepository.save(survey); - - Response r1 = new Response(survey); - - r1.addAnswer(q1.getId(), "hi"); - r1.addAnswer(q2.getId(), "b"); - - survey.addResponse(r1); - - surveyRepository.save(survey); - - survey.setClosed(true); - - Response r2 = new Response(survey); - - r2.addAnswer(q1.getId(), "yo"); - r2.addAnswer(q2.getId(), "a"); - - survey.addResponse(r2); - - surveyRepository.save(survey); - - List results = userRepository.findAll(); - for (AppUser u : results){ - System.out.println(u.toString()); - } - } -} \ No newline at end of file diff --git a/src/test/java/com/opinionowl/opinionowl/IntegrationTests/CreateSurveyIntegrationTest.java b/src/test/java/com/opinionowl/opinionowl/IntegrationTests/CreateSurveyIntegrationTest.java new file mode 100644 index 0000000..50829f7 --- /dev/null +++ b/src/test/java/com/opinionowl/opinionowl/IntegrationTests/CreateSurveyIntegrationTest.java @@ -0,0 +1,40 @@ +package com.opinionowl.opinionowl.IntegrationTests; + +import org.junit.jupiter.api.Test; +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.http.MediaType; +import org.springframework.test.web.servlet.MockMvc; + +import java.util.List; + +import static org.hamcrest.Matchers.containsString; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +@SpringBootTest +@AutoConfigureMockMvc +public class CreateSurveyIntegrationTest { + @Autowired + private MockMvc mockMvc; + + /** + * Test method for POSTing a survey and doing a GET after + */ + @Test + public void testCreateAndRetrieveSurvey() throws Exception { + String postData = "{\"radioQuestions\":{\"Test2\":[\"a\",\"b\"]},\"numericRanges\":{\"Test3\":[0,11]},\"title\":\"Form Title\",\"textQuestions\":[\"Test1\"]}"; + + // Create a survey using the POST request. + this.mockMvc.perform(post("/api/v1/createSurvey") + .contentType(MediaType.APPLICATION_JSON).content(postData)) + .andExpect(status().isOk()); + + // Expect the survey to show up on the home page. + this.mockMvc.perform(get("/")).andExpect(status().isOk()) + .andExpect(content().string(containsString("Form Title"))); + + } +} \ No newline at end of file diff --git a/src/test/java/com/opinionowl/opinionowl/OpinionOwlApplicationTests.java b/src/test/java/com/opinionowl/opinionowl/IntegrationTests/OpinionOwlApplicationTests.java similarity index 78% rename from src/test/java/com/opinionowl/opinionowl/OpinionOwlApplicationTests.java rename to src/test/java/com/opinionowl/opinionowl/IntegrationTests/OpinionOwlApplicationTests.java index a763869..b5d8b38 100644 --- a/src/test/java/com/opinionowl/opinionowl/OpinionOwlApplicationTests.java +++ b/src/test/java/com/opinionowl/opinionowl/IntegrationTests/OpinionOwlApplicationTests.java @@ -1,4 +1,4 @@ -package com.opinionowl.opinionowl; +package com.opinionowl.opinionowl.IntegrationTests; import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; diff --git a/src/test/java/com/opinionowl/opinionowl/AnswerTest.java b/src/test/java/com/opinionowl/opinionowl/ModelTests/AnswerTest.java similarity index 71% rename from src/test/java/com/opinionowl/opinionowl/AnswerTest.java rename to src/test/java/com/opinionowl/opinionowl/ModelTests/AnswerTest.java index d68b9f9..9548865 100644 --- a/src/test/java/com/opinionowl/opinionowl/AnswerTest.java +++ b/src/test/java/com/opinionowl/opinionowl/ModelTests/AnswerTest.java @@ -1,10 +1,11 @@ -package com.opinionowl.opinionowl; +package com.opinionowl.opinionowl.ModelTests; import com.opinionowl.opinionowl.models.*; import com.opinionowl.opinionowl.repos.*; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import java.util.*; + import static org.junit.jupiter.api.Assertions.*; /** @@ -49,7 +50,7 @@ public void testAnswer() { @Test public void testPersist() { AppUser u1 = new AppUser("AnswerTest", "123"); - Survey survey = new Survey(u1, "TEST_SURVEY"); + Survey survey = new Survey(u1, "ANSWER_TEST_SURVEY"); u1.addSurvey(survey); userRepository.save(u1); @@ -58,27 +59,17 @@ public void testPersist() { survey.addQuestion(q1); Response r1 = new Response(survey); - r1.addAnswer(q1.getId(), "hi"); - r1.addAnswer(q2.getId(), "b"); + r1.addAnswer(q1.getId(), "ANSWER1"); + r1.addAnswer(q2.getId(), "ANSWER2"); survey.addResponse(r1); - survey.setClosed(true); - surveyRepository.save(survey); - Answer a1 = new Answer(r1, q1.getId(), "hi"); - Answer a2 = new Answer(r1, q1.getId(), "b"); - answerRepository.save(a1); - answerRepository.save(a2); + Answer a1 = r1.getAnswers().get(0); + Answer a2 = r1.getAnswers().get(1); - List expectedR = new ArrayList<>(); - expectedR.add(new Answer(r1, q1.getId(), "hi")); - expectedR.add(new Answer(r1, q1.getId(), "b")); + surveyRepository.save(survey); List results = answerRepository.findAll(); - for (Answer a : results){ - System.out.println(a.toString()); - if(a.getResponse().getAnswers().containsAll(expectedR)) { - System.out.println(a.getResponse().getAnswers()); - } - } + assertTrue(results.stream().anyMatch(answer -> answer.getContent().equals(a1.getContent()))); + assertTrue(results.stream().anyMatch(answer -> answer.getContent().equals(a2.getContent()))); } } diff --git a/src/test/java/com/opinionowl/opinionowl/AppUserTest.java b/src/test/java/com/opinionowl/opinionowl/ModelTests/AppUserTest.java similarity index 96% rename from src/test/java/com/opinionowl/opinionowl/AppUserTest.java rename to src/test/java/com/opinionowl/opinionowl/ModelTests/AppUserTest.java index 25093f2..c598c16 100644 --- a/src/test/java/com/opinionowl/opinionowl/AppUserTest.java +++ b/src/test/java/com/opinionowl/opinionowl/ModelTests/AppUserTest.java @@ -1,4 +1,4 @@ -package com.opinionowl.opinionowl; +package com.opinionowl.opinionowl.ModelTests; import com.opinionowl.opinionowl.models.*; import com.opinionowl.opinionowl.repos.*; import org.junit.jupiter.api.Test; @@ -29,7 +29,7 @@ public void testAddSurvey(){ u1.addSurvey(survey); assertEquals(1, u1.getListSurveys().size()); - List expected = Arrays.asList(survey); + List expected = List.of(survey); assertEquals(expected, u1.getListSurveys()); } diff --git a/src/test/java/com/opinionowl/opinionowl/QuestionTest.java b/src/test/java/com/opinionowl/opinionowl/ModelTests/QuestionTest.java similarity index 85% rename from src/test/java/com/opinionowl/opinionowl/QuestionTest.java rename to src/test/java/com/opinionowl/opinionowl/ModelTests/QuestionTest.java index 273bf3d..774fc60 100644 --- a/src/test/java/com/opinionowl/opinionowl/QuestionTest.java +++ b/src/test/java/com/opinionowl/opinionowl/ModelTests/QuestionTest.java @@ -1,4 +1,4 @@ -package com.opinionowl.opinionowl; +package com.opinionowl.opinionowl.ModelTests; import com.opinionowl.opinionowl.models.*; import com.opinionowl.opinionowl.repos.*; import org.junit.jupiter.api.Test; @@ -18,9 +18,6 @@ public class QuestionTest { @Autowired private UserRepository userRepository; - @Autowired - private SurveyRepository surveyRepository; - /** * Method to test Long Answer Type questions added to the survey. */ @@ -75,28 +72,28 @@ public void testPersist() { survey.addQuestion(q2); survey.addQuestion(q3); - surveyRepository.save(survey); questionRepository.save(q1); questionRepository.save(q2); questionRepository.save(q3); - survey.setClosed(true); List results = questionRepository.findAll(); for (Question q : results){ - System.out.println(q.toString()); + if (!q.getSurvey().getTitle().equals(survey.getTitle())){ + continue; + } if(q.getType() == QuestionType.LONG_ANSWER) { LongAnswerQuestion laq = (LongAnswerQuestion) q; - System.out.println(laq.getCharLimit()); + assertEquals(laq.getCharLimit(), q1.getCharLimit()); } else if (q.getType() == QuestionType.RADIO_CHOICE) { RadioChoiceQuestion rcq = (RadioChoiceQuestion) q; - System.out.println(Arrays.toString(rcq.getChoices())); + assertArrayEquals(rcq.getChoices(), q2.getChoices()); } else { RangeQuestion rq = (RangeQuestion) q; - System.out.println(rq.getUpper()); - System.out.println(rq.getLower()); - System.out.println(rq.getIncrement()); + assertEquals(rq.getUpper(), q3.getUpper()); + assertEquals(rq.getLower(), q3.getLower()); + assertEquals(rq.getIncrement(), q3.getIncrement()); } } } diff --git a/src/test/java/com/opinionowl/opinionowl/ResponseTest.java b/src/test/java/com/opinionowl/opinionowl/ModelTests/ResponseTest.java similarity index 73% rename from src/test/java/com/opinionowl/opinionowl/ResponseTest.java rename to src/test/java/com/opinionowl/opinionowl/ModelTests/ResponseTest.java index 947ad77..b67bfe4 100644 --- a/src/test/java/com/opinionowl/opinionowl/ResponseTest.java +++ b/src/test/java/com/opinionowl/opinionowl/ModelTests/ResponseTest.java @@ -1,4 +1,4 @@ -package com.opinionowl.opinionowl; +package com.opinionowl.opinionowl.ModelTests; import com.opinionowl.opinionowl.models.*; import com.opinionowl.opinionowl.repos.*; import org.junit.jupiter.api.Test; @@ -19,9 +19,6 @@ public class ResponseTest { @Autowired private UserRepository userRepository; - @Autowired - private SurveyRepository surveyRepository; - /** * Method to test that the correct response is returned. */ @@ -49,7 +46,7 @@ public void testResponse() { @Test public void testPersist() { AppUser u1 = new AppUser("Test", "123"); - Survey survey = new Survey(u1, "TEST_SURVEY"); + Survey survey = new Survey(u1, "RESPONSE_TEST_SURVEY"); u1.addSurvey(survey); userRepository.save(u1); @@ -63,25 +60,16 @@ public void testPersist() { survey.addResponse(r1); responseRepository.save(r1); - survey.setClosed(true); - - Response r2 = new Response(survey); - - r2.addAnswer(q1.getId(), "yo"); - r2.addAnswer(q2.getId(), "a"); - - surveyRepository.save(survey); - responseRepository.save(r2); List results = responseRepository.findAll(); - List expectedR = new ArrayList<>(); - expectedR.add(new Answer(r1, q1.getId(), "hi")); - expectedR.add(new Answer(r1, q1.getId(), "b")); + + Response retrievedResponse = null; for (Response r : results){ - System.out.println(r.toString()); - if(r.getAnswers().containsAll(expectedR)) { - System.out.println(r.getAnswers()); + if (r.getSurvey().getTitle().equals(survey.getTitle())){ + retrievedResponse = r; } } + assertNotNull(retrievedResponse); + assertEquals(retrievedResponse.getAnswers().size(), r1.getAnswers().size()); } } \ No newline at end of file diff --git a/src/test/java/com/opinionowl/opinionowl/SurveyTest.java b/src/test/java/com/opinionowl/opinionowl/ModelTests/SurveyTest.java similarity index 93% rename from src/test/java/com/opinionowl/opinionowl/SurveyTest.java rename to src/test/java/com/opinionowl/opinionowl/ModelTests/SurveyTest.java index 7470a88..3ea8b3d 100644 --- a/src/test/java/com/opinionowl/opinionowl/SurveyTest.java +++ b/src/test/java/com/opinionowl/opinionowl/ModelTests/SurveyTest.java @@ -1,4 +1,4 @@ -package com.opinionowl.opinionowl; +package com.opinionowl.opinionowl.ModelTests; import com.opinionowl.opinionowl.models.*; import com.opinionowl.opinionowl.repos.*; import org.junit.jupiter.api.Test; @@ -196,7 +196,7 @@ public void testRangeResponse() { @Test public void testPersist() { AppUser u1 = new AppUser("SurveyTest", "123"); - Survey survey = new Survey(u1, "TEST_SURVEY"); + Survey survey = new Survey(u1, "SURVEY_TEST_SURVEY"); u1.addSurvey(survey); userRepository.save(u1); @@ -229,14 +229,15 @@ public void testPersist() { surveyRepository.save(survey); List results = surveyRepository.findAll(); - for (Survey r : results){ - System.out.println(r.toString()); - for (Question q : r.getQuestions()){ - if (q.getType() == QuestionType.LONG_ANSWER){ - LongAnswerQuestion laq = (LongAnswerQuestion) q; - System.out.println(laq.getCharLimit()); - } + Survey retrievedSurvey = null; + for (Survey r : results) { + if (r.getTitle().equals(survey.getTitle())) { + retrievedSurvey = r; + break; } } + assertNotNull(retrievedSurvey); + assertEquals(retrievedSurvey.getQuestions().size(), survey.getQuestions().size()); + assertEquals(retrievedSurvey.getResponses().size(), survey.getResponses().size()); } } \ No newline at end of file