Search in sources :

Example 51 with InvalidParametersException

use of teammates.common.exception.InvalidParametersException in project teammates by TEAMMATES.

the class BackDoorLogicTest method testPersistDataBundle.

@Test
public void testPersistDataBundle() throws Exception {
    ______TS("empty data bundle");
    String status = backDoorLogic.persistDataBundle(new DataBundle());
    assertEquals(Const.StatusCodes.BACKDOOR_STATUS_SUCCESS, status);
    backDoorLogic.removeDataBundle(dataBundle);
    backDoorLogic.persistDataBundle(dataBundle);
    verifyPresentInDatastore(dataBundle);
    ______TS("try to persist while entities exist");
    backDoorLogic.persistDataBundle(loadDataBundle("/FeedbackSessionResultsTest.json"));
    verifyPresentInDatastore(loadDataBundle("/FeedbackSessionResultsTest.json"));
    ______TS("null parameter");
    try {
        backDoorLogic.persistDataBundle(null);
        signalFailureToDetectException();
    } catch (InvalidParametersException e) {
        assertEquals(Const.StatusCodes.NULL_PARAMETER, e.errorCode);
    }
    ______TS("invalid parameters in an entity");
    CourseAttributes invalidCourse = CourseAttributes.builder("invalid id", "valid course name", ZoneId.of("UTC")).build();
    dataBundle = new DataBundle();
    dataBundle.courses.put("invalid", invalidCourse);
    try {
        backDoorLogic.persistDataBundle(dataBundle);
        signalFailureToDetectException();
    } catch (InvalidParametersException e) {
        assertTrue(e.getMessage().equals(getPopulatedErrorMessage(FieldValidator.COURSE_ID_ERROR_MESSAGE, "invalid id", FieldValidator.COURSE_ID_FIELD_NAME, FieldValidator.REASON_INCORRECT_FORMAT, FieldValidator.COURSE_ID_MAX_LENGTH)));
    }
// Not checking for invalid values in other entities because they
// should be checked at lower level methods
}
Also used : DataBundle(teammates.common.datatransfer.DataBundle) InvalidParametersException(teammates.common.exception.InvalidParametersException) CourseAttributes(teammates.common.datatransfer.attributes.CourseAttributes) Test(org.testng.annotations.Test)

Example 52 with InvalidParametersException

use of teammates.common.exception.InvalidParametersException in project teammates by TEAMMATES.

the class StudentsDbTest method testUpdateStudentWithoutDocument.

@Test
public void testUpdateStudentWithoutDocument() throws InvalidParametersException, EntityDoesNotExistException {
    // Create a new student with valid attributes
    StudentAttributes s = createNewStudent();
    studentsDb.updateStudentWithoutSearchability(s.course, s.email, "new-name", "new-team", "new-section", "new@email.com", "new.google.id", "lorem ipsum dolor si amet");
    ______TS("non-existent case");
    try {
        studentsDb.updateStudentWithoutSearchability("non-existent-course", "non@existent.email", "no-name", "non-existent-team", "non-existent-section", "non.existent.ID", "blah", "blah");
        signalFailureToDetectException();
    } catch (EntityDoesNotExistException e) {
        assertEquals(StudentsDb.ERROR_UPDATE_NON_EXISTENT_STUDENT + "non-existent-course/non@existent.email", e.getMessage());
    }
    // Only check first 2 params (course & email) which are used to identify the student entry.
    // The rest are actually allowed to be null.
    ______TS("null course case");
    try {
        studentsDb.updateStudentWithoutSearchability(null, s.email, "new-name", "new-team", "new-section", "new@email.com", "new.google.id", "lorem ipsum dolor si amet");
        signalFailureToDetectException();
    } catch (AssertionError ae) {
        assertEquals(Const.StatusCodes.DBLEVEL_NULL_INPUT, ae.getMessage());
    }
    ______TS("null email case");
    try {
        studentsDb.updateStudentWithoutSearchability(s.course, null, "new-name", "new-team", "new-section", "new@email.com", "new.google.id", "lorem ipsum dolor si amet");
        signalFailureToDetectException();
    } catch (AssertionError ae) {
        assertEquals(Const.StatusCodes.DBLEVEL_NULL_INPUT, ae.getMessage());
    }
    ______TS("duplicate email case");
    s = createNewStudent();
    // Create a second student with different email address
    StudentAttributes s2 = createNewStudent("valid2@email.com");
    try {
        studentsDb.updateStudentWithoutSearchability(s.course, s.email, "new-name", "new-team", "new-section", s2.email, "new.google.id", "lorem ipsum dolor si amet");
        signalFailureToDetectException();
    } catch (InvalidParametersException e) {
        assertEquals(StudentsDb.ERROR_UPDATE_EMAIL_ALREADY_USED + s2.name + "/" + s2.email, e.getMessage());
    }
    ______TS("typical success case");
    String originalEmail = s.email;
    s.name = "new-name-2";
    s.team = "new-team-2";
    s.email = "new-email-2@email.com";
    s.googleId = "new-id-2";
    s.comments = "this are new comments";
    studentsDb.updateStudentWithoutSearchability(s.course, originalEmail, s.name, s.team, s.section, s.email, s.googleId, s.comments);
    StudentAttributes updatedStudent = studentsDb.getStudentForEmail(s.course, s.email);
    assertTrue(updatedStudent.isEnrollInfoSameAs(s));
}
Also used : InvalidParametersException(teammates.common.exception.InvalidParametersException) StudentAttributes(teammates.common.datatransfer.attributes.StudentAttributes) EntityDoesNotExistException(teammates.common.exception.EntityDoesNotExistException) Test(org.testng.annotations.Test)

