Skip to content

Commit

Permalink
[#12048] Add integration tests for FeedbackResponseCommentsDb (#12849)
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

* Refactor tests for readability

* Fix broken test cases

* Rename test section key

* Use separate json bundle for test data

* Clear session when set up

---------

Co-authored-by: Cedric Ong <[email protected]>
  • Loading branch information
xenosf and cedricongjh authored Mar 12, 2024
1 parent 2ad2242 commit b802335
Show file tree
Hide file tree
Showing 2 changed files with 1,701 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
package teammates.it.storage.sqlapi;

import java.util.ArrayList;
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.FeedbackParticipantType;
import teammates.common.datatransfer.SqlDataBundle;
import teammates.common.exception.EntityAlreadyExistsException;
import teammates.common.exception.InvalidParametersException;
import teammates.common.util.HibernateUtil;
import teammates.it.test.BaseTestCaseWithSqlDatabaseAccess;
import teammates.storage.sqlapi.FeedbackResponseCommentsDb;
import teammates.storage.sqlentity.Course;
import teammates.storage.sqlentity.FeedbackQuestion;
import teammates.storage.sqlentity.FeedbackResponse;
import teammates.storage.sqlentity.FeedbackResponseComment;
import teammates.storage.sqlentity.FeedbackSession;
import teammates.storage.sqlentity.Section;

/**
Expand All @@ -22,36 +30,37 @@ public class FeedbackResponseCommentsDbIT extends BaseTestCaseWithSqlDatabaseAcc

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 testGetFeedbackResponseCommentForResponseFromParticipant() {
______TS("success: typical case");
FeedbackResponse fr = typicalDataBundle.feedbackResponses.get("response1ForQ1");
FeedbackResponse fr = testDataBundle.feedbackResponses.get("response1ForQ1");

FeedbackResponseComment expectedComment = typicalDataBundle.feedbackResponseComments.get("comment1ToResponse1ForQ1");
FeedbackResponseComment expectedComment = testDataBundle.feedbackResponseComments.get("comment1ToResponse1ForQ1");
FeedbackResponseComment actualComment = frcDb.getFeedbackResponseCommentForResponseFromParticipant(fr.getId());

assertEquals(expectedComment, actualComment);
}

private FeedbackResponseComment prepareSqlInjectionTest() {
FeedbackResponseComment frc = typicalDataBundle.feedbackResponseComments.get("comment1ToResponse1ForQ1");
FeedbackResponseComment frc = testDataBundle.feedbackResponseComments.get("comment1ToResponse1ForQ1");
assertNotNull(frcDb.getFeedbackResponseComment(frc.getId()));

return frc;
Expand Down Expand Up @@ -85,8 +94,8 @@ public void testSqlInjectionInUpdateLastEditorEmailOfFeedbackResponseComments()
public void testSqlInjectionInCreateFeedbackResponseComment() throws Exception {
FeedbackResponseComment frc = prepareSqlInjectionTest();

FeedbackResponse fr = typicalDataBundle.feedbackResponses.get("response1ForQ1");
Section s = typicalDataBundle.sections.get("section2InCourse1");
FeedbackResponse fr = testDataBundle.feedbackResponses.get("response1ForQ1");
Section s = testDataBundle.sections.get("section2InCourse1");

String sqli = "'');/**/DELETE/**/FROM/**/feedback_response_comments;[email protected]";
FeedbackResponseComment newFrc = new FeedbackResponseComment(
Expand All @@ -109,4 +118,189 @@ public void testSqlInjectionInUpdateFeedbackResponseComment() throws Exception {

checkSqlInjectionFailed(frc);
}

@Test
public void testGetFeedbackResponseCommentsForSession_matchFound_success() {
Course course = testDataBundle.courses.get("course1");

______TS("Session with comments");
FeedbackSession sessionWithComments = testDataBundle.feedbackSessions.get("session1InCourse1");
List<FeedbackResponseComment> expected = List.of(
testDataBundle.feedbackResponseComments.get("comment1ToResponse1ForQ1"),
testDataBundle.feedbackResponseComments.get("comment2ToResponse1ForQ1"),
testDataBundle.feedbackResponseComments.get("comment2ToResponse2ForQ1"),
testDataBundle.feedbackResponseComments.get("comment1ToResponse1ForQ2s"),
testDataBundle.feedbackResponseComments.get("comment1ToResponse1ForQ3"),
testDataBundle.feedbackResponseComments.get("comment1ToResponse4ForQ1")
);
List<FeedbackResponseComment> results = frcDb.getFeedbackResponseCommentsForSession(
course.getId(), sessionWithComments.getName());
assertListCommentsEqual(expected, results);
}

@Test
public void testGetFeedbackResponseCommentsForSession_matchNotFound_shouldReturnEmptyList() {
Course course = testDataBundle.courses.get("course1");
FeedbackSession session = testDataBundle.feedbackSessions.get("session1InCourse1");

______TS("Course not found");
List<FeedbackResponseComment> results = frcDb.getFeedbackResponseCommentsForSession("not_exist", session.getName());
assertEquals(0, results.size());

______TS("Session not found");
results = frcDb.getFeedbackResponseCommentsForSession(course.getId(), "Nonexistent session");
assertEquals(0, results.size());

______TS("Session without comments");
FeedbackSession sessionWithoutComments = testDataBundle.feedbackSessions.get("ongoingSession1InCourse1");
results = frcDb.getFeedbackResponseCommentsForSession(course.getId(), sessionWithoutComments.getName());
assertEquals(0, results.size());
}

@Test
public void testGetFeedbackResponseCommentsForQuestion_matchFound_success() {
______TS("Question with comments");
FeedbackQuestion questionWithComments = testDataBundle.feedbackQuestions.get("qn1InSession1InCourse1");
List<FeedbackResponseComment> expectedComments = List.of(
testDataBundle.feedbackResponseComments.get("comment1ToResponse1ForQ1"),
testDataBundle.feedbackResponseComments.get("comment2ToResponse1ForQ1"),
testDataBundle.feedbackResponseComments.get("comment2ToResponse2ForQ1"),
testDataBundle.feedbackResponseComments.get("comment1ToResponse4ForQ1")
);
List<FeedbackResponseComment> results = frcDb.getFeedbackResponseCommentsForQuestion(questionWithComments.getId());
assertListCommentsEqual(expectedComments, results);
}

@Test
public void testGetFeedbackResponseCommentsForQuestion_matchNotFound_shouldReturnEmptyList() {
______TS("Question not found");
UUID nonexistentQuestionId = UUID.fromString("11110000-0000-0000-0000-000000000000");
List<FeedbackResponseComment> results = frcDb.getFeedbackResponseCommentsForQuestion(nonexistentQuestionId);
assertEquals(0, results.size());

______TS("Question without comments");
FeedbackQuestion questionWithoutComments = testDataBundle.feedbackQuestions.get("qn5InSession1InCourse1");
results = frcDb.getFeedbackResponseCommentsForQuestion(questionWithoutComments.getId());
assertEquals(0, results.size());
}

@Test
public void testGetFeedbackResponseCommentsForSessionInSection_matchFound_success()
throws EntityAlreadyExistsException, InvalidParametersException {
Section section1 = testDataBundle.sections.get("section1InCourse1");
Section section2 = testDataBundle.sections.get("section2InCourse1");
Course course = testDataBundle.courses.get("course1");
FeedbackSession session1 = testDataBundle.feedbackSessions.get("session1InCourse1");
FeedbackSession session2 = testDataBundle.feedbackSessions.get("session2InTypicalCourse");

______TS("Section 1 Session 2 match");
List<FeedbackResponseComment> expected = List.of(
testDataBundle.feedbackResponseComments.get("comment1ToResponse1ForQ1InSession2")
);
List<FeedbackResponseComment> results = frcDb.getFeedbackResponseCommentsForSessionInSection(
course.getId(), session2.getName(), section1.getName());
assertListCommentsEqual(expected, results);

______TS("Section 2 Session 1 match");
expected = List.of(
testDataBundle.feedbackResponseComments.get("comment1ToResponse4ForQ1")
);
results = frcDb.getFeedbackResponseCommentsForSessionInSection(
course.getId(), session1.getName(), section2.getName());
assertListCommentsEqual(expected, results);
}

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

______TS("Course not found");
List<FeedbackResponseComment> results = frcDb.getFeedbackResponseCommentsForSessionInSection(
"not_exist", session1.getName(), section1.getName());
assertEquals(0, results.size());

______TS("Session not found");
results = frcDb.getFeedbackResponseCommentsForSessionInSection(
course.getId(), "Nonexistent session", section1.getName());
assertEquals(0, results.size());

______TS("Section not found");
results = frcDb.getFeedbackResponseCommentsForSessionInSection(
course.getId(), session1.getName(), "Nonexistent section");
assertEquals(0, results.size());

______TS("No matching comments exist");
results = frcDb.getFeedbackResponseCommentsForSessionInSection(
course.getId(), session2.getName(), section2.getName());
assertEquals(0, results.size());
}

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

______TS("Section 1 Question 1 match");
List<FeedbackResponseComment> expected = List.of(
testDataBundle.feedbackResponseComments.get("comment1ToResponse1ForQ1"),
testDataBundle.feedbackResponseComments.get("comment2ToResponse1ForQ1"),
testDataBundle.feedbackResponseComments.get("comment2ToResponse2ForQ1"),
testDataBundle.feedbackResponseComments.get("comment1ToResponse4ForQ1")
);
List<FeedbackResponseComment> results = frcDb.getFeedbackResponseCommentsForQuestionInSection(
question1.getId(), section1.getName());
assertListCommentsEqual(expected, results);

______TS("Section 2 Question 1 match");
expected = List.of(
testDataBundle.feedbackResponseComments.get("comment1ToResponse4ForQ1")
);
results = frcDb.getFeedbackResponseCommentsForQuestionInSection(
question1.getId(), section2.getName());
assertListCommentsEqual(expected, results);

______TS("Section 1 Question 2 match");
expected = List.of(
testDataBundle.feedbackResponseComments.get("comment1ToResponse1ForQ2s")
);
results = frcDb.getFeedbackResponseCommentsForQuestionInSection(
question2.getId(), section1.getName());
assertListCommentsEqual(expected, results);
}

@Test
public void testGetFeedbackResponseCommentsForQuestionInSection_matchNotFound_shouldReturnEmptyList() {
Section section = testDataBundle.sections.get("section1InCourse1");
FeedbackQuestion question1 = testDataBundle.feedbackQuestions.get("qn1InSession1InCourse1");
FeedbackQuestion question2 = testDataBundle.feedbackQuestions.get("qn4InSession1InCourse1");

______TS("Question not found");
UUID nonexistentQuestionId = UUID.fromString("11110000-0000-0000-0000-000000000000");
List<FeedbackResponseComment> results = frcDb.getFeedbackResponseCommentsForQuestionInSection(
nonexistentQuestionId, section.getName());
assertEquals(0, results.size());

______TS("Section not found");
results = frcDb.getFeedbackResponseCommentsForQuestionInSection(question1.getId(), "Nonexistent section");
assertEquals(0, results.size());

______TS("No matching comments exist");
results = frcDb.getFeedbackResponseCommentsForQuestionInSection(question2.getId(), section.getName());
assertEquals(0, results.size());
}

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

}
Loading

0 comments on commit b802335

Please sign in to comment.