From 04bf04a88fcb24cbb8c82b747d336d8af9eb3a7b Mon Sep 17 00:00:00 2001 From: "julian.lxs" Date: Thu, 14 Nov 2024 15:49:29 +0100 Subject: [PATCH] add import of ExternalAssignment objects from TMS --- build.gradle | 1 + .../service/GradingService.java | 39 ++++++++----- .../api/QueryGetExternalAssignmentsTest.java | 55 +++++++++++++++++++ 3 files changed, 81 insertions(+), 14 deletions(-) create mode 100644 src/test/java/de/unistuttgart/iste/meitrex/assignment_service/api/QueryGetExternalAssignmentsTest.java diff --git a/build.gradle b/build.gradle index 0cbf193..bf0aac6 100644 --- a/build.gradle +++ b/build.gradle @@ -109,6 +109,7 @@ dependencies { implementation 'com.graphql-java:graphql-java-extended-validation:22.0' implementation 'io.dapr:dapr-sdk:1.9.0' // Dapr's core SDK with all features, except Actors. implementation 'io.dapr:dapr-sdk-springboot:1.9.0' // Dapr's SDK integration with SpringBoot + implementation 'org.json:json:20240303' // JSON parsing for import from TMS compileOnly 'org.projectlombok:lombok' developmentOnly 'org.springframework.boot:spring-boot-devtools' runtimeOnly 'org.postgresql:postgresql' diff --git a/src/main/java/de/unistuttgart/iste/meitrex/assignment_service/service/GradingService.java b/src/main/java/de/unistuttgart/iste/meitrex/assignment_service/service/GradingService.java index 5abf567..a8b9291 100644 --- a/src/main/java/de/unistuttgart/iste/meitrex/assignment_service/service/GradingService.java +++ b/src/main/java/de/unistuttgart/iste/meitrex/assignment_service/service/GradingService.java @@ -14,13 +14,13 @@ import jakarta.persistence.EntityNotFoundException; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; +import org.json.*; -import java.net.Authenticator; -import java.net.PasswordAuthentication; import java.net.URI; import java.net.http.HttpClient; import java.net.http.HttpRequest; import java.net.http.HttpResponse; +import java.util.ArrayList; import java.util.List; import java.util.UUID; import java.util.concurrent.CompletableFuture; @@ -57,28 +57,39 @@ public List getExternalAssignments(final UUID courseId, fina return null; } - // get stuff from TMS here + // these need to be set! + String authToken = ""; + String basePath = ""; + String body; CompletableFuture response; - try (HttpClient client = HttpClient.newBuilder().authenticator(new Authenticator() { - @Override - protected PasswordAuthentication getPasswordAuthentication() { - return new PasswordAuthentication( - "username", - "password".toCharArray()); - } - }).build()) { - HttpRequest request = HttpRequest.newBuilder().uri(URI.create("")).build(); + try (HttpClient client = HttpClient.newBuilder().build()) { + HttpRequest request = HttpRequest.newBuilder().uri(URI.create(basePath + "api/sheet")) + //.header("Authorization", "Basic " + Base64.getEncoder().encodeToString("username:password".getBytes())) + .header("Cookie", "connect.sid=" + authToken) + .build(); response = client.sendAsync(request, HttpResponse.BodyHandlers.ofString()).thenApply(HttpResponse::body); + body = response.join(); } - String body = response.join(); + if (body == null) return null; List externalAssignments = this.parseStringIntoList(body); return externalAssignments; } private List parseStringIntoList(final String string) { - return null; + + JSONArray sheetArray = new JSONArray(string); + List externalAssignmentList = new ArrayList<>(sheetArray.length()); + + for (int i = 0; i < sheetArray.length(); i++) { + ExternalAssignment externalAssignment = new ExternalAssignment(); + JSONObject sheetObject = sheetArray.getJSONObject(i); + externalAssignment.setExternalId(sheetObject.getString("id")); + externalAssignment.setSheetNo(sheetObject.getDouble("sheetNo")); + externalAssignmentList.add(externalAssignment); + } + return externalAssignmentList; } } diff --git a/src/test/java/de/unistuttgart/iste/meitrex/assignment_service/api/QueryGetExternalAssignmentsTest.java b/src/test/java/de/unistuttgart/iste/meitrex/assignment_service/api/QueryGetExternalAssignmentsTest.java new file mode 100644 index 0000000..56138a0 --- /dev/null +++ b/src/test/java/de/unistuttgart/iste/meitrex/assignment_service/api/QueryGetExternalAssignmentsTest.java @@ -0,0 +1,55 @@ +package de.unistuttgart.iste.meitrex.assignment_service.api; + +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.ExternalAssignment; +import org.hamcrest.Matchers; +import org.junit.jupiter.api.Test; +import org.springframework.graphql.test.tester.GraphQlTester; + +import java.util.List; +import java.util.UUID; + +import static de.unistuttgart.iste.meitrex.common.testutil.TestUsers.userWithMembershipInCourseWithId; +import static org.hamcrest.MatcherAssert.assertThat; + +@GraphQlApiTest +public class QueryGetExternalAssignmentsTest { + + + private final UUID courseId = UUID.randomUUID(); + + @InjectCurrentUserHeader + private final LoggedInUser loggedInUser = userWithMembershipInCourseWithId(courseId, LoggedInUser.UserRoleInCourse.ADMINISTRATOR); + + + @Test + void testQueryGetExternalAssignments(final GraphQlTester tester) { + final String query = """ + query($courseId: UUID!) { + getExternalAssignments (courseId: $courseId) { + externalId, + sheetNo + } + } + """; + + + List externalAssignmentList = tester.document(query) + .variable("courseId", courseId) + .execute() + .path("getExternalAssignments") + .entityList(ExternalAssignment.class) + .get(); + + List expectedExternalAssignments = List.of( + new ExternalAssignment("657b4ae4-e341-441e-87b1-46b3306c5ef0", 1.0), + new ExternalAssignment("929af54d-0f33-48e6-b05d-1b9cfdc7e0a3", 2.0) + ); + + assertThat(externalAssignmentList, Matchers.equalTo(expectedExternalAssignments)); + } + + +}