From 1b9b65f702831be84203ce421d60b5761a31acf5 Mon Sep 17 00:00:00 2001 From: AlvaroGlezC <uo251891@uniovi.es> Date: Fri, 26 Apr 2024 20:30:57 +0200 Subject: [PATCH] #11 #12 #13 Tests de todas las capas and tests e2e --- syg-backend/SYG-bootstrap/pom.xml | 32 +++- .../java/syg/bootstrap/SYGdbContainer.java | 46 +++++ .../syg/bootstrap/configuration/E2ETests.java | 20 +++ .../java/syg/bootstrap/e2e/CategoryTests.java | 61 +++++++ .../syg/bootstrap/e2e/QuestionsTests.java | 82 +++++++++ .../java/syg/bootstrap/e2e/UserTests.java | 166 ++++++++++++++++++ .../resources/db/initial-data-bootstrap.sql | 83 +++++++++ .../syg/bootstrap/BootstrapApplication.class | Bin 1017 -> 1017 bytes syg-backend/SYG-domain/pom.xml | 5 +- .../test/java/enums/CategoryEnumTests.java | 41 +++++ .../exception/ConflictExceptionTests.java | 35 ++++ .../exception/NotFoundExceptionTests.java | 35 ++++ .../{unit => }/CategoryServiceTests.java | 2 +- .../{unit => }/QuestionServiceTests.java | 24 ++- .../services/{unit => }/UserServiceTests.java | 32 +++- syg-backend/SYG-mysql-adapter/pom.xml | 12 +- .../integration/QuestionAdapterIT.java | 29 +++ .../adapter/unit/QuestionAdapterTests.java | 19 +- .../src/test/resources/application.properties | 0 .../syg/controller/CategoryController.java | 2 +- .../syg/controller/QuestionsController.java | 2 +- .../java/syg/controller/UserController.java | 21 +-- .../controller/CategoryControllerTests.java | 9 + .../controller/QuestionControllerTests.java | 22 +++ .../java/controller/UserControllerTests.java | 86 ++++++++- syg-backend/lombok.config | 2 + syg-backend/pom.xml | 42 +++-- 27 files changed, 854 insertions(+), 56 deletions(-) create mode 100644 syg-backend/SYG-bootstrap/src/test/java/syg/bootstrap/SYGdbContainer.java create mode 100644 syg-backend/SYG-bootstrap/src/test/java/syg/bootstrap/configuration/E2ETests.java create mode 100644 syg-backend/SYG-bootstrap/src/test/java/syg/bootstrap/e2e/CategoryTests.java create mode 100644 syg-backend/SYG-bootstrap/src/test/java/syg/bootstrap/e2e/QuestionsTests.java create mode 100644 syg-backend/SYG-bootstrap/src/test/java/syg/bootstrap/e2e/UserTests.java create mode 100644 syg-backend/SYG-bootstrap/src/test/resources/db/initial-data-bootstrap.sql create mode 100644 syg-backend/SYG-domain/src/test/java/enums/CategoryEnumTests.java create mode 100644 syg-backend/SYG-domain/src/test/java/exception/ConflictExceptionTests.java create mode 100644 syg-backend/SYG-domain/src/test/java/exception/NotFoundExceptionTests.java rename syg-backend/SYG-domain/src/test/java/services/{unit => }/CategoryServiceTests.java (98%) rename syg-backend/SYG-domain/src/test/java/services/{unit => }/QuestionServiceTests.java (86%) rename syg-backend/SYG-domain/src/test/java/services/{unit => }/UserServiceTests.java (85%) delete mode 100644 syg-backend/SYG-mysql-adapter/src/test/resources/application.properties create mode 100644 syg-backend/lombok.config diff --git a/syg-backend/SYG-bootstrap/pom.xml b/syg-backend/SYG-bootstrap/pom.xml index 5d263b9..11b699f 100644 --- a/syg-backend/SYG-bootstrap/pom.xml +++ b/syg-backend/SYG-bootstrap/pom.xml @@ -18,7 +18,6 @@ <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> - <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> @@ -37,6 +36,37 @@ <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> + + <!-- TEST DEPENDENCIES --> + <dependency> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-starter-test</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>com.h2database</groupId> + <artifactId>h2</artifactId> + </dependency> + <dependency> + <groupId>org.testcontainers</groupId> + <artifactId>testcontainers</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.testcontainers</groupId> + <artifactId>mysql</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.testcontainers</groupId> + <artifactId>junit-jupiter</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.springframework.security</groupId> + <artifactId>spring-security-test</artifactId> + <scope>test</scope> + </dependency> <!-- SYG MODULES --> <dependency> diff --git a/syg-backend/SYG-bootstrap/src/test/java/syg/bootstrap/SYGdbContainer.java b/syg-backend/SYG-bootstrap/src/test/java/syg/bootstrap/SYGdbContainer.java new file mode 100644 index 0000000..406c649 --- /dev/null +++ b/syg-backend/SYG-bootstrap/src/test/java/syg/bootstrap/SYGdbContainer.java @@ -0,0 +1,46 @@ +package syg.bootstrap; + +import org.springframework.test.context.DynamicPropertyRegistry; +import org.springframework.test.context.DynamicPropertySource; +import org.testcontainers.containers.MySQLContainer; +import org.testcontainers.junit.jupiter.Testcontainers; +import org.testcontainers.utility.DockerImageName; + +/** + * Test que arranca un contenedor con una base de datos mySQL. + * + * Dado que los tests se ejecutan en un contexto transaccional no se afectan + * unos a otros. + * + * + * Los datos iniciales se cargan de una imagen. + * + */ +@Testcontainers +public abstract class SYGdbContainer { + + static final MySQLContainer<?> sygdbContainer; + + static { + sygdbContainer = new MySQLContainer<>( + DockerImageName.parse("mysql:8.0.36").asCompatibleSubstituteFor("mysql")) + .withDatabaseName("syg-db") + .withUsername("sygAdmin") + .withPassword("sygAdmin") + .withInitScript("db/initial-data-bootstrap.sql"); + sygdbContainer.start(); + } + + /** + * Asignamos las propiedades del DataSource de Spring utilizando las del + * contenedor + */ + @DynamicPropertySource + static void mysqlProperties(DynamicPropertyRegistry registry) { + registry.add("spring.datasource.url", sygdbContainer::getJdbcUrl); + registry.add("spring.datasource.password", sygdbContainer::getPassword); + registry.add("spring.datasource.username", sygdbContainer::getUsername); + registry.add("spring.datasource.driver-class-name", sygdbContainer::getDriverClassName); + } + +} diff --git a/syg-backend/SYG-bootstrap/src/test/java/syg/bootstrap/configuration/E2ETests.java b/syg-backend/SYG-bootstrap/src/test/java/syg/bootstrap/configuration/E2ETests.java new file mode 100644 index 0000000..5804053 --- /dev/null +++ b/syg-backend/SYG-bootstrap/src/test/java/syg/bootstrap/configuration/E2ETests.java @@ -0,0 +1,20 @@ +package syg.bootstrap.configuration; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import org.junit.jupiter.api.MethodOrderer; +import org.junit.jupiter.api.TestMethodOrder; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +@AutoConfigureMockMvc +@TestMethodOrder(value = MethodOrderer.OrderAnnotation.class) +@Target(ElementType.TYPE) +@Retention(RetentionPolicy.RUNTIME) +public @interface E2ETests { + +} diff --git a/syg-backend/SYG-bootstrap/src/test/java/syg/bootstrap/e2e/CategoryTests.java b/syg-backend/SYG-bootstrap/src/test/java/syg/bootstrap/e2e/CategoryTests.java new file mode 100644 index 0000000..2dbe28d --- /dev/null +++ b/syg-backend/SYG-bootstrap/src/test/java/syg/bootstrap/e2e/CategoryTests.java @@ -0,0 +1,61 @@ +package syg.bootstrap.e2e; + +import static org.hamcrest.CoreMatchers.is; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Order; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.http.HttpStatus; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.result.MockMvcResultHandlers; +import org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors; + + +import syg.bootstrap.SYGdbContainer; +import syg.bootstrap.configuration.BootstrapRunner; +import syg.bootstrap.configuration.E2ETests; + +@E2ETests +public class CategoryTests extends SYGdbContainer { + + @MockBean + private BootstrapRunner bootstrapRunner; + + @Autowired + private MockMvc mockMvc; + + @Test + @DisplayName("Se buscan todas las categorias en base de datos") + @Order(1) + void find_all_categories() throws Exception{ + mockMvc.perform(get("/category") + .with(SecurityMockMvcRequestPostProcessors.jwt())) + .andDo(MockMvcResultHandlers.print()).andExpect(status().is(HttpStatus.OK.value())) + .andExpect(jsonPath("$.length()", is(5))); + } + + @Test + @DisplayName("Se busca una pregunta a traves de un id en base de datos") + @Order(2) + void find_question_by_id() throws Exception{ + mockMvc.perform(get("/category/id").param("id", "1") + .with(SecurityMockMvcRequestPostProcessors.jwt())) + .andDo(MockMvcResultHandlers.print()).andExpect(status().is(HttpStatus.OK.value())) + .andExpect(jsonPath("$.id", is(1))) + .andExpect(jsonPath("$.name", is("animales"))); + } + + @Test + @DisplayName("Se busca una categoria a traves de un id que no existe") + @Order(3) + void find_category_by_not_exist_id() throws Exception{ + mockMvc.perform(get("/category/id").param("id", "100") + .with(SecurityMockMvcRequestPostProcessors.jwt())) + .andDo(MockMvcResultHandlers.print()).andExpect(status().is(HttpStatus.NOT_FOUND.value())); + } +} diff --git a/syg-backend/SYG-bootstrap/src/test/java/syg/bootstrap/e2e/QuestionsTests.java b/syg-backend/SYG-bootstrap/src/test/java/syg/bootstrap/e2e/QuestionsTests.java new file mode 100644 index 0000000..839a6f2 --- /dev/null +++ b/syg-backend/SYG-bootstrap/src/test/java/syg/bootstrap/e2e/QuestionsTests.java @@ -0,0 +1,82 @@ +package syg.bootstrap.e2e; + +import static org.hamcrest.CoreMatchers.is; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Order; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.http.HttpStatus; +import org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.result.MockMvcResultHandlers; + +import syg.bootstrap.SYGdbContainer; +import syg.bootstrap.configuration.BootstrapRunner; +import syg.bootstrap.configuration.E2ETests; + +@E2ETests +public class QuestionsTests extends SYGdbContainer { + + //Evita que el runner se lance en cada testContainer + @MockBean + private BootstrapRunner bootstrapRunner; + + @Autowired + private MockMvc mockMvc; + + @Test + @DisplayName("Se buscan todas las preguntas en base de datos") + @Order(1) + void find_all_questions() throws Exception { + mockMvc.perform(get("/question") + .with(SecurityMockMvcRequestPostProcessors.jwt())) + .andDo(MockMvcResultHandlers.print()) + .andExpect(status().is(HttpStatus.OK.value())) + .andExpect(jsonPath("$.length()", is(8))); + } + + @Test + @DisplayName("Se busca una pregunta a traves de un id en base de datos") + @Order(2) + void find_question_by_id() throws Exception { + mockMvc.perform(get("/question/id").param("id", "1") + .with(SecurityMockMvcRequestPostProcessors.jwt())) + .andDo(MockMvcResultHandlers.print()).andExpect(status().is(HttpStatus.OK.value())) + .andExpect(jsonPath("$.id", is(1))) + .andExpect(jsonPath("$.text", is("¿Cual es el animal que no puede saltar?"))); + } + + @Test + @DisplayName("Se busca una pregunta a traves de un id que no existe") + @Order(3) + void find_question_by_not_exist_id() throws Exception { + mockMvc.perform(get("/question/id").param("id", "10000") + .with(SecurityMockMvcRequestPostProcessors.jwt())) + .andDo(MockMvcResultHandlers.print()).andExpect(status().is(HttpStatus.NOT_FOUND.value())); + } + + @Test + @DisplayName("Se buscan preguntas a traves de una categoria") + @Order(4) + void find_questions_by_category() throws Exception{ + mockMvc.perform(get("/question/category").param("categoryId", "1") + .with(SecurityMockMvcRequestPostProcessors.jwt())) + .andDo(MockMvcResultHandlers.print()).andExpect(status().is(HttpStatus.OK.value())) + .andExpect(jsonPath("$.length()", is(3))); + } + + @Test + @DisplayName("Se buscan preguntas a traves de una categoria que no existe") + @Order(5) + void find_question_by_not_exist_category() throws Exception{ + mockMvc.perform(get("/question/category").param("categoryId", "100") + .with(SecurityMockMvcRequestPostProcessors.jwt())) + .andDo(MockMvcResultHandlers.print()).andExpect(status().is(HttpStatus.OK.value())) + .andExpect(jsonPath("$.length()", is(0))); + } +} diff --git a/syg-backend/SYG-bootstrap/src/test/java/syg/bootstrap/e2e/UserTests.java b/syg-backend/SYG-bootstrap/src/test/java/syg/bootstrap/e2e/UserTests.java new file mode 100644 index 0000000..dbec66b --- /dev/null +++ b/syg-backend/SYG-bootstrap/src/test/java/syg/bootstrap/e2e/UserTests.java @@ -0,0 +1,166 @@ +package syg.bootstrap.e2e; + +import static org.hamcrest.CoreMatchers.is; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Order; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; +import org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.result.MockMvcResultHandlers; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; + +import syg.bootstrap.SYGdbContainer; +import syg.bootstrap.configuration.BootstrapRunner; +import syg.bootstrap.configuration.E2ETests; +import syg.domain.model.User; + +@E2ETests +public class UserTests extends SYGdbContainer { + + @MockBean + private BootstrapRunner bootstrapRunner; + + @Autowired + private MockMvc mockMvc; + + @Test + @DisplayName("Se buscan todos los usuarios") + @Order(1) + void find_all_users() throws Exception{ + mockMvc.perform(get("/user") + .with(SecurityMockMvcRequestPostProcessors.jwt())) + .andDo(MockMvcResultHandlers.print()).andExpect(status().is(HttpStatus.OK.value())) + .andExpect(jsonPath("$.length()", is(2))); + } + + @Test + @DisplayName("Se busca un usuario a traves de un id en base de datos") + @Order(2) + void find_user_by_id() throws Exception{ + mockMvc.perform(get("/user/userId").param("id", "5366fdc8-b32d-46bc-9df8-2e8ce68f0743") + .with(SecurityMockMvcRequestPostProcessors.jwt())) + .andDo(MockMvcResultHandlers.print()).andExpect(status().is(HttpStatus.OK.value())) + .andExpect(jsonPath("$.id", is("5366fdc8-b32d-46bc-9df8-2e8ce68f0743"))) + .andExpect(jsonPath("$.name", is("Pablo"))); + } + + @Test + @DisplayName("Se busca un usuarios a traves de un id que no existe") + @Order(3) + void find_user_by_not_exist_id() throws Exception{ + mockMvc.perform(get("/user/userId").param("id", "9566fdc8-b32d-46bc-9df8-2e8ce68f0743") + .with(SecurityMockMvcRequestPostProcessors.jwt())) + .andDo(MockMvcResultHandlers.print()).andExpect(status().is(HttpStatus.NOT_FOUND.value())); + } + + @Test + @DisplayName("Se busca un usuario a traves de su nombre") + @Order(4) + void find_user_by_name() throws Exception{ + mockMvc.perform(get("/user/name").param("userName", "Pablo") + .with(SecurityMockMvcRequestPostProcessors.jwt())) + .andDo(MockMvcResultHandlers.print()).andExpect(status().is(HttpStatus.OK.value())) + .andExpect(jsonPath("$.id", is("5366fdc8-b32d-46bc-9df8-2e8ce68f0743"))); + } + + @Test + @DisplayName("Se busca un usuario a traves de nombre que no existe") + @Order(5) + void find_user_by_not_exist_name() throws Exception{ + mockMvc.perform(get("/user/name").param("userName", "NotExistPlayer") + .with(SecurityMockMvcRequestPostProcessors.jwt())) + .andDo(MockMvcResultHandlers.print()).andExpect(status().is(HttpStatus.NOT_FOUND.value())); + } + + @Test + @DisplayName("Se crea un usuario") + @Order(6) + void create_user() throws Exception{ + User userToCreate = new User("9366fdc8-b32d-46bc-9df8-2e8ce68f0743", "Jugador 3", 1, 3, 1, 100, "Deportes"); + + mockMvc.perform(post("/user").contentType(MediaType.APPLICATION_JSON) + .content(asJsonString(userToCreate)) + .with(SecurityMockMvcRequestPostProcessors.jwt())) + .andDo(MockMvcResultHandlers.print()).andExpect(status().is(HttpStatus.OK.value())) + .andExpect(jsonPath("$.id", is("9366fdc8-b32d-46bc-9df8-2e8ce68f0743"))) + .andExpect(jsonPath("$.name", is("Jugador 3"))); + } + + @Test + @DisplayName("Se crea un usuario con un id ya existente") + @Order(7) + void create_user_with_existent_id() throws Exception{ + User userToCreate = new User("9366fdc8-b32d-46bc-9df8-2e8ce68f0743", "Jugador 4", 1, 3, 1, 100, "Deportes"); + + mockMvc.perform(post("/user").contentType(MediaType.APPLICATION_JSON) + .content(asJsonString(userToCreate)) + .with(SecurityMockMvcRequestPostProcessors.jwt())) + .andDo(MockMvcResultHandlers.print()).andExpect(status().is(HttpStatus.CONFLICT.value())); + } + + @Test + @DisplayName("Se crea un usuario con un nombre ya existente") + @Order(8) + void create_user_with_existent_name() throws Exception{ + User userToCreate = new User("7366fdc8-b32d-46bc-9df8-2e8ce68f0743", "Pablo", 1, 3, 1, 100, "Deportes"); + + mockMvc.perform(post("/user").contentType(MediaType.APPLICATION_JSON) + .content(asJsonString(userToCreate)) + .with(SecurityMockMvcRequestPostProcessors.jwt())) + .andDo(MockMvcResultHandlers.print()).andExpect(status().is(HttpStatus.CONFLICT.value())); + } + + @Test + @DisplayName("Se actualiza un usuario") + @Order(9) + void update_user_() throws Exception{ + User userToUpdate = new User("4366fdc8-b32d-46bc-9df8-2e8ce68f0743", "Alvaro", 9, 40, 1, 105, "Deportes"); + + mockMvc.perform(put("/user").contentType(MediaType.APPLICATION_JSON) + .content(asJsonString(userToUpdate)) + .with(SecurityMockMvcRequestPostProcessors.jwt())) + .andDo(MockMvcResultHandlers.print()).andExpect(status().is(HttpStatus.OK.value())) + .andExpect(jsonPath("$.id", is("4366fdc8-b32d-46bc-9df8-2e8ce68f0743"))) + .andExpect(jsonPath("$.totalGames", is(9))); + } + + @Test + @DisplayName("Se actualiza un usuario con un id que no existe") + @Order(10) + void update_user_with_not_existent_id() throws Exception{ + User userToUpdate = new User("9166fdc8-b32d-46bc-9df8-2e8ce68f0743", "Alvaro", 1, 3, 1, 100, "Deportes"); + + mockMvc.perform(put("/user").contentType(MediaType.APPLICATION_JSON) + .content(asJsonString(userToUpdate)) + .with(SecurityMockMvcRequestPostProcessors.jwt())) + .andDo(MockMvcResultHandlers.print()).andExpect(status().is(HttpStatus.NOT_FOUND.value())); + } + + @Test + @DisplayName("Se actualiza un usuario con un id nulo") + @Order(11) + void update_user_with_null_id() throws Exception{ + User userToUpdate = new User(null, "Alvaro", 1, 3, 1, 100, "Deportes"); + + mockMvc.perform(put("/user").contentType(MediaType.APPLICATION_JSON) + .content(asJsonString(userToUpdate)) + .with(SecurityMockMvcRequestPostProcessors.jwt())) + .andDo(MockMvcResultHandlers.print()).andExpect(status().is(HttpStatus.NOT_FOUND.value())); + } + + private String asJsonString(Object obj) throws JsonProcessingException { + return new ObjectMapper().writeValueAsString(obj); +} +} diff --git a/syg-backend/SYG-bootstrap/src/test/resources/db/initial-data-bootstrap.sql b/syg-backend/SYG-bootstrap/src/test/resources/db/initial-data-bootstrap.sql new file mode 100644 index 0000000..4c9bd09 --- /dev/null +++ b/syg-backend/SYG-bootstrap/src/test/resources/db/initial-data-bootstrap.sql @@ -0,0 +1,83 @@ +CREATE TABLE categories( + id BIGINT AUTO_INCREMENT PRIMARY KEY, + name varchar(255) NOT NULL +); + +CREATE TABLE questions( + id BIGINT AUTO_INCREMENT PRIMARY KEY, + text varchar(255) NOT NULL, + time_limit NUMERIC, + category BIGINT, + FOREIGN KEY (category) REFERENCES categories(id) +); + +CREATE TABLE answers( + id BIGINT AUTO_INCREMENT PRIMARY KEY, + text varchar(255) NOT NULL, + is_correct boolean DEFAULT false, + question BIGINT, + FOREIGN KEY (question) REFERENCES questions(id) +); + +CREATE TABLE users( + id varchar(36) PRIMARY KEY, + name varchar(255) NOT NULL, + total_games NUMERIC NOT NULL default 0, + correct_answers NUMERIC NOT NULL default 0, + incorrect_answers NUMERIC NOT NULL default 0, + total_questions_answered NUMERIC NOT NULL default 0, + last_category_played varchar(255) NOT NULL, + UNIQUE KEY unique_name_password (name) +); + + +INSERT INTO users (ID, NAME, TOTAL_GAMES, CORRECT_ANSWERS, INCORRECT_ANSWERS, TOTAL_QUESTIONS_ANSWERED, last_category_played) VALUES ('4366fdc8-b32d-46bc-9df8-2e8ce68f0743', 'Alvaro', 4, 33, 1, 100, 'Deportes'); +INSERT INTO users (ID, NAME, TOTAL_GAMES, CORRECT_ANSWERS, INCORRECT_ANSWERS, TOTAL_QUESTIONS_ANSWERED, last_category_played) VALUES ('5366fdc8-b32d-46bc-9df8-2e8ce68f0743', 'Pablo', 8, 24, 12, 100, 'Deportes'); + +INSERT INTO categories (ID, NAME) VALUES (1, 'animales'); +INSERT INTO categories (ID, NAME) VALUES (2, 'politica'); +INSERT INTO categories (ID, NAME) VALUES (3, 'planta'); +INSERT INTO categories (ID, NAME) VALUES (4, 'deportes'); +INSERT INTO categories (ID, NAME) VALUES (5, 'paises'); + +INSERT INTO questions (ID, TEXT, TIME_LIMIT, CATEGORY) VALUES (1, '¿Cual es el animal que no puede saltar?', 60, 1); +INSERT INTO questions (ID, TEXT, TIME_LIMIT, CATEGORY) VALUES (2, '¿Cual es la planta mas alta del mundo?', 60, 3); +INSERT INTO questions (ID, TEXT, TIME_LIMIT, CATEGORY) VALUES (3, '¿Que animal es un primate?', 60, 1); +INSERT INTO questions (ID, TEXT, TIME_LIMIT, CATEGORY) VALUES (4, '¿En que continente se encuenta España?', 60, 5); +INSERT INTO questions (ID, TEXT, TIME_LIMIT, CATEGORY) VALUES (5, '¿Que pais fue el ultimo campeon del mundo?', 60, 4); +INSERT INTO questions (ID, TEXT, TIME_LIMIT, CATEGORY) VALUES (6, '¿En que pais existe la monarquia?', 60, 5); +INSERT INTO questions (ID, TEXT, TIME_LIMIT, CATEGORY) VALUES (7, '¿Cual es el animal mas rápido?', 60, 1); +INSERT INTO questions (ID, TEXT, TIME_LIMIT, CATEGORY) VALUES (8, '¿Quien es el mayor ganador de Wimbeldon de la historia?', 60, 4); + +INSERT INTO answers (ID, TEXT, IS_CORRECT, QUESTION) VALUES (1, 'cocodrilo', 0, 1); +INSERT INTO answers (ID, TEXT, IS_CORRECT, QUESTION) VALUES (2, 'perro', 0, 1); +INSERT INTO answers (ID, TEXT, IS_CORRECT, QUESTION) VALUES (3, 'elefante', 1, 1); +INSERT INTO answers (ID, TEXT, IS_CORRECT, QUESTION) VALUES (4, 'gato', 0, 1); +INSERT INTO answers (ID, TEXT, IS_CORRECT, QUESTION) VALUES (5, 'cocodrilo', 0, 2); +INSERT INTO answers (ID, TEXT, IS_CORRECT, QUESTION) VALUES (6, 'cocodrilo', 1, 2); +INSERT INTO answers (ID, TEXT, IS_CORRECT, QUESTION) VALUES (7, 'cocodrilo', 0, 2); +INSERT INTO answers (ID, TEXT, IS_CORRECT, QUESTION) VALUES (8, 'cocodrilo', 0, 2); +INSERT INTO answers (ID, TEXT, IS_CORRECT, QUESTION) VALUES (9, 'cocodrilo', 1, 3); +INSERT INTO answers (ID, TEXT, IS_CORRECT, QUESTION) VALUES (10, 'cocodrilo', 0, 3); +INSERT INTO answers (ID, TEXT, IS_CORRECT, QUESTION) VALUES (11, 'cocodrilo', 0, 3); +INSERT INTO answers (ID, TEXT, IS_CORRECT, QUESTION) VALUES (12, 'cocodrilo', 0, 3); +INSERT INTO answers (ID, TEXT, IS_CORRECT, QUESTION) VALUES (13, 'cocodrilo', 0, 4); +INSERT INTO answers (ID, TEXT, IS_CORRECT, QUESTION) VALUES (14, 'cocodrilo', 0, 4); +INSERT INTO answers (ID, TEXT, IS_CORRECT, QUESTION) VALUES (15, 'cocodrilo', 0, 4); +INSERT INTO answers (ID, TEXT, IS_CORRECT, QUESTION) VALUES (16, 'cocodrilo', 1, 4); +INSERT INTO answers (ID, TEXT, IS_CORRECT, QUESTION) VALUES (17, 'cocodrilo', 0, 5); +INSERT INTO answers (ID, TEXT, IS_CORRECT, QUESTION) VALUES (18, 'cocodrilo', 0, 5); +INSERT INTO answers (ID, TEXT, IS_CORRECT, QUESTION) VALUES (19, 'cocodrilo', 1, 5); +INSERT INTO answers (ID, TEXT, IS_CORRECT, QUESTION) VALUES (20, 'cocodrilo', 0, 5); +INSERT INTO answers (ID, TEXT, IS_CORRECT, QUESTION) VALUES (21, 'cocodrilo', 1, 6); +INSERT INTO answers (ID, TEXT, IS_CORRECT, QUESTION) VALUES (22, 'cocodrilo', 0, 6); +INSERT INTO answers (ID, TEXT, IS_CORRECT, QUESTION) VALUES (23, 'cocodrilo', 0, 6); +INSERT INTO answers (ID, TEXT, IS_CORRECT, QUESTION) VALUES (24, 'cocodrilo', 0, 6); +INSERT INTO answers (ID, TEXT, IS_CORRECT, QUESTION) VALUES (25, 'cocodrilo', 0, 7); +INSERT INTO answers (ID, TEXT, IS_CORRECT, QUESTION) VALUES (26, 'cocodrilo', 0, 7); +INSERT INTO answers (ID, TEXT, IS_CORRECT, QUESTION) VALUES (27, 'cocodrilo', 1, 7); +INSERT INTO answers (ID, TEXT, IS_CORRECT, QUESTION) VALUES (28, 'cocodrilo', 0, 7); +INSERT INTO answers (ID, TEXT, IS_CORRECT, QUESTION) VALUES (29, 'cocodrilo', 0, 8); +INSERT INTO answers (ID, TEXT, IS_CORRECT, QUESTION) VALUES (30, 'cocodrilo', 1, 8); +INSERT INTO answers (ID, TEXT, IS_CORRECT, QUESTION) VALUES (31, 'cocodrilo', 0, 8); +INSERT INTO answers (ID, TEXT, IS_CORRECT, QUESTION) VALUES (32, 'cocodrilo', 0, 8); diff --git a/syg-backend/SYG-bootstrap/target/classes/syg/bootstrap/BootstrapApplication.class b/syg-backend/SYG-bootstrap/target/classes/syg/bootstrap/BootstrapApplication.class index 520a6a62522bd4e81a0c0d30d19ac4ddf88afedd..560a2383766f836befd2f5b528e2fed76e76f171 100644 GIT binary patch delta 204 zcmey#{*zt&)W2Q(7#J9A8C19!m>8JZ8CZB2SQ*$RnkoU=920p}dAS(47`WLPcz76i z8TckX)txNPn8)IrpOQK;qjYl<<7UQscA!oWU}9hd=>?MfK%PF3W(3l#TH6^IH-e=F zfFwwo0ceyUkmP|VU=U&u2J$!=L>NSYA|TCXV8a*~IE2`>b}(>;BlLru$-p4Mzz#G4 KV#eex%&q{-JQn@{ delta 208 zcmYLBJq`gu7=7Q)u9+P(>@K1gnoUHfq7r9t0oqlmRSq!O(j^E9Q8<7bZ6m%}5?}FN z-uHegSxNhPKb`>Q7)k_gHWCt{>km>wxY({Y8UnFFghpv;r!-P@Br;cwBZDq_?pRz7 zbfP_R&|d)ZX-3Iv!c5FjeuFyKwr2D;V9{hW-vF~^6{CeVe|&jbj{z#9{ElSx?`w8| Mgj4Bnqr8gc2NfF@umAu6 diff --git a/syg-backend/SYG-domain/pom.xml b/syg-backend/SYG-domain/pom.xml index 77699b5..2edfa2d 100644 --- a/syg-backend/SYG-domain/pom.xml +++ b/syg-backend/SYG-domain/pom.xml @@ -22,6 +22,7 @@ <artifactId>spring-boot-starter-validation</artifactId> </dependency> + <!-- WIKIDATA --> <dependency> <groupId>org.eclipse.rdf4j</groupId> <artifactId>rdf4j-repository-sparql</artifactId> @@ -35,9 +36,7 @@ <scope>provided</scope> </dependency> - <!-- ============================================== --> - <!-- ============== TEST DEPENDENCIES ============= --> - <!-- ============================================== --> + <!-- TEST DEPENDENCIES --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> diff --git a/syg-backend/SYG-domain/src/test/java/enums/CategoryEnumTests.java b/syg-backend/SYG-domain/src/test/java/enums/CategoryEnumTests.java new file mode 100644 index 0000000..4f764d5 --- /dev/null +++ b/syg-backend/SYG-domain/src/test/java/enums/CategoryEnumTests.java @@ -0,0 +1,41 @@ +package enums; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import syg.domain.model.enums.CategoryEnum; + +public class CategoryEnumTests { + + @Test + @DisplayName("Verifica que las constantes del enumerado estan presented") + public void testEnumConstants() { + assertNotNull(CategoryEnum.valueOf("Sport")); + assertNotNull(CategoryEnum.valueOf("Countries")); + assertNotNull(CategoryEnum.valueOf("Science")); + assertNotNull(CategoryEnum.valueOf("Cine")); + assertNotNull(CategoryEnum.valueOf("Videogames")); + } + + @Test + @DisplayName("Verifica la relación entre el valor y la label") + public void testEnumProperties() { + assertEquals("Q349", CategoryEnum.Sport.getValue()); + assertEquals("Deportes", CategoryEnum.Sport.getLabel()); + + assertEquals("Q6256", CategoryEnum.Countries.getValue()); + assertEquals("Paises", CategoryEnum.Countries.getLabel()); + + assertEquals("Q336", CategoryEnum.Science.getValue()); + assertEquals("Ciencia", CategoryEnum.Science.getLabel()); + + assertEquals("Q11424", CategoryEnum.Cine.getValue()); + assertEquals("Cine", CategoryEnum.Cine.getLabel()); + + assertEquals("Q7889", CategoryEnum.Videogames.getValue()); + assertEquals("Videojuegos", CategoryEnum.Videogames.getLabel()); + } +} \ No newline at end of file diff --git a/syg-backend/SYG-domain/src/test/java/exception/ConflictExceptionTests.java b/syg-backend/SYG-domain/src/test/java/exception/ConflictExceptionTests.java new file mode 100644 index 0000000..172cbaf --- /dev/null +++ b/syg-backend/SYG-domain/src/test/java/exception/ConflictExceptionTests.java @@ -0,0 +1,35 @@ +package exception; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import org.junit.jupiter.api.Test; + +import configuration.UnitDomainTest; +import syg.domain.exception.ConflictException; + +@UnitDomainTest +public class ConflictExceptionTests { + + @Test + public void testConflictExceptionMessage() { + String expectedMessage = "This is a conflict error"; + ConflictException exception = assertThrows(ConflictException.class, () -> { + throw new ConflictException(expectedMessage); + }); + assertEquals(expectedMessage, exception.getMessage()); + } + + @Test + public void testConflictExceptionCatch() { + boolean exceptionCaught = false; + try { + throw new ConflictException("Conflict exception"); + } catch (ConflictException e) { + exceptionCaught = true; + assertEquals("Conflict exception", e.getMessage()); + } + assertTrue(exceptionCaught, "The exception should be caught"); + } +} \ No newline at end of file diff --git a/syg-backend/SYG-domain/src/test/java/exception/NotFoundExceptionTests.java b/syg-backend/SYG-domain/src/test/java/exception/NotFoundExceptionTests.java new file mode 100644 index 0000000..8a444a9 --- /dev/null +++ b/syg-backend/SYG-domain/src/test/java/exception/NotFoundExceptionTests.java @@ -0,0 +1,35 @@ +package exception; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import org.junit.jupiter.api.Test; + +import configuration.UnitDomainTest; +import syg.domain.exception.NotFoundException; + +@UnitDomainTest +public class NotFoundExceptionTests { + + @Test + public void testNotFoundExceptionMessage() { + String expectedMessage = "This is a not found error"; + NotFoundException exception = assertThrows(NotFoundException.class, () -> { + throw new NotFoundException(expectedMessage); + }); + assertEquals(expectedMessage, exception.getMessage()); + } + + @Test + public void testNotFoundExceptionCatch() { + boolean exceptionCaught = false; + try { + throw new NotFoundException("Conflict exception"); + } catch (NotFoundException e) { + exceptionCaught = true; + assertEquals("Conflict exception", e.getMessage()); + } + assertTrue(exceptionCaught, "The exception should be caught"); + } +} diff --git a/syg-backend/SYG-domain/src/test/java/services/unit/CategoryServiceTests.java b/syg-backend/SYG-domain/src/test/java/services/CategoryServiceTests.java similarity index 98% rename from syg-backend/SYG-domain/src/test/java/services/unit/CategoryServiceTests.java rename to syg-backend/SYG-domain/src/test/java/services/CategoryServiceTests.java index 43fb70b..08c18d6 100644 --- a/syg-backend/SYG-domain/src/test/java/services/unit/CategoryServiceTests.java +++ b/syg-backend/SYG-domain/src/test/java/services/CategoryServiceTests.java @@ -1,4 +1,4 @@ -package services.unit; +package services; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; diff --git a/syg-backend/SYG-domain/src/test/java/services/unit/QuestionServiceTests.java b/syg-backend/SYG-domain/src/test/java/services/QuestionServiceTests.java similarity index 86% rename from syg-backend/SYG-domain/src/test/java/services/unit/QuestionServiceTests.java rename to syg-backend/SYG-domain/src/test/java/services/QuestionServiceTests.java index aa2c4b0..713ca55 100644 --- a/syg-backend/SYG-domain/src/test/java/services/unit/QuestionServiceTests.java +++ b/syg-backend/SYG-domain/src/test/java/services/QuestionServiceTests.java @@ -1,7 +1,9 @@ -package services.unit; +package services; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.Mockito.doNothing; +import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import java.util.ArrayList; @@ -98,5 +100,25 @@ void find_question_by_not_exist_category() { List<Question> questions = questionService.findByCategory(100L); assertEquals(0, IterableUtil.sizeOf(questions)); } + + @Test + @DisplayName("Se borran todas las preguntas") + void delete_all_question() { + doNothing().when(questionPersistence).deleteQuestions(); + + questionService.deleteQuestions(); + + verify(questionPersistence).deleteQuestions(); + } + + @Test + @DisplayName("Se generan las preguntas") + void generate_questions() { + doNothing().when(questionPersistence).generatedQuestions(); + + questionService.generateQuestions(); + + verify(questionPersistence).generatedQuestions(); + } } diff --git a/syg-backend/SYG-domain/src/test/java/services/unit/UserServiceTests.java b/syg-backend/SYG-domain/src/test/java/services/UserServiceTests.java similarity index 85% rename from syg-backend/SYG-domain/src/test/java/services/unit/UserServiceTests.java rename to syg-backend/SYG-domain/src/test/java/services/UserServiceTests.java index f19b6f6..b9e5538 100644 --- a/syg-backend/SYG-domain/src/test/java/services/unit/UserServiceTests.java +++ b/syg-backend/SYG-domain/src/test/java/services/UserServiceTests.java @@ -1,4 +1,4 @@ -package services.unit; +package services; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; @@ -7,6 +7,9 @@ import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; +import java.util.ArrayList; +import java.util.List; + import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.function.Executable; @@ -29,6 +32,33 @@ public class UserServiceTests { @MockBean private UserPersistence userPersistence; + @Test + @DisplayName("Se busca todos los usuarios existentes") + void find_all_users() { + List<User> usersExpected = new ArrayList<User>(); + usersExpected.add(new User("4366fdc8-b32d-46bc-9df8-2e8ce68f0743", "Pablo", 8, 24, 12, 100, "Deportes")); + usersExpected.add(new User("4366fdc8-b32d-46bc-9df8-2e8ce68f0753", "Alvaro", 8, 4, 1, 100, "Animales")); + usersExpected.add(new User("4366fdc8-b32d-46bc-9df8-2e8ce68f0763", "Juan", 5, 2, 2, 100, "Deportes")); + + when(userPersistence.findAll()).thenReturn(usersExpected); + + List<User> users = userService.findAll(); + verify(userPersistence, times(1)).findAll(); + assertEquals(3, users.size()); + } + + @Test + @DisplayName("Se busca todos los usuarios existentes pero no existe ninguno") + void find_all_users_but_no_one_exist() { + List<User> usersExpected = new ArrayList<User>(); + + when(userPersistence.findAll()).thenReturn(usersExpected); + + List<User> users = userService.findAll(); + verify(userPersistence, times(1)).findAll(); + assertEquals(0, users.size()); + } + @Test @DisplayName("Se busca un usuario a traves de un id") void find_user_by_id() { diff --git a/syg-backend/SYG-mysql-adapter/pom.xml b/syg-backend/SYG-mysql-adapter/pom.xml index e915f9f..8bf5ce7 100644 --- a/syg-backend/SYG-mysql-adapter/pom.xml +++ b/syg-backend/SYG-mysql-adapter/pom.xml @@ -18,13 +18,6 @@ <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> - - <!-- POSTGRESQL --> -<!-- <dependency> - <groupId>org.postgresql</groupId> - <artifactId>postgresql</artifactId> - <scope>runtime</scope> - </dependency>--> <!-- MYSQL --> <dependency> @@ -47,9 +40,7 @@ <version>0.14.7</version> </dependency> - <!-- ============================================== --> - <!-- ============== TEST DEPENDENCIES ============= --> - <!-- ============================================== --> + <!-- TEST DEPENDENCIES --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> @@ -75,6 +66,7 @@ <scope>test</scope> </dependency> + <!-- SYG MODULES --> <dependency> <groupId>SYG-domain</groupId> <artifactId>SYG-domain</artifactId> diff --git a/syg-backend/SYG-mysql-adapter/src/test/java/syg/mysql/adapter/integration/QuestionAdapterIT.java b/syg-backend/SYG-mysql-adapter/src/test/java/syg/mysql/adapter/integration/QuestionAdapterIT.java index f2ebcce..031f3f2 100644 --- a/syg-backend/SYG-mysql-adapter/src/test/java/syg/mysql/adapter/integration/QuestionAdapterIT.java +++ b/syg-backend/SYG-mysql-adapter/src/test/java/syg/mysql/adapter/integration/QuestionAdapterIT.java @@ -7,7 +7,9 @@ import org.assertj.core.util.IterableUtil; import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.MethodOrderer.OrderAnnotation; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestMethodOrder; import org.junit.jupiter.api.function.Executable; import org.springframework.beans.factory.annotation.Autowired; @@ -18,6 +20,7 @@ import syg.mysql.configuration.IntegrationAdapterTest; @IntegrationAdapterTest +@TestMethodOrder(OrderAnnotation.class) public class QuestionAdapterIT extends SYGdbContainerIT { @Autowired @@ -72,4 +75,30 @@ void find_question_by_not_exist_category() { assertEquals(0, IterableUtil.sizeOf(questions)); } + + @Test + @DisplayName("Se borran todas las preguntas") + void delete_all_question() { + List<Question> questions = questionAdapter.findAll(); + assertEquals(8, IterableUtil.sizeOf(questions)); + + questionAdapter.deleteQuestions(); + List<Question> questionsAfterDelete = questionAdapter.findAll(); + assertEquals(0, IterableUtil.sizeOf(questionsAfterDelete)); + } + + @Test + @DisplayName("Se genera preguntas de wikidata") + void generate_wikidata_question() { + List<Question> questions = questionAdapter.findAll(); + assertEquals(8, IterableUtil.sizeOf(questions)); + + questionAdapter.deleteQuestions(); + List<Question> questionsAfterDelete = questionAdapter.findAll(); + assertEquals(0, IterableUtil.sizeOf(questionsAfterDelete)); + + questionAdapter.generatedQuestions(); + List<Question> questionsAfterGenerate = questionAdapter.findAll(); + assertEquals(222, IterableUtil.sizeOf(questionsAfterGenerate)); + } } diff --git a/syg-backend/SYG-mysql-adapter/src/test/java/syg/mysql/adapter/unit/QuestionAdapterTests.java b/syg-backend/SYG-mysql-adapter/src/test/java/syg/mysql/adapter/unit/QuestionAdapterTests.java index 923ff0f..de31842 100644 --- a/syg-backend/SYG-mysql-adapter/src/test/java/syg/mysql/adapter/unit/QuestionAdapterTests.java +++ b/syg-backend/SYG-mysql-adapter/src/test/java/syg/mysql/adapter/unit/QuestionAdapterTests.java @@ -2,6 +2,8 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.Mockito.doNothing; +import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import java.util.ArrayList; @@ -17,6 +19,7 @@ import syg.domain.exception.NotFoundException; import syg.domain.model.Question; +import syg.domain.model.WikiData; import syg.mysql.adapter.QuestionAdapter; import syg.mysql.configuration.UnitAdapterTest; import syg.mysql.entities.CategoryEntity; @@ -40,6 +43,9 @@ public class QuestionAdapterTests { @MockBean private CategoryRepository categoryRepository; + @MockBean + private WikiData wikiData; + @Test @DisplayName("Se buscan todas las preguntas en base de datos") void find_all_questions() { @@ -108,5 +114,14 @@ void find_question_by_not_exist_category() { List<Question> questions = questionAdapter.findByCategory(100L); assertEquals(0, IterableUtil.sizeOf(questions)); } - -} + + @Test + @DisplayName("Se borran todas las preguntas") + void delete_all_question() { + doNothing().when(questionRepository).deleteAll(); + + questionAdapter.deleteQuestions(); + + verify(questionRepository).deleteAll(); + } +} \ No newline at end of file diff --git a/syg-backend/SYG-mysql-adapter/src/test/resources/application.properties b/syg-backend/SYG-mysql-adapter/src/test/resources/application.properties deleted file mode 100644 index e69de29..0000000 diff --git a/syg-backend/SYG-rest-controller/src/main/java/syg/controller/CategoryController.java b/syg-backend/SYG-rest-controller/src/main/java/syg/controller/CategoryController.java index 38afcd9..1ece506 100644 --- a/syg-backend/SYG-rest-controller/src/main/java/syg/controller/CategoryController.java +++ b/syg-backend/SYG-rest-controller/src/main/java/syg/controller/CategoryController.java @@ -36,7 +36,7 @@ public ResponseEntity<Object> findById(@RequestParam(name = "id") Long id) { return ResponseEntity.status(HttpStatus.NOT_FOUND).body(e.getMessage()); } catch (Exception e) { - return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getMessage()); + return ResponseEntity.status(HttpStatus.SERVICE_UNAVAILABLE).body(e.getMessage()); } } } diff --git a/syg-backend/SYG-rest-controller/src/main/java/syg/controller/QuestionsController.java b/syg-backend/SYG-rest-controller/src/main/java/syg/controller/QuestionsController.java index 8161273..979d5f9 100644 --- a/syg-backend/SYG-rest-controller/src/main/java/syg/controller/QuestionsController.java +++ b/syg-backend/SYG-rest-controller/src/main/java/syg/controller/QuestionsController.java @@ -40,7 +40,7 @@ public ResponseEntity<Object> findById(@RequestParam(name = "id") Long id) { } catch (NotFoundException e) { return ResponseEntity.status(HttpStatus.NOT_FOUND).body(e.getMessage()); } catch (Exception e) { - return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getMessage()); + return ResponseEntity.status(HttpStatus.SERVICE_UNAVAILABLE).body(e.getMessage()); } } @GetMapping("/category") diff --git a/syg-backend/SYG-rest-controller/src/main/java/syg/controller/UserController.java b/syg-backend/SYG-rest-controller/src/main/java/syg/controller/UserController.java index 5412328..923ee26 100644 --- a/syg-backend/SYG-rest-controller/src/main/java/syg/controller/UserController.java +++ b/syg-backend/SYG-rest-controller/src/main/java/syg/controller/UserController.java @@ -27,15 +27,8 @@ public class UserController { @GetMapping public ResponseEntity<Object> findUsers() { - try { - List<User> users = userService.findAll(); - return ResponseEntity.status(HttpStatus.OK).body(users); - }catch (NotFoundException e) { - return ResponseEntity.status(HttpStatus.NOT_FOUND).body(e.getMessage()); - } - catch (Exception e) { - return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getMessage()); - } + List<User> users = userService.findAll(); + return ResponseEntity.status(HttpStatus.OK).body(users); } @GetMapping("/userId") @@ -47,7 +40,7 @@ public ResponseEntity<Object> findUser(@RequestParam(name = "id") String id) { return ResponseEntity.status(HttpStatus.NOT_FOUND).body(e.getMessage()); } catch (Exception e) { - return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getMessage()); + return ResponseEntity.status(HttpStatus.SERVICE_UNAVAILABLE).body(e.getMessage()); } } @@ -60,7 +53,7 @@ public ResponseEntity<Object> findName(@RequestParam(name = "userName") String u return ResponseEntity.status(HttpStatus.NOT_FOUND).body(e.getMessage()); } catch (Exception e) { - return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getMessage()); + return ResponseEntity.status(HttpStatus.SERVICE_UNAVAILABLE).body(e.getMessage()); } } @@ -69,12 +62,10 @@ public ResponseEntity<Object> createUser(@RequestBody User user) { try { User userCreated = userService.createUser(user); return ResponseEntity.status(HttpStatus.OK).body(userCreated); - } catch (NotFoundException e) { - return ResponseEntity.status(HttpStatus.NOT_FOUND).body(e.getMessage()); } catch (ConflictException e) { return ResponseEntity.status(HttpStatus.CONFLICT).body(e.getMessage()); } catch (Exception e) { - return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getMessage()); + return ResponseEntity.status(HttpStatus.SERVICE_UNAVAILABLE).body(e.getMessage()); } } @@ -88,7 +79,7 @@ public ResponseEntity<Object> updateUser(@RequestBody User user) { } catch (ConflictException e) { return ResponseEntity.status(HttpStatus.CONFLICT).body(e.getMessage()); } catch (Exception e) { - return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getMessage()); + return ResponseEntity.status(HttpStatus.SERVICE_UNAVAILABLE).body(e.getMessage()); } } } diff --git a/syg-backend/SYG-rest-controller/src/test/java/controller/CategoryControllerTests.java b/syg-backend/SYG-rest-controller/src/test/java/controller/CategoryControllerTests.java index 708727c..be95fc0 100644 --- a/syg-backend/SYG-rest-controller/src/test/java/controller/CategoryControllerTests.java +++ b/syg-backend/SYG-rest-controller/src/test/java/controller/CategoryControllerTests.java @@ -75,4 +75,13 @@ void find_category_by_not_exist_id() throws Exception { .andDo(MockMvcResultHandlers.print()).andExpect(status().is(HttpStatus.NOT_FOUND.value())); } + @Test + @DisplayName("Se busca una categoria a traves de un id y algo falla") + void find_category_by_id_and_something_goes_wrong() throws Exception { + when(categoryService.findById(1L)).thenThrow(new RuntimeException("Service is unavailable")); + + mockMvc.perform(get("/category/id").param("id", "1")) + .andDo(MockMvcResultHandlers.print()).andExpect(status().is(HttpStatus.SERVICE_UNAVAILABLE.value())); + } + } diff --git a/syg-backend/SYG-rest-controller/src/test/java/controller/QuestionControllerTests.java b/syg-backend/SYG-rest-controller/src/test/java/controller/QuestionControllerTests.java index 82d4906..0a04dba 100644 --- a/syg-backend/SYG-rest-controller/src/test/java/controller/QuestionControllerTests.java +++ b/syg-backend/SYG-rest-controller/src/test/java/controller/QuestionControllerTests.java @@ -57,6 +57,19 @@ void find_all_questions() throws Exception { .andExpect(jsonPath("$.length()", is(3))); } + @Test + @DisplayName("Se buscan todas las preguntas en base de datos y no hay ninguna") + void find_all_questions_but_no_one_exists() throws Exception { + List<Question> questionResponse = new ArrayList<Question>(); + + when(questionService.findAll()).thenReturn(questionResponse); + + mockMvc.perform(get("/question")) + .andDo(MockMvcResultHandlers.print()) + .andExpect(status().is(HttpStatus.OK.value())) + .andExpect(jsonPath("$.length()", is(0))); + } + @Test @DisplayName("Se busca una pregunta a traves de un id en base de datos") void find_question_by_id() throws Exception { @@ -78,6 +91,15 @@ void find_question_by_not_exist_id() throws Exception { .andDo(MockMvcResultHandlers.print()).andExpect(status().is(HttpStatus.NOT_FOUND.value())); } + @Test + @DisplayName("Se busca una pregunta a traves de un id y algo va mal") + void find_question_by_id_and_something_goes_wrong() throws Exception { + when(questionService.findById(1L)).thenThrow(new RuntimeException("Service is unavailable")); + + mockMvc.perform(get("/question/id").param("id", "1")) + .andDo(MockMvcResultHandlers.print()).andExpect(status().is(HttpStatus.SERVICE_UNAVAILABLE.value())); + } + @Test @DisplayName("Se buscan preguntas a traves de una categoria") void find_questions_by_category() throws Exception { diff --git a/syg-backend/SYG-rest-controller/src/test/java/controller/UserControllerTests.java b/syg-backend/SYG-rest-controller/src/test/java/controller/UserControllerTests.java index 54257d3..47791c9 100644 --- a/syg-backend/SYG-rest-controller/src/test/java/controller/UserControllerTests.java +++ b/syg-backend/SYG-rest-controller/src/test/java/controller/UserControllerTests.java @@ -8,6 +8,9 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +import java.util.ArrayList; +import java.util.List; + import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; @@ -43,6 +46,33 @@ public class UserControllerTests { @MockBean private UserService userService; + @Test + @DisplayName("Se buscan todos los usuarios") + void find_all_users() throws Exception { + List<User> usersExpected = new ArrayList<User>(); + usersExpected.add(new User("4366fdc8-b32d-46bc-9df8-2e8ce68f0743", "Pablo", 8, 24, 12, 100, "Deportes")); + usersExpected.add(new User("4366fdc8-b32d-46bc-9df8-2e8ce68f0753", "aAlaro", 8, 4, 2, 100, "Animales")); + usersExpected.add(new User("4366fdc8-b32d-46bc-9df8-2e8ce68f0763", "Juan", 3, 2, 1, 100, "Deportes")); + + when(userService.findAll()).thenReturn(usersExpected); + + mockMvc.perform(get("/user")) + .andDo(MockMvcResultHandlers.print()).andExpect(status().is(HttpStatus.OK.value())) + .andExpect(jsonPath("$.length()", is(3))); + } + + @Test + @DisplayName("Se buscan todos los usuarios pero ninguno existe") + void find_all_users_but_no_one_exist() throws Exception { + List<User> usersExpected = new ArrayList<User>(); + + when(userService.findAll()).thenReturn(usersExpected); + + mockMvc.perform(get("/user")) + .andDo(MockMvcResultHandlers.print()).andExpect(status().is(HttpStatus.OK.value())) + .andExpect(jsonPath("$.length()", is(0))); + } + @Test @DisplayName("Se busca un usuario a traves de un id") void find_user_by_id() throws Exception { @@ -64,6 +94,15 @@ void find_user_by_not_exist_id() throws Exception { .andDo(MockMvcResultHandlers.print()).andExpect(status().is(HttpStatus.NOT_FOUND.value())); } + @Test + @DisplayName("Se busca un usuario a traves de un id y algo va mal") + void find_user_by_id_but_something_goes_wrong() throws Exception { + when(userService.findById("5566fdc8-b32d-46bc-9df8-2e8ce68f0743")).thenThrow(new RuntimeException("Service is unavailable")); + + mockMvc.perform(get("/user/userId").param("id", "5566fdc8-b32d-46bc-9df8-2e8ce68f0743")) + .andDo(MockMvcResultHandlers.print()).andExpect(status().is(HttpStatus.SERVICE_UNAVAILABLE.value())); + } + @Test @DisplayName("Se busca un usuario a traves de su nombre") void find_user_by_name() throws Exception { @@ -86,6 +125,15 @@ void find_user_by_not_exist_name() throws Exception { .andDo(MockMvcResultHandlers.print()).andExpect(status().is(HttpStatus.NOT_FOUND.value())); } + @Test + @DisplayName("Se busca un usuario a traves de un nombre y algo va mal") + void find_user_by_name_but_something_goes_wrong() throws Exception { + when(userService.findByName("ServiceUnavailablePlayer")).thenThrow(new RuntimeException("Service is unavailable")); + + mockMvc.perform(get("/user/name").param("userName", "ServiceUnavailablePlayer")) + .andDo(MockMvcResultHandlers.print()).andExpect(status().is(HttpStatus.SERVICE_UNAVAILABLE.value())); + } + @Test @DisplayName("Se crea un usuario") void create_user() throws Exception { @@ -124,6 +172,18 @@ void create_user_with_existent_name() throws Exception { .andDo(MockMvcResultHandlers.print()).andExpect(status().is(HttpStatus.CONFLICT.value())); } + @Test + @DisplayName("Se crea un usuario pero algo va mal") + void create_user_but_something_goes_wrong() throws Exception { + User userToCreate = new User(null, "Jugador 30", 1, 3, 1, 100, "Deportes"); + + when(userService.createUser(userToCreate)).thenThrow(new RuntimeException("Service is unavailable")); + + mockMvc.perform(post("/user").contentType(MediaType.APPLICATION_JSON) + .content(asJsonString(userToCreate))) + .andDo(MockMvcResultHandlers.print()).andExpect(status().is(HttpStatus.SERVICE_UNAVAILABLE.value())); + } + @Test @DisplayName("Se actualiza un usuario") void update_user_() throws Exception { @@ -162,7 +222,31 @@ void update_user_with_null_id() throws Exception { .andDo(MockMvcResultHandlers.print()).andExpect(status().is(HttpStatus.NOT_FOUND.value())); } - public String asJsonString(Object obj) throws JsonProcessingException { + @Test + @DisplayName("Se actualiza un usuario con un nombre que ya existe") + void update_user_with_exist_name() throws Exception { + User userToUpdate = new User("1166fdc8-b32d-46bc-9df8-2e8ce68f0743", "Pablo", 1, 3, 1, 100, "Deportes"); + + when(userService.updateUser(userToUpdate)).thenThrow(ConflictException.class); + + mockMvc.perform(put("/user").contentType(MediaType.APPLICATION_JSON) + .content(asJsonString(userToUpdate))) + .andDo(MockMvcResultHandlers.print()).andExpect(status().is(HttpStatus.CONFLICT.value())); + } + + @Test + @DisplayName("Se actualiza un usuario pero algo va mal") + void update_user_but_something_goes_wrong() throws Exception { + User userToUpdate = new User("4366fdc8-b32d-46bc-9df8-2e8ce68f0743", "Jugador 3", 1, 3, 1, 100, "Deportes"); + + when(userService.updateUser(userToUpdate)).thenThrow(new RuntimeException("Service is unavailable")); + + mockMvc.perform(put("/user").contentType(MediaType.APPLICATION_JSON) + .content(asJsonString(userToUpdate))) + .andDo(MockMvcResultHandlers.print()).andExpect(status().is(HttpStatus.SERVICE_UNAVAILABLE.value())); + } + + private String asJsonString(Object obj) throws JsonProcessingException { return new ObjectMapper().writeValueAsString(obj); } } diff --git a/syg-backend/lombok.config b/syg-backend/lombok.config new file mode 100644 index 0000000..a23edb4 --- /dev/null +++ b/syg-backend/lombok.config @@ -0,0 +1,2 @@ +config.stopBubbling = true +lombok.addLombokGeneratedAnnotation = true \ No newline at end of file diff --git a/syg-backend/pom.xml b/syg-backend/pom.xml index da25bb1..4bf101f 100644 --- a/syg-backend/pom.xml +++ b/syg-backend/pom.xml @@ -33,25 +33,7 @@ <scope>import</scope> </dependency> - <!-- TESTS --> - <dependency> - <groupId>org.springframework.boot</groupId> - <artifactId>spring-boot-starter-test</artifactId> - <version>2.5.0</version> - </dependency> - <dependency> - <groupId>org.junit.jupiter</groupId> - <artifactId>junit-jupiter-api</artifactId> - <version>${junit-jupiter.version}</version> - <scope>test</scope> - </dependency> - <dependency> - <groupId>org.junit.jupiter</groupId> - <artifactId>junit-jupiter-engine</artifactId> - <version>${junit-jupiter.version}</version> - <scope>test</scope> - </dependency> - + <!-- MYSQL --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> @@ -72,6 +54,23 @@ <artifactId>h2</artifactId> <version>2.2.224</version> </dependency> + <dependency> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-starter-test</artifactId> + <version>2.5.0</version> + </dependency> + <dependency> + <groupId>org.junit.jupiter</groupId> + <artifactId>junit-jupiter-api</artifactId> + <version>${junit-jupiter.version}</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.junit.jupiter</groupId> + <artifactId>junit-jupiter-engine</artifactId> + <version>${junit-jupiter.version}</version> + <scope>test</scope> + </dependency> </dependencies> </dependencyManagement> @@ -148,6 +147,11 @@ <goals> <goal>report</goal> </goals> + <configuration> + <excludes> + <exclude>syg/mysql/entities/**/*.java</exclude> <!-- Ruta de exclusión --> + </excludes> + </configuration> </execution> </executions> </plugin>