-
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
* Migrate CreateFeedbackQuestionActionTest Test execute method with some positive and negative test cases Test checkSpecificAccessControl method with some positive and negative test cases * Add whitespace for better style * Fix test case naming Added type of exception thrown --------- Co-authored-by: Jason Qiu <[email protected]>
- Loading branch information
1 parent
7aeb740
commit 2076737
Showing
1 changed file
with
280 additions
and
0 deletions.
There are no files selected for viewing
280 changes: 280 additions & 0 deletions
280
src/test/java/teammates/sqlui/webapi/CreateFeedbackQuestionActionTest.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,280 @@ | ||
package teammates.sqlui.webapi; | ||
|
||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.when; | ||
import static teammates.common.util.Const.InstructorPermissionRoleNames.INSTRUCTOR_PERMISSION_ROLE_OBSERVER; | ||
|
||
import java.util.ArrayList; | ||
|
||
import org.testng.annotations.BeforeMethod; | ||
import org.testng.annotations.Test; | ||
|
||
import teammates.common.datatransfer.FeedbackParticipantType; | ||
import teammates.common.datatransfer.InstructorPrivileges; | ||
import teammates.common.datatransfer.questions.FeedbackContributionQuestionDetails; | ||
import teammates.common.datatransfer.questions.FeedbackQuestionType; | ||
import teammates.common.datatransfer.questions.FeedbackTextQuestionDetails; | ||
import teammates.common.util.Const; | ||
import teammates.storage.sqlentity.Course; | ||
import teammates.storage.sqlentity.FeedbackQuestion; | ||
import teammates.storage.sqlentity.FeedbackSession; | ||
import teammates.storage.sqlentity.Instructor; | ||
import teammates.ui.output.FeedbackQuestionData; | ||
import teammates.ui.output.NumberOfEntitiesToGiveFeedbackToSetting; | ||
import teammates.ui.request.FeedbackQuestionCreateRequest; | ||
import teammates.ui.webapi.CreateFeedbackQuestionAction; | ||
import teammates.ui.webapi.JsonResult; | ||
|
||
/** | ||
* SUT: {@link CreateFeedbackQuestionAction}. | ||
*/ | ||
public class CreateFeedbackQuestionActionTest extends BaseActionTest<CreateFeedbackQuestionAction> { | ||
|
||
private Instructor typicalInstructor; | ||
private Course typicalCourse; | ||
private FeedbackSession typicalFeedbackSession; | ||
|
||
@Override | ||
String getActionUri() { | ||
return Const.ResourceURIs.QUESTION; | ||
} | ||
|
||
@Override | ||
String getRequestMethod() { | ||
return POST; | ||
} | ||
|
||
@BeforeMethod | ||
void setUp() { | ||
typicalInstructor = getTypicalInstructor(); | ||
typicalCourse = typicalInstructor.getCourse(); | ||
typicalFeedbackSession = getTypicalFeedbackSessionForCourse(typicalCourse); | ||
} | ||
|
||
@Test | ||
void testExecute_typicalCase_success() throws Exception { | ||
FeedbackQuestion createdQuestion = getCreatedFeedbackQuestion(); | ||
|
||
when(mockLogic.getFeedbackSession(typicalFeedbackSession.getName(), typicalCourse.getId())) | ||
.thenReturn(typicalFeedbackSession); | ||
when(mockLogic.createFeedbackQuestion(any(FeedbackQuestion.class))).thenReturn(createdQuestion); | ||
|
||
String[] params = { | ||
Const.ParamsNames.COURSE_ID, typicalCourse.getId(), | ||
Const.ParamsNames.FEEDBACK_SESSION_NAME, typicalFeedbackSession.getName(), | ||
}; | ||
|
||
FeedbackQuestionCreateRequest createRequest = getTypicalTextQuestionCreateRequest(); | ||
CreateFeedbackQuestionAction a = getAction(createRequest, params); | ||
JsonResult r = getJsonResult(a); | ||
|
||
FeedbackQuestionData response = (FeedbackQuestionData) r.getOutput(); | ||
|
||
assertEquals(response.getQuestionNumber(), createdQuestion.getQuestionNumber().intValue()); | ||
assertEquals(response.getQuestionNumber(), 2); | ||
|
||
assertEquals(response.getQuestionDescription(), "this is the description"); | ||
assertEquals(response.getQuestionDescription(), createdQuestion.getDescription()); | ||
|
||
assertEquals(response.getQuestionBrief(), "this is the brief"); | ||
assertEquals(response.getQuestionBrief(), createdQuestion.getQuestionDetailsCopy().getQuestionText()); | ||
|
||
assertEquals(response.getQuestionType(), FeedbackQuestionType.TEXT); | ||
assertEquals(response.getQuestionType(), createdQuestion.getQuestionDetailsCopy().getQuestionType()); | ||
|
||
assertEquals(response.getGiverType(), FeedbackParticipantType.STUDENTS); | ||
assertEquals(response.getGiverType(), createdQuestion.getGiverType()); | ||
|
||
assertEquals(response.getRecipientType(), FeedbackParticipantType.INSTRUCTORS); | ||
assertEquals(response.getRecipientType(), createdQuestion.getRecipientType()); | ||
|
||
assertEquals(response.getNumberOfEntitiesToGiveFeedbackToSetting(), | ||
NumberOfEntitiesToGiveFeedbackToSetting.UNLIMITED); | ||
|
||
assertTrue(response.getShowResponsesTo().isEmpty()); | ||
assertTrue(createdQuestion.getShowResponsesTo().isEmpty()); | ||
|
||
assertTrue(response.getShowGiverNameTo().isEmpty()); | ||
assertTrue(createdQuestion.getShowGiverNameTo().isEmpty()); | ||
|
||
assertTrue(response.getShowRecipientNameTo().isEmpty()); | ||
assertTrue(createdQuestion.getShowRecipientNameTo().isEmpty()); | ||
} | ||
|
||
@Test | ||
void testExecute_missingParameters_throwsInvalidHttpParameterException() { | ||
verifyHttpParameterFailure(); | ||
} | ||
|
||
@Test | ||
void testExecute_missingCourseId_throwsInvalidHttpParameterException() { | ||
String[] params = { | ||
Const.ParamsNames.FEEDBACK_SESSION_NAME, typicalFeedbackSession.getName(), | ||
}; | ||
|
||
verifyHttpParameterFailure(params); | ||
} | ||
|
||
@Test | ||
void testExecute_missingFeedbackSessionName_throwsInvalidHttpParameterException() { | ||
String[] params = { | ||
Const.ParamsNames.COURSE_ID, typicalCourse.getId(), | ||
}; | ||
|
||
verifyHttpParameterFailure(params); | ||
} | ||
|
||
@Test | ||
void testExecute_nullQuestionType_throwsInvalidHttpRequestBodyException() { | ||
String[] params = { | ||
Const.ParamsNames.COURSE_ID, typicalCourse.getId(), | ||
Const.ParamsNames.FEEDBACK_SESSION_NAME, typicalFeedbackSession.getName(), | ||
}; | ||
|
||
FeedbackQuestionCreateRequest createRequest = getTypicalTextQuestionCreateRequest(); | ||
createRequest.setQuestionType(null); | ||
verifyHttpRequestBodyFailure(createRequest, params); | ||
} | ||
|
||
@Test | ||
void testExecute_invalidQuestionNumber_throwsInvalidHttpRequestBodyException() { | ||
String[] params = { | ||
Const.ParamsNames.COURSE_ID, typicalCourse.getId(), | ||
Const.ParamsNames.FEEDBACK_SESSION_NAME, typicalFeedbackSession.getName(), | ||
}; | ||
|
||
FeedbackQuestionCreateRequest createRequest = getTypicalTextQuestionCreateRequest(); | ||
createRequest.setQuestionNumber(-1); | ||
|
||
verifyHttpRequestBodyFailure(createRequest, params); | ||
} | ||
|
||
@Test | ||
void testExecute_invalidGiverType_throwsInvalidHttpRequestBodyException() { | ||
when(mockLogic.getFeedbackSession(typicalFeedbackSession.getName(), typicalCourse.getId())) | ||
.thenReturn(typicalFeedbackSession); | ||
|
||
String[] params = { | ||
Const.ParamsNames.COURSE_ID, typicalCourse.getId(), | ||
Const.ParamsNames.FEEDBACK_SESSION_NAME, typicalFeedbackSession.getName(), | ||
}; | ||
|
||
FeedbackQuestionCreateRequest createRequest = getTypicalTextQuestionCreateRequest(); | ||
createRequest.setQuestionType(FeedbackQuestionType.CONTRIB); | ||
createRequest.setQuestionDetails(new FeedbackContributionQuestionDetails()); | ||
createRequest.setGiverType(FeedbackParticipantType.NONE); | ||
|
||
verifyHttpRequestBodyFailure(createRequest, params); | ||
} | ||
|
||
@Test | ||
void testExecute_invalidRecommendedLength_throwsInvalidHttpRequestBodyException() { | ||
when(mockLogic.getFeedbackSession(typicalFeedbackSession.getName(), typicalCourse.getId())) | ||
.thenReturn(typicalFeedbackSession); | ||
|
||
String[] params = { | ||
Const.ParamsNames.COURSE_ID, typicalCourse.getId(), | ||
Const.ParamsNames.FEEDBACK_SESSION_NAME, typicalFeedbackSession.getName(), | ||
}; | ||
|
||
FeedbackQuestionCreateRequest createRequest = getTypicalTextQuestionCreateRequest(); | ||
FeedbackTextQuestionDetails textQuestionDetails = new FeedbackTextQuestionDetails(); | ||
textQuestionDetails.setRecommendedLength(-1); | ||
createRequest.setQuestionDetails(textQuestionDetails); | ||
|
||
verifyHttpRequestBodyFailure(createRequest, params); | ||
} | ||
|
||
@Test | ||
void testExecute_emptyQuestionBrief_throwsInvalidHttpRequestBodyException() { | ||
String[] params = { | ||
Const.ParamsNames.COURSE_ID, typicalCourse.getId(), | ||
Const.ParamsNames.FEEDBACK_SESSION_NAME, typicalFeedbackSession.getName(), | ||
}; | ||
|
||
FeedbackQuestionCreateRequest createRequest = getTypicalTextQuestionCreateRequest(); | ||
createRequest.setQuestionBrief(""); | ||
|
||
verifyHttpRequestBodyFailure(createRequest, params); | ||
} | ||
|
||
@Test | ||
void testExecute_nonExistentFeedbackSession_throwsEntityNotFoundException() { | ||
when(mockLogic.getFeedbackSession(typicalFeedbackSession.getName(), typicalCourse.getId())).thenReturn(null); | ||
|
||
String[] params = { | ||
Const.ParamsNames.COURSE_ID, typicalCourse.getId(), | ||
Const.ParamsNames.FEEDBACK_SESSION_NAME, typicalFeedbackSession.getName(), | ||
}; | ||
loginAsInstructor(typicalInstructor.getGoogleId()); | ||
|
||
verifyEntityNotFoundAcl(params); | ||
} | ||
|
||
@Test | ||
void testSpecificAccessControl_withModifySessionPrivilege_canAccess() { | ||
when(mockLogic.getFeedbackSession(typicalFeedbackSession.getName(), typicalCourse.getId())) | ||
.thenReturn(typicalFeedbackSession); | ||
when(mockLogic.getInstructorByGoogleId(typicalCourse.getId(), typicalInstructor.getGoogleId())) | ||
.thenReturn(typicalInstructor); | ||
|
||
String[] params = { | ||
Const.ParamsNames.COURSE_ID, typicalCourse.getId(), | ||
Const.ParamsNames.FEEDBACK_SESSION_NAME, typicalFeedbackSession.getName(), | ||
}; | ||
|
||
verifyCanAccess(params); | ||
} | ||
|
||
@Test | ||
void testSpecificAccessControl_withoutModifySessionPrivilege_cannotAccess() { | ||
// create instructor without modify session privilege | ||
Instructor instructorWithoutAccess = getTypicalInstructor(); | ||
instructorWithoutAccess.setPrivileges(new InstructorPrivileges(INSTRUCTOR_PERMISSION_ROLE_OBSERVER)); | ||
|
||
when(mockLogic.getFeedbackSession(typicalFeedbackSession.getName(), typicalCourse.getId())) | ||
.thenReturn(typicalFeedbackSession); | ||
when(mockLogic.getInstructorByGoogleId(typicalCourse.getId(), typicalInstructor.getGoogleId())) | ||
.thenReturn(instructorWithoutAccess); | ||
|
||
String[] params = { | ||
Const.ParamsNames.COURSE_ID, typicalCourse.getId(), | ||
Const.ParamsNames.FEEDBACK_SESSION_NAME, typicalFeedbackSession.getName(), | ||
}; | ||
|
||
loginAsInstructor(instructorWithoutAccess.getGoogleId()); | ||
verifyCannotAccess(params); | ||
} | ||
|
||
private FeedbackQuestionCreateRequest getTypicalTextQuestionCreateRequest() { | ||
FeedbackQuestionCreateRequest createRequest = new FeedbackQuestionCreateRequest(); | ||
createRequest.setQuestionNumber(2); | ||
createRequest.setQuestionBrief("this is the brief"); | ||
createRequest.setQuestionDescription("this is the description"); | ||
FeedbackTextQuestionDetails textQuestionDetails = new FeedbackTextQuestionDetails(); | ||
textQuestionDetails.setRecommendedLength(800); | ||
createRequest.setQuestionDetails(textQuestionDetails); | ||
createRequest.setQuestionType(FeedbackQuestionType.TEXT); | ||
createRequest.setGiverType(FeedbackParticipantType.STUDENTS); | ||
createRequest.setRecipientType(FeedbackParticipantType.INSTRUCTORS); | ||
createRequest.setNumberOfEntitiesToGiveFeedbackToSetting(NumberOfEntitiesToGiveFeedbackToSetting.UNLIMITED); | ||
|
||
createRequest.setShowResponsesTo(new ArrayList<>()); | ||
createRequest.setShowGiverNameTo(new ArrayList<>()); | ||
createRequest.setShowRecipientNameTo(new ArrayList<>()); | ||
|
||
return createRequest; | ||
} | ||
|
||
private FeedbackQuestion getCreatedFeedbackQuestion() { | ||
FeedbackTextQuestionDetails textQuestionDetails = | ||
new FeedbackTextQuestionDetails("this is the brief"); | ||
textQuestionDetails.setRecommendedLength(800); | ||
|
||
return FeedbackQuestion.makeQuestion(typicalFeedbackSession, 2, | ||
"this is the description", FeedbackParticipantType.STUDENTS, | ||
FeedbackParticipantType.INSTRUCTORS, Const.MAX_POSSIBLE_RECIPIENTS, new ArrayList<>(), | ||
new ArrayList<>(), new ArrayList<>(), textQuestionDetails); | ||
} | ||
|
||
} |