Example 53 with InvalidParametersException

use of teammates.common.exception.InvalidParametersException in project teammates by TEAMMATES.

the class StudentsDbTest method testCreateStudent.

@Test
public void testCreateStudent() throws Exception {
    StudentAttributes s = StudentAttributes.builder("course id", "valid student", "valid-fresh@email.com").withComments("").withTeam("validTeamName").withSection("validSectionName").withGoogleId("validGoogleId").withLastName("student").build();
    ______TS("fail : invalid params");
    s.course = "invalid id space";
    try {
        studentsDb.createEntity(s);
        signalFailureToDetectException();
    } catch (InvalidParametersException e) {
        AssertHelper.assertContains(getPopulatedErrorMessage(COURSE_ID_ERROR_MESSAGE, s.course, FieldValidator.COURSE_ID_FIELD_NAME, REASON_INCORRECT_FORMAT, FieldValidator.COURSE_ID_MAX_LENGTH), e.getMessage());
    }
    verifyAbsentInDatastore(s);
    ______TS("success : valid params");
    s.course = "valid-course";
    // remove possibly conflicting entity from the database
    studentsDb.deleteStudent(s.course, s.email);
    studentsDb.createEntity(s);
    verifyPresentInDatastore(s);
    StudentAttributes retrievedStudent = studentsDb.getStudentForGoogleId(s.course, s.googleId);
    assertTrue(retrievedStudent.isEnrollInfoSameAs(s));
    assertNull(studentsDb.getStudentForGoogleId(s.course + "not existing", s.googleId));
    assertNull(studentsDb.getStudentForGoogleId(s.course, s.googleId + "not existing"));
    assertNull(studentsDb.getStudentForGoogleId(s.course + "not existing", s.googleId + "not existing"));
    ______TS("fail : duplicate");
    try {
        studentsDb.createEntity(s);
        signalFailureToDetectException();
    } catch (EntityAlreadyExistsException e) {
        AssertHelper.assertContains(String.format(StudentsDb.ERROR_CREATE_ENTITY_ALREADY_EXISTS, s.getEntityTypeAsString()) + s.getIdentificationString(), e.getMessage());
    }
    ______TS("null params check");
    try {
        studentsDb.createEntity(null);
        signalFailureToDetectException();
    } catch (AssertionError ae) {
        assertEquals(Const.StatusCodes.DBLEVEL_NULL_INPUT, ae.getMessage());
    }
}
Also used : EntityAlreadyExistsException(teammates.common.exception.EntityAlreadyExistsException) InvalidParametersException(teammates.common.exception.InvalidParametersException) StudentAttributes(teammates.common.datatransfer.attributes.StudentAttributes) Test(org.testng.annotations.Test)

Example 54 with InvalidParametersException

use of teammates.common.exception.InvalidParametersException in project teammates by TEAMMATES.

the class DataMigrationForSanitizedDataInStudentAttributes method updateStudent.

