generated from Arquisoft/wiq_0
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
22 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ | |
@Getter | ||
@Setter | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
@ToString | ||
public class AnswerDto { | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,22 @@ | ||
package com.uniovi; | ||
|
||
import com.uniovi.dto.*; | ||
import com.uniovi.entities.*; | ||
import com.uniovi.repositories.*; | ||
import com.uniovi.services.*; | ||
import com.uniovi.services.impl.*; | ||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.node.ArrayNode; | ||
import com.uniovi.components.generators.geography.BorderQuestionGenerator; | ||
import com.uniovi.components.generators.geography.CapitalQuestionGenerator; | ||
import com.uniovi.components.generators.geography.ContinentQuestionGeneration; | ||
import com.uniovi.dto.PlayerDto; | ||
import com.uniovi.dto.RoleDto; | ||
import com.uniovi.entities.*; | ||
import com.uniovi.repositories.*; | ||
import com.uniovi.services.*; | ||
import org.json.JSONArray; | ||
import org.json.JSONException; | ||
import org.json.JSONObject; | ||
import org.junit.Assert; | ||
import org.junit.jupiter.api.*; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import com.uniovi.dto.*; | ||
import com.uniovi.entities.*; | ||
import com.uniovi.repositories.*; | ||
import com.uniovi.services.*; | ||
import com.uniovi.services.impl.*; | ||
import org.junit.jupiter.api.*; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.PageImpl; | ||
import org.springframework.data.domain.PageRequest; | ||
import org.springframework.security.crypto.password.PasswordEncoder; | ||
import org.springframework.test.annotation.DirtiesContext; | ||
import org.springframework.test.context.ActiveProfiles; | ||
|
||
import java.io.IOException; | ||
import java.awt.print.Pageable; | ||
import java.time.LocalDateTime; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import java.io.IOException; | ||
import java.net.URI; | ||
import java.net.URLEncoder; | ||
|
@@ -49,13 +27,6 @@ | |
import java.time.LocalDateTime; | ||
import java.util.*; | ||
|
||
import static org.junit.Assert.*; | ||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
@SpringBootTest | ||
@Tag("unit") | ||
@DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_EACH_TEST_METHOD) | ||
|
@@ -96,77 +67,22 @@ public class Wiq_UnitTests { | |
|
||
private final HttpClient httpClient = HttpClient.newHttpClient(); | ||
|
||
private Player createPlayer(){ | ||
return new Player("name","[email protected]","password"); | ||
private Player createPlayer() { | ||
return new Player("name", "[email protected]", "password"); | ||
} | ||
|
||
@Test | ||
@Order(1) | ||
public void testPlayerService() { | ||
List<Player> players = playerService.getUsersByRole("ROLE_USER"); | ||
Assertions.assertEquals(1, players.size()); | ||
} | ||
|
||
@Test | ||
@Order(2) | ||
public void testQuestions() throws InterruptedException, IOException { | ||
sampleDataService.insertSampleQuestions(); | ||
sampleDataService.generateSampleData(); | ||
@Order(3) | ||
public void testQuestionsGenerator() throws IOException { | ||
questionGeneratorService.generateTestQuestions(); | ||
List<Question> questions = questionService.getAllQuestions(); | ||
Assertions.assertFalse(questions.isEmpty()); | ||
|
||
} | ||
@Test | ||
@Order(2) | ||
public void testRandomQuestions() throws InterruptedException, IOException { | ||
sampleDataService.insertSampleQuestions(); | ||
sampleDataService.generateSampleData(); | ||
List<Question> questions = questionService.getRandomQuestions(5); | ||
Assertions.assertEquals(5,questions.size()); | ||
} | ||
|
||
@Test | ||
@Order(3) | ||
public void testBorderQuestionsGenerator() throws InterruptedException, IOException { | ||
BorderQuestionGenerator borderQuestionGenerator=new BorderQuestionGenerator(categoryService,Question.SPANISH); | ||
List<Question> questions = borderQuestionGenerator.getQuestions(); | ||
Assertions.assertFalse(questions.isEmpty()); | ||
|
||
for (Question question : questions) { | ||
Assertions.assertNotNull(question.getCorrectAnswer()); | ||
Assertions.assertEquals(4, question.getOptions().size()); | ||
Assertions.assertTrue(question.getOptions().contains(question.getCorrectAnswer())); | ||
} | ||
} | ||
|
||
@Test | ||
@Order(4) | ||
public void testCapitalQuestionsGenerator() throws InterruptedException, IOException { | ||
CapitalQuestionGenerator capitalQuestionGenerator=new CapitalQuestionGenerator(categoryService,Question.SPANISH); | ||
List<Question> questions = capitalQuestionGenerator.getQuestions(); | ||
Assertions.assertFalse(questions.isEmpty()); | ||
|
||
for (Question question : questions) { | ||
Assertions.assertNotNull(question.getCorrectAnswer()); | ||
Assertions.assertEquals(4, question.getOptions().size()); | ||
Assertions.assertTrue(question.getOptions().contains(question.getCorrectAnswer())); | ||
} | ||
} | ||
|
||
@Test | ||
@Order(5) | ||
public void testContinentQuestionsGenerator() throws InterruptedException, IOException { | ||
ContinentQuestionGeneration continentQuestionGenerator=new ContinentQuestionGeneration(categoryService,Question.SPANISH); | ||
List<Question> questions = continentQuestionGenerator.getQuestions(); | ||
Assertions.assertFalse(questions.isEmpty()); | ||
|
||
for (Question question : questions) { | ||
Assertions.assertNotNull(question.getCorrectAnswer()); | ||
Assertions.assertEquals(4, question.getOptions().size()); | ||
Assertions.assertTrue(question.getOptions().contains(question.getCorrectAnswer())); | ||
} | ||
} | ||
|
||
@Test | ||
|
@@ -393,6 +309,7 @@ public void testGetDuration() { | |
|
||
Assertions.assertEquals("00:05:00", gameSession.getDuration()); | ||
} | ||
|
||
@Test | ||
@Order(22) | ||
public void testPlayerToJson() { | ||
|
@@ -460,8 +377,7 @@ public void testScrambleOptions() { | |
question.addOption(option2); | ||
question.addOption(option3); | ||
|
||
question.scrambleOptions(); | ||
List<Answer> scrambledOptions = question.getOptions(); | ||
List<Answer> scrambledOptions = question.returnScrambledOptions(); | ||
|
||
Assertions.assertTrue(scrambledOptions.contains(option1)); | ||
Assertions.assertTrue(scrambledOptions.contains(option2)); | ||
|
@@ -661,7 +577,7 @@ public void testCreatePlayerValid() throws IOException, InterruptedException, JS | |
data.put("username", "newUser"); | ||
data.put("email", "[email protected]"); | ||
data.put("password", "password"); | ||
data.put("roles", new String[] {"ROLE_USER"}); | ||
data.put("roles", new String[]{"ROLE_USER"}); | ||
|
||
HttpResponse<String> response = sendRequest("POST", "/api/players", Map.of("API-KEY", apiKey.getKeyToken()), | ||
data); | ||
|
@@ -884,7 +800,7 @@ public void testGetQuestionsByCategoryName() throws IOException, InterruptedExce | |
ApiKey apiKey = player.getApiKey(); | ||
|
||
HttpResponse<String> response = sendRequest("GET", "/api/questions", Map.of(), | ||
Map.of("apiKey", apiKey.getKeyToken(), "category", "Geography")); | ||
Map.of("apiKey", apiKey.getKeyToken(), "category", "Science")); | ||
|
||
Assertions.assertEquals(200, response.statusCode()); | ||
JSONObject json = parseJsonResponse(response); | ||
|
@@ -898,7 +814,7 @@ public void testGetQuestionsByCategoryId() throws IOException, InterruptedExcept | |
insertSomeQuestions(); | ||
Player player = playerService.getUsersByRole("ROLE_USER").get(0); | ||
ApiKey apiKey = player.getApiKey(); | ||
Category cat = categoryService.getCategoryByName("Geography"); | ||
Category cat = categoryService.getCategoryByName("Science"); | ||
|
||
HttpResponse<String> response = sendRequest("GET", "/api/questions", Map.of(), | ||
Map.of("apiKey", apiKey.getKeyToken(), "category", cat.getId())); | ||
|
@@ -1564,17 +1480,18 @@ public void testDeleteQuestion() throws IOException, InterruptedException, JSONE | |
|
||
/** | ||
* Sends an HTTP request to the API | ||
* @param method HTTP method | ||
* @param uri URI to send the request to | ||
* | ||
* @param method HTTP method | ||
* @param uri URI to send the request to | ||
* @param headers Headers to include in the request | ||
* @param data Data to send in the request | ||
* @param data Data to send in the request | ||
* @return The response from the server | ||
* @throws IOException | ||
* @throws InterruptedException | ||
*/ | ||
private HttpResponse<String> sendRequest(String method, String uri, | ||
Map<String, String> headers, | ||
Map<String, Object> data) throws IOException, InterruptedException { | ||
Map<String, String> headers, | ||
Map<String, Object> data) throws IOException, InterruptedException { | ||
HttpRequest.Builder requestBuilder = HttpRequest.newBuilder(); | ||
|
||
uri = Wiq_IntegrationTests.URL.substring(0, Wiq_IntegrationTests.URL.length() - 1) + uri; | ||
|
@@ -1603,6 +1520,7 @@ private HttpResponse<String> sendRequest(String method, String uri, | |
|
||
/** | ||
* Builds a query string from a map of data | ||
* | ||
* @param data The data to include in the query string | ||
* @return The query string | ||
*/ | ||
|
@@ -1615,6 +1533,7 @@ private String buildQueryString(Map<String, Object> data) { | |
|
||
/** | ||
* Parses the JSON response from the server | ||
* | ||
* @param response The response from the server | ||
* @return The JSON object | ||
* @throws JSONException | ||
|
@@ -1627,7 +1546,6 @@ private JSONObject parseJsonResponse(HttpResponse<String> response) throws JSONE | |
* Inserts some sample questions into the database | ||
*/ | ||
private void insertSomeQuestions() throws InterruptedException, IOException { | ||
List<Question> qs = new ContinentQuestionGeneration(categoryService, Question.SPANISH).getQuestions(); | ||
qs.forEach(questionService::addNewQuestion); | ||
questionGeneratorService.generateTestQuestions(); | ||
} | ||
} |