Ejemplo n.º 1
0
  @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());
    }
  }
Ejemplo n.º 2
0
  @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));
  }
Ejemplo n.º 3
0
  public void testCreateCourseAndInstructor() throws Exception {

    /* Explanation: SUT has 5 paths. They are,
     * path 1 - exit because the account doesn't' exist.
     * path 2 - exit because the account exists but doesn't have instructor privileges.
     * path 3 - exit because course creation failed.
     * path 4 - exit because instructor creation failed.
     * path 5 - success.
     * Accordingly, we have 5 test cases.
     */

    ______TS("fails: account doesn't exist");

    CourseAttributes c = new CourseAttributes();
    c.id = "fresh-course-tccai";
    c.name = "Fresh course for tccai";

    @SuppressWarnings("deprecation")
    InstructorAttributes i =
        new InstructorAttributes(
            "instructor-for-tccai", c.id, "Instructor for tccai", "*****@*****.**");

    try {
      coursesLogic.createCourseAndInstructor(i.googleId, c.id, c.name);
      signalFailureToDetectException();
    } catch (AssertionError e) {
      AssertHelper.assertContains("for a non-existent instructor", e.getMessage());
    }
    TestHelper.verifyAbsentInDatastore(c);
    TestHelper.verifyAbsentInDatastore(i);

    ______TS("fails: account doesn't have instructor privileges");

    AccountAttributes a = new AccountAttributes();
    a.googleId = i.googleId;
    a.name = i.name;
    a.email = i.email;
    a.institute = "TEAMMATES Test Institute 5";
    a.isInstructor = false;
    a.studentProfile = new StudentProfileAttributes();
    a.studentProfile.googleId = i.googleId;
    accountsDb.createAccount(a);
    try {
      coursesLogic.createCourseAndInstructor(i.googleId, c.id, c.name);
      signalFailureToDetectException();
    } catch (AssertionError e) {
      AssertHelper.assertContains("doesn't have instructor privileges", e.getMessage());
    }
    TestHelper.verifyAbsentInDatastore(c);
    TestHelper.verifyAbsentInDatastore(i);

    ______TS("fails: error during course creation");

    a.isInstructor = true;
    accountsDb.updateAccount(a);

    c.id = "invalid id";

    try {
      coursesLogic.createCourseAndInstructor(i.googleId, c.id, c.name);
      signalFailureToDetectException();
    } catch (InvalidParametersException e) {
      AssertHelper.assertContains("not acceptable to TEAMMATES as a Course ID", e.getMessage());
    }
    TestHelper.verifyAbsentInDatastore(c);
    TestHelper.verifyAbsentInDatastore(i);

    ______TS("fails: error during instructor creation due to duplicate instructor");

    c.id = "fresh-course-tccai";
    instructorsDb.createEntity(i); // create a duplicate instructor

    try {
      coursesLogic.createCourseAndInstructor(i.googleId, c.id, c.name);
      signalFailureToDetectException();
    } catch (AssertionError e) {
      AssertHelper.assertContains(
          "Unexpected exception while trying to create instructor for a new course",
          e.getMessage());
    }
    TestHelper.verifyAbsentInDatastore(c);

    ______TS("fails: error during instructor creation due to invalid parameters");

    i.email = "ins.for.iccai.gmail.tmt";

    try {
      coursesLogic.createCourseAndInstructor(i.googleId, c.id, c.name);
      signalFailureToDetectException();
    } catch (AssertionError e) {
      AssertHelper.assertContains(
          "Unexpected exception while trying to create instructor for a new course",
          e.getMessage());
    }
    TestHelper.verifyAbsentInDatastore(c);

    ______TS("success: typical case");

    i.email = "*****@*****.**";

    // remove the duplicate instructor object from the datastore.
    instructorsDb.deleteInstructor(i.courseId, i.email);

    coursesLogic.createCourseAndInstructor(i.googleId, c.id, c.name);
    TestHelper.verifyPresentInDatastore(c);
    TestHelper.verifyPresentInDatastore(i);

    ______TS("Null parameter");

    try {
      coursesLogic.createCourseAndInstructor(null, c.id, c.name);
      signalFailureToDetectException();
    } catch (AssertionError e) {
      assertEquals("Supplied parameter was null\n", e.getMessage());
    }
  }