Skip to content

Commit

Permalink
#11 #12 #13 Tests de todas las capas and tests e2e
Browse files Browse the repository at this point in the history
  • Loading branch information
AlvaroGlezC committed Apr 26, 2024
1 parent 4d66d5e commit 1b9b65f
Show file tree
Hide file tree
Showing 27 changed files with 854 additions and 56 deletions.
32 changes: 31 additions & 1 deletion syg-backend/SYG-bootstrap/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand All @@ -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>
Expand Down
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);
}

}
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 {

}
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()));
}
}
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)));
}
}
Loading

0 comments on commit 1b9b65f

Please sign in to comment.