private void updateStudent(String originalEmail, StudentAttributes student) throws InvalidParametersException, EntityDoesNotExistException {
    studentsDb.verifyStudentExists(student.course, originalEmail);
    StudentAttributes originalStudent = studentsDb.getStudentForEmail(student.course, originalEmail);
    // prepare new student
    student.updateWithExistingRecord(originalStudent);
    if (!student.isValid()) {
        throw new InvalidParametersException(student.getInvalidityInfo());
    }
    studentsDb.updateStudent(student.course, originalEmail, student.name, student.team, student.section, student.email, student.googleId, student.comments, true);
}
Also used : InvalidParametersException(teammates.common.exception.InvalidParametersException) StudentAttributes(teammates.common.datatransfer.attributes.StudentAttributes)

Example 55 with InvalidParametersException

use of teammates.common.exception.InvalidParametersException in project teammates by TEAMMATES.

the class CoursesLogic method validateAndCreateCourseAttributes.

/**
 * Checks that {@code courseTimeZone} is valid and then returns a {@link CourseAttributes}.
 * Field validation is usually done in {@link CoursesDb} by calling {@link CourseAttributes#getInvalidityInfo()}.
 * However, a {@link CourseAttributes} cannot be created with an invalid time zone string.
 * Hence, validation of this field is carried out here.
 *
 * @throws InvalidParametersException containing error messages for all fields if {@code courseTimeZone} is invalid
 */
private CourseAttributes validateAndCreateCourseAttributes(String courseId, String courseName, String courseTimeZone) throws InvalidParametersException {
    // Imitate `CourseAttributes.getInvalidityInfo`
    FieldValidator validator = new FieldValidator();
    String timeZoneErrorMessage = validator.getInvalidityInfoForTimeZone(courseTimeZone);
    if (!timeZoneErrorMessage.isEmpty()) {
        // Leave validation of other fields to `CourseAttributes.getInvalidityInfo`
        CourseAttributes dummyCourse = CourseAttributes.builder(courseId, courseName, Const.DEFAULT_TIME_ZONE).build();
        List<String> errors = dummyCourse.getInvalidityInfo();
        errors.add(timeZoneErrorMessage);
        // Imitate exception throwing in `CoursesDb`
        throw new InvalidParametersException(errors);
    }
    // If time zone field is valid, leave validation  of other fields to `CoursesDb` like usual
    return CourseAttributes.builder(courseId, courseName, ZoneId.of(courseTimeZone)).build();
}
Also used : FieldValidator(teammates.common.util.FieldValidator) InvalidParametersException(teammates.common.exception.InvalidParametersException) CourseAttributes(teammates.common.datatransfer.attributes.CourseAttributes)

Aggregations

InvalidParametersException (teammates.common.exception.InvalidParametersException)83 EntityDoesNotExistException (teammates.common.exception.EntityDoesNotExistException)37 InstructorAttributes (teammates.common.datatransfer.attributes.InstructorAttributes)24 StatusMessage (teammates.common.util.StatusMessage)21 Test (org.testng.annotations.Test)19 EntityAlreadyExistsException (teammates.common.exception.EntityAlreadyExistsException)19 FeedbackSessionAttributes (teammates.common.datatransfer.attributes.FeedbackSessionAttributes)13 StudentAttributes (teammates.common.datatransfer.attributes.StudentAttributes)12 CourseAttributes (teammates.common.datatransfer.attributes.CourseAttributes)9 FeedbackSession (teammates.storage.entity.FeedbackSession)9 Text (com.google.appengine.api.datastore.Text)8 FeedbackQuestionAttributes (teammates.common.datatransfer.attributes.FeedbackQuestionAttributes)8 AccountAttributes (teammates.common.datatransfer.attributes.AccountAttributes)6 FeedbackResponseAttributes (teammates.common.datatransfer.attributes.FeedbackResponseAttributes)6 ArrayList (java.util.ArrayList)5 InstructorPrivileges (teammates.common.datatransfer.InstructorPrivileges)5 VoidWork (com.googlecode.objectify.VoidWork)4 StudentProfileAttributes (teammates.common.datatransfer.attributes.StudentProfileAttributes)4 PageData (teammates.ui.pagedata.PageData)4 FeedbackResponseCommentAttributes (teammates.common.datatransfer.attributes.FeedbackResponseCommentAttributes)3