Skip to content

Commit

Permalink
add test for grading import
Browse files Browse the repository at this point in the history
  • Loading branch information
julianlxs committed Nov 22, 2024
1 parent 81cbd80 commit f680c08
Show file tree
Hide file tree
Showing 2 changed files with 208 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ public class GradingService {
private final TopicPublisher topicPublisher;
private final AssignmentService assignmentService;

// these need to be set!
private static final String authToken = "";
private static final String basePath = "";

public Grading getGradingForAssignmentForStudent(final UUID assignmentId, final UUID studentId, final LoggedInUser currentUser) {
final AssignmentEntity assignment = assignmentService.requireAssignmentExists(assignmentId); // throws EntityNotFoundException "Assignment with assessmentId %s not found"
try {
Expand All @@ -62,16 +66,12 @@ public Grading getGradingForAssignmentForStudent(final UUID assignmentId, final
* @param assignmentId id of the assignment of which the gradings should be imported
* @param currentUser the user requesting the import (needs to be admin)
*/
private void importGradingsForAssignment(final UUID assignmentId, final LoggedInUser currentUser) {
public void importGradingsForAssignment(final UUID assignmentId, final LoggedInUser currentUser) {
final AssignmentEntity assignment = assignmentService.requireAssignmentExists(assignmentId); // throws EntityNotFoundException "Assignment with assessmentId %s not found"
validateUserHasAccessToCourse(currentUser, LoggedInUser.UserRoleInCourse.ADMINISTRATOR, assignment.getCourseId());

String externalId = assignment.getExternalId();

// these need to be set!
String authToken = "";
String basePath = "";

String body;
CompletableFuture<String> response;
try (HttpClient client = HttpClient.newBuilder().build()) {
Expand Down Expand Up @@ -120,13 +120,15 @@ private GradingEntity parseIntoGradingEntity(final JSONObject jsonObject, final
final GradingEntity gradingEntity = new GradingEntity();

String externalStudentId = jsonObject.getString("studentId"); // TODO match this to Meitrex student id
UUID studentId = null;
UUID studentId = UUID.nameUUIDFromBytes("this needs to be changed".getBytes());

JSONObject gradingData = jsonObject.getJSONObject("gradingData");

gradingEntity.setPrimaryKey(new GradingEntity.PrimaryKey(assignmentEntity.getId(), studentId));
gradingEntity.setAchievedCredits(jsonObject.getDouble("points"));
gradingEntity.setAchievedCredits(gradingData.getDouble("points"));
gradingEntity.setDate(OffsetDateTime.now()); // TODO can this be more precise?

JSONArray exerciseArray = jsonObject.getJSONArray("exerciseGradings");
JSONArray exerciseArray = gradingData.getJSONArray("exerciseGradings");
List<ExerciseGradingEntity> exerciseGradingEntities = new ArrayList<>(exerciseArray.length());
List<ExerciseEntity> exerciseEntities = assignmentEntity.getExercises();

Expand Down Expand Up @@ -238,10 +240,6 @@ public List<ExternalAssignment> getExternalAssignments(final UUID courseId, fina
return null;
}

// these need to be set!
String authToken = "";
String basePath = "";

String body;
CompletableFuture<String> response;
try (HttpClient client = HttpClient.newBuilder().build()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
package de.unistuttgart.iste.meitrex.assignment_service.api;

import de.unistuttgart.iste.meitrex.assignment_service.persistence.entity.ExerciseGradingEntity;
import de.unistuttgart.iste.meitrex.assignment_service.persistence.entity.GradingEntity;
import de.unistuttgart.iste.meitrex.assignment_service.persistence.entity.SubexerciseGradingEntity;
import de.unistuttgart.iste.meitrex.assignment_service.persistence.mapper.AssignmentMapper;
import de.unistuttgart.iste.meitrex.assignment_service.service.AssignmentService;
import de.unistuttgart.iste.meitrex.assignment_service.service.GradingService;
import de.unistuttgart.iste.meitrex.assignment_service.test_utils.TestUtils;
import de.unistuttgart.iste.meitrex.common.testutil.GraphQlApiTest;
import de.unistuttgart.iste.meitrex.common.testutil.InjectCurrentUserHeader;
import de.unistuttgart.iste.meitrex.common.user_handling.LoggedInUser;
import de.unistuttgart.iste.meitrex.generated.dto.*;
import jakarta.transaction.Transactional;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.graphql.test.tester.GraphQlTester;
import org.springframework.test.annotation.Commit;

import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.UUID;

import static de.unistuttgart.iste.meitrex.common.testutil.TestUsers.userWithMembershipInCourseWithId;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;


@GraphQlApiTest
public class ImportGradingsForAssignmentTest {

@Autowired
private GradingService gradingService;
private final UUID courseId = UUID.randomUUID();

@InjectCurrentUserHeader
private final LoggedInUser loggedInUser = userWithMembershipInCourseWithId(courseId, LoggedInUser.UserRoleInCourse.ADMINISTRATOR);

@Autowired
private TestUtils testUtils;
@Autowired
private AssignmentService assignmentService;
@Autowired
private AssignmentMapper assignmentMapper;

@Test
@Transactional
@Commit
void testImportGradingsForAssignment(final GraphQlTester tester) {
// create sheetNo 1 in MEITREX

UUID exerciseId1 = UUID.randomUUID();
UUID exerciseId2 = UUID.randomUUID();
UUID subexerciseId1 = UUID.randomUUID();
UUID subexerciseId2 = UUID.randomUUID();
UUID subexerciseId3 = UUID.randomUUID();
UUID subexerciseId4 = UUID.randomUUID();
UUID assessmentId = UUID.randomUUID();

final List<CreateSubexerciseInput> createSubexerciseInputList1 = List.of(
CreateSubexerciseInput.builder()
.setItemId(subexerciseId1)
.setNumber("<")
.setTotalSubexerciseCredits(15)
.setParentExerciseId(exerciseId1)
.build(),
CreateSubexerciseInput.builder()
.setItemId(subexerciseId2)
.setNumber("c")
.setTotalSubexerciseCredits(46)
.setParentExerciseId(exerciseId1)
.build(),
CreateSubexerciseInput.builder()
.setItemId(subexerciseId3)
.setNumber("2")
.setTotalSubexerciseCredits(1)
.setParentExerciseId(exerciseId1)
.build()
);

final List<CreateSubexerciseInput> createSubexerciseInputList2 = List.of(
CreateSubexerciseInput.builder()
.setItemId(subexerciseId4)
.setNumber("l,")
.setTotalSubexerciseCredits(0.5)
.setParentExerciseId(exerciseId2)
.build()
);

final List<CreateExerciseInput> createExerciseInputList = List.of(
CreateExerciseInput.builder()
.setTotalExerciseCredits(62)
.setItemId(exerciseId1)
.setNumber("1")
.setSubexercises(createSubexerciseInputList1)
.build(),
CreateExerciseInput.builder()
.setTotalExerciseCredits(0.5)
.setItemId(exerciseId2)
.setNumber("2")
.setSubexercises(createSubexerciseInputList2)
.build()
);

final CreateAssignmentInput createAssignmentInput = CreateAssignmentInput.builder()
.setAssignmentType(AssignmentType.EXERCISE_SHEET)
.setDate(OffsetDateTime.now())
.setDescription("sheetNo 1.0")
.setExternalId("657b4ae4-e341-441e-87b1-46b3306c5ef0")
.setTotalCredits(62.5)
.setExercises(createExerciseInputList)
.build();

Assignment assignment = assignmentService.createAssignment(courseId, assessmentId, createAssignmentInput);

// import gradings from TMS (the important part)

gradingService.importGradingsForAssignment(assessmentId, loggedInUser);

// querying the grading from the repo

final String query2 = """
query($assignmentId: UUID!, $studentId: UUID!) {
getGradingForAssignmentForStudent(assessmentId: $assignmentId, studentId: $studentId) {
assessmentId
studentId
date
achievedCredits
exerciseGradings {
itemId
studentId
achievedCredits
subexerciseGradings {
itemId
studentId
achievedCredits
}
}
}
}
""";

UUID studentId = UUID.nameUUIDFromBytes("this needs to be changed".getBytes());

Grading receivedGrading = tester.document(query2)
.variable("assignmentId", assessmentId)
.variable("studentId", studentId) // null because we can't map student ids yet
.execute()
.path("getGradingForAssignmentForStudent")
.entity(Grading.class)
.get();

// create reference grading
GradingEntity expectedGradingEntity = GradingEntity.builder()
.primaryKey(new GradingEntity.PrimaryKey(assessmentId, studentId))
.date(OffsetDateTime.now())
.achievedCredits(40.5)
.build();
ExerciseGradingEntity exerciseGrading1 = ExerciseGradingEntity.builder()
.primaryKey(new ExerciseGradingEntity.PrimaryKey(assignment.getExercises().getFirst().getItemId(), studentId))
.parentGrading(expectedGradingEntity)
.achievedCredits(40)
.build();
exerciseGrading1.setSubexerciseGradings(
List.of(
new SubexerciseGradingEntity(new SubexerciseGradingEntity.PrimaryKey(assignment.getExercises().getFirst().getSubexercises().get(0).getItemId(), studentId),
14, exerciseGrading1),
new SubexerciseGradingEntity(new SubexerciseGradingEntity.PrimaryKey(assignment.getExercises().getFirst().getSubexercises().get(1).getItemId(), studentId),
25, exerciseGrading1),
new SubexerciseGradingEntity(new SubexerciseGradingEntity.PrimaryKey(assignment.getExercises().getFirst().getSubexercises().get(2).getItemId(), studentId),
1, exerciseGrading1)
)
);
ExerciseGradingEntity exerciseGrading2 = ExerciseGradingEntity.builder()
.primaryKey(new ExerciseGradingEntity.PrimaryKey(assignment.getExercises().getLast().getItemId(), studentId))
.parentGrading(expectedGradingEntity)
.achievedCredits(0.5)
.build();
exerciseGrading2.setSubexerciseGradings(
List.of(
new SubexerciseGradingEntity(new SubexerciseGradingEntity.PrimaryKey(assignment.getExercises().getLast().getSubexercises().getFirst().getItemId(), studentId),
0.5, exerciseGrading2)
)
);
expectedGradingEntity.setExerciseGradings(List.of(exerciseGrading1, exerciseGrading2));

// check if gradings match

// times need to be adjusted because repository (presumably) rounds to milliseconds and converts to UTC
expectedGradingEntity.setDate(expectedGradingEntity.getDate().truncatedTo(ChronoUnit.MILLIS).withOffsetSameInstant(ZoneOffset.UTC));
receivedGrading.setDate(receivedGrading.getDate().truncatedTo(ChronoUnit.MILLIS).withOffsetSameInstant(ZoneOffset.UTC));

assertThat(assignmentMapper.gradingEntityToDto(expectedGradingEntity), is(receivedGrading));
}

}

0 comments on commit f680c08

Please sign in to comment.