Skip to content

Commit

Permalink
Merge pull request #29 from bardia-p/milestone1_issue25_integrationTests
Browse files Browse the repository at this point in the history
  • Loading branch information
bardia-p authored Nov 11, 2023
2 parents 33e75f0 + dda450d commit 61f5cb2
Show file tree
Hide file tree
Showing 14 changed files with 102 additions and 137 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/opinionowl/opinionowl/models/Answer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

}
14 changes: 14 additions & 0 deletions src/main/java/com/opinionowl/opinionowl/models/Question.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import lombok.Setter;
import lombok.NoArgsConstructor;

import java.util.Objects;

/**
* The Question class which keeps track of the survey questions.
*/
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -50,7 +50,6 @@ public String toString(){
}

/**
*
* @param o object that is being compared with
* @return boolean value saying whether objects are equal
*/
Expand All @@ -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());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

}
6 changes: 3 additions & 3 deletions src/main/java/com/opinionowl/opinionowl/models/Survey.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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));
}

/**
Expand All @@ -106,7 +106,7 @@ public List<String> getResponsesForQuestion(Long questionId){
List<String> 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());
}
}
Expand Down
65 changes: 0 additions & 65 deletions src/test/java/com/opinionowl/opinionowl/IntegrationTest.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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")));

}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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.*;

/**
Expand Down Expand Up @@ -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);

Expand All @@ -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<Answer> expectedR = new ArrayList<>();
expectedR.add(new Answer(r1, q1.getId(), "hi"));
expectedR.add(new Answer(r1, q1.getId(), "b"));
surveyRepository.save(survey);

List<Answer> 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())));
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -29,7 +29,7 @@ public void testAddSurvey(){
u1.addSurvey(survey);

assertEquals(1, u1.getListSurveys().size());
List<Survey> expected = Arrays.asList(survey);
List<Survey> expected = List.of(survey);

assertEquals(expected, u1.getListSurveys());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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.
*/
Expand Down Expand Up @@ -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<Question> 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());
}
}
}
Expand Down
Loading

0 comments on commit 61f5cb2

Please sign in to comment.