Skip to content

Commit

Permalink
Fixed unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
uo287545 committed Apr 25, 2024
1 parent fb1d726 commit 047bf0f
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 106 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,6 @@ private List<Question> generateQuestion(JsonNode question, Category cat) throws
// Generate the question
Question q = new Question(questionStatement, options, correct, cat, language);

// Scramble the options
q.scrambleOptions();

// Add the question to the list
questions.add(q);
}
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/uniovi/dto/AnswerDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class AnswerDto {

Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/uniovi/dto/CategoryDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class CategoryDto {

@Schema(description = "The name of the category", example = "Geography")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
Expand Down
122 changes: 20 additions & 102 deletions src/test/java/com/uniovi/Wiq_UnitTests.java
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;
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -393,6 +309,7 @@ public void testGetDuration() {

Assertions.assertEquals("00:05:00", gameSession.getDuration());
}

@Test
@Order(22)
public void testPlayerToJson() {
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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()));
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
*/
Expand All @@ -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
Expand All @@ -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();
}
}

0 comments on commit 047bf0f

Please sign in to comment.