Search in sources :

Example 16 with EntityAlreadyExistsException

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

the class FeedbackSessionsDbTest method testCreateDeleteFeedbackSession.

@Test
public void testCreateDeleteFeedbackSession() throws Exception {
    ______TS("standard success case");
    FeedbackSessionAttributes fsa = getNewFeedbackSession();
    fsDb.createEntity(fsa);
    verifyPresentInDatastore(fsa);
    ______TS("duplicate");
    try {
        fsDb.createEntity(fsa);
        signalFailureToDetectException();
    } catch (EntityAlreadyExistsException e) {
        AssertHelper.assertContains(String.format(FeedbackSessionsDb.ERROR_CREATE_ENTITY_ALREADY_EXISTS, fsa.getEntityTypeAsString()) + fsa.getIdentificationString(), e.getMessage());
    }
    fsDb.deleteEntity(fsa);
    verifyAbsentInDatastore(fsa);
    ______TS("null params");
    try {
        fsDb.createEntity(null);
        signalFailureToDetectException();
    } catch (AssertionError e) {
        AssertHelper.assertContains(Const.StatusCodes.DBLEVEL_NULL_INPUT, e.getLocalizedMessage());
    }
    ______TS("invalid params");
    try {
        fsa.setStartTime(Instant.now());
        fsDb.createEntity(fsa);
        signalFailureToDetectException();
    } catch (InvalidParametersException e) {
        // start time is now after end time
        AssertHelper.assertContains("start time", e.getLocalizedMessage());
    }
}
Also used : FeedbackSessionAttributes(teammates.common.datatransfer.attributes.FeedbackSessionAttributes) EntityAlreadyExistsException(teammates.common.exception.EntityAlreadyExistsException) InvalidParametersException(teammates.common.exception.InvalidParametersException) Test(org.testng.annotations.Test)

Example 17 with EntityAlreadyExistsException

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

the class InstructorsDbTest method testCreateInstructor.

@Test
public void testCreateInstructor() throws Exception {
    ______TS("Success: create an instructor");
    String googleId = "valid.fresh.id";
    String courseId = "valid.course.Id";
    String name = "valid.name";
    String email = "valid@email.tmt";
    String role = Const.InstructorPermissionRoleNames.INSTRUCTOR_PERMISSION_ROLE_COOWNER;
    String displayedName = InstructorAttributes.DEFAULT_DISPLAY_NAME;
    InstructorPrivileges privileges = new InstructorPrivileges(Const.InstructorPermissionRoleNames.INSTRUCTOR_PERMISSION_ROLE_COOWNER);
    InstructorAttributes i = InstructorAttributes.builder(googleId, courseId, name, email).withRole(role).withDisplayedName(displayedName).withPrivileges(privileges).build();
    instructorsDb.deleteEntity(i);
    instructorsDb.createEntity(i);
    verifyPresentInDatastore(i);
    ______TS("Failure: create a duplicate instructor");
    try {
        instructorsDb.createEntity(i);
        signalFailureToDetectException();
    } catch (EntityAlreadyExistsException e) {
        AssertHelper.assertContains(String.format(InstructorsDb.ERROR_CREATE_ENTITY_ALREADY_EXISTS, "Instructor"), e.getMessage());
    }
    ______TS("Failure: create an instructor with invalid parameters");
    i.googleId = "invalid id with spaces";
    try {
        instructorsDb.createEntity(i);
        signalFailureToDetectException();
    } catch (InvalidParametersException e) {
        AssertHelper.assertContains(getPopulatedErrorMessage(FieldValidator.GOOGLE_ID_ERROR_MESSAGE, i.googleId, FieldValidator.GOOGLE_ID_FIELD_NAME, FieldValidator.REASON_INCORRECT_FORMAT, FieldValidator.GOOGLE_ID_MAX_LENGTH), e.getMessage());
    }
    i.googleId = "valid.fresh.id";
    i.email = "invalid.email.tmt";
    i.role = "role invalid";
    try {
        instructorsDb.createEntity(i);
        signalFailureToDetectException();
    } catch (InvalidParametersException e) {
        AssertHelper.assertContains(getPopulatedErrorMessage(FieldValidator.EMAIL_ERROR_MESSAGE, i.email, FieldValidator.EMAIL_FIELD_NAME, FieldValidator.REASON_INCORRECT_FORMAT, FieldValidator.EMAIL_MAX_LENGTH) + System.lineSeparator() + String.format(FieldValidator.ROLE_ERROR_MESSAGE, i.role), e.getMessage());
    }
    ______TS("Failure: null parameters");
    try {
        instructorsDb.createEntity(null);
        signalFailureToDetectException();
    } catch (AssertionError e) {
        assertEquals(Const.StatusCodes.DBLEVEL_NULL_INPUT, e.getMessage());
    }
}
Also used : EntityAlreadyExistsException(teammates.common.exception.EntityAlreadyExistsException) InvalidParametersException(teammates.common.exception.InvalidParametersException) InstructorPrivileges(teammates.common.datatransfer.InstructorPrivileges) InstructorAttributes(teammates.common.datatransfer.attributes.InstructorAttributes) Test(org.testng.annotations.Test)

