Skip to content

Commit

Permalink
add import of ExternalAssignment objects from TMS
Browse files Browse the repository at this point in the history
  • Loading branch information
julianlxs committed Nov 14, 2024
1 parent 9e26017 commit 04bf04a
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 14 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -57,28 +57,39 @@ public List<ExternalAssignment> getExternalAssignments(final UUID courseId, fina
return null;
}

// get stuff from TMS here
// these need to be set!
String authToken = "";
String basePath = "";

String body;
CompletableFuture<String> 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<ExternalAssignment> externalAssignments = this.parseStringIntoList(body);

return externalAssignments;
}

private List<ExternalAssignment> parseStringIntoList(final String string) {
return null;

JSONArray sheetArray = new JSONArray(string);
List<ExternalAssignment> 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;
}

}
Original file line number Diff line number Diff line change
@@ -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<ExternalAssignment> externalAssignmentList = tester.document(query)
.variable("courseId", courseId)
.execute()
.path("getExternalAssignments")
.entityList(ExternalAssignment.class)
.get();

List<ExternalAssignment> 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));
}


}

0 comments on commit 04bf04a

Please sign in to comment.