Skip to content

Commit

Permalink
Add testcases for FeedbackResponseCommentsDbTest (#12755)
Browse files Browse the repository at this point in the history
* Add CRUD testcases

* Fix linting

* Refactor getTypicalComment

* Amend testDeleteComment testcase

* Amend testDeleteComment testcase

* Fix compile error

* Revert linting changes

---------

Co-authored-by: Nicolas <[email protected]>
  • Loading branch information
mingyuanc and NicolasCwy authored Feb 19, 2024
1 parent 02b710d commit d6c67fc
Show file tree
Hide file tree
Showing 3 changed files with 156 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package teammates.storage.sqlapi;

import static teammates.common.util.Const.ERROR_CREATE_ENTITY_ALREADY_EXISTS;
import static teammates.common.util.Const.ERROR_UPDATE_NON_EXISTENT;

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

import teammates.common.exception.EntityAlreadyExistsException;
import teammates.common.exception.EntityDoesNotExistException;
import teammates.common.exception.InvalidParametersException;
import teammates.common.util.HibernateUtil;
import teammates.storage.sqlentity.Course;
Expand Down Expand Up @@ -190,9 +192,18 @@ private List<FeedbackResponseComment> getFeedbackResponseCommentEntitiesForLastE
/**
* Updates the feedback response comment.
*/
public FeedbackResponseComment updateFeedbackResponseComment(FeedbackResponseComment feedbackResponseComment) {
public FeedbackResponseComment updateFeedbackResponseComment(FeedbackResponseComment feedbackResponseComment)
throws InvalidParametersException, EntityDoesNotExistException {
assert feedbackResponseComment != null;

if (!feedbackResponseComment.isValid()) {
throw new InvalidParametersException(feedbackResponseComment.getInvalidityInfo());
}

if (getFeedbackResponseComment(feedbackResponseComment.getId()) == null) {
throw new EntityDoesNotExistException(ERROR_UPDATE_NON_EXISTENT);
}

return merge(feedbackResponseComment);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
package teammates.storage.sqlapi;

import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;

import org.mockito.MockedStatic;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import teammates.common.datatransfer.FeedbackParticipantType;
import teammates.common.exception.EntityAlreadyExistsException;
import teammates.common.exception.EntityDoesNotExistException;
import teammates.common.exception.InvalidParametersException;
import teammates.common.util.HibernateUtil;
import teammates.storage.sqlentity.FeedbackResponseComment;
import teammates.test.BaseTestCase;

/**
* SUT: {@code FeedbackResponseCommentsDb}.
*/

public class FeedbackResponseCommentsDbTest extends BaseTestCase {

private static final Long TYPICAL_ID = 100L;

private static final Long NOT_TYPICAL_ID = 101L;
private FeedbackResponseCommentsDb feedbackResponseCommentsDb;
private MockedStatic<HibernateUtil> mockHibernateUtil;

@BeforeMethod
public void setUpMethod() {
mockHibernateUtil = mockStatic(HibernateUtil.class);
feedbackResponseCommentsDb = spy(FeedbackResponseCommentsDb.class);
}

@AfterMethod
public void teardownMethod() {
mockHibernateUtil.close();

}

@Test
public void testCreateComment_commentDoesNotExist_success()
throws InvalidParametersException, EntityAlreadyExistsException {
FeedbackResponseComment comment = getTypicalResponseComment(TYPICAL_ID);

feedbackResponseCommentsDb.createFeedbackResponseComment(comment);

mockHibernateUtil.verify(() -> HibernateUtil.persist(comment));
}

@Test
public void testCreateComment_commentAlreadyExists_throwsEntityAlreadyExistsException() {
FeedbackResponseComment comment = getTypicalResponseComment(TYPICAL_ID);

mockHibernateUtil.when(() -> HibernateUtil.get(FeedbackResponseComment.class, TYPICAL_ID)).thenReturn(comment);

EntityAlreadyExistsException ex = assertThrows(EntityAlreadyExistsException.class,
() -> feedbackResponseCommentsDb.createFeedbackResponseComment(comment));

assertEquals("Trying to create an entity that exists: " + comment.toString(), ex.getMessage());
mockHibernateUtil.verify(() -> HibernateUtil.persist(comment), never());
}

@Test
public void testGetComment_commentAlreadyExists_success() {
FeedbackResponseComment comment = getTypicalResponseComment(TYPICAL_ID);

mockHibernateUtil.when(() -> HibernateUtil.get(FeedbackResponseComment.class, TYPICAL_ID)).thenReturn(comment);

FeedbackResponseComment commentFetched = feedbackResponseCommentsDb.getFeedbackResponseComment(TYPICAL_ID);

mockHibernateUtil.when(() -> HibernateUtil.get(FeedbackResponseComment.class, TYPICAL_ID)).thenReturn(comment);
assertEquals(comment, commentFetched);
}

@Test
public void testGetComment_commentDoesNotExist_returnsNull() {
mockHibernateUtil.when(() -> HibernateUtil.get(FeedbackResponseComment.class, NOT_TYPICAL_ID)).thenReturn(null);

FeedbackResponseComment commentFetched = feedbackResponseCommentsDb.getFeedbackResponseComment(NOT_TYPICAL_ID);

mockHibernateUtil.verify(() -> HibernateUtil.get(FeedbackResponseComment.class, NOT_TYPICAL_ID), times(1));
assertNull(commentFetched);
}

@Test
public void testDeleteComment_commentExists_success() {
FeedbackResponseComment comment = getTypicalResponseComment(TYPICAL_ID);

mockHibernateUtil.when(() -> HibernateUtil.get(FeedbackResponseComment.class, TYPICAL_ID)).thenReturn(comment);
feedbackResponseCommentsDb.deleteFeedbackResponseComment(TYPICAL_ID);

mockHibernateUtil.verify(() -> HibernateUtil.remove(comment));
}

@Test
public void testUpdateComment_commentInvalid_throwsInvalidParametersException() {
FeedbackResponseComment comment = getTypicalResponseComment(TYPICAL_ID);
comment.setGiverType(FeedbackParticipantType.SELF);

assertThrows(InvalidParametersException.class,
() -> feedbackResponseCommentsDb.updateFeedbackResponseComment(comment));

mockHibernateUtil.verify(() -> HibernateUtil.merge(comment), never());
}

@Test
public void testUpdateComment_commentDoesNotExist_throwsEntityDoesNotExistException() {
FeedbackResponseComment comment = getTypicalResponseComment(NOT_TYPICAL_ID);

assertThrows(EntityDoesNotExistException.class,
() -> feedbackResponseCommentsDb.updateFeedbackResponseComment(comment));

mockHibernateUtil.verify(() -> HibernateUtil.merge(comment), never());
}

@Test
public void testUpdateCourse_success() throws InvalidParametersException, EntityDoesNotExistException {
FeedbackResponseComment comment = getTypicalResponseComment(TYPICAL_ID);
comment.setCommentText("Placeholder Text");

doReturn(comment).when(feedbackResponseCommentsDb).getFeedbackResponseComment(anyLong());
feedbackResponseCommentsDb.updateFeedbackResponseComment(comment);

mockHibernateUtil.verify(() -> HibernateUtil.merge(comment));
}

}
10 changes: 10 additions & 0 deletions src/test/java/teammates/test/BaseTestCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import teammates.storage.sqlentity.Account;
import teammates.storage.sqlentity.Course;
import teammates.storage.sqlentity.FeedbackQuestion;
import teammates.storage.sqlentity.FeedbackResponseComment;
import teammates.storage.sqlentity.FeedbackSession;
import teammates.storage.sqlentity.Instructor;
import teammates.storage.sqlentity.Notification;
Expand Down Expand Up @@ -170,6 +171,15 @@ protected FeedbackQuestion getTypicalFeedbackQuestionForSession(FeedbackSession
new FeedbackTextQuestionDetails("test question text"));
}

protected FeedbackResponseComment getTypicalResponseComment(Long id) {
FeedbackResponseComment comment = new FeedbackResponseComment(null, "",
FeedbackParticipantType.STUDENTS, null, null, "",
false, false,
null, null, null);
comment.setId(id);
return comment;
}

/**
* Populates the feedback question and response IDs within the data bundle.
*
Expand Down

0 comments on commit d6c67fc

Please sign in to comment.