Example 18 with EntityAlreadyExistsException

use of teammates.common.exception.EntityAlreadyExistsException 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 19 with EntityAlreadyExistsException

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

the class CoursesLogic method createCourseAndInstructor.

/**
 * Creates a Course object and an Instructor object for the Course.
 */
public void createCourseAndInstructor(String instructorGoogleId, String courseId, String courseName, String courseTimeZone) throws InvalidParametersException, EntityAlreadyExistsException {
    AccountAttributes courseCreator = accountsLogic.getAccount(instructorGoogleId);
    Assumption.assertNotNull("Trying to create a course for a non-existent instructor :" + instructorGoogleId, courseCreator);
    Assumption.assertTrue("Trying to create a course for a person who doesn't have instructor privileges :" + instructorGoogleId, courseCreator.isInstructor);
    createCourse(courseId, courseName, courseTimeZone);
    /* Create the initial instructor for the course */
    InstructorPrivileges privileges = new InstructorPrivileges(Const.InstructorPermissionRoleNames.INSTRUCTOR_PERMISSION_ROLE_COOWNER);
    InstructorAttributes instructor = InstructorAttributes.builder(instructorGoogleId, courseId, courseCreator.name, courseCreator.email).withPrivileges(privileges).build();
    try {
        instructorsLogic.createInstructor(instructor);
    } catch (EntityAlreadyExistsException | InvalidParametersException e) {
        // roll back the transaction
        coursesDb.deleteCourse(courseId);
        String errorMessage = "Unexpected exception while trying to create instructor for a new course " + System.lineSeparator() + instructor.toString() + System.lineSeparator() + TeammatesException.toStringWithStackTrace(e);
        Assumption.fail(errorMessage);
    }
}
Also used : AccountAttributes(teammates.common.datatransfer.attributes.AccountAttributes) EntityAlreadyExistsException(teammates.common.exception.EntityAlreadyExistsException) InvalidParametersException(teammates.common.exception.InvalidParametersException) InstructorPrivileges(teammates.common.datatransfer.InstructorPrivileges) InstructorAttributes(teammates.common.datatransfer.attributes.InstructorAttributes)

Example 20 with EntityAlreadyExistsException

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

the class InstructorFeedbackEditCopyAction method execute.

