Merge pull request 'test: "Adding tests"' (#70) from 2210970/Backend:main into restfull

Reviewed-on: Maradona/Backend#70
pull/1/head
Mohammad Hawrami 2024-11-08 16:58:34 +01:00
commit 1729e0a15e
2 changed files with 256 additions and 0 deletions

View File

@ -0,0 +1,57 @@
package com.maradona.backend.controllers.api;
import com.maradona.backend.controllers.api.EmployeeController;
import com.maradona.backend.entities.Employee;
import com.maradona.backend.services.EmployeeService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import java.time.LocalTime;
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.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
@WebMvcTest(EmployeeController.class)
public class EmployeeControllerTest {
@Autowired
private MockMvc mockMvc;
@MockBean
private EmployeeService employeeService;
@Test
public void testGetEmployeeById() throws Exception {
// Arrange: Mock an employee
Employee employee = new Employee();
employee.setId(1L);
employee.setEmployeeNr(123);
employee.setFirstName("John");
employee.setLastName("Doe");
employee.setDStart(LocalTime.of(9, 0));
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
mockMvc.perform(get("/api/employee")
.param("id", "1")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$.id").value(1))
.andExpect(jsonPath("$.employeeNr").value(123))
.andExpect(jsonPath("$.firstName").value("John"))
.andExpect(jsonPath("$.lastName").value("Doe"))
.andExpect(jsonPath("$.dStart").value("09:00:00"))
.andExpect(jsonPath("$.dEnd").value("17:00:00"));
}
}

View File

@ -0,0 +1,199 @@
package com.maradona.backend.controllers.api;
import com.maradona.backend.controllers.api.ProjectController;
import com.maradona.backend.entities.Project;
import com.maradona.backend.services.ProjectService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import java.time.LocalDate;
import java.util.Arrays;
import java.util.Optional;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;
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;
@WebMvcTest(ProjectController.class)
public class ProjectControllerTest {
// MockMvc to simulate HTTP requests to the controller
@Autowired
private MockMvc mockMvc;
// Mocked ProjectService to simulate service calls
@MockBean
private ProjectService projectService;
@Test
public void testGetProjectById() throws Exception {
//Arrange: Mock an project
Project project = new Project();
project.setId(1L);
project.setName("Skillmanagementsystem erstellen");
project.setStartDate(LocalDate.of(2024, 11,8));
project.setEndDate(LocalDate.of(2024, 11,20));
project.setWorkload(12);
project.setDescription("Skillmanagementsystem erstellen für die Firma");
//Define the behavior of the mocked ProjectService: return the project when ID 1 is requested
when(projectService.getProjectById(1L)).thenReturn(Optional.of(project));
//Act & Assert: Send GET request an expect a 200 OK status and JSON response
//GET /api/project/
mockMvc.perform(get("/api/project")
.param("id", "1")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$.id").value(1))
.andExpect(jsonPath("$.name").value("Skillmanagementsystem erstellen"))
.andExpect(jsonPath("$.startDate").value("2024-11-08"))
.andExpect(jsonPath("$.endDate").value("2024-11-20"))
.andExpect(jsonPath("$.workload").value(12))
.andExpect(jsonPath("$.description").value("Skillmanagementsystem erstellen für die Firma"));
}
@Test
public void testGetAllProjects() throws Exception {
//Arrange: Creat a list of mock projects
Project project = new Project();
project.setId(1L);
project.setName("Skillmanagementsystem erstellen");
project.setStartDate(LocalDate.of(2024, 11,8));
project.setEndDate(LocalDate.of(2024, 11,20));
project.setWorkload(12);
project.setDescription("Skillmanagementsystem erstellen für die Firma");
Project project2 = new Project();
project2.setId(2L);
project2.setName("EAFC 25");
project2.setStartDate(LocalDate.of(2024, 11,20));
project2.setEndDate(LocalDate.of(2024, 11,30));
project2.setWorkload(2);
project2.setDescription("Entwicklung von EAFC 25 für neues Spaß erlebnis");
//Define the behavior of the mocked ProjectService: return a list of projects when requested
when(projectService.getAllProjects()).thenReturn(Arrays.asList(project, project2));
//Act & Assert: Send GET request an expect a 200 Ok status and JSON response with the list of projects
mockMvc.perform(get("/api/project/all")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$[0].id").value(1))
.andExpect(jsonPath("$[0].name").value("Skillmanagementsystem erstellen"))
.andExpect(jsonPath("$[0].startDate").value("2024-11-08"))
.andExpect(jsonPath("$[0].endDate").value("2024-11-20"))
.andExpect(jsonPath("$[0].workload").value(12))
.andExpect(jsonPath("$[0].description").value("Skillmanagementsystem erstellen für die Firma"))
.andExpect(jsonPath("$[1].id").value(2))
.andExpect(jsonPath("$[1].name").value("EAFC 25"))
.andExpect(jsonPath("$[1].startDate").value("2024-11-20"))
.andExpect(jsonPath("$[1].endDate").value("2024-11-30"))
.andExpect(jsonPath("$[1].workload").value(2))
.andExpect(jsonPath("$[1].description").value("Entwicklung von EAFC 25 für neues Spaß erlebnis"));
}
@Test
public void testGetProjectsByUserId() throws Exception {
// Arrange: Mock projects for a specific user
Project project1 = new Project();
project1.setId(1L);
project1.setName("Skill Management System");
project1.setStartDate(LocalDate.of(2024, 11, 8));
project1.setEndDate(LocalDate.of(2024, 11, 20));
project1.setWorkload(12);
project1.setDescription("Create a skill management system for the company");
Project project2 = new Project();
project2.setId(2L);
project2.setName("Project Management Tool");
project2.setStartDate(LocalDate.of(2024, 12, 1));
project2.setEndDate(LocalDate.of(2024, 12, 15));
project2.setWorkload(10);
project2.setDescription("Develop a project management tool");
Long userId = 123L;
// Mock the ProjectService to return projects for a specific user
when(projectService.getProjectsByUserId(userId)).thenReturn(Arrays.asList(project1, project2));
// Act & Assert: Send GET request and expect a 200 OK status with the correct JSON response
mockMvc.perform(get("/api/project/from-user")
.param("userId", String.valueOf(userId))
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$[0].id").value(1))
.andExpect(jsonPath("$[0].name").value("Skill Management System"))
.andExpect(jsonPath("$[1].id").value(2))
.andExpect(jsonPath("$[1].name").value("Project Management Tool"));
}
@Test
public void testCreateProject() throws Exception {
// Arrange: Create a new project and set all required fields
Project savedProject = new Project();
savedProject.setId(1L); // ID setzen
savedProject.setName("New Project");
savedProject.setStartDate(LocalDate.of(2024, 11, 10));
savedProject.setEndDate(LocalDate.of(2024, 12, 10));
savedProject.setWorkload(15);
savedProject.setDescription("A new project for testing");
// Mocking the saveProject method to return this specific project
when(projectService.saveProject(any(Project.class))).thenReturn(savedProject);
// Act & Assert: Send POST request and check for 201 Created with JSON response
mockMvc.perform(post("/api/project")
.contentType(MediaType.APPLICATION_JSON)
.content("{\"name\":\"New Project\",\"startDate\":\"2024-11-10\",\"endDate\":\"2024-12-10\",\"workload\":15,\"description\":\"A new project for testing\"}"))
.andExpect(status().isCreated())
.andExpect(jsonPath("$.id").value(1)) // Ensure the response JSON contains id = 1
.andExpect(jsonPath("$.name").value("New Project"))
.andExpect(jsonPath("$.workload").value(15))
.andExpect(jsonPath("$.description").value("A new project for testing"));
}
@Test
public void testUpdateProject() throws Exception {
// Arrange: Create an existing project
Project project = new Project();
project.setId(1L);
project.setName("Updated Project");
project.setStartDate(LocalDate.of(2024, 11, 10));
project.setEndDate(LocalDate.of(2024, 12, 10));
project.setWorkload(20);
project.setDescription("An updated project description");
// Mock the ProjectService to return the updated project
when(projectService.saveProject(project)).thenReturn(project);
// Act & Assert: Send PUT request and expect a 200 OK status with the updated project as JSON response
mockMvc.perform(put("/api/project")
.contentType(MediaType.APPLICATION_JSON)
.content("{\"id\":1,\"name\":\"Updated Project\",\"startDate\":\"2024-11-10\",\"endDate\":\"2024-12-10\",\"workload\":20,\"description\":\"An updated project description\"}"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.id").value(1))
.andExpect(jsonPath("$.name").value("Updated Project"))
.andExpect(jsonPath("$.workload").value(20))
.andExpect(jsonPath("$.description").value("An updated project description"));
}
@Test
public void testDeleteProject() throws Exception {
// Arrange: Define the ID of the project to delete
Long projectId = 1L;
// No need to mock the delete method as it returns void, we just ensure no exception is thrown
// Act & Assert: Send DELETE request and expect a 204 No Content status
mockMvc.perform(delete("/api/project")
.param("id", String.valueOf(projectId))
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isNoContent());
}
}