Skip to content

Commit

Permalink
Fix users db integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelfangjw committed Mar 1, 2023
1 parent 163b303 commit 4eafa0c
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 57 deletions.
62 changes: 17 additions & 45 deletions src/it/java/teammates/it/storage/sqlapi/UsersDbIT.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package teammates.it.storage.sqlapi;

import static org.mockito.Mockito.mock;
import static org.junit.Assert.assertSame;

import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

Expand All @@ -25,70 +24,43 @@
*/
public class UsersDbIT extends BaseTestCaseWithSqlDatabaseAccess {

private static final int SHARED_ID = 1;

private final UsersDb usersDb = UsersDb.inst();
private final CoursesDb coursesDb = CoursesDb.inst();
private Course course;
private Section section;
private Team team;
private Instructor instructor;

@BeforeTest
public void setUp() throws EntityAlreadyExistsException, InvalidParametersException {
HibernateUtil.beginTransaction();

course = new Course("course-id", "course-name", null, "institute");
course = new Course("course-id", "course-name", Const.DEFAULT_TIME_ZONE, "institute");
coursesDb.createCourse(course);

section = new Section(course, "section-name");
section.setId(1);
HibernateUtil.getCurrentSession().persist(section);

team = new Team(section, "team-name");
team.setId(1);
HibernateUtil.getCurrentSession().persist(team);

HibernateUtil.commitTransaction();
}

@AfterTest
public void tearDown() {
coursesDb.deleteCourse(course);
Section section = new Section(course, "section-name");
HibernateUtil.persist(section);

HibernateUtil.getCurrentSession().remove(section);
HibernateUtil.getCurrentSession().remove(team);
}

@Test
public void testGetInstructor() throws EntityAlreadyExistsException, InvalidParametersException {
______TS("success: gets an instructor that already exists");
Team team = new Team(section, "team-name");
HibernateUtil.persist(team);

InstructorPrivileges instructorPrivileges =
new InstructorPrivileges(Const.InstructorPermissionRoleNames.INSTRUCTOR_PERMISSION_ROLE_COOWNER);
InstructorPermissionRole role = InstructorPermissionRole
.getEnum(Const.InstructorPermissionRoleNames.INSTRUCTOR_PERMISSION_ROLE_COOWNER);
Instructor newInstructor = new Instructor(coursesDb.getCourse("course-id"), team, "valid.name", "[email protected]",
instructor = new Instructor(course, team, "valid.name", "[email protected]",
false, Const.DEFAULT_DISPLAY_NAME_FOR_INSTRUCTOR, role, instructorPrivileges);
usersDb.createInstructor(instructor);

newInstructor.setId(SHARED_ID);

// HibernateUtil.getCurrentSession().merge(newInstructor);
// HibernateUtil.getCurrentSession().getTransaction().commit();

System.out.println("course " + coursesDb.getCourse("course-id"));
System.out.println("team " + HibernateUtil.getCurrentSession().get(Team.class, 1));
System.out.println("section " + HibernateUtil.getCurrentSession().get(Section.class, 1));

usersDb.createInstructor(newInstructor);

// HibernateUtil.commitTransaction();
HibernateUtil.flushSession();
}

Integer instructorId = newInstructor.getId();
Instructor actualInstructor = usersDb.getInstructor(instructorId);
verifyEquals(newInstructor, actualInstructor);
@Test
public void testGetInstructor() throws EntityAlreadyExistsException, InvalidParametersException {
______TS("success: gets an instructor that already exists");
Instructor actualInstructor = usersDb.getInstructor(instructor.getId());
assertSame(instructor, actualInstructor);

______TS("success: gets an instructor that does not exist");
Integer nonExistentId = Integer.MIN_VALUE;
Integer nonExistentId = instructor.getId() + 1000;
Instructor nonExistentInstructor = usersDb.getInstructor(nonExistentId);
assertNull(nonExistentInstructor);
}
Expand Down
17 changes: 9 additions & 8 deletions src/main/java/teammates/storage/sqlapi/UsersDb.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ public Instructor createInstructor(Instructor instructor)
String email = instructor.getEmail();

if (hasExistingInstructor(courseId, email)) {
throw new EntityAlreadyExistsException(String.format(ERROR_CREATE_ENTITY_ALREADY_EXISTS, instructor.toString()));
throw new EntityAlreadyExistsException(
String.format(ERROR_CREATE_ENTITY_ALREADY_EXISTS, instructor.toString()));
}

persist(instructor);
Expand All @@ -76,7 +77,8 @@ public Student createStudent(Student student)
String email = student.getEmail();

if (hasExistingStudent(courseId, email)) {
throw new EntityAlreadyExistsException(String.format(ERROR_CREATE_ENTITY_ALREADY_EXISTS, student.toString()));
throw new EntityAlreadyExistsException(
String.format(ERROR_CREATE_ENTITY_ALREADY_EXISTS, student.toString()));
}

persist(student);
Expand Down Expand Up @@ -166,13 +168,12 @@ public long getNumStudentsByTimeRange(Instant startTime, Instant endTime) {
private <T extends User> boolean hasExistingInstructor(String courseId, String email) {
Session session = HibernateUtil.getCurrentSession();
CriteriaBuilder cb = session.getCriteriaBuilder();
CriteriaQuery<User> cr = cb.createQuery(User.class);
Root<User> userRoot = cr.from(User.class);
Join<User, Course> userCourseJoin = userRoot.join("course");
CriteriaQuery<Instructor> cr = cb.createQuery(Instructor.class);
Root<Instructor> instructorRoot = cr.from(Instructor.class);

cr.select(userRoot).where(cb.and(
cb.equal(userCourseJoin.get("id"), courseId),
cb.equal(userRoot.get("email"), email)));
cr.select(instructorRoot).where(cb.and(
cb.equal(instructorRoot.get("courseId"), courseId),
cb.equal(instructorRoot.get("email"), email)));

return session.createQuery(cr).getSingleResultOrNull() != null;
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/teammates/storage/sqlentity/BaseEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ public Duration convertToEntityAttribute(Long minutes) {
@Converter
public static class JsonConverter<T> implements AttributeConverter<T, String> {
@Override
public String convertToDatabaseColumn(T questionDetails) {
return JsonUtils.toJson(questionDetails);
public String convertToDatabaseColumn(T entity) {
return JsonUtils.toJson(entity);
}

@Override
Expand Down
16 changes: 15 additions & 1 deletion src/main/java/teammates/storage/sqlentity/Instructor.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@

import teammates.common.datatransfer.InstructorPermissionRole;
import teammates.common.datatransfer.InstructorPrivileges;
import teammates.common.datatransfer.InstructorPrivilegesLegacy;
import teammates.common.util.FieldValidator;
import teammates.common.util.JsonUtils;
import teammates.common.util.SanitizationHelper;

import jakarta.persistence.Column;
Expand All @@ -32,7 +34,7 @@ public class Instructor extends User {
@Enumerated(EnumType.STRING)
private InstructorPermissionRole role;

@Column(nullable = false)
@Column(nullable = false, columnDefinition="text")
@Convert(converter = InstructorPrivilegesConverter.class)
private InstructorPrivileges instructorPrivileges;

Expand Down Expand Up @@ -107,5 +109,17 @@ public List<String> getInvalidityInfo() {
@Converter
public static class InstructorPrivilegesConverter
extends JsonConverter<InstructorPrivileges> {

@Override
public String convertToDatabaseColumn(InstructorPrivileges instructorPrivileges) {
return JsonUtils.toJson(instructorPrivileges.toLegacyFormat(), InstructorPrivilegesLegacy.class);
}

@Override
public InstructorPrivileges convertToEntityAttribute(String instructorPriviledgesAsString) {
InstructorPrivilegesLegacy privilegesLegacy =
JsonUtils.fromJson(instructorPriviledgesAsString, InstructorPrivilegesLegacy.class);
return new InstructorPrivileges(privilegesLegacy);
}
}
}
5 changes: 4 additions & 1 deletion src/main/java/teammates/storage/sqlentity/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,11 @@ public abstract class User extends BaseEntity {
@JoinColumn(name = "accountId")
private Account account;

@Column(nullable = false, insertable = false, updatable = false)
private String courseId;

@ManyToOne
@JoinColumn(name = "courseId")
@JoinColumn(name = "courseId", nullable = false)
private Course course;

@ManyToOne
Expand Down

0 comments on commit 4eafa0c

Please sign in to comment.