@Override public ActionResult execute() throws EntityDoesNotExistException { String newCourseId = getRequestParamValue(Const.ParamsNames.COURSE_ID); Assumption.assertNotNull(newCourseId); String newCourseName = getRequestParamValue(Const.ParamsNames.COURSE_NAME); Assumption.assertNotNull(newCourseName); /* Check if user has the right to execute the action */ new GateKeeper().verifyInstructorPrivileges(account); /* Create a new course in the database */ data = new InstructorCoursesPageData(account); CourseAttributes newCourse = new CourseAttributes(newCourseId, newCourseName); createCourse(newCourse); /* Prepare data for the refreshed page after executing the adding action */ List<CourseDetailsBundle> allCourses = new ArrayList<CourseDetailsBundle>( logic.getCourseSummariesForInstructor(data.account.googleId).values()); CourseDetailsBundle.sortDetailedCoursesByCourseId(allCourses); List<CourseDetailsBundle> activeCourses = logic.extractActiveCourses(allCourses, data.account.googleId); List<CourseDetailsBundle> archivedCourses = logic.extractArchivedCourses(allCourses, data.account.googleId); String CourseIdToShowParam = ""; String CourseNameToShowParam = ""; if (isError) { // there is error in adding the course CourseIdToShowParam = Sanitizer.sanitizeForHtml(newCourse.id); CourseNameToShowParam = Sanitizer.sanitizeForHtml(newCourse.name); List<String> statusMessageTexts = new ArrayList<String>(); for (StatusMessage msg : statusToUser) { statusMessageTexts.add(msg.getText()); } statusToAdmin = StringHelper.toString(statusMessageTexts, "<br>"); } else { statusToAdmin = "Course added : " + newCourse.id; statusToAdmin += "<br>Total courses: " + allCourses.size(); } List<CourseAttributes> courseList = logic.getCoursesForInstructor(data.account.googleId); Map<String, InstructorAttributes> instructorsForCourses = new HashMap<String, InstructorAttributes>(); for (CourseAttributes course : courseList) { instructorsForCourses.put( course.id, logic.getInstructorForGoogleId(course.id, data.account.googleId)); } data.init( activeCourses, archivedCourses, instructorsForCourses, CourseIdToShowParam, CourseNameToShowParam); return createShowPageResult(Const.ViewURIs.INSTRUCTOR_COURSES, data); }
@Test public void testValidate() { EvaluationAttributes e = new EvaluationAttributes(); e.courseId = ""; e.name = ""; e.instructions = new Text("Instruction to students."); e.startTime = TimeHelper.getDateOffsetToCurrentTime(1); e.endTime = TimeHelper.getDateOffsetToCurrentTime(2); e.activated = false; e.published = false; e.timeZone = 0.0; e.gracePeriod = 5; e.p2pEnabled = true; assertEquals("invalid values", false, e.isValid()); String errorMessage = String.format(COURSE_ID_ERROR_MESSAGE, e.courseId, REASON_EMPTY) + EOL + String.format(EVALUATION_NAME_ERROR_MESSAGE, e.name, REASON_EMPTY); assertEquals("valid values", errorMessage, StringHelper.toString(e.getInvalidityInfo())); e.courseId = "valid-course"; e.name = "valid name"; e.instructions = new Text("valid instructions"); assertTrue("valid, minimal properties", e.isValid()); assertEquals("valid values", true, e.isValid()); e.startTime = null; try { e.getInvalidityInfo(); signalFailureToDetectException("null start time not detected"); } catch (AssertionError e1) { ignoreExpectedException(); } e.startTime = TimeHelper.getDateOffsetToCurrentTime(1); e.endTime = null; try { e.getInvalidityInfo(); signalFailureToDetectException("null end time not detected"); } catch (AssertionError e1) { ignoreExpectedException(); } // SUCCESS : end == start e.endTime = TimeHelper.getDateOffsetToCurrentTime(1); e.startTime = e.endTime; assertTrue(e.isValid()); // FAIL : end before start e.endTime = TimeHelper.getDateOffsetToCurrentTime(1); e.startTime = TimeHelper.getDateOffsetToCurrentTime(2); assertFalse(e.isValid()); errorMessage = String.format( TIME_FRAME_ERROR_MESSAGE, END_TIME_FIELD_NAME, EVALUATION_NAME, START_TIME_FIELD_NAME); assertEquals(errorMessage, StringHelper.toString(e.getInvalidityInfo())); // FAIL : published before endtime: invalid e.published = true; e.startTime = TimeHelper.getDateOffsetToCurrentTime(0); e.endTime = TimeHelper.getMsOffsetToCurrentTime(5); assertFalse(e.isValid()); assertEquals( FieldValidator.EVALUATION_END_TIME_ERROR_MESSAGE, StringHelper.toString(e.getInvalidityInfo())); // SUCCESS : just after endtime and published: valid e.startTime = TimeHelper.getDateOffsetToCurrentTime(-1); e.endTime = TimeHelper.getMsOffsetToCurrentTime(-5); e.published = true; assertTrue(e.isValid()); // FAIL : activated before start time: invalid e.startTime = TimeHelper.getDateOffsetToCurrentTime(1); e.endTime = TimeHelper.getDateOffsetToCurrentTime(2); e.published = false; e.activated = true; assertFalse(e.isValid()); assertEquals( FieldValidator.EVALUATION_START_TIME_ERROR_MESSAGE, StringHelper.toString(e.getInvalidityInfo())); }