-
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.
- Loading branch information
SAMUEL BRISTOT LOLI
committed
Apr 3, 2022
1 parent
9c66917
commit d708d78
Showing
3 changed files
with
114 additions
and
0 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
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,8 @@ | ||
spring.datasource.url=jdbc:h2:mem:testdb;MODE=PostgreSQL; | ||
spring.jpa.hibernate.ddl-auto=none | ||
spring.jpa.hibernate.show-sql=true | ||
spring.h2.console.enabled=true | ||
spring.h2.console.path=/h2-console | ||
spring.h2.console.settings.web-allow-others=true | ||
spring.flyway.baseline-on-migrate = true | ||
spring.mvc.pathmatch.matching-strategy = ANT_PATH_MATCHER |
101 changes: 101 additions & 0 deletions
101
src/test/java/com/samuelapp/demoshop/controler/EmployeeControllerTest.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,101 @@ | ||
package com.samuelapp.demoshop.controler; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.samuelapp.demoshop.model.Employee; | ||
import com.samuelapp.demoshop.model.dto.EmployeeDto; | ||
import org.junit.jupiter.api.*; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.test.context.ActiveProfiles; | ||
import org.springframework.test.context.junit.jupiter.SpringExtension; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; | ||
import java.net.URI; | ||
import java.time.LocalDate; | ||
import java.util.Date; | ||
|
||
|
||
@ExtendWith(SpringExtension.class) | ||
@SpringBootTest | ||
@AutoConfigureMockMvc | ||
@ActiveProfiles("dev") | ||
@TestMethodOrder(MethodOrderer.OrderAnnotation.class) | ||
public class EmployeeControllerTest { | ||
|
||
@Autowired | ||
private MockMvc mockMvc; | ||
|
||
@Autowired | ||
private ObjectMapper objectMapper; | ||
|
||
@Test | ||
@Order(1) | ||
void sholdReturn200toGetEmployees() throws Exception { | ||
URI uri = new URI("/employees/"); | ||
mockMvc | ||
.perform(get(uri).contentType(MediaType.APPLICATION_JSON)) | ||
.andExpect(status().isOk()); | ||
} | ||
|
||
@Test | ||
@Order(2) | ||
void sholdReturnEmployeeJhon() throws Exception { | ||
Employee employee = Employee.builder().id(1).name("Jhon").build(); | ||
URI uri = new URI("/employees/"+employee.getId()); | ||
String result = mockMvc | ||
.perform(get(uri).contentType(MediaType.APPLICATION_JSON)) | ||
.andReturn().getResponse().getContentAsString(); | ||
Assertions.assertEquals(objectMapper.readValue(result, Employee.class).getName(),employee.getName()); | ||
} | ||
|
||
@Test | ||
@Order(3) | ||
void sholdReturnNotFound() throws Exception { | ||
URI uri = new URI("/employees/2"); | ||
mockMvc | ||
.perform(get(uri).contentType(MediaType.APPLICATION_JSON)) | ||
.andExpect(status().isNotFound()); | ||
} | ||
|
||
@Test | ||
@Order(4) | ||
void shouldSaveNewEmployeeAndReturn201() throws Exception { | ||
EmployeeDto employeeDto = EmployeeDto.builder() | ||
.name("Maria").idEmployeType(1).idShop(1).address("Test Street") | ||
.telephone("4899184578").employmentDate(LocalDate.of(2020, 1, 8)).build(); | ||
URI uri = new URI("/employees/"); | ||
mockMvc.perform(post(uri).contentType(MediaType.APPLICATION_JSON).content(objectMapper.writeValueAsString(employeeDto))) | ||
.andExpect(status().isCreated()); | ||
} | ||
|
||
@Test | ||
@Order(5) | ||
void sholdUpdateEmployee() throws Exception { | ||
EmployeeDto employeeDto = EmployeeDto.builder() | ||
.name("Jhon after update").idEmployeType(1).idShop(1).address("Test Street") | ||
.telephone("4899184578").employmentDate(LocalDate.of(2020, 1, 8)).build(); | ||
URI uri = new URI("/employees/1"); | ||
mockMvc.perform(put(uri).contentType(MediaType.APPLICATION_JSON) | ||
.content(objectMapper.writeValueAsString(employeeDto))) | ||
.andExpect(status().isOk()); | ||
String employeeUpdated = mockMvc | ||
.perform(get(uri).contentType(MediaType.APPLICATION_JSON)) | ||
.andReturn().getResponse().getContentAsString(); | ||
Assertions.assertEquals(objectMapper.readValue(employeeUpdated, Employee.class).getName(),employeeDto.getName()); | ||
} | ||
|
||
@Test | ||
@Order(6) | ||
void sholdRemoveEmployee() throws Exception { | ||
URI uri = new URI("/employees/2"); | ||
mockMvc | ||
.perform(delete(uri).contentType(MediaType.APPLICATION_JSON)) | ||
.andExpect(status().isOk()); | ||
} | ||
|
||
|
||
} |