From 4661957b668fbf8fd972839c932606e900abc7a7 Mon Sep 17 00:00:00 2001 From: Pelayori <31128562+Pelayori@users.noreply.github.com> Date: Sat, 27 Apr 2024 14:07:03 +0200 Subject: [PATCH 1/8] Do some e2e tests for multiplayer --- src/test/java/com/uniovi/steps/GameStep.java | 37 +++++++++++++++---- src/test/resources/features/Game.feature | 2 +- .../features/MultiplayerGame.feature | 11 ++++++ 3 files changed, 41 insertions(+), 9 deletions(-) create mode 100644 src/test/resources/features/MultiplayerGame.feature diff --git a/src/test/java/com/uniovi/steps/GameStep.java b/src/test/java/com/uniovi/steps/GameStep.java index 6b8eb0f7..ba479c8f 100644 --- a/src/test/java/com/uniovi/steps/GameStep.java +++ b/src/test/java/com/uniovi/steps/GameStep.java @@ -5,6 +5,7 @@ import com.uniovi.services.QuestionGeneratorService; import com.uniovi.util.PropertiesExtractor; import com.uniovi.util.SeleniumUtils; +import io.cucumber.java.en.And; import io.cucumber.java.en.Then; import io.cucumber.java.en.When; import org.openqa.selenium.By; @@ -32,13 +33,13 @@ public void iPressPlay() throws IOException, InterruptedException { elems.get(0).click(); } - @Then("I should start playing") - public void iShouldStartPlaying() { + @Then("I should start playing until I see message {string}") + public void iShouldStartPlaying(String messageKey) { boolean playing = true; - String xpath = "//*[contains(text(),'" + p.getString("game.finish", PropertiesExtractor.getSPANISH()) + "')]"; + String xpath = "//*[contains(text(),'" + p.getString(messageKey, PropertiesExtractor.getSPANISH()) + "')]"; int i= 0; List finalMessage; - while(playing){ + while(playing) { //I obtain the buttons for the answers List elems = By.cssSelector(".container .row button").findElements(driver); log.info("Found " + elems.size() + " buttons"); @@ -46,15 +47,35 @@ public void iShouldStartPlaying() { //I click on the first button elems.get(0).click(); - try{ - finalMessage= SeleniumUtils.waitLoadElementsByXpath(driver, xpath, 10); - }catch(Exception e){ + try { + finalMessage = SeleniumUtils.waitLoadElementsByXpath(driver, xpath, 10); + } catch(Exception e) { continue; } - if(finalMessage.size()>0){ + + if(!finalMessage.isEmpty()) { playing = false; } i++; } } + + @When("I press Play With Friends") + public void iPressPlayWithFriends() throws IOException, InterruptedException { + questionGeneratorService.generateTestQuestions(); + List elems = SeleniumUtils.waitLoadElementsBy(driver, "id", "multiplayerBtn", 5); + elems.get(0).click(); + } + + @And("I create a code") + public void iCreateACode() { + List elems = SeleniumUtils.waitLoadElementsBy(driver, "id", "createBtn", 5); + elems.get(0).click(); + } + + @And("I press start") + public void iPressStart() { + List elems = SeleniumUtils.waitLoadElementsBy(driver, "id", "startBtn", 5); + elems.get(0).click(); + } } diff --git a/src/test/resources/features/Game.feature b/src/test/resources/features/Game.feature index b3120c41..5b6b0837 100644 --- a/src/test/resources/features/Game.feature +++ b/src/test/resources/features/Game.feature @@ -6,4 +6,4 @@ Feature: I try to play a normal game And I press the register button And I go to the home page When I press Play - Then I should start playing \ No newline at end of file + Then I should start playing until I see message "game.finish" \ No newline at end of file diff --git a/src/test/resources/features/MultiplayerGame.feature b/src/test/resources/features/MultiplayerGame.feature new file mode 100644 index 00000000..0ec53959 --- /dev/null +++ b/src/test/resources/features/MultiplayerGame.feature @@ -0,0 +1,11 @@ +Feature: I play a multiplayer game + Scenario: I start a multiplayer game alone + Given I am not registered or logged in + And I am on the register page + When I fill in the form with valid data username: "userGame" email: "userGame@gmail.com" password: "password" password_confirmation: "password" + And I press the register button + And I go to the home page + When I press Play With Friends + And I create a code + And I press start + Then I should start playing until I see message "multi.finished" \ No newline at end of file From d39e4d226b7f87d0a7f61b8461cd622405e56336 Mon Sep 17 00:00:00 2001 From: Pelayori <31128562+Pelayori@users.noreply.github.com> Date: Sat, 27 Apr 2024 14:51:35 +0200 Subject: [PATCH 2/8] Do some e2e tests for multiplayer --- pom.xml | 1 + src/main/java/com/uniovi/entities/Associations.java | 9 --------- 2 files changed, 1 insertion(+), 9 deletions(-) diff --git a/pom.xml b/pom.xml index 52e13e70..3c8f1c2c 100644 --- a/pom.xml +++ b/pom.xml @@ -17,6 +17,7 @@ 17 src/main/java,src/test/resources/features ${project.basedir}/target/jacoco.exec + **/controllers/CustomErrorController.java diff --git a/src/main/java/com/uniovi/entities/Associations.java b/src/main/java/com/uniovi/entities/Associations.java index efa34f78..6046ffe1 100644 --- a/src/main/java/com/uniovi/entities/Associations.java +++ b/src/main/java/com/uniovi/entities/Associations.java @@ -3,11 +3,7 @@ import java.util.*; public class Associations { - private static final String UTILITY_CLASS = "Utility class"; - private Associations() { throw new IllegalStateException(UTILITY_CLASS); } - public static class PlayerRole { - private PlayerRole() { throw new IllegalStateException(UTILITY_CLASS); } /** * Add a new association between a player and a role * @@ -32,7 +28,6 @@ public static void removeRole(Player player, Role role) { } public static class PlayerApiKey { - private PlayerApiKey() { throw new IllegalStateException(UTILITY_CLASS); } /** * Add a new association between a player and an API key * @@ -57,7 +52,6 @@ public static void removeApiKey(Player player, ApiKey apiKey) { } public static class ApiKeyAccessLog { - private ApiKeyAccessLog() { throw new IllegalStateException(UTILITY_CLASS); } /** * Add a new association between an API key and an access log * @@ -82,7 +76,6 @@ public static void removeAccessLog(ApiKey apiKey, RestApiAccessLog accessLog) { } public static class PlayerGameSession { - private PlayerGameSession() { throw new IllegalStateException(UTILITY_CLASS); } /** * Add a new association between a player and a game session * @@ -108,7 +101,6 @@ public static void removeGameSession(Player player, GameSession gameSession) { } public static class QuestionAnswers { - private QuestionAnswers() { throw new IllegalStateException(UTILITY_CLASS); } /** * Add a new association between a question and an answer * @@ -141,7 +133,6 @@ public static void removeAnswer(Question question, List answer) { } public static class QuestionsCategory { - private QuestionsCategory() { throw new IllegalStateException(UTILITY_CLASS); } /** * Add a new association between a question and a category * From 95c5c45bb00308b826f2367c864f1ccbb3e766a2 Mon Sep 17 00:00:00 2001 From: Pelayori <31128562+Pelayori@users.noreply.github.com> Date: Sat, 27 Apr 2024 15:11:39 +0200 Subject: [PATCH 3/8] Do some e2e tests for multiplayer --- src/test/java/com/uniovi/Wiq_UnitTests.java | 53 ++++++++++++++++++++- 1 file changed, 51 insertions(+), 2 deletions(-) diff --git a/src/test/java/com/uniovi/Wiq_UnitTests.java b/src/test/java/com/uniovi/Wiq_UnitTests.java index 302eb65d..a9ffdc02 100644 --- a/src/test/java/com/uniovi/Wiq_UnitTests.java +++ b/src/test/java/com/uniovi/Wiq_UnitTests.java @@ -804,6 +804,22 @@ public void testGetQuestions() throws IOException, InterruptedException, JSONExc Assertions.assertTrue(json.getJSONArray("questions").length() > 0); } + @Test + @Order(50) + public void testGetQuestionsInvalidId() throws IOException, InterruptedException, JSONException { + insertSomeQuestions();; + Player player = playerService.getUsersByRole("ROLE_USER").get(0); + ApiKey apiKey = player.getApiKey(); + + HttpResponse response = sendRequest("GET", "/api/questions", Map.of("id", "notnumeric"), + Map.of("apiKey", apiKey.getKeyToken())); + + Assertions.assertEquals(200, response.statusCode()); + JSONObject json = parseJsonResponse(response); + Assertions.assertTrue(json.has("questions")); + Assertions.assertTrue(json.getJSONArray("questions").length() > 0); + } + @Test @Order(51) public void testGetQuestionsByCategoryName() throws IOException, InterruptedException, JSONException { @@ -1364,9 +1380,8 @@ public void testModifyQuestionMissingData() throws IOException, InterruptedExcep @Test @Order(91) - @Tag("flaky") public void testModifyQuestion() throws IOException, InterruptedException, JSONException { - insertSomeQuestions();; + insertSomeQuestions(); Question question = questionService.getAllQuestions().get(0); Player player = playerService.getUsersByRole("ROLE_USER").get(0); ApiKey apiKey = player.getApiKey(); @@ -1397,6 +1412,40 @@ public void testModifyQuestion() throws IOException, InterruptedException, JSONE Assertions.assertEquals("Modified question", updatedQuestion.get().getStatement()); } + @Test + @Order(91) + public void testModifyQuestionNewCategory() throws IOException, InterruptedException, JSONException { + insertSomeQuestions();; + Question question = questionService.getAllQuestions().get(0); + Player player = playerService.getUsersByRole("ROLE_USER").get(0); + ApiKey apiKey = player.getApiKey(); + Category category = categoryService.getCategoryByName("Geography"); + + Map data = new HashMap<>(); + data.put("statement", "Modified question"); + + List> opts = new ArrayList<>(); + opts.add(Map.of("text", "Option A", "correct", true)); + opts.add(Map.of("text", "Option B", "correct", false)); + opts.add(Map.of("text", "Option C", "correct", false)); + opts.add(Map.of("text", "Option D", "correct", false)); + + data.put("options", opts); + data.put("category", Map.of("name", "NewCreatedCategory")); + data.put("language", "en"); + + HttpResponse response = sendRequest("PATCH", "/api/questions/" + question.getId(), Map.of("API-KEY", apiKey.getKeyToken()), + data); + + Assertions.assertEquals(200, response.statusCode()); + JSONObject json = parseJsonResponse(response); + Assertions.assertTrue(json.getBoolean("success")); + + Optional updatedQuestion = questionService.getQuestion(question.getId()); + Assertions.assertTrue(updatedQuestion.isPresent()); + Assertions.assertEquals("Modified question", updatedQuestion.get().getStatement()); + } + @Test @Order(92) public void testModifyQuestionWithLessOptions() throws IOException, InterruptedException, JSONException { From 2e5230ba79935a98879c8f294f3adde51ebc17bf Mon Sep 17 00:00:00 2001 From: Pelayori <31128562+Pelayori@users.noreply.github.com> Date: Sat, 27 Apr 2024 15:49:04 +0200 Subject: [PATCH 4/8] Do some e2e tests for multiplayer --- src/test/java/com/uniovi/Wiq_UnitTests.java | 29 +++++++++++++++++---- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/src/test/java/com/uniovi/Wiq_UnitTests.java b/src/test/java/com/uniovi/Wiq_UnitTests.java index a9ffdc02..7f285984 100644 --- a/src/test/java/com/uniovi/Wiq_UnitTests.java +++ b/src/test/java/com/uniovi/Wiq_UnitTests.java @@ -796,7 +796,7 @@ public void testGetQuestions() throws IOException, InterruptedException, JSONExc ApiKey apiKey = player.getApiKey(); HttpResponse response = sendRequest("GET", "/api/questions", Map.of(), - Map.of("apiKey", apiKey.getKeyToken())); + Map.of("apiKey", apiKey.getKeyToken(), "lang", "es")); Assertions.assertEquals(200, response.statusCode()); JSONObject json = parseJsonResponse(response); @@ -806,18 +806,18 @@ public void testGetQuestions() throws IOException, InterruptedException, JSONExc @Test @Order(50) - public void testGetQuestionsInvalidId() throws IOException, InterruptedException, JSONException { + void testGetQuestionsInvalidId() throws IOException, InterruptedException, JSONException { insertSomeQuestions();; Player player = playerService.getUsersByRole("ROLE_USER").get(0); ApiKey apiKey = player.getApiKey(); - HttpResponse response = sendRequest("GET", "/api/questions", Map.of("id", "notnumeric"), - Map.of("apiKey", apiKey.getKeyToken())); + HttpResponse response = sendRequest("GET", "/api/questions", Map.of(), + Map.of("apiKey", apiKey.getKeyToken(), "id", "notnumeric")); Assertions.assertEquals(200, response.statusCode()); JSONObject json = parseJsonResponse(response); Assertions.assertTrue(json.has("questions")); - Assertions.assertTrue(json.getJSONArray("questions").length() > 0); + Assertions.assertEquals(0, json.getJSONArray("questions").length()); } @Test @@ -874,6 +874,25 @@ public void testGetQuestionById() throws IOException, InterruptedException, JSON Assertions.assertEquals(question.getStatement(), questionJson.getString("statement")); } + @Test + @Order(53) + public void testGetQuestionByStatement() throws IOException, InterruptedException, JSONException { + insertSomeQuestions();; + Player player = playerService.getUsersByRole("ROLE_USER").get(0); + ApiKey apiKey = player.getApiKey(); + Question question = questionService.getAllQuestions().get(0); + + HttpResponse response = sendRequest("GET", "/api/questions", Map.of(), + Map.of("apiKey", apiKey.getKeyToken(), + "statement", question.getStatement())); + + Assertions.assertEquals(200, response.statusCode()); + JSONObject json = parseJsonResponse(response); + JSONObject questionJson = json.getJSONArray("questions").getJSONObject(0); + Assertions.assertEquals(question.getId(), questionJson.getLong("id")); + Assertions.assertEquals(question.getStatement(), questionJson.getString("statement")); + } + @Test @Order(54) void PlayerServiceImpl_addNewPlayer_UsedEmail() { From 008d58099f2ad083dd230ef854c0b720a908cc9a Mon Sep 17 00:00:00 2001 From: Pelayori <31128562+Pelayori@users.noreply.github.com> Date: Sat, 27 Apr 2024 16:15:43 +0200 Subject: [PATCH 5/8] Do some e2e tests for multiplayer --- pom.xml | 2 +- src/test/java/com/uniovi/Wiq_UnitTests.java | 40 +++++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 3c8f1c2c..dd4c5024 100644 --- a/pom.xml +++ b/pom.xml @@ -17,7 +17,7 @@ 17 src/main/java,src/test/resources/features ${project.basedir}/target/jacoco.exec - **/controllers/CustomErrorController.java + **/controllers/CustomErrorController.java, **/**/InsertDataSampleService.java diff --git a/src/test/java/com/uniovi/Wiq_UnitTests.java b/src/test/java/com/uniovi/Wiq_UnitTests.java index 7f285984..19cd164a 100644 --- a/src/test/java/com/uniovi/Wiq_UnitTests.java +++ b/src/test/java/com/uniovi/Wiq_UnitTests.java @@ -560,6 +560,46 @@ public void testGetPlayersByEmails() throws IOException, InterruptedException, J } } + @Test + @Order(35) + public void testGetPlayersByEmailsAndRole() throws IOException, InterruptedException, JSONException { + Player player = playerService.getUsersByRole("ROLE_USER").get(0); + ApiKey apiKey = player.getApiKey(); + + HttpResponse response = sendRequest("GET", "/api/players", Map.of(), + Map.of("apiKey", apiKey.getKeyToken(), + "emails", player.getEmail(), "role", "ROLE_USER")); + + Assertions.assertEquals(200, response.statusCode()); + JSONObject json = parseJsonResponse(response); + JSONArray players = json.getJSONArray("players"); + Assertions.assertTrue(players.length() > 0); + for (int i = 0; i < players.length(); i++) { + JSONObject playerJson = players.getJSONObject(i); + Assertions.assertEquals(player.getEmail(), playerJson.getString("email")); + } + } + + @Test + @Order(35) + public void testGetPlayersByRole() throws IOException, InterruptedException, JSONException { + Player player = playerService.getUsersByRole("ROLE_USER").get(0); + ApiKey apiKey = player.getApiKey(); + + HttpResponse response = sendRequest("GET", "/api/players", Map.of(), + Map.of("apiKey", apiKey.getKeyToken(), + "role", "ROLE_USER")); + + Assertions.assertEquals(200, response.statusCode()); + JSONObject json = parseJsonResponse(response); + JSONArray players = json.getJSONArray("players"); + Assertions.assertTrue(players.length() > 0); + for (int i = 0; i < players.length(); i++) { + JSONObject playerJson = players.getJSONObject(i); + Assertions.assertEquals(player.getEmail(), playerJson.getString("email")); + } + } + @Test @Order(36) public void testCreatePlayerEmptyApiKey() throws IOException, InterruptedException, JSONException { From 47e4a789f342c2e5b04af0dc60c86d6511af2ba3 Mon Sep 17 00:00:00 2001 From: Pelayori <31128562+Pelayori@users.noreply.github.com> Date: Sat, 27 Apr 2024 18:03:27 +0200 Subject: [PATCH 6/8] More tests, and some fixes --- .../uniovi/controllers/GameController.java | 8 ++++- .../services/MultiplayerSessionService.java | 2 ++ .../services/impl/MultiplayerSessionImpl.java | 35 +++++++++++++------ .../services/impl/PlayerServiceImpl.java | 8 +++-- src/main/resources/messages.properties | 1 + src/main/resources/messages_en.properties | 2 +- src/main/resources/messages_es.properties | 2 +- src/main/resources/messages_fr.properties | 2 +- .../templates/game/multiplayerGame.html | 1 + src/test/java/com/uniovi/steps/GameStep.java | 26 +++++++++++++- .../features/MultiplayerGame.feature | 23 +++++++++++- 11 files changed, 90 insertions(+), 20 deletions(-) diff --git a/src/main/java/com/uniovi/controllers/GameController.java b/src/main/java/com/uniovi/controllers/GameController.java index 3762ac5d..1baa3664 100644 --- a/src/main/java/com/uniovi/controllers/GameController.java +++ b/src/main/java/com/uniovi/controllers/GameController.java @@ -74,10 +74,15 @@ public String getMultiplayerGame() { @GetMapping("/multiplayerGame/{code}") public String joinMultiplayerGame(@PathVariable String code, HttpSession session, Principal principal, Model model) { + if (!multiplayerSessionService.existsCode(code)) { + model.addAttribute("errorKey", "multi.code.invalid"); + return "game/multiplayerGame"; + } + Optional player = playerService.getUserByUsername(principal.getName()); Player p = player.orElse(null); isMultiPlayer=true; - if(playerService.changeMultiplayerCode(p.getId(),code)){ + if (playerService.changeMultiplayerCode(p.getId(),code)) { multiplayerSessionService.addToLobby(code,p.getId()); model.addAttribute("multiplayerGameCode",code); session.setAttribute("multiplayerCode",code); @@ -153,6 +158,7 @@ public List updatePlayerList(@PathVariable String code) { Collections.sort(playerNames); return playerNames; } + @GetMapping("/game/lobby") public String createLobby( HttpSession session, Model model) { int code = Integer.parseInt((String)session.getAttribute("multiplayerCode")); diff --git a/src/main/java/com/uniovi/services/MultiplayerSessionService.java b/src/main/java/com/uniovi/services/MultiplayerSessionService.java index c0932339..7b917458 100644 --- a/src/main/java/com/uniovi/services/MultiplayerSessionService.java +++ b/src/main/java/com/uniovi/services/MultiplayerSessionService.java @@ -19,4 +19,6 @@ public interface MultiplayerSessionService { void addToLobby(String code, Long id); void changeScore(String code,Long id,int score); + + boolean existsCode(String code); } diff --git a/src/main/java/com/uniovi/services/impl/MultiplayerSessionImpl.java b/src/main/java/com/uniovi/services/impl/MultiplayerSessionImpl.java index 0c10044e..4570b8ad 100644 --- a/src/main/java/com/uniovi/services/impl/MultiplayerSessionImpl.java +++ b/src/main/java/com/uniovi/services/impl/MultiplayerSessionImpl.java @@ -34,7 +34,7 @@ public Map getPlayersWithScores(int multiplayerCode) { List sortedPlayers = playerScores.entrySet().stream() .sorted(Map.Entry.comparingByValue(Comparator.reverseOrder())) .map(Map.Entry::getKey) - .collect(Collectors.toList()); + .toList(); Map playersSorted = new HashMap<>(); for (Player player : sortedPlayers) { @@ -45,25 +45,38 @@ public Map getPlayersWithScores(int multiplayerCode) { @Override public void multiCreate(String code, Long id) { - Player p = playerRepository.findById(id).get(); - multiplayerSessionRepository.save(new MultiplayerSession(code,p)); + Player p = playerRepository.findById(id).orElse(null); + + if (p != null) + multiplayerSessionRepository.save(new MultiplayerSession(code,p)); } @Override @Transactional public void addToLobby(String code, Long id) { - Player p = playerRepository.findById(id).get(); - MultiplayerSession ms=multiplayerSessionRepository.findByMultiplayerCode(code); - ms.addPlayer(p); - multiplayerSessionRepository.save(ms); + Player p = playerRepository.findById(id).orElse(null); + + if (p != null) { + MultiplayerSession ms = multiplayerSessionRepository.findByMultiplayerCode(code); + ms.addPlayer(p); + multiplayerSessionRepository.save(ms); + } } @Override @Transactional public void changeScore(String code, Long id, int score) { - Player p = playerRepository.findById(id).get(); - MultiplayerSession ms=multiplayerSessionRepository.findByMultiplayerCode(code); - ms.getPlayerScores().put(p,score); - multiplayerSessionRepository.save(ms); + Player p = playerRepository.findById(id).orElse(null); + + if (p != null) { + MultiplayerSession ms = multiplayerSessionRepository.findByMultiplayerCode(code); + ms.getPlayerScores().put(p, score); + multiplayerSessionRepository.save(ms); + } + } + + @Override + public boolean existsCode(String code) { + return multiplayerSessionRepository.findByMultiplayerCode(code) != null; } } diff --git a/src/main/java/com/uniovi/services/impl/PlayerServiceImpl.java b/src/main/java/com/uniovi/services/impl/PlayerServiceImpl.java index f8cde7ee..c3c7e566 100644 --- a/src/main/java/com/uniovi/services/impl/PlayerServiceImpl.java +++ b/src/main/java/com/uniovi/services/impl/PlayerServiceImpl.java @@ -15,17 +15,19 @@ import org.springframework.stereotype.Service; import com.uniovi.entities.Role; +import java.security.SecureRandom; import java.util.ArrayList; import java.util.List; import java.util.Optional; +import java.util.Random; @Service public class PlayerServiceImpl implements PlayerService { private final PlayerRepository playerRepository; private final RoleService roleService; private final PasswordEncoder passwordEncoder; - - private MultiplayerSessionService multiplayerSessionService; + private final MultiplayerSessionService multiplayerSessionService; + private final Random random = new SecureRandom(); public PlayerServiceImpl(PlayerRepository playerRepository, RoleService roleService, MultiplayerSessionService multiplayerSessionService,PasswordEncoder passwordEncoder) { this.playerRepository = playerRepository; @@ -190,7 +192,7 @@ public int createMultiplayerGame(Long id){ return -1; Player p = player.get(); - int code = (int)Math.floor(Math.random()*10000); + int code = random.nextInt(10000); p.setMultiplayerCode(code); playerRepository.save(p); return code; diff --git a/src/main/resources/messages.properties b/src/main/resources/messages.properties index ea14131d..d8d8fa7f 100644 --- a/src/main/resources/messages.properties +++ b/src/main/resources/messages.properties @@ -108,6 +108,7 @@ multi.results =Ver resultados multi.finished= Partida finalizada multi.points = Puntuaciones multi.menu = Ir a la página de inicio +multi.code.invalid = Código de partida inválido # -------------------Statements for the apiHome.html file--------------------- api.doc.title=Documentación de la API api.doc.description=Esta es la documentación de la API de WIQ. Aquí puedes encontrar información sobre los recursos disponibles, los parámetros que aceptan y los ejemplos de uso. diff --git a/src/main/resources/messages_en.properties b/src/main/resources/messages_en.properties index 479c891f..be8a7d16 100644 --- a/src/main/resources/messages_en.properties +++ b/src/main/resources/messages_en.properties @@ -110,7 +110,7 @@ multi.results = See results multi.finished= Finished game multi.points = Points multi.menu =Go to home page - +multi.code.invalid = Invalid game code # -------------------Statements for the apiHome.html file--------------------- api.doc.title=API Documentation diff --git a/src/main/resources/messages_es.properties b/src/main/resources/messages_es.properties index 3a860636..08bd8b34 100644 --- a/src/main/resources/messages_es.properties +++ b/src/main/resources/messages_es.properties @@ -110,7 +110,7 @@ multi.results =Ver resultados multi.finished= Partida finalizada multi.points = Puntuaciones multi.menu = Ir a la página de inicio - +multi.code.invalid = Código de partida inválido # -------------------Statements for the apiHome.html file--------------------- api.doc.title=Documentación de la API diff --git a/src/main/resources/messages_fr.properties b/src/main/resources/messages_fr.properties index 96a377f1..ff7f4cee 100644 --- a/src/main/resources/messages_fr.properties +++ b/src/main/resources/messages_fr.properties @@ -103,7 +103,7 @@ multi.results=Voir les résultats multi.finished= Jeu terminé multi.points = Points multi.menu =aller à la page d'accueil - +multi.code.invalid = Code invalide # -------------------Statements for the apiHome.html file--------------------- api.doc.title=Documentation de l'API diff --git a/src/main/resources/templates/game/multiplayerGame.html b/src/main/resources/templates/game/multiplayerGame.html index 6b26fe71..a25a8d44 100644 --- a/src/main/resources/templates/game/multiplayerGame.html +++ b/src/main/resources/templates/game/multiplayerGame.html @@ -7,6 +7,7 @@
+
elems = SeleniumUtils.waitLoadElementsBy(driver, "id", "startBtn", 5); elems.get(0).click(); } + + @And("I save the code") + public void iSaveACode() { + List elems = SeleniumUtils.waitLoadElementsBy(driver, "id", "lobbyCode", 5); + Assertions.assertEquals(1, elems.size()); + code = elems.get(0).getText(); + } + + @And("I fill in the saved code") + public void iFillInTheSavedCode() { + List elems = SeleniumUtils.waitLoadElements(driver, By.xpath("//input[contains(@id,'code')]"), 5); + elems.get(0).sendKeys(code); + elems = SeleniumUtils.waitLoadElementsBy(driver, "id", "joinBtn", 5); + elems.get(0).click(); + } + + @And("I see the multiplayer results") + public void iSeeTheMultiplayerResults() { + List elems = SeleniumUtils.waitLoadElementsBy(driver, "id", "createBtn", 5); + Assertions.assertEquals(1, elems.size()); + elems.get(0).click(); + } } diff --git a/src/test/resources/features/MultiplayerGame.feature b/src/test/resources/features/MultiplayerGame.feature index 0ec53959..c7e93ef1 100644 --- a/src/test/resources/features/MultiplayerGame.feature +++ b/src/test/resources/features/MultiplayerGame.feature @@ -8,4 +8,25 @@ Feature: I play a multiplayer game When I press Play With Friends And I create a code And I press start - Then I should start playing until I see message "multi.finished" \ No newline at end of file + Then I should start playing until I see message "multi.finished" + + Scenario: I start a multiplayer game + Given I am not registered or logged in + And I am on the register page + When I fill in the form with valid data username: "userGame" email: "userGame@gmail.com" password: "password" password_confirmation: "password" + And I press the register button + And I go to the home page + When I press Play With Friends + And I create a code + And I save the code + Then I logout + Given I am not registered or logged in + And I am on the register page + When I fill in the form with valid data username: "userGame2" email: "userGame2@gmail.com" password: "password" password_confirmation: "password" + And I press the register button + And I go to the home page + And I press Play With Friends + And I fill in the saved code + And I press start + Then I should start playing until I see message "multi.finished" + And I see the multiplayer results \ No newline at end of file From 6ee27e12bdf42931add3b2de5197b01b89bb3b6f Mon Sep 17 00:00:00 2001 From: Pelayori <31128562+Pelayori@users.noreply.github.com> Date: Sat, 27 Apr 2024 18:16:49 +0200 Subject: [PATCH 7/8] More tests, and some fixes --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index dd4c5024..52a0051b 100644 --- a/pom.xml +++ b/pom.xml @@ -17,7 +17,7 @@ 17 src/main/java,src/test/resources/features ${project.basedir}/target/jacoco.exec - **/controllers/CustomErrorController.java, **/**/InsertDataSampleService.java + **/controllers/CustomErrorController.java, **/**/InsertSampleDataService.java From 6802453bbc20d9d748adbf7b5bdf44d2cb9136e6 Mon Sep 17 00:00:00 2001 From: Pelayori <31128562+Pelayori@users.noreply.github.com> Date: Sat, 27 Apr 2024 18:57:46 +0200 Subject: [PATCH 8/8] More tests, and some fixes --- .../java/com/uniovi/controllers/GameController.java | 11 ----------- src/test/java/com/uniovi/steps/GameStep.java | 8 ++++++++ src/test/resources/features/Game.feature | 1 + src/test/resources/features/MultiplayerGame.feature | 2 ++ 4 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/uniovi/controllers/GameController.java b/src/main/java/com/uniovi/controllers/GameController.java index 1baa3664..c147ea6e 100644 --- a/src/main/java/com/uniovi/controllers/GameController.java +++ b/src/main/java/com/uniovi/controllers/GameController.java @@ -253,17 +253,6 @@ public String updateGame(Model model, HttpSession session, Principal principal) return "game/fragments/gameFrame"; } - @GetMapping("/game/finished/{points}") - public String finishGame(@PathVariable int points, Model model, HttpSession session) { - GameSession gameSession = (GameSession) session.getAttribute(GAMESESSION_STR); - if (gameSession != null) { - gameSessionService.endGame(gameSession); - session.removeAttribute(GAMESESSION_STR); - } - model.addAttribute("score", points); - return "game/gameFinished"; - } - @GetMapping("/game/points") @ResponseBody public String getPoints(HttpSession session) { diff --git a/src/test/java/com/uniovi/steps/GameStep.java b/src/test/java/com/uniovi/steps/GameStep.java index 59fdb022..166722c8 100644 --- a/src/test/java/com/uniovi/steps/GameStep.java +++ b/src/test/java/com/uniovi/steps/GameStep.java @@ -73,6 +73,7 @@ public void iPressPlayWithFriends() throws IOException, InterruptedException { public void iCreateACode() { List elems = SeleniumUtils.waitLoadElementsBy(driver, "id", "createBtn", 5); elems.get(0).click(); + SeleniumUtils.waitSeconds(driver, 5); } @And("I press start") @@ -94,6 +95,7 @@ public void iFillInTheSavedCode() { elems.get(0).sendKeys(code); elems = SeleniumUtils.waitLoadElementsBy(driver, "id", "joinBtn", 5); elems.get(0).click(); + SeleniumUtils.waitSeconds(driver, 5); } @And("I see the multiplayer results") @@ -101,5 +103,11 @@ public void iSeeTheMultiplayerResults() { List elems = SeleniumUtils.waitLoadElementsBy(driver, "id", "createBtn", 5); Assertions.assertEquals(1, elems.size()); elems.get(0).click(); + SeleniumUtils.waitSeconds(driver, 5); + } + + @And("I reload the page") + public void iReloadThePage() { + driver.navigate().refresh(); } } diff --git a/src/test/resources/features/Game.feature b/src/test/resources/features/Game.feature index 5b6b0837..7b56cabf 100644 --- a/src/test/resources/features/Game.feature +++ b/src/test/resources/features/Game.feature @@ -6,4 +6,5 @@ Feature: I try to play a normal game And I press the register button And I go to the home page When I press Play + And I reload the page Then I should start playing until I see message "game.finish" \ No newline at end of file diff --git a/src/test/resources/features/MultiplayerGame.feature b/src/test/resources/features/MultiplayerGame.feature index c7e93ef1..714268d1 100644 --- a/src/test/resources/features/MultiplayerGame.feature +++ b/src/test/resources/features/MultiplayerGame.feature @@ -9,6 +9,7 @@ Feature: I play a multiplayer game And I create a code And I press start Then I should start playing until I see message "multi.finished" + And I see the multiplayer results Scenario: I start a multiplayer game Given I am not registered or logged in @@ -28,5 +29,6 @@ Feature: I play a multiplayer game And I press Play With Friends And I fill in the saved code And I press start + And I reload the page Then I should start playing until I see message "multi.finished" And I see the multiplayer results \ No newline at end of file