Merge pull request 'test: Adding new tests for the employeecontrollertest' (#78) from 2210970/Backend:main into restfull

Reviewed-on: Maradona/Backend#78
pull/1/head
Mohammad Hawrami 2024-11-11 09:35:36 +01:00
commit 254b42307c
1 changed files with 116 additions and 2 deletions

View File

@ -11,10 +11,11 @@ import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import java.time.LocalTime;
import java.util.List;
import java.util.Optional;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
@ -39,7 +40,6 @@ public class EmployeeControllerTest {
employee.setDEnd(LocalTime.of(17, 0));
// Assuming FormOfAddress and EmployeeSecondarySkill are also set up if needed for your test.
when(employeeService.getEmployeeById(1L)).thenReturn(Optional.of(employee));
// Act & Assert: Send GET request and expect a 200 OK status and JSON response
@ -54,4 +54,118 @@ public class EmployeeControllerTest {
.andExpect(jsonPath("$.dStart").value("09:00:00"))
.andExpect(jsonPath("$.dEnd").value("17:00:00"));
}
//Test if an employee is not found (404 Not Found)
@Test
public void testGetEmployeeByIdNotFound() throws Exception {
//Arrange
when(employeeService.getEmployeeById(999L)).thenReturn(Optional.empty());
//Act & Assert: Send GET request and expect a 404 Not Found status
mockMvc.perform(get("/api/employee")
.param("id", "999")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isNotFound());
}
//Testing the getAll method for all employees
@Test
public void testGetAllEmployees() throws Exception {
//Arrange: Mock a list of employees
Employee employee1 = new Employee();
employee1.setId(1L);
employee1.setEmployeeNr(123);
employee1.setFirstName("Mohammad");
employee1.setLastName("Hawrami");
Employee employee2 = new Employee();
employee2.setId(2L);
employee2.setEmployeeNr(124);
employee2.setFirstName("Tarik");
employee2.setLastName("Gökmen");
when(employeeService.getAllEmployees()).thenReturn(List.of(employee1, employee2));
//Act & Assert: Send GET request and expect a 200 OK status and JSON array response
mockMvc.perform(get("/api/employees/all")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$[0].employeeNr").value(1))
.andExpect(jsonPath("$[1].employeeNr").value(2));
}
//Testing the getFromSkill method with a specific Skilld and level
@Test
public void testGetEmployeesBySecondarySkill() throws Exception {
//Arrange: Mock a list of employees with specific skills
Employee employee = new Employee();
employee.setId(1L);
employee.setEmployeeNr(123);
employee.setFirstName("Mohammad");
employee.setLastName("Hawrami");
when(employeeService.getEmployeesBySecondarySkill(1L,2)).thenReturn(List.of(employee));
//Act & Assert: Send GET request and expect a 200 OK status and JSON array response
mockMvc.perform(get("/api/employee/from-skill")
.param("skillId", "1")
.param("level", "2")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$[0].id").value(1))
.andExpect(jsonPath("$[0].employeeNr").value(123));
}
//Testing the postSkillPrototype method with a valid SkillPrototype object
@Test
public void testPostSkillPrototype() throws Exception {
//Arrange: Create a SkillPrototype JSON payload
String skillPrototypeJson = "{\"skillId\":\"1\",\"level\":\"3\"}";
//Act & Assert: Send POST request and expect a 201 Creat status
mockMvc.perform(post("/api/employee/skill/prototype")
.content(skillPrototypeJson)
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isCreated());
}
//Testing the putSkillLevel method to update an existing level
@Test
public void testPutSkillLevel() throws Exception {
// Act & Assert: Send PUT request and expect a 200 OK status
mockMvc.perform(put("/api/employee/skill/level")
.param("skillId", "1")
.param("level", "5")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk());
}
//Testing the delete method for removing a secondary skill java
@Test
public void testDeleteSecondarySkill() throws Exception {
// Act & Assert: Send DELETE request and expect a 204 No Content status
mockMvc.perform(delete("/api/employee/skill")
.param("id", "1")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isNoContent());
}
//Testing the postSkillPrototype methode with invalid payload (e.g., no Skilld)
@Test
public void testPostSkillPrototype_BadRequest() throws Exception {
// Arrange: Create an invalid JSON payload (missing skillId)
String invalidSkillPrototypeJson = "{\"level\":3}";
// Act & Assert: Send POST request and expect a 400 Bad Request status
mockMvc.perform(post("/api/employee/skill/prototype")
.content(invalidSkillPrototypeJson)
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isBadRequest());
}
}