@Override
protected ActionResult execute() throws EntityDoesNotExistException {
    String newFeedbackSessionName = getRequestParamValue(Const.ParamsNames.COPIED_FEEDBACK_SESSION_NAME);
    String[] coursesIdToCopyTo = getRequestParamValues(Const.ParamsNames.COPIED_COURSES_ID);
    String originalFeedbackSessionName = getRequestParamValue(Const.ParamsNames.FEEDBACK_SESSION_NAME);
    String originalCourseId = getRequestParamValue(Const.ParamsNames.COURSE_ID);
    String nextUrl = getRequestParamValue(Const.ParamsNames.NEXT_URL);
    Assumption.assertPostParamNotNull(Const.ParamsNames.COURSE_ID, originalCourseId);
    Assumption.assertPostParamNotNull(Const.ParamsNames.FEEDBACK_SESSION_NAME, originalFeedbackSessionName);
    Assumption.assertPostParamNotNull(Const.ParamsNames.COPIED_FEEDBACK_SESSION_NAME, newFeedbackSessionName);
    if (nextUrl == null) {
        nextUrl = Const.ActionURIs.INSTRUCTOR_FEEDBACK_SESSIONS_PAGE;
    }
    if (coursesIdToCopyTo == null || coursesIdToCopyTo.length == 0) {
        return createAjaxResultWithErrorMessage(Const.StatusMessages.FEEDBACK_SESSION_COPY_NONESELECTED);
    }
    InstructorAttributes instructor = logic.getInstructorForGoogleId(originalCourseId, account.googleId);
    FeedbackSessionAttributes fsa = logic.getFeedbackSession(originalFeedbackSessionName, originalCourseId);
    gateKeeper.verifyAccessible(instructor, logic.getCourse(originalCourseId), Const.ParamsNames.INSTRUCTOR_PERMISSION_VIEW_SESSION_IN_SECTIONS);
    gateKeeper.verifyAccessible(instructor, fsa, false);
    try {
        // Check if there are no conflicting feedback sessions in all the courses
        List<String> conflictCourses = filterConflictsInCourses(newFeedbackSessionName, coursesIdToCopyTo);
        if (!conflictCourses.isEmpty()) {
            String commaSeparatedListOfCourses = StringHelper.toString(conflictCourses, ",");
            String errorToUser = String.format(Const.StatusMessages.FEEDBACK_SESSION_COPY_ALREADYEXISTS, newFeedbackSessionName, commaSeparatedListOfCourses);
            return createAjaxResultWithErrorMessage(errorToUser);
        }
        FeedbackSessionAttributes fs = null;
        // TODO: consider doing this as a batch insert
        for (String courseIdToCopyTo : coursesIdToCopyTo) {
            InstructorAttributes instructorForCourse = logic.getInstructorForGoogleId(courseIdToCopyTo, account.googleId);
            gateKeeper.verifyAccessible(instructorForCourse, logic.getCourse(courseIdToCopyTo), Const.ParamsNames.INSTRUCTOR_PERMISSION_MODIFY_SESSION);
            fs = logic.copyFeedbackSession(newFeedbackSessionName, courseIdToCopyTo, originalFeedbackSessionName, originalCourseId, instructor.email);
        }
        List<String> courses = Arrays.asList(coursesIdToCopyTo);
        String commaSeparatedListOfCourses = StringHelper.toString(courses, ",");
        statusToUser.add(new StatusMessage(Const.StatusMessages.FEEDBACK_SESSION_COPIED, StatusMessageColor.SUCCESS));
        statusToAdmin = "Copying to multiple feedback sessions.<br>" + "New Feedback Session <span class=\"bold\">(" + fs.getFeedbackSessionName() + ")</span> " + "for Courses: <br>" + commaSeparatedListOfCourses + "<br>" + "<span class=\"bold\">From:</span> " + fs.getStartTime() + "<span class=\"bold\"> to</span> " + fs.getEndTime() + "<br>" + "<span class=\"bold\">Session visible from:</span> " + fs.getSessionVisibleFromTime() + "<br>" + "<span class=\"bold\">Results visible from:</span> " + fs.getResultsVisibleFromTime() + "<br><br>" + "<span class=\"bold\">Instructions:</span> " + fs.getInstructions() + "<br>" + "Copied from <span class=\"bold\">(" + originalFeedbackSessionName + ")</span> for Course " + "<span class=\"bold\">[" + originalCourseId + "]</span> created.<br>";
        // so that the instructor can see the new feedback sessions
        return createAjaxResultWithoutClearingStatusMessage(new InstructorFeedbackEditCopyData(account, sessionToken, Config.getAppUrl(nextUrl).withParam(Const.ParamsNames.ERROR, Boolean.FALSE.toString()).withParam(Const.ParamsNames.USER_ID, account.googleId)));
    } catch (EntityAlreadyExistsException e) {
        // If conflicts are checked above, this will only occur via race condition
        setStatusForException(e, Const.StatusMessages.FEEDBACK_SESSION_EXISTS);
        return createAjaxResultWithErrorMessage(Const.StatusMessages.FEEDBACK_SESSION_EXISTS);
    } catch (InvalidParametersException e) {
        setStatusForException(e);
        return createAjaxResultWithErrorMessage(e.getMessage());
    }
}
Also used : FeedbackSessionAttributes(teammates.common.datatransfer.attributes.FeedbackSessionAttributes) EntityAlreadyExistsException(teammates.common.exception.EntityAlreadyExistsException) InstructorFeedbackEditCopyData(teammates.ui.pagedata.InstructorFeedbackEditCopyData) InvalidParametersException(teammates.common.exception.InvalidParametersException) InstructorAttributes(teammates.common.datatransfer.attributes.InstructorAttributes) StatusMessage(teammates.common.util.StatusMessage)

Aggregations

EntityAlreadyExistsException (teammates.common.exception.EntityAlreadyExistsException)23 InvalidParametersException (teammates.common.exception.InvalidParametersException)19 InstructorAttributes (teammates.common.datatransfer.attributes.InstructorAttributes)10 Test (org.testng.annotations.Test)7 FeedbackSessionAttributes (teammates.common.datatransfer.attributes.FeedbackSessionAttributes)7 StatusMessage (teammates.common.util.StatusMessage)7 CourseAttributes (teammates.common.datatransfer.attributes.CourseAttributes)4 FeedbackResponseAttributes (teammates.common.datatransfer.attributes.FeedbackResponseAttributes)4 ArrayList (java.util.ArrayList)3 InstructorPrivileges (teammates.common.datatransfer.InstructorPrivileges)3 FeedbackQuestionAttributes (teammates.common.datatransfer.attributes.FeedbackQuestionAttributes)3 StudentAttributes (teammates.common.datatransfer.attributes.StudentAttributes)3 EntityDoesNotExistException (teammates.common.exception.EntityDoesNotExistException)3 Text (com.google.appengine.api.datastore.Text)2 List (java.util.List)2 EnrollException (teammates.common.exception.EnrollException)2 InstructorCourseEnrollPageData (teammates.ui.pagedata.InstructorCourseEnrollPageData)2 InstructorCourseEnrollResultPageData (teammates.ui.pagedata.InstructorCourseEnrollResultPageData)2 Comparator (java.util.Comparator)1 CourseEnrollmentResult (teammates.common.datatransfer.CourseEnrollmentResult)1