generated from Arquisoft/wiq_0
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
4d66d5e
commit 1b9b65f
Showing
27 changed files
with
854 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
syg-backend/SYG-bootstrap/src/test/java/syg/bootstrap/SYGdbContainer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
|
||
} |
20 changes: 20 additions & 0 deletions
20
syg-backend/SYG-bootstrap/src/test/java/syg/bootstrap/configuration/E2ETests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 { | ||
|
||
} |
61 changes: 61 additions & 0 deletions
61
syg-backend/SYG-bootstrap/src/test/java/syg/bootstrap/e2e/CategoryTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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())); | ||
} | ||
} |
82 changes: 82 additions & 0 deletions
82
syg-backend/SYG-bootstrap/src/test/java/syg/bootstrap/e2e/QuestionsTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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))); | ||
} | ||
} |
Oops, something went wrong.