Skip to content

Commit

Permalink
Merge pull request #32 from msi-se/add-timestamps-to-forms
Browse files Browse the repository at this point in the history
add timestamps
  • Loading branch information
johannesbrandenburger authored Mar 14, 2024
2 parents a4608b5 + d3cd571 commit fc472d5
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package de.htwg_konstanz.mobilelearning.models;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.bson.types.ObjectId;

import de.htwg_konstanz.mobilelearning.enums.FormStatus;

public abstract class Form {
public class Form {
public ObjectId id;
public ObjectId courseId;
public String name;
Expand All @@ -16,6 +17,7 @@ public abstract class Form {
public FormStatus status;
public Integer connectCode;
public String key;
public Date startTimestamp;

public Form() {
}
Expand All @@ -27,7 +29,7 @@ public Form(ObjectId courseId, String name, String description, List<QuestionWra
this.description = description;
this.questions = questions != null ? questions : new ArrayList<QuestionWrapper>();
this.status = status;

this.startTimestamp = null;
this.key = "";

// generate 6-digit connect code (100000 - 999999)
Expand Down Expand Up @@ -113,4 +115,17 @@ public void setDescription(String description) {
this.description = description;
}

public void setStartTimestamp(Date startTimestamp) {
this.startTimestamp = startTimestamp;
}

public void setStartTimestamp() {
this.startTimestamp = new Date();
}

public Date getStartTimestamp() {
return this.startTimestamp;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,11 @@ public RestResponse<String> participate(@RestPath String courseId, @RestPath Str
if (course == null) { throw new NotFoundException("Course not found"); }
FeedbackForm feedbackForm = course.getFeedbackFormById(formObjectId);
if (feedbackForm == null) { throw new NotFoundException("FeedbackForm not found"); }

// check if user is student of the course
if (!course.isStudent(userId)) {
return RestResponse.status(Response.Status.FORBIDDEN, "User is not student of the course");
}

// add the participant
Boolean successfullyAdded = feedbackForm.addParticipant(new ObjectId(userId));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import de.htwg_konstanz.mobilelearning.models.QuestionWrapper;
import de.htwg_konstanz.mobilelearning.models.Result;
import de.htwg_konstanz.mobilelearning.models.auth.User;
import de.htwg_konstanz.mobilelearning.models.auth.UserRole;
import de.htwg_konstanz.mobilelearning.models.feedback.FeedbackForm;
import de.htwg_konstanz.mobilelearning.repositories.CourseRepository;
import de.htwg_konstanz.mobilelearning.repositories.UserRepository;
Expand Down Expand Up @@ -289,6 +288,11 @@ private Boolean changeFormStatus(LiveFeedbackSocketMessage feedbackSocketMessage
this.broadcast(outgoingMessage, course.getId().toHexString(), formId);
}

// if it is set to STARTED set the timestamp
if (formStatusEnum == FormStatus.STARTED) {
form.setStartTimestamp();
}

// send the updated form to all receivers (stringify the form)
LiveFeedbackSocketMessage outgoingMessage = new LiveFeedbackSocketMessage("FORM_STATUS_CHANGED",
form.status.toString(), null, null, form);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,10 @@ public RestResponse<String> participate(String alias, @RestPath String courseId,
QuizForm quizForm = course.getQuizFormById(formObjectId);
if (quizForm == null) { throw new NotFoundException("QuizForm not found"); }

// TODO: check if user is student of the course
// check if user is student of the course
if (!course.isStudent(userId)) {
return RestResponse.status(Response.Status.FORBIDDEN, "User is not student of the course");
}

// add the participant and check if the alias is already taken
Boolean successfullyAdded = quizForm.addParticipant(new ObjectId(userId), alias);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,11 @@ private Boolean changeFormStatus(LiveQuizSocketMessage quizSocketMessage, Course
this.broadcast(outgoingMessage, course.getId().toHexString(), formId);
}

// if it is set to STARTED set the timestamp
if (formStatusEnum == FormStatus.STARTED) {
form.setStartTimestamp();
}

// send the updated form to all receivers (stringify the form)
LiveQuizSocketMessage outgoingMessage = new LiveQuizSocketMessage("FORM_STATUS_CHANGED", form.status.toString(), null, null, form);
this.broadcast(outgoingMessage, course.getId().toHexString(), formId);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package de.htwg_konstanz.mobilelearning.services;

import static io.restassured.RestAssured.given;
import static org.hamcrest.CoreMatchers.is;

import java.net.URI;
import java.util.List;

import org.bson.types.ObjectId;
Expand All @@ -13,22 +10,17 @@
import org.junit.jupiter.api.TestInfo;

import de.htwg_konstanz.mobilelearning.Helper;
import de.htwg_konstanz.mobilelearning.SocketClient;
import de.htwg_konstanz.mobilelearning.MockMongoTestProfile;
import de.htwg_konstanz.mobilelearning.MockUser;
import de.htwg_konstanz.mobilelearning.models.Course;
import de.htwg_konstanz.mobilelearning.models.auth.User;
import de.htwg_konstanz.mobilelearning.models.quiz.QuizForm;
import de.htwg_konstanz.mobilelearning.repositories.CourseRepository;
import de.htwg_konstanz.mobilelearning.repositories.UserRepository;
import de.htwg_konstanz.mobilelearning.services.CourseService;
import de.htwg_konstanz.mobilelearning.services.auth.UserService;
import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.junit.TestProfile;
import io.restassured.response.Response;
import jakarta.inject.Inject;
import jakarta.websocket.ContainerProvider;
import jakarta.websocket.Session;

@QuarkusTest
@TestProfile(MockMongoTestProfile.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,13 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInfo;

import com.google.gson.Gson;

import de.htwg_konstanz.mobilelearning.Helper;
import de.htwg_konstanz.mobilelearning.SocketClient;
import de.htwg_konstanz.mobilelearning.MockMongoTestProfile;
import de.htwg_konstanz.mobilelearning.MockUser;
import de.htwg_konstanz.mobilelearning.models.Course;
import de.htwg_konstanz.mobilelearning.models.FormShell;
import de.htwg_konstanz.mobilelearning.models.feedback.FeedbackForm;
import de.htwg_konstanz.mobilelearning.repositories.UserRepository;
import de.htwg_konstanz.mobilelearning.services.CourseService;
import de.htwg_konstanz.mobilelearning.services.auth.UserService;
import de.htwg_konstanz.mobilelearning.services.quiz.socket.LiveQuizSocket;
import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.junit.TestProfile;
import io.restassured.response.Response;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,14 @@ public void participateUniqueAlias() {
String courseId = courses.getFirst().getId().toString();
String formId = courses.getFirst().getQuizForms().get(0).getId().toString();

// create 2 students and sync the courses to
MockUser student1 = Helper.createMockUser("Student-1");
given().header("Authorization", "Bearer " + student1.getJwt()).when().get("/course").then().statusCode(200);
MockUser student2 = Helper.createMockUser("Student-2");
given().header("Authorization", "Bearer " + student2.getJwt()).when().get("/course").then().statusCode(200);

// Check successful RestResponse
given().header("Authorization", "Bearer " + Helper.createMockUser("Student1").getJwt())
given().header("Authorization", "Bearer " + student1.getJwt())
.pathParam("courseId", courseId)
.pathParam("formId", formId)
.body("alias")
Expand All @@ -168,7 +174,7 @@ public void participateUniqueAlias() {
.body(is("Successfully added"));

// 200 if alias already taken by the same user
given().header("Authorization", "Bearer " + Helper.createMockUser("Student1").getJwt())
given().header("Authorization", "Bearer " + student1.getJwt())
.pathParam("courseId", courseId)
.pathParam("formId", formId)
.body("alias")
Expand All @@ -179,7 +185,7 @@ public void participateUniqueAlias() {
.body(is("Successfully added"));

// 409 if alias already taken by another user
given().header("Authorization", "Bearer " + Helper.createMockUser("Student2").getJwt())
given().header("Authorization", "Bearer " + student2.getJwt())
.pathParam("courseId", courseId)
.pathParam("formId", formId)
.body("alias")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,13 @@
import static io.restassured.RestAssured.given;

import java.net.URI;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import java.util.Date;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInfo;

import com.fasterxml.jackson.databind.ObjectMapper;

import de.htwg_konstanz.mobilelearning.Helper;
import de.htwg_konstanz.mobilelearning.SocketClient;
import de.htwg_konstanz.mobilelearning.MockMongoTestProfile;
Expand Down Expand Up @@ -57,6 +53,9 @@ public void startQuizForm() {
MockUser prof = Helper.createMockUser("Prof");
given().header("Authorization", "Bearer " + prof.getJwt()).get("/course").then().statusCode(200);

// store a timestamp to check if the form's startTimestamp has been set
Date startTimestamp = new Date();

// create a websocket client
try {
SocketClient client = new SocketClient();
Expand All @@ -81,6 +80,11 @@ public void startQuizForm() {
System.out.println(e);
Assertions.fail(e.getMessage());
}

// check if the startTimestamp has been set and is less than 1 minute ago
Date formStartTimestamp = courseService.getCourse(courseId).getQuizForms().get(0).getStartTimestamp();
Assertions.assertNotNull(formStartTimestamp);
Assertions.assertTrue(formStartTimestamp.after(startTimestamp));
}

@Test
Expand Down

0 comments on commit fc472d5

Please sign in to comment.