-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Add test for FeedbackSessionsDb * update test cases --------- Co-authored-by: Cedric Ong <[email protected]>
- Loading branch information
1 parent
225d54f
commit 5b3a96f
Showing
2 changed files
with
253 additions
and
2 deletions.
There are no files selected for viewing
237 changes: 237 additions & 0 deletions
237
src/test/java/teammates/storage/sqlapi/FeedbackSessionsDbTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,237 @@ | ||
package teammates.storage.sqlapi; | ||
|
||
import static org.mockito.ArgumentMatchers.any; | ||
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 java.util.UUID; | ||
|
||
import org.mockito.MockedStatic; | ||
import org.testng.annotations.AfterMethod; | ||
import org.testng.annotations.BeforeMethod; | ||
import org.testng.annotations.Test; | ||
|
||
import teammates.common.exception.EntityAlreadyExistsException; | ||
import teammates.common.exception.EntityDoesNotExistException; | ||
import teammates.common.exception.InvalidParametersException; | ||
import teammates.common.util.HibernateUtil; | ||
import teammates.common.util.TimeHelperExtension; | ||
import teammates.storage.sqlentity.FeedbackSession; | ||
import teammates.test.BaseTestCase; | ||
|
||
/** | ||
* SUT: {@code FeedbackSessionsDb}. | ||
*/ | ||
|
||
public class FeedbackSessionsDbTest extends BaseTestCase { | ||
private FeedbackSessionsDb feedbackSessionsDb; | ||
private MockedStatic<HibernateUtil> mockHibernateUtil; | ||
|
||
@BeforeMethod | ||
public void setUpMethod() { | ||
mockHibernateUtil = mockStatic(HibernateUtil.class); | ||
feedbackSessionsDb = spy(FeedbackSessionsDb.class); | ||
} | ||
|
||
@AfterMethod | ||
public void teardownMethod() { | ||
mockHibernateUtil.close(); | ||
} | ||
|
||
@Test | ||
public void testCreateSession_sessionDoesNotExist_success() | ||
throws InvalidParametersException, EntityAlreadyExistsException { | ||
FeedbackSession feedbackSession = getTypicalFeedbackSessionForCourse(getTypicalCourse()); | ||
|
||
feedbackSessionsDb.createFeedbackSession(feedbackSession); | ||
|
||
mockHibernateUtil.verify(() -> HibernateUtil.persist(feedbackSession), times(1)); | ||
} | ||
|
||
@Test | ||
public void testCreateSession_duplicateSession_throwsEntityAlreadyExistsException() | ||
throws InvalidParametersException, EntityAlreadyExistsException { | ||
FeedbackSession feedbackSession = getTypicalFeedbackSessionForCourse(getTypicalCourse()); | ||
UUID uuid = feedbackSession.getId(); | ||
doReturn(feedbackSession).when(feedbackSessionsDb).getFeedbackSession(uuid); | ||
|
||
assertThrows(EntityAlreadyExistsException.class, | ||
() -> feedbackSessionsDb.createFeedbackSession(feedbackSession)); | ||
mockHibernateUtil.verify(() -> HibernateUtil.persist(feedbackSession), never()); | ||
} | ||
|
||
@Test | ||
public void testCreateSession_invalidParams_throwsInvalidParametersException() | ||
throws InvalidParametersException, EntityAlreadyExistsException { | ||
FeedbackSession feedbackSession = getTypicalFeedbackSessionForCourse(getTypicalCourse()); | ||
feedbackSession.setName(""); | ||
|
||
assertThrows(InvalidParametersException.class, () -> feedbackSessionsDb.createFeedbackSession(feedbackSession)); | ||
mockHibernateUtil.verify(() -> HibernateUtil.persist(feedbackSession), never()); | ||
} | ||
|
||
@Test | ||
public void testCreateSession_nullParams_throwsAssertionError() | ||
throws InvalidParametersException, EntityAlreadyExistsException { | ||
assertThrows(AssertionError.class, () -> feedbackSessionsDb.createFeedbackSession(null)); | ||
} | ||
|
||
@Test | ||
public void testGetFeedbackSession_sessionExists_success() { | ||
FeedbackSession feedbackSession = getTypicalFeedbackSessionForCourse(getTypicalCourse()); | ||
UUID uuid = feedbackSession.getId(); | ||
mockHibernateUtil.when(() -> HibernateUtil.get(FeedbackSession.class, uuid)).thenReturn(feedbackSession); | ||
|
||
FeedbackSession sessionFetched = feedbackSessionsDb.getFeedbackSession(uuid); | ||
|
||
mockHibernateUtil.verify(() -> HibernateUtil.get(FeedbackSession.class, uuid), times(1)); | ||
assertEquals(feedbackSession, sessionFetched); | ||
} | ||
|
||
@Test | ||
public void testGetFeedbackSession_sessionDoesNotExists_returnNull() { | ||
UUID randomUuid = UUID.randomUUID(); | ||
mockHibernateUtil.when(() -> HibernateUtil.get(FeedbackSession.class, randomUuid)).thenReturn(null); | ||
|
||
FeedbackSession sessionFetched = feedbackSessionsDb.getFeedbackSession(randomUuid); | ||
|
||
mockHibernateUtil.verify(() -> HibernateUtil.get(FeedbackSession.class, randomUuid), times(1)); | ||
assertNull(sessionFetched); | ||
} | ||
|
||
@Test | ||
public void testUpdateFeedbackSession_success() throws InvalidParametersException, EntityDoesNotExistException { | ||
FeedbackSession feedbackSession = getTypicalFeedbackSessionForCourse(getTypicalCourse()); | ||
doReturn(feedbackSession).when(feedbackSessionsDb).getFeedbackSession(any(UUID.class)); | ||
|
||
feedbackSessionsDb.updateFeedbackSession(feedbackSession); | ||
|
||
mockHibernateUtil.verify(() -> HibernateUtil.merge(feedbackSession), times(1)); | ||
} | ||
|
||
@Test | ||
public void testUpdateFeedbackSession_sessionDoesNotExist_throwsEntityDoesNotExistException() | ||
throws InvalidParametersException, EntityDoesNotExistException { | ||
FeedbackSession feedbackSession = getTypicalFeedbackSessionForCourse(getTypicalCourse()); | ||
UUID uuid = feedbackSession.getId(); | ||
doReturn(null).when(feedbackSessionsDb).getFeedbackSession(uuid); | ||
|
||
assertThrows(EntityDoesNotExistException.class, | ||
() -> feedbackSessionsDb.updateFeedbackSession(feedbackSession)); | ||
mockHibernateUtil.verify(() -> HibernateUtil.merge(feedbackSession), never()); | ||
} | ||
|
||
@Test | ||
public void testUpdateFeedbackSession_sessionInvalid_throwsInvalidParametersException() | ||
throws InvalidParametersException, EntityDoesNotExistException { | ||
FeedbackSession feedbackSession = getTypicalFeedbackSessionForCourse(getTypicalCourse()); | ||
UUID uuid = feedbackSession.getId(); | ||
feedbackSession.setName(""); | ||
doReturn(feedbackSession).when(feedbackSessionsDb).getFeedbackSession(uuid); | ||
|
||
assertThrows(InvalidParametersException.class, () -> feedbackSessionsDb.updateFeedbackSession(feedbackSession)); | ||
mockHibernateUtil.verify(() -> HibernateUtil.merge(feedbackSession), never()); | ||
} | ||
|
||
@Test | ||
public void testDeleteFeedbackSession_success() throws InvalidParametersException, EntityDoesNotExistException { | ||
FeedbackSession feedbackSession = getTypicalFeedbackSessionForCourse(getTypicalCourse()); | ||
|
||
feedbackSessionsDb.deleteFeedbackSession(feedbackSession); | ||
|
||
mockHibernateUtil.verify(() -> HibernateUtil.remove(feedbackSession), times(1)); | ||
} | ||
|
||
@Test | ||
public void testGetSoftDeletedFeedbackSession_isSoftDeleted_success() { | ||
FeedbackSession feedbackSession = getTypicalFeedbackSessionForCourse(getTypicalCourse()); | ||
String sessionName = feedbackSession.getName(); | ||
String courseId = feedbackSession.getCourse().getId(); | ||
feedbackSession.setDeletedAt(TimeHelperExtension.getInstantDaysOffsetFromNow(2)); | ||
doReturn(feedbackSession).when(feedbackSessionsDb).getFeedbackSession(sessionName, courseId); | ||
|
||
FeedbackSession sessionFetched = feedbackSessionsDb.getSoftDeletedFeedbackSession(sessionName, courseId); | ||
|
||
assertEquals(feedbackSession, sessionFetched); | ||
} | ||
|
||
@Test | ||
public void testGetSoftDeletedFeedbackSession_notSoftDeleted_returnNull() { | ||
FeedbackSession feedbackSession = getTypicalFeedbackSessionForCourse(getTypicalCourse()); | ||
String sessionName = feedbackSession.getName(); | ||
String courseId = feedbackSession.getCourse().getId(); | ||
doReturn(feedbackSession).when(feedbackSessionsDb).getFeedbackSession(sessionName, courseId); | ||
|
||
FeedbackSession sessionFetched = feedbackSessionsDb.getSoftDeletedFeedbackSession(sessionName, courseId); | ||
|
||
assertNull(sessionFetched); | ||
} | ||
|
||
@Test | ||
public void testGetSoftDeletedFeedbackSession_sessionDoesNotExist_returnNull() { | ||
FeedbackSession feedbackSession = getTypicalFeedbackSessionForCourse(getTypicalCourse()); | ||
String sessionName = feedbackSession.getName(); | ||
String courseId = feedbackSession.getCourse().getId(); | ||
doReturn(null).when(feedbackSessionsDb).getFeedbackSession(sessionName, courseId); | ||
|
||
FeedbackSession sessionFetched = feedbackSessionsDb.getSoftDeletedFeedbackSession(sessionName, courseId); | ||
|
||
assertNull(sessionFetched); | ||
} | ||
|
||
@Test | ||
public void testRestoreDeletedFeedbackSession_success() throws EntityDoesNotExistException { | ||
FeedbackSession feedbackSession = getTypicalFeedbackSessionForCourse(getTypicalCourse()); | ||
String sessionName = feedbackSession.getName(); | ||
String courseId = feedbackSession.getCourse().getId(); | ||
feedbackSession.setDeletedAt(TimeHelperExtension.getInstantDaysOffsetFromNow(2)); | ||
doReturn(feedbackSession).when(feedbackSessionsDb).getFeedbackSession(sessionName, courseId); | ||
|
||
feedbackSessionsDb.restoreDeletedFeedbackSession(sessionName, courseId); | ||
|
||
assertNull(feedbackSession.getDeletedAt()); | ||
mockHibernateUtil.verify(() -> HibernateUtil.merge(feedbackSession), times(1)); | ||
} | ||
|
||
@Test | ||
public void testRestoreDeletedFeedbackSession_sessionDoesNotExist_throwsEntityDoesNotExistException() | ||
throws EntityDoesNotExistException { | ||
FeedbackSession feedbackSession = getTypicalFeedbackSessionForCourse(getTypicalCourse()); | ||
String sessionName = feedbackSession.getName(); | ||
String courseId = feedbackSession.getCourse().getId(); | ||
doReturn(null).when(feedbackSessionsDb).getFeedbackSession(sessionName, courseId); | ||
|
||
assertThrows(EntityDoesNotExistException.class, | ||
() -> feedbackSessionsDb.restoreDeletedFeedbackSession(sessionName, courseId)); | ||
mockHibernateUtil.verify(() -> HibernateUtil.merge(feedbackSession), never()); | ||
} | ||
|
||
@Test | ||
public void testSoftDeleteFeedbackSession_success() throws EntityDoesNotExistException { | ||
FeedbackSession feedbackSession = getTypicalFeedbackSessionForCourse(getTypicalCourse()); | ||
String sessionName = feedbackSession.getName(); | ||
String courseId = feedbackSession.getCourse().getId(); | ||
doReturn(feedbackSession).when(feedbackSessionsDb).getFeedbackSession(sessionName, courseId); | ||
|
||
feedbackSessionsDb.softDeleteFeedbackSession(sessionName, courseId); | ||
|
||
assertNotNull(feedbackSession.getDeletedAt()); | ||
mockHibernateUtil.verify(() -> HibernateUtil.merge(feedbackSession), times(1)); | ||
} | ||
|
||
@Test | ||
public void testSoftDeleteFeedbackSession_sessionDoesNotExist_throwsEntityDoesNotExistException() | ||
throws EntityDoesNotExistException { | ||
FeedbackSession feedbackSession = getTypicalFeedbackSessionForCourse(getTypicalCourse()); | ||
String sessionName = feedbackSession.getName(); | ||
String courseId = feedbackSession.getCourse().getId(); | ||
doReturn(null).when(feedbackSessionsDb).getFeedbackSession(sessionName, courseId); | ||
|
||
assertThrows(EntityDoesNotExistException.class, | ||
() -> feedbackSessionsDb.restoreDeletedFeedbackSession(sessionName, courseId)); | ||
mockHibernateUtil.verify(() -> HibernateUtil.merge(feedbackSession), never()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
|
||
import java.io.IOException; | ||
import java.lang.reflect.Method; | ||
import java.time.Duration; | ||
import java.time.Instant; | ||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
|
@@ -23,6 +24,7 @@ | |
import teammates.common.util.Const; | ||
import teammates.common.util.FieldValidator; | ||
import teammates.common.util.JsonUtils; | ||
import teammates.common.util.TimeHelperExtension; | ||
import teammates.sqllogic.core.DataBundleLogic; | ||
import teammates.storage.sqlentity.Account; | ||
import teammates.storage.sqlentity.Course; | ||
|
@@ -160,8 +162,20 @@ protected Team getTypicalTeam() { | |
} | ||
|
||
protected FeedbackSession getTypicalFeedbackSessionForCourse(Course course) { | ||
return new FeedbackSession("test-feedbacksession", course, "testemail", "test-instructions", null, | ||
null, null, null, null, false, false, false); | ||
Instant startTime = TimeHelperExtension.getInstantDaysOffsetFromNow(1); | ||
Instant endTime = TimeHelperExtension.getInstantDaysOffsetFromNow(7); | ||
return new FeedbackSession("test-feedbacksession", | ||
course, | ||
"[email protected]", | ||
"test-instructions", | ||
startTime, | ||
endTime, | ||
startTime, | ||
endTime, | ||
Duration.ofMinutes(5), | ||
false, | ||
false, | ||
false); | ||
} | ||
|
||
protected FeedbackQuestion getTypicalFeedbackQuestionForSession(FeedbackSession session) { | ||
|