use of teammates.common.exception.InvalidParametersException in project teammates by TEAMMATES.
the class CoursesLogicTest method testCreateCourseAndInstructor.
private void testCreateCourseAndInstructor() throws Exception {
/* Explanation: SUT has 5 paths. They are,
* path 1 - exit because the account doesn't' exist.
* path 2 - exit because the account exists but doesn't have instructor privileges.
* path 3 - exit because course creation failed.
* path 4 - exit because instructor creation failed.
* path 5 - success.
* Accordingly, we have 5 test cases.
*/
______TS("fails: account doesn't exist");
CourseAttributes c = CourseAttributes.builder("fresh-course-tccai", "Fresh course for tccai", ZoneId.of("America/Los_Angeles")).build();
@SuppressWarnings("deprecation") InstructorAttributes i = InstructorAttributes.builder("instructor-for-tccai", c.getId(), "Instructor for tccai", "ins.for.iccai@gmail.tmt").build();
try {
coursesLogic.createCourseAndInstructor(i.googleId, c.getId(), c.getName(), c.getTimeZone().getId());
signalFailureToDetectException();
} catch (AssertionError e) {
AssertHelper.assertContains("for a non-existent instructor", e.getMessage());
}
verifyAbsentInDatastore(c);
verifyAbsentInDatastore(i);
______TS("fails: account doesn't have instructor privileges");
AccountAttributes a = AccountAttributes.builder().withGoogleId(i.googleId).withName(i.name).withIsInstructor(false).withEmail(i.email).withInstitute("TEAMMATES Test Institute 5").withDefaultStudentProfileAttributes(i.googleId).build();
accountsDb.createAccount(a);
try {
coursesLogic.createCourseAndInstructor(i.googleId, c.getId(), c.getName(), c.getTimeZone().getId());
signalFailureToDetectException();
} catch (AssertionError e) {
AssertHelper.assertContains("doesn't have instructor privileges", e.getMessage());
}
verifyAbsentInDatastore(c);
verifyAbsentInDatastore(i);
______TS("fails: error during course creation");
a.isInstructor = true;
accountsDb.updateAccount(a);
CourseAttributes invalidCourse = CourseAttributes.builder("invalid id", "Fresh course for tccai", ZoneId.of("UTC")).build();
String expectedError = "\"" + invalidCourse.getId() + "\" is not acceptable to TEAMMATES as a/an course ID because" + " it is not in the correct format. " + "A course ID can contain letters, numbers, fullstops, hyphens, underscores, and dollar signs. " + "It cannot be longer than 40 characters, cannot be empty and cannot contain spaces.";
try {
coursesLogic.createCourseAndInstructor(i.googleId, invalidCourse.getId(), invalidCourse.getName(), invalidCourse.getTimeZone().getId());
signalFailureToDetectException();
} catch (InvalidParametersException e) {
assertEquals(expectedError, e.getMessage());
}
verifyAbsentInDatastore(invalidCourse);
verifyAbsentInDatastore(i);
______TS("fails: error during instructor creation due to duplicate instructor");
CourseAttributes courseWithDuplicateInstructor = CourseAttributes.builder("fresh-course-tccai", "Fresh course for tccai", ZoneId.of("UTC")).build();
// create a duplicate instructor
instructorsDb.createEntity(i);
try {
coursesLogic.createCourseAndInstructor(i.googleId, courseWithDuplicateInstructor.getId(), courseWithDuplicateInstructor.getName(), courseWithDuplicateInstructor.getTimeZone().getId());
signalFailureToDetectException();
} catch (AssertionError e) {
AssertHelper.assertContains("Unexpected exception while trying to create instructor for a new course", e.getMessage());
}
verifyAbsentInDatastore(courseWithDuplicateInstructor);
______TS("fails: error during instructor creation due to invalid parameters");
i.email = "ins.for.iccai.gmail.tmt";
try {
coursesLogic.createCourseAndInstructor(i.googleId, courseWithDuplicateInstructor.getId(), courseWithDuplicateInstructor.getName(), courseWithDuplicateInstructor.getTimeZone().getId());
signalFailureToDetectException();
} catch (AssertionError e) {
AssertHelper.assertContains("Unexpected exception while trying to create instructor for a new course", e.getMessage());
}
verifyAbsentInDatastore(courseWithDuplicateInstructor);
______TS("success: typical case");
i.email = "ins.for.iccai@gmail.tmt";
// remove the duplicate instructor object from the datastore.
instructorsDb.deleteInstructor(i.courseId, i.email);
coursesLogic.createCourseAndInstructor(i.googleId, courseWithDuplicateInstructor.getId(), courseWithDuplicateInstructor.getName(), courseWithDuplicateInstructor.getTimeZone().getId());
verifyPresentInDatastore(courseWithDuplicateInstructor);
verifyPresentInDatastore(i);
______TS("Null parameter");
try {
coursesLogic.createCourseAndInstructor(null, courseWithDuplicateInstructor.getId(), courseWithDuplicateInstructor.getName(), courseWithDuplicateInstructor.getTimeZone().getId());
signalFailureToDetectException();
} catch (AssertionError e) {
assertEquals(Const.StatusCodes.DBLEVEL_NULL_INPUT, e.getMessage());
}
}
use of teammates.common.exception.InvalidParametersException in project teammates by TEAMMATES.
the class CoursesLogicTest method testCreateCourse.
private void testCreateCourse() throws Exception {
______TS("typical case");
CourseAttributes c = CourseAttributes.builder("Computing101-fresh", "Basic Computing", ZoneId.of("Asia/Singapore")).build();
coursesLogic.createCourse(c.getId(), c.getName(), c.getTimeZone().getId());
verifyPresentInDatastore(c);
coursesLogic.deleteCourseCascade(c.getId());
______TS("Null parameter");
try {
coursesLogic.createCourse(null, c.getName(), c.getTimeZone().getId());
signalFailureToDetectException();
} catch (AssertionError e) {
assertEquals("Non-null value expected", e.getMessage());
}
______TS("Invalid time zone");
String invalidTimeZone = "Invalid Timezone";
try {
coursesLogic.createCourse(c.getId(), c.getName(), invalidTimeZone);
signalFailureToDetectException();
} catch (InvalidParametersException e) {
String expectedErrorMessage = getPopulatedErrorMessage(FieldValidator.TIME_ZONE_ERROR_MESSAGE, invalidTimeZone, FieldValidator.TIME_ZONE_FIELD_NAME, FieldValidator.REASON_UNAVAILABLE_AS_CHOICE);
assertEquals(expectedErrorMessage, e.getMessage());
}
}
use of teammates.common.exception.InvalidParametersException in project teammates by TEAMMATES.
the class CoursesLogicTest method testUpdateCourse.
private void testUpdateCourse() throws Exception {
CourseAttributes c = CourseAttributes.builder("Computing101-getthis", "Basic Computing Getting", ZoneId.of("UTC")).build();
coursesDb.createEntity(c);
______TS("Typical case");
String newName = "New Course Name";
String validTimeZone = "Asia/Singapore";
coursesLogic.updateCourse(c.getId(), newName, validTimeZone);
c.setName(newName);
c.setTimeZone(ZoneId.of(validTimeZone));
verifyPresentInDatastore(c);
______TS("Invalid time zone and name");
String emptyName = "";
String invalidTimeZone = "Invalid Timezone";
try {
coursesLogic.updateCourse(c.getId(), emptyName, invalidTimeZone);
signalFailureToDetectException();
} catch (InvalidParametersException e) {
String expectedErrorMessage = getPopulatedEmptyStringErrorMessage(FieldValidator.SIZE_CAPPED_NON_EMPTY_STRING_ERROR_MESSAGE_EMPTY_STRING, FieldValidator.COURSE_NAME_FIELD_NAME, FieldValidator.COURSE_NAME_MAX_LENGTH) + System.lineSeparator() + getPopulatedErrorMessage(FieldValidator.TIME_ZONE_ERROR_MESSAGE, invalidTimeZone, FieldValidator.TIME_ZONE_FIELD_NAME, FieldValidator.REASON_UNAVAILABLE_AS_CHOICE);
assertEquals(expectedErrorMessage, e.getMessage());
verifyPresentInDatastore(c);
}
}
use of teammates.common.exception.InvalidParametersException in project teammates by TEAMMATES.
the class FeedbackQuestionsLogicTest method testUpdateQuestion.
private void testUpdateQuestion() throws Exception {
______TS("standard update, no existing responses, with 'keep existing' policy");
FeedbackQuestionAttributes questionToUpdate = getQuestionFromDatastore("qn2InSession2InCourse2");
questionToUpdate.questionMetaData = new Text("new question text");
questionToUpdate.questionNumber = 3;
List<FeedbackParticipantType> newVisibility = new LinkedList<>();
newVisibility.add(FeedbackParticipantType.INSTRUCTORS);
questionToUpdate.showResponsesTo = newVisibility;
// Check keep existing policy.
String originalCourseId = questionToUpdate.courseId;
questionToUpdate.courseId = null;
fqLogic.updateFeedbackQuestion(questionToUpdate);
questionToUpdate.courseId = originalCourseId;
FeedbackQuestionAttributes updatedQuestion = fqLogic.getFeedbackQuestion(questionToUpdate.getId());
assertEquals(updatedQuestion.toString(), questionToUpdate.toString());
______TS("cascading update, non-destructive changes, existing responses are preserved");
questionToUpdate = getQuestionFromDatastore("qn2InSession1InCourse1");
questionToUpdate.questionMetaData = new Text("new question text 2");
questionToUpdate.numberOfEntitiesToGiveFeedbackTo = 2;
int numberOfResponses = frLogic.getFeedbackResponsesForQuestion(questionToUpdate.getId()).size();
fqLogic.updateFeedbackQuestion(questionToUpdate);
updatedQuestion = fqLogic.getFeedbackQuestion(questionToUpdate.getId());
assertEquals(updatedQuestion.toString(), questionToUpdate.toString());
assertEquals(frLogic.getFeedbackResponsesForQuestion(questionToUpdate.getId()).size(), numberOfResponses);
______TS("cascading update, destructive changes, delete all existing responses");
questionToUpdate = getQuestionFromDatastore("qn2InSession1InCourse1");
questionToUpdate.questionMetaData = new Text("new question text 3");
questionToUpdate.recipientType = FeedbackParticipantType.INSTRUCTORS;
assertFalse(frLogic.getFeedbackResponsesForQuestion(questionToUpdate.getId()).isEmpty());
fqLogic.updateFeedbackQuestion(questionToUpdate);
updatedQuestion = fqLogic.getFeedbackQuestion(questionToUpdate.getId());
assertEquals(updatedQuestion.toString(), questionToUpdate.toString());
assertEquals(frLogic.getFeedbackResponsesForQuestion(questionToUpdate.getId()).size(), 0);
______TS("failure: question does not exist");
questionToUpdate = getQuestionFromDatastore("qn3InSession1InCourse1");
fqLogic.deleteFeedbackQuestionCascade(questionToUpdate.getId());
try {
fqLogic.updateFeedbackQuestion(questionToUpdate);
signalFailureToDetectException("Expected EntityDoesNotExistException not caught.");
} catch (EntityDoesNotExistException e) {
assertEquals(e.getMessage(), "Trying to update a feedback question that does not exist.");
}
______TS("failure: invalid parameters");
questionToUpdate = getQuestionFromDatastore("qn3InSession1InCourse1");
questionToUpdate.giverType = FeedbackParticipantType.TEAMS;
questionToUpdate.recipientType = FeedbackParticipantType.OWN_TEAM_MEMBERS;
try {
fqLogic.updateFeedbackQuestion(questionToUpdate);
signalFailureToDetectException("Expected InvalidParametersException not caught.");
} catch (InvalidParametersException e) {
assertEquals(e.getMessage(), String.format(FieldValidator.PARTICIPANT_TYPE_TEAM_ERROR_MESSAGE, questionToUpdate.recipientType.toDisplayRecipientName(), questionToUpdate.giverType.toDisplayGiverName()));
}
}
use of teammates.common.exception.InvalidParametersException in project teammates by TEAMMATES.
the class FeedbackSessionsLogicTest method testCreateAndDeleteFeedbackSession.
private void testCreateAndDeleteFeedbackSession() throws InvalidParametersException, EntityAlreadyExistsException {
______TS("test create");
FeedbackSessionAttributes fs = getNewFeedbackSession();
fsLogic.createFeedbackSession(fs);
verifyPresentInDatastore(fs);
______TS("test create with invalid session name");
fs.setFeedbackSessionName("test & test");
try {
fsLogic.createFeedbackSession(fs);
signalFailureToDetectException();
} catch (Exception e) {
assertEquals("The provided feedback session name is not acceptable to TEAMMATES " + "as it cannot contain the following special html characters in brackets: " + "(< > " / ' &)", e.getMessage());
}
fs.setFeedbackSessionName("test %| test");
try {
fsLogic.createFeedbackSession(fs);
signalFailureToDetectException();
} catch (Exception e) {
assertEquals("\"test %| test\" is not acceptable to TEAMMATES as a/an feedback session name " + "because it contains invalid characters. A/An feedback session name " + "must start with an alphanumeric character, and cannot contain " + "any vertical bar (|) or percent sign (%).", e.getMessage());
}
______TS("test delete");
fs = getNewFeedbackSession();
// Create a question under the session to test for cascading during delete.
FeedbackQuestionAttributes fq = new FeedbackQuestionAttributes();
fq.feedbackSessionName = fs.getFeedbackSessionName();
fq.courseId = fs.getCourseId();
fq.questionNumber = 1;
fq.creatorEmail = fs.getCreatorEmail();
fq.numberOfEntitiesToGiveFeedbackTo = Const.MAX_POSSIBLE_RECIPIENTS;
fq.giverType = FeedbackParticipantType.STUDENTS;
fq.recipientType = FeedbackParticipantType.TEAMS;
fq.questionMetaData = new Text("question to be deleted through cascade");
fq.questionType = FeedbackQuestionType.TEXT;
fq.showResponsesTo = new ArrayList<>();
fq.showRecipientNameTo = new ArrayList<>();
fq.showGiverNameTo = new ArrayList<>();
fqLogic.createFeedbackQuestion(fq);
fsLogic.deleteFeedbackSessionCascade(fs.getFeedbackSessionName(), fs.getCourseId());
verifyAbsentInDatastore(fs);
verifyAbsentInDatastore(fq);
}
Aggregations