Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: e2e tests #150

Merged
merged 2 commits into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 3 additions & 26 deletions server/src/test/java/de/uftos/e2e/StudentsTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package de.uftos.e2e;

import static de.uftos.utils.JsonGenerator.generatePageJson;
import static de.uftos.utils.JsonGenerator.generateStudentJson;
import static de.uftos.utils.JsonGenerator.generateTagJson;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.notNullValue;
Expand All @@ -8,7 +11,6 @@
import java.util.Collections;
import java.util.List;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -89,31 +91,6 @@ static void deleteCreatedStudents() {
.statusCode(200);
}

private static String generateStudentJson(String firstName, String lastName, List<String> tags)
throws JSONException {
return new JSONObject()
.put("firstName", firstName)
.put("lastName", lastName)
.put("tags", tags)
.toString();
}

private static String generateTagJson(String name)
throws JSONException {
return new JSONObject()
.put("tagName", name)
.toString();
}

private static String generatePageJson(int page, int size, List<String> sort)
throws JSONException {
return new JSONObject()
.put("page", page)
.put("size", size)
.put("sort", sort)
.toString();
}

@Test
void getAllStudents() throws JSONException {
given().contentType(ContentType.JSON)
Expand Down
21 changes: 2 additions & 19 deletions server/src/test/java/de/uftos/e2e/SubjectsTest.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package de.uftos.e2e;

import static de.uftos.utils.JsonGenerator.generatePageJson;
import static de.uftos.utils.JsonGenerator.generateSubjectJson;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.notNullValue;

import io.restassured.http.ContentType;
import java.util.Collections;
import java.util.List;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -63,23 +63,6 @@ static void deleteCreatedStudents() {
.statusCode(200);
}

private static String generateSubjectJson(String name)
throws JSONException {
return new JSONObject()
.put("name", name)
.put("tags", Collections.emptyList())
.toString();
}

private static String generatePageJson(int page, int size, List<String> sort)
throws JSONException {
return new JSONObject()
.put("page", page)
.put("size", size)
.put("sort", sort)
.toString();
}

@Test
void getAllSubjects() throws JSONException {
given().contentType(ContentType.JSON)
Expand Down
81 changes: 81 additions & 0 deletions server/src/test/java/de/uftos/utils/JsonGenerator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package de.uftos.utils;

import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
* This class helps to generate the JSON needed for the E2E tests.
*/
public class JsonGenerator {

/**
* Generates the student JSON.
*
* @param firstName The first name of the student
* @param lastName The last name of the student
* @param tags The ids of the tags the student has
* @return The requested JSON
* @throws JSONException If something is malformed.
*/
public static String generateStudentJson(String firstName, String lastName, List<String> tags)
throws JSONException {
JSONArray jsonArray = new JSONArray();
tags.forEach(jsonArray::put);
return new JSONObject()
.put("firstName", firstName)
.put("lastName", lastName)
.put("tagIds", jsonArray)
.toString();
}

/**
* Generates the subject JSON.
*
* @param name The name of the subject
* @return The requested JSON
* @throws JSONException If something is malformed.
*/
public static String generateSubjectJson(String name)
throws JSONException {
return new JSONObject()
.put("name", name)
.put("tagIds", new JSONArray())
.toString();
}

/**
* Generates the tag JSON.
*
* @param name The name of the tag
* @return The requested JSON
* @throws JSONException If something is malformed.
*/
public static String generateTagJson(String name)
throws JSONException {
return new JSONObject()
.put("tagName", name)
.toString();
}

/**
* Generates the page JSON.
*
* @param page The page number
* @param size The size of the page
* @param sort How the data should be sorted
* @return The requested JSON
* @throws JSONException If something is malformed.
*/
public static String generatePageJson(int page, int size, List<String> sort)
throws JSONException {
JSONArray jsonArray = new JSONArray();
sort.forEach(jsonArray::put);
return new JSONObject()
.put("page", page)
.put("size", size)
.put("sort", jsonArray)
.toString();
}
}