From 42cdeb545c5302589cd1f6de62666f9896d0cb51 Mon Sep 17 00:00:00 2001 From: sinne10 Date: Fri, 8 Mar 2024 16:14:26 +0100 Subject: [PATCH 1/5] Added tests for question generator --- .../com/wiq/wiq/QuestionGeneratorTests.java | 74 +++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 backend/wiq/src/test/java/com/wiq/wiq/QuestionGeneratorTests.java diff --git a/backend/wiq/src/test/java/com/wiq/wiq/QuestionGeneratorTests.java b/backend/wiq/src/test/java/com/wiq/wiq/QuestionGeneratorTests.java new file mode 100644 index 00000000..4c0e2f23 --- /dev/null +++ b/backend/wiq/src/test/java/com/wiq/wiq/QuestionGeneratorTests.java @@ -0,0 +1,74 @@ +package com.wiq.wiq; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +import com.wiq.wiq.services.questionGenerator.QuestionGenerator; +import com.wiq.wiq.services.questionGenerator.question.QuestionType; + +import static org.junit.jupiter.api.Assertions.*; + +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; + +@SpringBootTest +public class QuestionGeneratorTests { + + private QuestionGenerator qgEN; + private QuestionGenerator qgES; + + private static QuestionType[] types = {QuestionType.POPULATION, QuestionType.CAPITAL, QuestionType.SIZE, QuestionType.LANGUAGE}; + + @BeforeEach + void setUp(){ + qgEN = new QuestionGenerator("en"); + qgES = new QuestionGenerator("es"); + } + + @Test + void testGenerateQuestionsEnglish() { + + String question; + JSONObject json; + + for(QuestionType t : types) { + for(int i=0; i<3; i++) { + question = qgEN.generateQuestion(t); + + //Check correct format + try { + json = new JSONObject(question); + } catch (JSONException e) { + fail("Not a JSON"); + return; + } + + //Check for expected fields + try { + // Test question + assertTrue(json.has("question")); + + + // Test answers + assertTrue(json.has("answers")); + + // Retrieve the "answers" field and check if it's an array + Object answersField = json.get("answers"); + assertTrue(answersField instanceof JSONArray, "The 'answers' field is not an array"); + + // Convert the field to a JSONArray + JSONArray answersArray = (JSONArray) answersField; + + // Check if the array has size 4 + assertEquals(4, answersArray.length(), "There aren't 4 answers"); + } catch (JSONException e) { + fail("Exception occurred while parsing JSON: " + e.getMessage()); + } + } + } + + } + +} From 15197a96147a53bef89c094b296766c1a7e61b2d Mon Sep 17 00:00:00 2001 From: sinne10 Date: Fri, 8 Mar 2024 16:24:54 +0100 Subject: [PATCH 2/5] Added spanish tests for questiongenerator --- .../QuestionGeneratorTests.java | 48 +++++++++++++++++-- 1 file changed, 45 insertions(+), 3 deletions(-) rename backend/wiq/src/test/java/com/wiq/wiq/{ => services}/QuestionGeneratorTests.java (59%) diff --git a/backend/wiq/src/test/java/com/wiq/wiq/QuestionGeneratorTests.java b/backend/wiq/src/test/java/com/wiq/wiq/services/QuestionGeneratorTests.java similarity index 59% rename from backend/wiq/src/test/java/com/wiq/wiq/QuestionGeneratorTests.java rename to backend/wiq/src/test/java/com/wiq/wiq/services/QuestionGeneratorTests.java index 4c0e2f23..318ddf1b 100644 --- a/backend/wiq/src/test/java/com/wiq/wiq/QuestionGeneratorTests.java +++ b/backend/wiq/src/test/java/com/wiq/wiq/services/QuestionGeneratorTests.java @@ -1,8 +1,7 @@ -package com.wiq.wiq; +package com.wiq.wiq.services; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.springframework.boot.test.context.SpringBootTest; import com.wiq.wiq.services.questionGenerator.QuestionGenerator; import com.wiq.wiq.services.questionGenerator.question.QuestionType; @@ -13,7 +12,6 @@ import org.json.JSONException; import org.json.JSONObject; -@SpringBootTest public class QuestionGeneratorTests { private QuestionGenerator qgEN; @@ -51,6 +49,50 @@ void testGenerateQuestionsEnglish() { assertTrue(json.has("question")); + // Test answers + assertTrue(json.has("answers")); + + // Retrieve the "answers" field and check if it's an array + Object answersField = json.get("answers"); + assertTrue(answersField instanceof JSONArray, "The 'answers' field is not an array"); + + // Convert the field to a JSONArray + JSONArray answersArray = (JSONArray) answersField; + + // Check if the array has size 4 + assertEquals(4, answersArray.length(), "There aren't 4 answers"); + } catch (JSONException e) { + fail("Exception occurred while parsing JSON: " + e.getMessage()); + } + } + } + + } + + @Test + void testGenerateQuestionsSpanish() { + + String question; + JSONObject json; + + for(QuestionType t : types) { + for(int i=0; i<3; i++) { + question = qgES.generateQuestion(t); + + //Check correct format + try { + json = new JSONObject(question); + } catch (JSONException e) { + fail("Not a JSON"); + return; + } + + //Check for expected fields + try { + // Test question + assertTrue(json.has("question")); + + // Test answers assertTrue(json.has("answers")); From 7113824e3db66012170488dad1b3c30ab055ad5b Mon Sep 17 00:00:00 2001 From: sinne10 Date: Fri, 8 Mar 2024 16:33:01 +0100 Subject: [PATCH 3/5] Bugfix --- .../com/wiq/wiq/services/QuestionGeneratorTests.java | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/backend/wiq/src/test/java/com/wiq/wiq/services/QuestionGeneratorTests.java b/backend/wiq/src/test/java/com/wiq/wiq/services/QuestionGeneratorTests.java index 318ddf1b..a2cccef7 100644 --- a/backend/wiq/src/test/java/com/wiq/wiq/services/QuestionGeneratorTests.java +++ b/backend/wiq/src/test/java/com/wiq/wiq/services/QuestionGeneratorTests.java @@ -29,20 +29,21 @@ void setUp(){ void testGenerateQuestionsEnglish() { String question; - JSONObject json; for(QuestionType t : types) { for(int i=0; i<3; i++) { question = qgEN.generateQuestion(t); + JSONObject json = null; //Check correct format try { json = new JSONObject(question); } catch (JSONException e) { fail("Not a JSON"); - return; } + assertNotNull(json, "JSONObject was not initialized"); + //Check for expected fields try { // Test question @@ -73,20 +74,21 @@ void testGenerateQuestionsEnglish() { void testGenerateQuestionsSpanish() { String question; - JSONObject json; for(QuestionType t : types) { for(int i=0; i<3; i++) { question = qgES.generateQuestion(t); + JSONObject json = null; //Check correct format try { json = new JSONObject(question); } catch (JSONException e) { fail("Not a JSON"); - return; } + assertNotNull(json, "JSONObject was not initialized"); + //Check for expected fields try { // Test question From 1f1273a647ea0ced9216b65865c66879b6fc13b7 Mon Sep 17 00:00:00 2001 From: sinne10 Date: Fri, 8 Mar 2024 16:47:55 +0100 Subject: [PATCH 4/5] Added tests for question class --- .../question/QuestionTests.java | 67 +++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 backend/wiq/src/test/java/com/wiq/wiq/services/questionGenerator/question/QuestionTests.java diff --git a/backend/wiq/src/test/java/com/wiq/wiq/services/questionGenerator/question/QuestionTests.java b/backend/wiq/src/test/java/com/wiq/wiq/services/questionGenerator/question/QuestionTests.java new file mode 100644 index 00000000..8590e646 --- /dev/null +++ b/backend/wiq/src/test/java/com/wiq/wiq/services/questionGenerator/question/QuestionTests.java @@ -0,0 +1,67 @@ +package com.wiq.wiq.services.questionGenerator.question; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +import java.util.Arrays; +import java.util.List; + +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; + +public class QuestionTests { + + private Question question; + + @BeforeEach + void setUp() { + List answers = Arrays.asList("A", "B", "C"); + question = new Question("What is the capital of France?", answers); + } + + @Test + void testGetQuestion() { + assertEquals("What is the capital of France?", question.getQuestion()); + } + + @Test + void testSetQuestion() { + question.setQuestion("What is the capital of Germany?"); + assertEquals("What is the capital of Germany?", question.getQuestion()); + } + + @Test + void testGetAnswers() { + List expectedAnswers = Arrays.asList("A", "B", "C"); + assertEquals(expectedAnswers, question.getAnswers()); + } + + @Test + void testSetAnswers() { + List newAnswers = Arrays.asList("X", "Y", "Z"); + question.setAnswers(newAnswers); + assertEquals(newAnswers, question.getAnswers()); + } + + @Test + void testAddRightAnswer() { + question.addRightAnswer("Paris"); + List expectedAnswers = Arrays.asList("Paris", "A", "B", "C"); + assertEquals(expectedAnswers, question.getAnswers()); + } + + @Test + void testGetJSON() { + try { + JSONObject expectedJson = new JSONObject() + .put("question", "What is the capital of France?") + .put("answers", new JSONArray().put("A").put("B").put("C")); + assertEquals(expectedJson.toString(), question.getJSON().toString()); + } catch (JSONException e) { + fail("JSONException occurred: " + e.getMessage()); + } + } +} From 45cca9241a86afc3557c710dc8ae35a47c4d8359 Mon Sep 17 00:00:00 2001 From: sinne10 Date: Fri, 8 Mar 2024 17:14:21 +0100 Subject: [PATCH 5/5] Added check for duplicated answers --- .../wiq/services/QuestionGeneratorTests.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/backend/wiq/src/test/java/com/wiq/wiq/services/QuestionGeneratorTests.java b/backend/wiq/src/test/java/com/wiq/wiq/services/QuestionGeneratorTests.java index a2cccef7..8c675bcf 100644 --- a/backend/wiq/src/test/java/com/wiq/wiq/services/QuestionGeneratorTests.java +++ b/backend/wiq/src/test/java/com/wiq/wiq/services/QuestionGeneratorTests.java @@ -8,6 +8,9 @@ import static org.junit.jupiter.api.Assertions.*; +import java.util.LinkedList; +import java.util.List; + import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; @@ -62,6 +65,14 @@ void testGenerateQuestionsEnglish() { // Check if the array has size 4 assertEquals(4, answersArray.length(), "There aren't 4 answers"); + + // Check for unique answers + List seenAnswers = new LinkedList(); + for (int j = 0; j < answersArray.length(); j++) { + String answer = answersArray.getString(j); + assertFalse(seenAnswers.contains(answer), "Answer: " + answer + " is duplicated"); + seenAnswers.add(answer); + } } catch (JSONException e) { fail("Exception occurred while parsing JSON: " + e.getMessage()); } @@ -107,6 +118,14 @@ void testGenerateQuestionsSpanish() { // Check if the array has size 4 assertEquals(4, answersArray.length(), "There aren't 4 answers"); + + // Check for unique answers + List seenAnswers = new LinkedList(); + for (int j = 0; j < answersArray.length(); j++) { + String answer = answersArray.getString(j); + assertFalse(seenAnswers.contains(answer), "Answer: " + answer + " is duplicated"); + seenAnswers.add(answer); + } } catch (JSONException e) { fail("Exception occurred while parsing JSON: " + e.getMessage()); }