@Test public void testCreateStudent() throws EntityAlreadyExistsException, InvalidParametersException { StudentAttributes s = new StudentAttributes(); s.name = "valid student"; s.email = "*****@*****.**"; s.team = "validTeamName"; s.comments = ""; s.googleId = "validGoogleId"; ______TS("fail : invalid params"); s.course = "invalid id space"; try { studentsDb.createEntity(s); Assert.fail(); } catch (InvalidParametersException e) { AssertHelper.assertContains( String.format(COURSE_ID_ERROR_MESSAGE, s.course, REASON_INCORRECT_FORMAT), e.getMessage()); } LogicTest.verifyAbsentInDatastore(s); ______TS("success : valid params"); s.course = "valid-course"; studentsDb.createEntity(s); LogicTest.verifyPresentInDatastore(s); StudentAttributes retrievedStudent = studentsDb.getStudentForGoogleId(s.course, s.googleId); assertEquals(true, retrievedStudent.isEnrollInfoSameAs(s)); assertEquals(null, studentsDb.getStudentForGoogleId(s.course + "not existing", s.googleId)); assertEquals(null, studentsDb.getStudentForGoogleId(s.course, s.googleId + "not existing")); assertEquals( null, studentsDb.getStudentForGoogleId(s.course + "not existing", s.googleId + "not existing")); ______TS("fail : duplicate"); try { studentsDb.createEntity(s); Assert.fail(); } 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); Assert.fail(); } catch (AssertionError a) { assertEquals(Const.StatusCodes.DBLEVEL_NULL_INPUT, a.getMessage()); } }
private StudentAttributes createNewStudent(String email) throws InvalidParametersException { StudentAttributes s = new StudentAttributes(); s.name = "valid student 2"; s.course = "valid-course"; s.email = email; s.team = "valid team name"; s.comments = ""; s.googleId = ""; try { studentsDb.createEntity(s); } catch (EntityAlreadyExistsException e) { // Okay if it's already inside } return s; }
@Test public void testExecuteAndPostProcess() throws Exception { StudentsDb studentsDb = new StudentsDb(); AccountsDb accountsDb = new AccountsDb(); StudentAttributes student1InCourse1 = dataBundle.students.get("student1InCourse1"); student1InCourse1 = studentsDb.getStudentForGoogleId(student1InCourse1.course, student1InCourse1.googleId); gaeSimulation.loginAsStudent(student1InCourse1.googleId); ______TS("not enough parameters"); verifyAssumptionFailure(); ______TS("invalid key"); String invalidKey = StringHelper.encrypt("invalid key"); String[] submissionParams = new String[] { Const.ParamsNames.REGKEY, invalidKey, Const.ParamsNames.NEXT_URL, Const.ActionURIs.STUDENT_HOME_PAGE }; try { StudentCourseJoinAuthenticatedAction authenticatedAction = getAction(submissionParams); getRedirectResult(authenticatedAction); } catch (UnauthorizedAccessException uae) { assertEquals("No student with given registration key:" + invalidKey, uae.getMessage()); } ______TS("already registered student"); submissionParams = new String[] { Const.ParamsNames.REGKEY, StringHelper.encrypt(student1InCourse1.key), Const.ParamsNames.NEXT_URL, Const.ActionURIs.STUDENT_PROFILE_PAGE }; StudentCourseJoinAuthenticatedAction authenticatedAction = getAction(submissionParams); RedirectResult redirectResult = getRedirectResult(authenticatedAction); assertEquals( Const.ActionURIs.STUDENT_HOME_PAGE + "?error=true&user="******"You (student1InCourse1) have already joined this course", redirectResult.getStatusMessage()); /*______TS("student object belongs to another account"); StudentAttributes student2InCourse1 = dataBundle.students .get("student2InCourse1"); student2InCourse1 = studentsDb.getStudentForGoogleId( student2InCourse1.course, student2InCourse1.googleId); submissionParams = new String[] { Const.ParamsNames.REGKEY, StringHelper.encrypt(student2InCourse1.key), Const.ParamsNames.NEXT_URL, Const.ActionURIs.STUDENT_HOME_PAGE }; authenticatedAction = getAction(submissionParams); redirectResult = getRedirectResult(authenticatedAction); assertEquals( Const.ActionURIs.STUDENT_HOME_PAGE + "?persistencecourse=" + student1InCourse1.course + "&error=true&user="******"The join link used belongs to a different user" + " whose Google ID is stude..ourse1 " + "(only part of the Google ID is shown to protect privacy)." + " If that Google ID is owned by you, " + "please logout and re-login using that Google account." + " If it doesn’t belong to you, please " + "<a href=\"mailto:" + Config.SUPPORT_EMAIL + "?body=Your name:%0AYour course:%0AYour university:\">" + "contact us</a> so that we can investigate.", redirectResult.getStatusMessage()); */ ______TS("join course with no feedback sessions, profile is empty"); AccountAttributes studentWithEmptyProfile = dataBundle.accounts.get("noFSStudent"); studentWithEmptyProfile = accountsDb.getAccount(studentWithEmptyProfile.googleId, true); assertNotNull(studentWithEmptyProfile.studentProfile); assertEquals("", studentWithEmptyProfile.studentProfile.pictureKey); assertEquals("", studentWithEmptyProfile.studentProfile.shortName); assertEquals("", studentWithEmptyProfile.studentProfile.nationality); assertEquals("", studentWithEmptyProfile.studentProfile.moreInfo); assertEquals("", studentWithEmptyProfile.studentProfile.email); StudentAttributes studentWithEmptyProfileAttributes = dataBundle.students.get("noFSStudentWithNoProfile"); studentWithEmptyProfileAttributes = studentsDb.getStudentForEmail( studentWithEmptyProfileAttributes.course, studentWithEmptyProfileAttributes.email); gaeSimulation.loginUser("idOfNoFSStudent"); submissionParams = new String[] { Const.ParamsNames.REGKEY, StringHelper.encrypt(studentWithEmptyProfileAttributes.key), Const.ParamsNames.NEXT_URL, Const.ActionURIs.STUDENT_HOME_PAGE }; authenticatedAction = getAction(submissionParams); redirectResult = getRedirectResult(authenticatedAction); assertEquals( Const.ActionURIs.STUDENT_HOME_PAGE + "?persistencecourse=idOfCourseNoEvals" + "&error=false&user=idOfNoFSStudent", redirectResult.getDestinationWithParams()); assertFalse(redirectResult.isError); assertEquals( String.format( Const.StatusMessages.STUDENT_COURSE_JOIN_SUCCESSFUL, "[idOfCourseNoEvals] Typical Course 3 with 0 Evals") + "<br>" + String.format( Const.StatusMessages.HINT_FOR_NO_SESSIONS_STUDENT, "[idOfCourseNoEvals] Typical Course 3 with 0 Evals") + "<br>" + Const.StatusMessages.STUDENT_UPDATE_PROFILE, redirectResult.getStatusMessage()); ______TS("join course with no feedback sessions, profile has only one missing field"); AccountAttributes studentWithoutProfilePicture = dataBundle.accounts.get("noFSStudent2"); studentWithoutProfilePicture = accountsDb.getAccount(studentWithoutProfilePicture.googleId, true); assertNotNull(studentWithoutProfilePicture.studentProfile); assertEquals("", studentWithoutProfilePicture.studentProfile.pictureKey); assertFalse(studentWithoutProfilePicture.studentProfile.nationality.isEmpty()); assertFalse(studentWithoutProfilePicture.studentProfile.shortName.isEmpty()); assertFalse(studentWithoutProfilePicture.studentProfile.moreInfo.isEmpty()); assertFalse(studentWithoutProfilePicture.studentProfile.email.isEmpty()); StudentAttributes studentWithoutProfilePictureAttributes = dataBundle.students.get("noFSStudentWithPartialProfile"); studentWithoutProfilePictureAttributes = studentsDb.getStudentForEmail( studentWithoutProfilePictureAttributes.course, studentWithoutProfilePictureAttributes.email); gaeSimulation.loginUser("idOfNoFSStudent2"); submissionParams = new String[] { Const.ParamsNames.REGKEY, StringHelper.encrypt(studentWithoutProfilePictureAttributes.key), Const.ParamsNames.NEXT_URL, Const.ActionURIs.STUDENT_HOME_PAGE }; authenticatedAction = getAction(submissionParams); redirectResult = getRedirectResult(authenticatedAction); assertEquals( Const.ActionURIs.STUDENT_HOME_PAGE + "?persistencecourse=idOfCourseNoEvals" + "&error=false&user=idOfNoFSStudent2", redirectResult.getDestinationWithParams()); assertFalse(redirectResult.isError); assertEquals( String.format( Const.StatusMessages.STUDENT_COURSE_JOIN_SUCCESSFUL, "[idOfCourseNoEvals] Typical Course 3 with 0 Evals") + "<br>" + String.format( Const.StatusMessages.HINT_FOR_NO_SESSIONS_STUDENT, "[idOfCourseNoEvals] Typical Course 3 with 0 Evals") + "<br>" + Const.StatusMessages.STUDENT_UPDATE_PROFILE_PICTURE, redirectResult.getStatusMessage()); ______TS("join course with no feedback sessions, profile has no missing field"); AccountAttributes studentWithFullProfile = dataBundle.accounts.get("noFSStudent3"); studentWithFullProfile = accountsDb.getAccount(studentWithFullProfile.googleId, true); assertNotNull(studentWithFullProfile.studentProfile); assertFalse(studentWithFullProfile.studentProfile.pictureKey.isEmpty()); assertFalse(studentWithoutProfilePicture.studentProfile.nationality.isEmpty()); assertFalse(studentWithoutProfilePicture.studentProfile.shortName.isEmpty()); assertFalse(studentWithoutProfilePicture.studentProfile.moreInfo.isEmpty()); assertFalse(studentWithoutProfilePicture.studentProfile.email.isEmpty()); StudentAttributes studentWithFullProfileAttributes = dataBundle.students.get("noFSStudentWithFullProfile"); studentWithFullProfileAttributes = studentsDb.getStudentForEmail( studentWithFullProfileAttributes.course, studentWithFullProfileAttributes.email); gaeSimulation.loginUser("idOfNoFSStudent3"); submissionParams = new String[] { Const.ParamsNames.REGKEY, StringHelper.encrypt(studentWithFullProfileAttributes.key), Const.ParamsNames.NEXT_URL, Const.ActionURIs.STUDENT_HOME_PAGE }; authenticatedAction = getAction(submissionParams); redirectResult = getRedirectResult(authenticatedAction); assertEquals( Const.ActionURIs.STUDENT_HOME_PAGE + "?persistencecourse=idOfCourseNoEvals" + "&error=false&user=idOfNoFSStudent3", redirectResult.getDestinationWithParams()); assertFalse(redirectResult.isError); assertEquals( String.format( Const.StatusMessages.STUDENT_COURSE_JOIN_SUCCESSFUL, "[idOfCourseNoEvals] Typical Course 3 with 0 Evals") + "<br>" + String.format( Const.StatusMessages.HINT_FOR_NO_SESSIONS_STUDENT, "[idOfCourseNoEvals] Typical Course 3 with 0 Evals"), redirectResult.getStatusMessage()); ______TS("typical case"); AccountAttributes newStudentAccount = new AccountAttributes( "idOfNewStudent", "nameOfNewStudent", false, "*****@*****.**", "TEAMMATES Test Institute 5"); accountsDb.createAccount(newStudentAccount); StudentAttributes newStudentAttributes = new StudentAttributes( student1InCourse1.section, student1InCourse1.team, "nameOfNewStudent", "*****@*****.**", "This is a new student", student1InCourse1.course); studentsDb.createEntity(newStudentAttributes); newStudentAttributes = studentsDb.getStudentForEmail(newStudentAttributes.course, newStudentAttributes.email); gaeSimulation.loginUser("idOfNewStudent"); submissionParams = new String[] { Const.ParamsNames.REGKEY, StringHelper.encrypt(newStudentAttributes.key), Const.ParamsNames.NEXT_URL, Const.ActionURIs.STUDENT_PROFILE_PAGE }; authenticatedAction = getAction(submissionParams); redirectResult = getRedirectResult(authenticatedAction); assertEquals( Const.ActionURIs.STUDENT_PROFILE_PAGE + "?persistencecourse=idOfTypicalCourse1" + "&error=false&user=idOfNewStudent", redirectResult.getDestinationWithParams()); assertFalse(redirectResult.isError); assertEquals( String.format( Const.StatusMessages.STUDENT_COURSE_JOIN_SUCCESSFUL, "[idOfTypicalCourse1] Typical Course 1 with 2 Evals"), redirectResult.getStatusMessage()); }
@Test public void testGetStudent() throws InvalidParametersException, EntityDoesNotExistException { int currentNumberOfStudent = studentsDb.getAllStudents().size(); StudentAttributes s = createNewStudent(); s.googleId = "validGoogleId"; s.googleId = "validTeam"; studentsDb.updateStudent(s.course, s.email, s.name, s.team, s.email, s.googleId, s.comments); ______TS("typical success case: existent"); StudentAttributes retrieved = studentsDb.getStudentForEmail(s.course, s.email); assertNotNull(retrieved); assertNotNull(studentsDb.getStudentForRegistrationKey(retrieved.key)); assertNotNull(studentsDb.getStudentForRegistrationKey(StringHelper.encrypt(retrieved.key))); assertNull(studentsDb.getStudentForRegistrationKey("notExistingKey")); ______TS("non existant student case"); retrieved = studentsDb.getStudentForEmail("any-course-id", "*****@*****.**"); assertNull(retrieved); StudentAttributes s2 = createNewStudent("*****@*****.**"); s2.googleId = "validGoogleId2"; studentsDb.updateStudent( s2.course, s2.email, s2.name, s2.team, s2.email, s2.googleId, s2.comments); studentsDb.deleteStudentsForGoogleId(s2.googleId); assertNull(studentsDb.getStudentForGoogleId(s2.course, s2.googleId)); s2 = createNewStudent("*****@*****.**"); assertEquals( true, studentsDb.getUnregisteredStudentsForCourse(s2.course).get(0).isEnrollInfoSameAs(s2)); s2.googleId = null; studentsDb.updateStudent( s2.course, s2.email, s2.name, s2.team, s2.email, s2.googleId, s2.comments); assertEquals( true, studentsDb.getUnregisteredStudentsForCourse(s2.course).get(0).isEnrollInfoSameAs(s2)); assertTrue(s.isEnrollInfoSameAs(studentsDb.getStudentsForGoogleId(s.googleId).get(0))); assertEquals(true, studentsDb.getStudentsForCourse(s.course).get(0).isEnrollInfoSameAs(s)); assertEquals( true, studentsDb.getStudentsForTeam(s.team, s.course).get(0).isEnrollInfoSameAs(s)); assertEquals(2 + currentNumberOfStudent, studentsDb.getAllStudents().size()); ______TS("null params case"); try { studentsDb.getStudentForEmail(null, "*****@*****.**"); Assert.fail(); } catch (AssertionError a) { assertEquals(Const.StatusCodes.DBLEVEL_NULL_INPUT, a.getMessage()); } try { studentsDb.getStudentForEmail("any-course-id", null); Assert.fail(); } catch (AssertionError a) { assertEquals(Const.StatusCodes.DBLEVEL_NULL_INPUT, a.getMessage()); } }
@Test public void testDeleteStudent() throws InvalidParametersException, EntityDoesNotExistException { StudentAttributes s = createNewStudent(); s.googleId = "validGoogleId"; studentsDb.updateStudent(s.course, s.email, s.name, s.team, s.email, s.googleId, s.comments); // Delete studentsDb.deleteStudent(s.course, s.email); StudentAttributes deleted = studentsDb.getStudentForEmail(s.course, s.email); assertNull(deleted); studentsDb.deleteStudentsForGoogleId(s.googleId); assertEquals(null, studentsDb.getStudentForGoogleId(s.course, s.googleId)); int currentStudentNum = studentsDb.getAllStudents().size(); s = createNewStudent(); createNewStudent("*****@*****.**"); assertEquals(2 + currentStudentNum, studentsDb.getAllStudents().size()); studentsDb.deleteStudentsForCourse(s.course); assertEquals(currentStudentNum, studentsDb.getAllStudents().size()); // delete again - should fail silently studentsDb.deleteStudent(s.course, s.email); // Null params check: try { studentsDb.deleteStudent(null, s.email); Assert.fail(); } catch (AssertionError a) { assertEquals(Const.StatusCodes.DBLEVEL_NULL_INPUT, a.getMessage()); } try { studentsDb.deleteStudent(s.course, null); Assert.fail(); } catch (AssertionError a) { assertEquals(Const.StatusCodes.DBLEVEL_NULL_INPUT, a.getMessage()); } }
@Test public void testUpdateStudent() throws InvalidParametersException, EntityDoesNotExistException { // Create a new student with valid attributes StudentAttributes s = createNewStudent(); studentsDb.updateStudent( s.course, s.email, "new-name", "new-team", "*****@*****.**", "new.google.id", "lorem ipsum dolor si amet"); ______TS("non-existent case"); try { studentsDb.updateStudent( "non-existent-course", "*****@*****.**", "no-name", "non-existent-team", "non.existent.ID", "blah", "blah"); signalFailureToDetectException(); } catch (EntityDoesNotExistException e) { assertEquals( StudentsDb.ERROR_UPDATE_NON_EXISTENT_STUDENT + "non-existent-course/[email protected]", 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.updateStudent( null, s.email, "new-name", "new-team", "*****@*****.**", "new.google.id", "lorem ipsum dolor si amet"); signalFailureToDetectException(); } catch (AssertionError a) { assertEquals(Const.StatusCodes.DBLEVEL_NULL_INPUT, a.getMessage()); } ______TS("null email case"); try { studentsDb.updateStudent( s.course, null, "new-name", "new-team", "*****@*****.**", "new.google.id", "lorem ipsum dolor si amet"); signalFailureToDetectException(); } catch (AssertionError a) { assertEquals(Const.StatusCodes.DBLEVEL_NULL_INPUT, a.getMessage()); } ______TS("duplicate email case"); s = createNewStudent(); // Create a second student with different email address StudentAttributes s2 = createNewStudent("*****@*****.**"); try { studentsDb.updateStudent( s.course, s.email, "new-name", "new-team", 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"; s.googleId = "new-id-2"; s.comments = "this are new comments"; studentsDb.updateStudent( s.course, originalEmail, s.name, s.team, s.email, s.googleId, s.comments); StudentAttributes updatedStudent = studentsDb.getStudentForEmail(s.course, s.email); assertTrue(updatedStudent.isEnrollInfoSameAs(s)); }