Skip to content

Commit

Permalink
[#12048] Add integration tests for FeedbackResponsesDb (#12856)
Browse files Browse the repository at this point in the history
* Migrate SessionResultsData

* Add default entities

* Add helper methods to assist migrated logic

* Migrate buildCompleteGiverRecipientMap

* Migrate checkSpecificAccessControl

* Add default team instance for instructor

* Migrate session results data logic

* Use default team entity for instructor instead of const

* Migrate non-db logic

* Refactor Datastore and SQL action logic out to separate methods

* Fix checkstyle errors

* Migrate DB logic

* Fix checkstyle errors

* Move default instructor team entity to const

* Add test for SqlSessionResultsBundle

* Fix SQL results bundle test

* Add IT for GetSessionResultsAction

* Fix action logic

* Fix checkstyle errors

* Remove unused method parameters

* Fix persistence issues in test cases

* Remove question getter for comment

* Rename boolean methods to start with verb

* Reword comment to clarify question ID

* Refactor getting question UUID from param value

* Remove unneeded getters

* Remove entities from Const

* Revert changes to SqlCourseRoster

* Create and use missing response class

* Refactor no response text to const

* Migrate preview-related functionality

* Migrate preview functionality for question output

* Fix recipient section filter

* Update test cases to handle question preview

* Merge duplicate methods

* Fix checkstyle errors

* Add missing questions with non-visible preview responses

* Remove outdated test

* Edit for style and readability

* Fix missing join

* Fix section filtering logic

* Fix checkstyle errors

* Add integration tests

* Update and use typical bundle

* Update test cases for updated bundle

* Revert typical data bundle

* Use separate data bundle for feedback responses test

* Revert unrelated tests

* Fix json formatting

---------

Co-authored-by: Cedric Ong <[email protected]>
  • Loading branch information
xenosf and cedricongjh authored Mar 14, 2024
1 parent ccdb6cc commit 931dea4
Showing 1 changed file with 215 additions and 19 deletions.
234 changes: 215 additions & 19 deletions src/it/java/teammates/it/storage/sqlapi/FeedbackResponsesDbIT.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package teammates.it.storage.sqlapi;

import java.util.HashSet;
import java.util.List;
import java.util.UUID;

import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import teammates.common.datatransfer.FeedbackResultFetchType;
import teammates.common.datatransfer.SqlDataBundle;
import teammates.common.datatransfer.questions.FeedbackResponseDetails;
import teammates.common.datatransfer.questions.FeedbackTextResponseDetails;
Expand All @@ -29,31 +32,33 @@ public class FeedbackResponsesDbIT extends BaseTestCaseWithSqlDatabaseAccess {
private final FeedbackResponsesDb frDb = FeedbackResponsesDb.inst();
private final FeedbackResponseCommentsDb frcDb = FeedbackResponseCommentsDb.inst();

private SqlDataBundle typicalDataBundle;
private SqlDataBundle testDataBundle;

@Override
@BeforeClass
public void setupClass() {
super.setupClass();
typicalDataBundle = getTypicalSqlDataBundle();
testDataBundle = loadSqlDataBundle("/FeedbackResponsesITBundle.json");
}

@Override
@BeforeMethod
protected void setUp() throws Exception {
super.setUp();
persistDataBundle(typicalDataBundle);
persistDataBundle(testDataBundle);
HibernateUtil.flushSession();
HibernateUtil.clearSession();
}

@Test
public void testGetFeedbackResponsesFromGiverForQuestion() {
______TS("success: typical case");
FeedbackQuestion fq = typicalDataBundle.feedbackQuestions.get("qn1InSession1InCourse1");
FeedbackResponse fr = typicalDataBundle.feedbackResponses.get("response1ForQ1");
FeedbackQuestion fq = testDataBundle.feedbackQuestions.get("qn1InSession1InCourse1");

List<FeedbackResponse> expectedQuestions = List.of(fr);
List<FeedbackResponse> expectedQuestions = List.of(
testDataBundle.feedbackResponses.get("response1ForQ1"),
testDataBundle.feedbackResponses.get("response3ForQ1")
);

List<FeedbackResponse> actualQuestions =
frDb.getFeedbackResponsesFromGiverForQuestion(fq.getId(), "[email protected]");
Expand All @@ -65,10 +70,10 @@ public void testGetFeedbackResponsesFromGiverForQuestion() {
@Test
public void testDeleteFeedbackResponsesForQuestionCascade() {
______TS("success: typical case");
FeedbackQuestion fq = typicalDataBundle.feedbackQuestions.get("qn1InSession1InCourse1");
FeedbackResponse fr1 = typicalDataBundle.feedbackResponses.get("response1ForQ1");
FeedbackResponse fr2 = typicalDataBundle.feedbackResponses.get("response2ForQ1");
FeedbackResponseComment frc1 = typicalDataBundle.feedbackResponseComments.get("comment1ToResponse1ForQ1");
FeedbackQuestion fq = testDataBundle.feedbackQuestions.get("qn1InSession1InCourse1");
FeedbackResponse fr1 = testDataBundle.feedbackResponses.get("response1ForQ1");
FeedbackResponse fr2 = testDataBundle.feedbackResponses.get("response2ForQ1");
FeedbackResponseComment frc1 = testDataBundle.feedbackResponseComments.get("comment1ToResponse1ForQ1");

frDb.deleteFeedbackResponsesForQuestionCascade(fq.getId());

Expand All @@ -80,7 +85,7 @@ public void testDeleteFeedbackResponsesForQuestionCascade() {
@Test
public void testDeleteFeedback() {
______TS("success: typical case");
FeedbackResponse fr1 = typicalDataBundle.feedbackResponses.get("response1ForQ1");
FeedbackResponse fr1 = testDataBundle.feedbackResponses.get("response1ForQ1");

frDb.deleteFeedbackResponse(fr1);

Expand All @@ -90,8 +95,8 @@ public void testDeleteFeedback() {
@Test
public void testHasResponsesFromGiverInSession() {
______TS("success: typical case");
Course course = typicalDataBundle.courses.get("course1");
FeedbackSession fs = typicalDataBundle.feedbackSessions.get("session1InCourse1");
Course course = testDataBundle.courses.get("course1");
FeedbackSession fs = testDataBundle.feedbackSessions.get("session1InCourse1");

boolean actualHasReponses1 =
frDb.hasResponsesFromGiverInSession("[email protected]", fs.getName(), course.getId());
Expand All @@ -108,15 +113,15 @@ public void testHasResponsesFromGiverInSession() {
@Test
public void testAreThereResponsesForQuestion() {
______TS("success: typical case");
FeedbackQuestion fq1 = typicalDataBundle.feedbackQuestions.get("qn1InSession1InCourse1");
FeedbackQuestion fq1 = testDataBundle.feedbackQuestions.get("qn1InSession1InCourse1");

boolean actualResponse1 =
frDb.areThereResponsesForQuestion(fq1.getId());

assertTrue(actualResponse1);

______TS("feedback question with no responses");
FeedbackQuestion fq2 = typicalDataBundle.feedbackQuestions.get("qn6InSession1InCourse1NoResponses");
FeedbackQuestion fq2 = testDataBundle.feedbackQuestions.get("qn6InSession1InCourse1NoResponses");

boolean actualResponse2 =
frDb.areThereResponsesForQuestion(fq2.getId());
Expand All @@ -127,7 +132,7 @@ public void testAreThereResponsesForQuestion() {
@Test
public void testHasResponsesForCourse() {
______TS("success: typical case");
Course course = typicalDataBundle.courses.get("course1");
Course course = testDataBundle.courses.get("course1");

boolean actual =
frDb.hasResponsesForCourse(course.getId());
Expand All @@ -136,7 +141,7 @@ public void testHasResponsesForCourse() {
}

private FeedbackResponse prepareSqlInjectionTest() {
FeedbackResponse fr = typicalDataBundle.feedbackResponses.get("response1ForQ1");
FeedbackResponse fr = testDataBundle.feedbackResponses.get("response1ForQ1");
assertNotNull(frDb.getFeedbackResponse(fr.getId()));

return fr;
Expand Down Expand Up @@ -207,8 +212,8 @@ public void testSqlInjectionInHasResponsesForCourse() {
public void testSqlInjectionInCreateFeedbackResponse() throws Exception {
FeedbackResponse fr = prepareSqlInjectionTest();

FeedbackQuestion fq = typicalDataBundle.feedbackQuestions.get("qn1InSession1InCourse1");
Section s = typicalDataBundle.sections.get("section1InCourse1");
FeedbackQuestion fq = testDataBundle.feedbackQuestions.get("qn1InSession1InCourse1");
Section s = testDataBundle.sections.get("section1InCourse1");
String dummyUuid = "00000000-0000-4000-8000-000000000001";
FeedbackResponseDetails frd = new FeedbackTextResponseDetails();

Expand All @@ -230,4 +235,195 @@ public void testSqlInjectionInCpdateFeedbackResponse() throws Exception {

checkSqliFailed(fr);
}

@Test
public void testGetFeedbackResponsesForRecipientForQuestion_matchNotFound_shouldReturnEmptyList() {
______TS("Question not found");
String recipient = "[email protected]";
UUID nonexistentQuestionId = UUID.fromString("11110000-0000-0000-0000-000000000000");
List<FeedbackResponse> results = frDb.getFeedbackResponsesForRecipientForQuestion(nonexistentQuestionId, recipient);
assertEquals(0, results.size());

______TS("No matching responses exist");
FeedbackQuestion questionWithNoResponses = testDataBundle.feedbackQuestions.get("qn4InSession1InCourse1");
results = frDb.getFeedbackResponsesForRecipientForQuestion(questionWithNoResponses.getId(), recipient);
assertEquals(0, results.size());

}

@Test
public void testGetFeedbackResponsesForRecipientForQuestion_matchFound_success() {
______TS("Matching responses exist");
String recipient = "[email protected]";
FeedbackQuestion question = testDataBundle.feedbackQuestions.get("qn1InSession1InCourse1");
List<FeedbackResponse> expected = List.of(
testDataBundle.feedbackResponses.get("response2ForQ1")
);
List<FeedbackResponse> actual = frDb.getFeedbackResponsesForRecipientForQuestion(question.getId(), recipient);
assertListResponsesEqual(expected, actual);

}

@Test
public void testGetFeedbackResponsesForSessionInSection_matchNotFound_shouldReturnEmptyList() {
String section3 = testDataBundle.sections.get("section3InCourse1").getName();
FeedbackSession session = testDataBundle.feedbackSessions.get("session1InCourse1");
String courseId = session.getCourse().getId();

______TS("No matching responses exist for giver section");
FeedbackResultFetchType fetchType = FeedbackResultFetchType.GIVER;
List<FeedbackResponse> results = frDb.getFeedbackResponsesForSessionInSection(
session, courseId, section3, fetchType);
assertEquals(0, results.size());

______TS("No matching responses exist for recipient section");
fetchType = FeedbackResultFetchType.RECEIVER;
results = frDb.getFeedbackResponsesForSessionInSection(session, courseId, section3, fetchType);
assertEquals(0, results.size());

______TS("No matching responses exist for both giver and recipient section");
fetchType = FeedbackResultFetchType.BOTH;
results = frDb.getFeedbackResponsesForSessionInSection(session, courseId, section3, fetchType);
assertEquals(0, results.size());
}

@Test
public void testGetFeedbackResponsesForSessionInSection_matchFound_success() {
Course course = testDataBundle.courses.get("course1");
FeedbackSession session1 = testDataBundle.feedbackSessions.get("session1InCourse1");
Section section1 = testDataBundle.sections.get("section1InCourse1");
Section section2 = testDataBundle.sections.get("section2InCourse1");

______TS("Match giver section 1 in session 1");
FeedbackResultFetchType fetchType = FeedbackResultFetchType.GIVER;
List<FeedbackResponse> expected = List.of(
testDataBundle.feedbackResponses.get("response1ForQ1"),
testDataBundle.feedbackResponses.get("response2ForQ1"),
testDataBundle.feedbackResponses.get("response1ForQ2"),
testDataBundle.feedbackResponses.get("response2ForQ2"),
testDataBundle.feedbackResponses.get("response1ForQ3"),
testDataBundle.feedbackResponses.get("response3ForQ1"),
testDataBundle.feedbackResponses.get("response3ForQ2")
);
List<FeedbackResponse> actual = frDb.getFeedbackResponsesForSessionInSection(
session1, course.getId(), section1.getName(), fetchType);
assertListResponsesEqual(expected, actual);

______TS("Match recipient section 2 in session 1");
fetchType = FeedbackResultFetchType.RECEIVER;
expected = List.of(
testDataBundle.feedbackResponses.get("response3ForQ1"),
testDataBundle.feedbackResponses.get("response3ForQ2"),
testDataBundle.feedbackResponses.get("response4ForQ1")
);
actual = frDb.getFeedbackResponsesForSessionInSection(session1, course.getId(),
section2.getName(), fetchType);
assertListResponsesEqual(expected, actual);

______TS("Match both giver and recipient section 2 in session 1");
fetchType = FeedbackResultFetchType.BOTH;
expected = List.of(
testDataBundle.feedbackResponses.get("response4ForQ1")
);
actual = frDb.getFeedbackResponsesForSessionInSection(session1, course.getId(),
section2.getName(), fetchType);
assertListResponsesEqual(expected, actual);
}

@Test
public void testGetFeedbackResponsesForQuestionInSection_matchNotFound_shouldReturnEmptyList() {
String section1 = testDataBundle.sections.get("section1InCourse1").getName();
String section3 = testDataBundle.sections.get("section3InCourse1").getName();

______TS("Question not found");
UUID nonexistentQuestionId = UUID.fromString("11110000-0000-0000-0000-000000000000");
FeedbackResultFetchType fetchType = FeedbackResultFetchType.BOTH;
List<FeedbackResponse> results = frDb.getFeedbackResponsesForQuestionInSection(nonexistentQuestionId,
section1, fetchType);
assertEquals(0, results.size());

______TS("No matching responses exist for giver section");
UUID questionId = testDataBundle.feedbackQuestions.get("qn1InSession1InCourse1").getId();
fetchType = FeedbackResultFetchType.GIVER;
results = frDb.getFeedbackResponsesForQuestionInSection(questionId, section3, fetchType);
assertEquals(0, results.size());

______TS("No matching responses exist for recipient section");
fetchType = FeedbackResultFetchType.RECEIVER;
results = frDb.getFeedbackResponsesForQuestionInSection(questionId, section3, fetchType);
assertEquals(0, results.size());

______TS("No matching responses exist for both giver and recipient section");
fetchType = FeedbackResultFetchType.BOTH;
results = frDb.getFeedbackResponsesForQuestionInSection(questionId, section3, fetchType);
assertEquals(0, results.size());
}

@Test
public void testGetFeedbackResponsesForQuestionInSection_matchFound_success() {
Section section1 = testDataBundle.sections.get("section1InCourse1");
Section section2 = testDataBundle.sections.get("section2InCourse1");
FeedbackQuestion question1 = testDataBundle.feedbackQuestions.get("qn1InSession1InCourse1");

______TS("Match giver section 1 for Q1");
FeedbackResultFetchType fetchType = FeedbackResultFetchType.GIVER;
List<FeedbackResponse> expected = List.of(
testDataBundle.feedbackResponses.get("response1ForQ1"),
testDataBundle.feedbackResponses.get("response2ForQ1"),
testDataBundle.feedbackResponses.get("response3ForQ1")
);
List<FeedbackResponse> actual = frDb.getFeedbackResponsesForQuestionInSection(question1.getId(),
section1.getName(), fetchType);
assertListResponsesEqual(expected, actual);

______TS("Match recipient section 2 for Q1");
fetchType = FeedbackResultFetchType.RECEIVER;
expected = List.of(
testDataBundle.feedbackResponses.get("response3ForQ1"),
testDataBundle.feedbackResponses.get("response4ForQ1")
);
actual = frDb.getFeedbackResponsesForQuestionInSection(question1.getId(), section2.getName(), fetchType);
assertListResponsesEqual(expected, actual);

______TS("Match both giver and recipient section 2 for Q1");
fetchType = FeedbackResultFetchType.BOTH;
expected = List.of(
testDataBundle.feedbackResponses.get("response4ForQ1")
);
actual = frDb.getFeedbackResponsesForQuestionInSection(question1.getId(), section2.getName(), fetchType);
assertListResponsesEqual(expected, actual);
}

@Test
public void testGetFeedbackResponsesForSession() {
______TS("Session has responses");
FeedbackSession sessionWithResponses = testDataBundle.feedbackSessions.get("session1InCourse1");
List<FeedbackResponse> expected = List.of(
testDataBundle.feedbackResponses.get("response1ForQ1"),
testDataBundle.feedbackResponses.get("response2ForQ1"),
testDataBundle.feedbackResponses.get("response1ForQ2"),
testDataBundle.feedbackResponses.get("response2ForQ2"),
testDataBundle.feedbackResponses.get("response1ForQ3"),
testDataBundle.feedbackResponses.get("response3ForQ1"),
testDataBundle.feedbackResponses.get("response3ForQ2"),
testDataBundle.feedbackResponses.get("response4ForQ1")
);
List<FeedbackResponse> actual = frDb.getFeedbackResponsesForSession(sessionWithResponses,
sessionWithResponses.getCourse().getId());
assertListResponsesEqual(expected, actual);

______TS("Session has no responses");
FeedbackSession sessionWithoutResponses = testDataBundle.feedbackSessions.get(
"unpublishedSession1InTypicalCourse");
actual = frDb.getFeedbackResponsesForSession(sessionWithoutResponses, sessionWithResponses.getCourse().getId());
assertEquals(0, actual.size());
}

private void assertListResponsesEqual(List<FeedbackResponse> expected, List<FeedbackResponse> actual) {
assertEquals("List size not equal.", expected.size(), actual.size());
assertTrue(
String.format("List contents are not equal.%nExpected: %s,%nActual: %s",
expected.toString(), actual.toString()),
new HashSet<>(expected).equals(new HashSet<>(actual)));
}
}

0 comments on commit 931dea4

Please sign in to comment.