Skip to content

Commit

Permalink
All tests working
Browse files Browse the repository at this point in the history
  • Loading branch information
uo287545 committed Apr 25, 2024
1 parent 047bf0f commit 8f4f72d
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 48 deletions.

This file was deleted.

11 changes: 10 additions & 1 deletion src/main/java/com/uniovi/services/QuestionGeneratorService.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.uniovi.components.generators.QuestionGenerator;
import com.uniovi.components.generators.QuestionGeneratorV2;
import com.uniovi.dto.QuestionDto;
import com.uniovi.entities.Answer;
import com.uniovi.entities.Category;
import com.uniovi.entities.Question;
import jakarta.transaction.Transactional;
Expand All @@ -27,7 +28,7 @@ public class QuestionGeneratorService {

private final QuestionService questionService;

private String jsonFilePath = "src/main/resources/static/JSON/QuestionTemplates.json";
public static final String jsonFilePath = "src/main/resources/static/JSON/QuestionTemplates.json";

private Deque<QuestionType> types = new ArrayDeque<>();

Expand Down Expand Up @@ -102,6 +103,14 @@ public void generateTestQuestions() throws IOException {
questions.forEach(questionService::addNewQuestion);
}

@Transactional
public void generateTestQuestions(String cat) throws IOException {
Answer a1 = new Answer("1", true);
List<Answer> answers = List.of(a1, new Answer("2", false), new Answer("3", false), new Answer("4", false));
Question q = new Question("Statement", answers, a1, new Category(cat), "es");
questionService.addNewQuestion(new QuestionDto(q));
}

private class QuestionType {

private JsonNode question;
Expand Down
4 changes: 0 additions & 4 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,3 @@ springdoc.packagesToScan=com.uniovi.controllers.api
management.endpoint.metrics.enabled=true
management.endpoints.web.exposure.include=prometheus
management.endpoints.jmx.exposure.include=*


# Question templates
question.json.path=src/main/resources/static/JSON/QuestionTemplates.json
12 changes: 7 additions & 5 deletions src/test/java/com/uniovi/Wiq_UnitTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -795,12 +795,13 @@ public void testGetQuestions() throws IOException, InterruptedException, JSONExc
@Test
@Order(51)
public void testGetQuestionsByCategoryName() throws IOException, InterruptedException, JSONException {
insertSomeQuestions();
String cat = "Science";
questionGeneratorService.generateTestQuestions(cat);
Player player = playerService.getUsersByRole("ROLE_USER").get(0);
ApiKey apiKey = player.getApiKey();

HttpResponse<String> response = sendRequest("GET", "/api/questions", Map.of(),
Map.of("apiKey", apiKey.getKeyToken(), "category", "Science"));
Map.of("apiKey", apiKey.getKeyToken(), "category", cat));

Assertions.assertEquals(200, response.statusCode());
JSONObject json = parseJsonResponse(response);
Expand All @@ -811,10 +812,11 @@ public void testGetQuestionsByCategoryName() throws IOException, InterruptedExce
@Test
@Order(52)
public void testGetQuestionsByCategoryId() throws IOException, InterruptedException, JSONException {
insertSomeQuestions();
String category = "Science";
questionGeneratorService.generateTestQuestions(category);
Player player = playerService.getUsersByRole("ROLE_USER").get(0);
ApiKey apiKey = player.getApiKey();
Category cat = categoryService.getCategoryByName("Science");
Category cat = categoryService.getCategoryByName(category);

HttpResponse<String> response = sendRequest("GET", "/api/questions", Map.of(),
Map.of("apiKey", apiKey.getKeyToken(), "category", cat.getId()));
Expand Down Expand Up @@ -1545,7 +1547,7 @@ private JSONObject parseJsonResponse(HttpResponse<String> response) throws JSONE
/**
* Inserts some sample questions into the database
*/
private void insertSomeQuestions() throws InterruptedException, IOException {
private void insertSomeQuestions() throws IOException {
questionGeneratorService.generateTestQuestions();
}
}
11 changes: 8 additions & 3 deletions src/test/java/com/uniovi/steps/GameStep.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.uniovi.Wiq_IntegrationTests;
import com.uniovi.services.InsertSampleDataService;
import com.uniovi.services.QuestionGeneratorService;
import com.uniovi.util.PropertiesExtractor;
import com.uniovi.util.SeleniumUtils;
import io.cucumber.java.en.Then;
Expand All @@ -12,23 +13,27 @@
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.IOException;
import java.util.List;

public class GameStep extends Wiq_IntegrationTests {

@Autowired
private InsertSampleDataService dataService;
@Autowired
private QuestionGeneratorService questionGeneratorService;

private Logger log = LoggerFactory.getLogger(GameStep.class);

@When("I press Play")
public void iPressPlay() {
dataService.generateTestQuestions();
public void iPressPlay() throws IOException {
questionGeneratorService.generateTestQuestions();
List<WebElement> elems = SeleniumUtils.waitLoadElementsBy(driver, "free", "//*[@href=\"/game\"]", 5);
elems.get(0).click();
}

@Then("I should start playing")
public void iShouldStartPlaying() throws InterruptedException {
public void iShouldStartPlaying() {
boolean playing = true;
String xpath = "//*[contains(text(),'" + p.getString("game.finish", PropertiesExtractor.getSPANISH()) + "')]";
int i= 0;
Expand Down

0 comments on commit 8f4f72d

Please sign in to comment.