private void createCourse(CourseAttributes course) { try { logic.createCourseAndInstructor(data.account.googleId, course.id, course.name); String statusMessage = Const.StatusMessages.COURSE_ADDED .replace("${courseEnrollLink}", data.getInstructorCourseEnrollLink(course.id)) .replace("${courseEditLink}", data.getInstructorCourseEditLink(course.id)); statusToUser.add(new StatusMessage(statusMessage, StatusMessageColor.SUCCESS)); isError = false; } catch (EntityAlreadyExistsException e) { setStatusForException(e, Const.StatusMessages.COURSE_EXISTS); } catch (InvalidParametersException e) { setStatusForException(e, Const.StatusMessages.COURSE_INVALID_ID); } if (isError) { return; } }
@Test public void testExecute() throws Exception { InstructorAttributes instructor1OfCourse1 = dataBundle.instructors.get("instructor1OfCourse1"); String instructorId = instructor1OfCourse1.googleId; String adminUserId = "admin.user"; gaeSimulation.loginAsInstructor(instructorId); ______TS("Not enough parameters"); verifyAssumptionFailure(); verifyAssumptionFailure(Const.ParamsNames.COURSE_NAME, "ticac tac name"); ______TS("Error: Invalid parameter for Course ID"); Action addAction = getAction( Const.ParamsNames.COURSE_ID, "ticac,tpa1,id", Const.ParamsNames.COURSE_NAME, "ticac tpa1 name"); ShowPageResult pageResult = (ShowPageResult) addAction.executeAndPostProcess(); assertEquals( Const.ViewURIs.INSTRUCTOR_COURSES + "?error=true&user=idOfInstructor1OfCourse1", pageResult.getDestinationWithParams()); assertEquals(true, pageResult.isError); assertEquals(Const.StatusMessages.COURSE_INVALID_ID, pageResult.getStatusMessage()); InstructorCoursesPageData pageData = (InstructorCoursesPageData) pageResult.data; assertEquals(1, pageData.allCourses.size()); String expectedLogMessage = "TEAMMATESLOG|||instructorCourseAdd|||instructorCourseAdd" + "|||true|||Instructor|||Instructor 1 of Course 1|||idOfInstructor1OfCourse1|||[email protected]" + "|||Please use only alphabets, numbers, dots, hyphens, underscores and dollar signs in course ID." + "|||/page/instructorCourseAdd"; assertEquals(expectedLogMessage, addAction.getLogMessage()); ______TS("Typical case, 1 existing course"); addAction = getAction( Const.ParamsNames.COURSE_ID, "ticac.tpa1.id", Const.ParamsNames.COURSE_NAME, "ticac tpa1 name"); pageResult = (ShowPageResult) addAction.executeAndPostProcess(); pageData = (InstructorCoursesPageData) pageResult.data; assertEquals(2, pageData.allCourses.size()); expectedLogMessage = "TEAMMATESLOG|||instructorCourseAdd" + "|||instructorCourseAdd|||true|||Instructor|||Instructor 1 of Course 1" + "|||idOfInstructor1OfCourse1|||[email protected]" + "|||Course added : ticac.tpa1.id<br>Total courses: 2|||/page/instructorCourseAdd"; assertEquals(expectedLogMessage, addAction.getLogMessage()); String expected = Const.StatusMessages.COURSE_ADDED .replace( "${courseEnrollLink}", "/page/instructorCourseEnrollPage?courseid=ticac.tpa1.id&user=idOfInstructor1OfCourse1") .replace( "${courseEditLink}", "/page/instructorCourseEditPage?courseid=ticac.tpa1.id&user=idOfInstructor1OfCourse1"); assertEquals(expected, pageResult.getStatusMessage()); ______TS("Error: Try to add the same course again"); addAction = getAction( Const.ParamsNames.COURSE_ID, "ticac.tpa1.id", Const.ParamsNames.COURSE_NAME, "ticac tpa1 name"); pageResult = (ShowPageResult) addAction.executeAndPostProcess(); assertEquals( Const.ViewURIs.INSTRUCTOR_COURSES + "?error=true&user=idOfInstructor1OfCourse1", pageResult.getDestinationWithParams()); assertEquals(true, pageResult.isError); assertEquals(Const.StatusMessages.COURSE_EXISTS, pageResult.getStatusMessage()); pageData = (InstructorCoursesPageData) pageResult.data; assertEquals(2, pageData.allCourses.size()); expectedLogMessage = "TEAMMATESLOG|||instructorCourseAdd|||instructorCourseAdd" + "|||true|||Instructor|||Instructor 1 of Course 1|||idOfInstructor1OfCourse1" + "|||[email protected]|||A course by the same ID already exists in the system, possibly created by another user. Please choose a different course ID" + "|||/page/instructorCourseAdd"; assertEquals(expectedLogMessage, addAction.getLogMessage()); ______TS("Masquerade mode, 0 courses"); CoursesLogic.inst().deleteCourseCascade(instructor1OfCourse1.courseId); CoursesLogic.inst().deleteCourseCascade("ticac.tpa1.id"); gaeSimulation.loginAsAdmin(adminUserId); addAction = getAction( Const.ParamsNames.USER_ID, instructorId, Const.ParamsNames.COURSE_ID, "ticac.tpa2.id", Const.ParamsNames.COURSE_NAME, "ticac tpa2 name"); pageResult = (ShowPageResult) addAction.executeAndPostProcess(); String expectedDestination = Const.ViewURIs.INSTRUCTOR_COURSES + "?error=false&user=idOfInstructor1OfCourse1"; assertEquals(expectedDestination, pageResult.getDestinationWithParams()); assertEquals(false, pageResult.isError); String expectedStatus = "The course has been added.. Click <a href=\"/page/instructorCourseEnrollPage?courseid=ticac.tpa2.id&user=idOfInstructor1OfCourse1\">here</a> to add students to the course or " + "click <a href=\"/page/instructorCourseEditPage?courseid=ticac.tpa2.id&user=idOfInstructor1OfCourse1\">here</a> to add other instructors.<br>If you don't see the course in the list below, please refresh the page after a few moments."; assertEquals(expectedStatus, pageResult.getStatusMessage()); pageData = (InstructorCoursesPageData) pageResult.data; assertEquals(1, pageData.allCourses.size()); expectedLogMessage = "TEAMMATESLOG|||instructorCourseAdd|||instructorCourseAdd" + "|||true|||Instructor(M)|||Instructor 1 of Course 1" + "|||idOfInstructor1OfCourse1|||[email protected]|||Course added : ticac.tpa2.id<br>Total courses: 1" + "|||/page/instructorCourseAdd"; assertEquals(expectedLogMessage, addAction.getLogMessage()); // delete the new course CoursesLogic.inst().deleteCourseCascade("ticac.tpa2.id"); }