test: Changing all test

pull/1/head
mohammad 2024-11-12 12:33:35 +01:00
parent e42ecaf134
commit 426cf0c32b
5 changed files with 182 additions and 255 deletions

View File

@ -1,8 +1,8 @@
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 com.maradona.backend.services.details.EmployeeDetails;
import com.maradona.backend.dto.SkillPrototype;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
@ -26,11 +26,10 @@ public class EmployeeControllerTest {
private MockMvc mockMvc;
@MockBean
private EmployeeService employeeService;
private EmployeeDetails employeeDetails;
@Test
public void testGetEmployeeById() throws Exception {
// Arrange: Mock an employee
Employee employee = new Employee();
employee.setEid(1L);
employee.setEmployeeNr(123);
@ -39,15 +38,13 @@ public class EmployeeControllerTest {
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.getEmployeeByEid(1L)).thenReturn(Optional.of(employee));
when(employeeDetails.getEmployeeByEid(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("$.eid").value(1))
.andExpect(jsonPath("$.employeeNr").value(123))
.andExpect(jsonPath("$.firstName").value("John"))
.andExpect(jsonPath("$.lastName").value("Doe"))
@ -55,73 +52,63 @@ public class EmployeeControllerTest {
.andExpect(jsonPath("$.dEnd").value("17:00:00"));
}
//Test if an employee is not found (404 Not Found)
// Test für den Fall, dass ein Employee nicht gefunden wird (404 Not Found)
@Test
public void testGetEmployeeByIdNotFound() throws Exception {
//Arrange
when(employeeService.getEmployeeByEid(999L)).thenReturn(Optional.empty());
// Arrange: Mockt eine leere Rückgabe für eine nicht existierende Employee-ID
when(employeeDetails.getEmployeeByEid(999L)).thenReturn(Optional.empty());
//Act & Assert: Send GET request and expect a 404 Not Found status
// Act & Assert: Sendet eine GET-Anfrage und erwartet einen 404 Not Found-Status
mockMvc.perform(get("/api/employee")
.param("id", "999")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isNotFound());
}
//Testing the getFromSkill method with a specific Skilld and level
// Test der getFromSkill-Methode mit einer spezifischen Skill-ID und einem Level
@Test
public void testGetEmployeesBySecondarySkill() throws Exception {
//Arrange: Mock a list of employees with specific skills
// Arrange: Mockt eine Liste von Mitarbeitern mit spezifischen Skills
Employee employee = new Employee();
employee.setEid(1L);
employee.setEmployeeNr(123);
employee.setFirstName("Mohammad");
employee.setLastName("Hawrami");
when(employeeService.getEmployeesBySecondarySkill(1L,2)).thenReturn(List.of(employee));
when(employeeDetails.getEmployeesBySecondarySkill(1L, 2)).thenReturn(List.of(employee));
//Act & Assert: Send GET request and expect a 200 OK status and JSON array response
// Act & Assert: Sendet eine GET-Anfrage und erwartet einen 200 OK-Status sowie eine JSON-Antwort
mockMvc.perform(get("/api/employee/from-skill")
.param("skillId", "1")
.param("ssid", "1") // Parameter "ssid" statt "skillId" verwenden
.param("level", "2")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$[0].id").value(1))
.andExpect(jsonPath("$[0].employeeNr").value(123));
.andExpect(jsonPath("$[0].eid").value(1)) // "eid" statt "id"
.andExpect(jsonPath("$[0].employeeNr").value(123)); // "employeeNr" korrekt spezifizieren
}
//Testing the putSkillLevel method to update an existing level
// Test der putSkillLevel-Methode zur Aktualisierung eines bestehenden Levels
@Test
public void testPutSkillLevel() throws Exception {
// Act & Assert: Send PUT request and expect a 200 OK status
// Act & Assert: Sendet eine PUT-Anfrage und erwartet einen 200 OK-Status
mockMvc.perform(put("/api/employee/skill/level")
.param("skillId", "1")
.param("ssid", "1") // Korrigiert zu "ssid" statt "skillId"
.param("level", "5")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk());
}
//Testing the delete method for removing a secondary skill java
// Testet die delete-Methode zum Entfernen einer Secondary Skill
@Test
public void testDeleteSecondarySkill() throws Exception {
// Act & Assert: Send DELETE request and expect a 204 No Content status
// Act & Assert: Sendet eine DELETE-Anfrage und erwartet einen 204 No Content-Status
mockMvc.perform(delete("/api/employee/skill")
.param("id", "1")
.param("ssid", "1") // Korrigiert zu "ssid" statt "id"
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isNoContent());
}
// Test GET /api/employee with a valid ID but the employee has no secondary skills
@Test
public void testGetEmployeeById_NoSecondarySkills() throws Exception {
@ -131,7 +118,7 @@ public class EmployeeControllerTest {
employee.setFirstName("John");
employee.setLastName("Doe");
when(employeeService.getEmployeeByEid(1L)).thenReturn(Optional.of(employee));
when(employeeDetails.getEmployeeByEid(1L)).thenReturn(Optional.of(employee));
mockMvc.perform(get("/api/employee")
.param("id", "1")
@ -159,7 +146,7 @@ public class EmployeeControllerTest {
employee2.setFirstName("Bob");
employee2.setLastName("Johnson");
when(employeeService.getAllEmployees()).thenReturn(List.of(employee1, employee2));
when(employeeDetails.getAllEmployees()).thenReturn(List.of(employee1, employee2));
mockMvc.perform(get("/api/employee/all")
.contentType(MediaType.APPLICATION_JSON))
@ -200,8 +187,8 @@ public class EmployeeControllerTest {
employee.setFirstName("John");
employee.setLastName("Doe");
when(employeeService.getEmployeesBySecondarySkill(1L, 1)).thenReturn(List.of(employee));
when(employeeService.getEmployeesBySecondarySkill(1L, 5)).thenReturn(List.of(employee));
when(employeeDetails.getEmployeesBySecondarySkill(1L, 1)).thenReturn(List.of(employee));
when(employeeDetails.getEmployeesBySecondarySkill(1L, 5)).thenReturn(List.of(employee));
// Test with minimum level
mockMvc.perform(get("/api/employee/from-skill")
@ -231,7 +218,7 @@ public class EmployeeControllerTest {
employee.setFirstName("Max");
employee.setLastName("Value");
when(employeeService.getEmployeeByEid(Long.MAX_VALUE)).thenReturn(Optional.of(employee));
when(employeeDetails.getEmployeeByEid(Long.MAX_VALUE)).thenReturn(Optional.of(employee));
mockMvc.perform(get("/api/employee")
.param("id", String.valueOf(Long.MAX_VALUE))
@ -246,7 +233,7 @@ public class EmployeeControllerTest {
// Test GET /api/employee mit einer nicht existierenden ID (extrem großer Wert)
@Test
public void testGetEmployeeById_NonExistentHighId() throws Exception {
when(employeeService.getEmployeeByEid(Long.MAX_VALUE)).thenReturn(Optional.empty());
when(employeeDetails.getEmployeeByEid(Long.MAX_VALUE)).thenReturn(Optional.empty());
mockMvc.perform(get("/api/employee")
.param("id", String.valueOf(Long.MAX_VALUE))
@ -254,19 +241,6 @@ public class EmployeeControllerTest {
.andExpect(status().isNotFound());
}
// // Test GET /api/employee/all mit einer großen Anzahl von Mitarbeitern
// @Test
// public void testGetAllEmployees_LargeDataset() throws Exception {
//
//
// when(employeeService.getAllEmployees()).thenReturn(largeEmployeeList);
//
// mockMvc.perform(get("/api/employee/all")
// .contentType(MediaType.APPLICATION_JSON))
// .andExpect(status().isOk())
// .andExpect(jsonPath("$.length()").value(largeEmployeeList.size()));
// }
// Test GET /api/employee/from-skill ohne level Parameter
@Test
public void testGetEmployeesBySecondarySkill_MissingLevelParameter() throws Exception {
@ -276,8 +250,6 @@ public class EmployeeControllerTest {
.andExpect(status().isBadRequest());
}
// Test PUT /api/employee/skill/level mit nicht numerischem Skill ID-Wert
@Test
public void testPutSkillLevel_InvalidSkillIdFormat() throws Exception {
@ -296,8 +268,6 @@ public class EmployeeControllerTest {
.andExpect(status().isBadRequest());
}
// Test PUT /api/employee/skill/level mit maximal gültigem Level
@Test
public void testPutSkillLevel_MaxLevel() throws Exception {
@ -311,7 +281,7 @@ public class EmployeeControllerTest {
// Test GET /api/employee/all mit leeren Rückgabedaten
@Test
public void testGetAllEmployees_EmptyList() throws Exception {
when(employeeService.getAllEmployees()).thenReturn(List.of());
when(employeeDetails.getAllEmployees()).thenReturn(List.of());
mockMvc.perform(get("/api/employee/all")
.contentType(MediaType.APPLICATION_JSON))
@ -319,7 +289,6 @@ public class EmployeeControllerTest {
.andExpect(jsonPath("$").isEmpty());
}
// Test GET /api/employee mit einem Mitarbeiter mit vollständigen Details und spezifischen Arbeitszeiten
@Test
public void testGetEmployeeById_WithFullDetails() throws Exception {
@ -331,7 +300,7 @@ public class EmployeeControllerTest {
employee.setDStart(LocalTime.of(8, 30));
employee.setDEnd(LocalTime.of(16, 30));
when(employeeService.getEmployeeByEid(10L)).thenReturn(Optional.of(employee));
when(employeeDetails.getEmployeeByEid(10L)).thenReturn(Optional.of(employee));
mockMvc.perform(get("/api/employee")
.param("id", "10")
@ -366,7 +335,7 @@ public class EmployeeControllerTest {
employee3.setFirstName("Jake");
employee3.setLastName("Smith");
when(employeeService.getAllEmployees()).thenReturn(List.of(employee1, employee2, employee3));
when(employeeDetails.getAllEmployees()).thenReturn(List.of(employee1, employee2, employee3));
mockMvc.perform(get("/api/employee/all")
.contentType(MediaType.APPLICATION_JSON))
@ -406,7 +375,7 @@ public class EmployeeControllerTest {
employee2.setFirstName("Liam");
employee2.setLastName("Wilson");
when(employeeService.getEmployeesBySecondarySkill(2L, 3)).thenReturn(List.of(employee1, employee2));
when(employeeDetails.getEmployeesBySecondarySkill(2L, 3)).thenReturn(List.of(employee1, employee2));
mockMvc.perform(get("/api/employee/from-skill")
.param("skillId", "2")
@ -451,7 +420,7 @@ public class EmployeeControllerTest {
employee.setDStart(LocalTime.of(7, 45));
employee.setDEnd(LocalTime.of(15, 45));
when(employeeService.getEmployeeByEid(15L)).thenReturn(Optional.of(employee));
when(employeeDetails.getEmployeeByEid(15L)).thenReturn(Optional.of(employee));
mockMvc.perform(get("/api/employee")
.param("id", "15")
@ -474,7 +443,7 @@ public class EmployeeControllerTest {
employee.setFirstName("Michael");
employee.setLastName("Jordan");
when(employeeService.getAllEmployees()).thenReturn(List.of(employee));
when(employeeDetails.getAllEmployees()).thenReturn(List.of(employee));
mockMvc.perform(get("/api/employee/all")
.contentType(MediaType.APPLICATION_JSON))
@ -495,7 +464,7 @@ public class EmployeeControllerTest {
employee.setFirstName("Sophia");
employee.setLastName("Brown");
when(employeeService.getEmployeesBySecondarySkill(3L, 2)).thenReturn(List.of(employee));
when(employeeDetails.getEmployeesBySecondarySkill(3L, 2)).thenReturn(List.of(employee));
mockMvc.perform(get("/api/employee/from-skill")
.param("skillId", "3")
@ -551,7 +520,7 @@ public class EmployeeControllerTest {
employee3.setFirstName("Samus");
employee3.setLastName("Aran");
when(employeeService.getAllEmployees()).thenReturn(List.of(employee1, employee2, employee3));
when(employeeDetails.getAllEmployees()).thenReturn(List.of(employee1, employee2, employee3));
mockMvc.perform(get("/api/employee/all")
.contentType(MediaType.APPLICATION_JSON))
@ -577,7 +546,7 @@ public class EmployeeControllerTest {
employee.setDStart(LocalTime.of(22, 0)); // Beginnt um 22:00 Uhr
employee.setDEnd(LocalTime.of(6, 0)); // Endet um 6:00 Uhr
when(employeeService.getEmployeeByEid(50L)).thenReturn(Optional.of(employee));
when(employeeDetails.getEmployeeByEid(50L)).thenReturn(Optional.of(employee));
mockMvc.perform(get("/api/employee")
.param("id", "50")
@ -610,7 +579,7 @@ public class EmployeeControllerTest {
partTimeEmployee.setDStart(LocalTime.of(10, 0));
partTimeEmployee.setDEnd(LocalTime.of(14, 0));
when(employeeService.getAllEmployees()).thenReturn(List.of(fullTimeEmployee, partTimeEmployee));
when(employeeDetails.getAllEmployees()).thenReturn(List.of(fullTimeEmployee, partTimeEmployee));
mockMvc.perform(get("/api/employee/all")
.contentType(MediaType.APPLICATION_JSON))
@ -622,8 +591,6 @@ public class EmployeeControllerTest {
.andExpect(jsonPath("$[1].firstName").value("Bob"));
}
// Test PUT /api/employee/skill/level für ein Update auf das maximal erlaubte Level
@Test
public void testPutSkillLevel_MaxAllowedLevel() throws Exception {
@ -649,7 +616,7 @@ public class EmployeeControllerTest {
employee2.setFirstName("Jane");
employee2.setLastName("Expert");
when(employeeService.getEmployeesBySecondarySkill(5L, 5)).thenReturn(List.of(employee1, employee2));
when(employeeDetails.getEmployeesBySecondarySkill(5L, 5)).thenReturn(List.of(employee1, employee2));
mockMvc.perform(get("/api/employee/from-skill")
.param("skillId", "5")

View File

@ -1,7 +1,8 @@
package com.maradona.backend.controllers.api;
import com.maradona.backend.entities.FormOfAddress;
import com.maradona.backend.services.FormOfAddressService;
import com.maradona.backend.services.details.FormOfAddressDetails;
import com.maradona.backend.services.actions.FormOfAddressActions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
@ -13,7 +14,6 @@ import java.util.Optional;
import java.util.Arrays;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
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;
@ -26,7 +26,10 @@ public class FormOfAdressControllerTest {
private MockMvc mockMvc;
@MockBean
private FormOfAddressService formOfAdressService;
private FormOfAddressDetails formOfAdressService;
@MockBean
private FormOfAddressActions formOfAdressActions;
@Test
public void testGetFormOfAdressById() throws Exception {
@ -37,10 +40,10 @@ public class FormOfAdressControllerTest {
when(formOfAdressService.getFormOfAddressByFid(1L)).thenReturn(Optional.of(formOfAddress));
mockMvc.perform(get("/api/form-of-adress")
.param("id", "1")
.param("fid", "1")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$.id").value(1))
.andExpect(jsonPath("$.fid").value(1))
.andExpect(jsonPath("$.description").value("Mr."));
}
@ -59,9 +62,9 @@ public class FormOfAdressControllerTest {
mockMvc.perform(get("/api/form-of-adress/all")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$[0].id").value(1))
.andExpect(jsonPath("$[0].fid").value(1))
.andExpect(jsonPath("$[0].description").value("Mr."))
.andExpect(jsonPath("$[1].id").value(2))
.andExpect(jsonPath("$[1].fid").value(2))
.andExpect(jsonPath("$[1].description").value("Ms."));
}
@ -71,7 +74,7 @@ public class FormOfAdressControllerTest {
formOfAddress.setFid(1L);
formOfAddress.setDescription("Dr.");
when(formOfAdressService.saveFormOfAddress(any(FormOfAddress.class))).thenReturn(formOfAddress);
when(formOfAdressActions.saveFormOfAddress(any(FormOfAddress.class))).thenReturn(formOfAddress);
mockMvc.perform(post("/api/form-of-adress")
.contentType(MediaType.APPLICATION_JSON)
@ -88,7 +91,7 @@ public class FormOfAdressControllerTest {
when(formOfAdressService.getFormOfAddressByFid(1L)).thenReturn(Optional.of(existingForm));
mockMvc.perform(put("/api/form-of-adress")
.param("id", "1")
.param("fid", "1")
.contentType(MediaType.APPLICATION_JSON)
.content("\"Prof.\""))
.andExpect(status().isOk());
@ -99,7 +102,7 @@ public class FormOfAdressControllerTest {
when(formOfAdressService.getFormOfAddressByFid(1L)).thenReturn(Optional.empty());
mockMvc.perform(put("/api/form-of-adress")
.param("id", "1")
.param("fid", "1")
.contentType(MediaType.APPLICATION_JSON)
.content("\"Prof.\""))
.andExpect(status().isNotFound());
@ -114,7 +117,7 @@ public class FormOfAdressControllerTest {
when(formOfAdressService.getFormOfAddressByFid(1L)).thenReturn(Optional.of(formOfAddress));
mockMvc.perform(delete("/api/form-of-adress")
.param("id", "1")
.param("fid", "1")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk());
}
@ -124,7 +127,7 @@ public class FormOfAdressControllerTest {
when(formOfAdressService.getFormOfAddressByFid(1L)).thenReturn(Optional.empty());
mockMvc.perform(delete("/api/form-of-adress")
.param("id", "1")
.param("fid", "1")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isNotFound());
}

View File

@ -2,11 +2,15 @@ package com.maradona.backend.controllers.api;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.maradona.backend.entities.PrimarySkill;
import com.maradona.backend.services.PrimarySkillService;
import com.maradona.backend.services.SecondarySkillService;
import com.maradona.backend.services.actions.PrimarySkillActions;
import com.maradona.backend.services.details.PrimarySkillDetails;
import com.maradona.backend.services.details.SecondarySkillDetails;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.*;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
@ -23,10 +27,13 @@ public class PrimarySkillControllerTest {
private MockMvc mockMvc;
@Mock
private PrimarySkillService primarySkillService;
private PrimarySkillDetails primarySkillDetails;
@Mock
private SecondarySkillService secondarySkillService;
private PrimarySkillActions primarySkillActions;
@Mock
private SecondarySkillDetails secondarySkillDetails;
@InjectMocks
private PrimarySkillController primarySkillController;
@ -47,23 +54,23 @@ public class PrimarySkillControllerTest {
@Test
public void testGetPrimarySkillById_Success() throws Exception {
// Mock das Service, um ein PrimarySkill zu liefern
Mockito.when(primarySkillService.getPrimarySkillByPsid(1L)).thenReturn(Optional.of(primarySkill));
Mockito.when(primarySkillDetails.getPrimarySkillByPsid(1L)).thenReturn(Optional.of(primarySkill));
// Führe die GET-Anfrage aus
mockMvc.perform(MockMvcRequestBuilders.get("/api/primary-skill")
.param("id", "1"))
.param("pid", "1"))
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.jsonPath("$.psid").value(1))
.andExpect(MockMvcResultMatchers.jsonPath("$.description").value("Test Primary Skill"));
// Verifiziere, dass der Service aufgerufen wurde
Mockito.verify(primarySkillService, Mockito.times(1)).getPrimarySkillByPsid(1L);
Mockito.verify(primarySkillDetails, Mockito.times(1)).getPrimarySkillByPsid(1L);
}
@Test
public void testGetAllPrimarySkills() throws Exception {
// Mock das Service, um eine Liste von PrimarySkills zu liefern
Mockito.when(primarySkillService.getAllPrimarySkills()).thenReturn(List.of(primarySkill));
Mockito.when(primarySkillDetails.getAllPrimarySkills()).thenReturn(List.of(primarySkill));
// Führe die GET-Anfrage aus
mockMvc.perform(MockMvcRequestBuilders.get("/api/primary-skill/all"))
@ -72,31 +79,30 @@ public class PrimarySkillControllerTest {
.andExpect(MockMvcResultMatchers.jsonPath("$[0].description").value("Test Primary Skill"));
// Verifiziere, dass der Service aufgerufen wurde
Mockito.verify(primarySkillService, Mockito.times(1)).getAllPrimarySkills();
Mockito.verify(primarySkillDetails, Mockito.times(1)).getAllPrimarySkills();
}
@Test
public void testCreatePrimarySkill() throws Exception {
// Mock das Service, um das PrimarySkill zu speichern
Mockito.when(primarySkillService.savePrimarySkill(ArgumentMatchers.any(PrimarySkill.class))).thenReturn(primarySkill);
Mockito.when(primarySkillActions.savePrimarySkill(any(PrimarySkill.class))).thenReturn(primarySkill);
// Führe die POST-Anfrage aus
mockMvc.perform(MockMvcRequestBuilders.post("/api/primary-skill")
.contentType(MediaType.APPLICATION_JSON)
.content(new ObjectMapper().writeValueAsString(primarySkill))) // Hier wird das PrimarySkill als JSON übermittelt
.andExpect(MockMvcResultMatchers.status().isCreated()) // Erwartet den Statuscode 201 Created
.andExpect(MockMvcResultMatchers.jsonPath("$.psid").value(1)) // Überprüft das psid in der Antwort
.andExpect(MockMvcResultMatchers.jsonPath("$.description").value("Test Primary Skill")); // Überprüft die Beschreibung
.andExpect(MockMvcResultMatchers.status().isCreated())
.andExpect(MockMvcResultMatchers.jsonPath("$.psid").value(1))
.andExpect(MockMvcResultMatchers.jsonPath("$.description").value("Test Primary Skill"));
// Verifiziere, dass der Service aufgerufen wurde
Mockito.verify(primarySkillService, Mockito.times(1)).savePrimarySkill(ArgumentMatchers.any(PrimarySkill.class));
Mockito.verify(primarySkillActions, Mockito.times(1)).savePrimarySkill(any(PrimarySkill.class));
}
@Test
public void testUpdatePrimarySkill() throws Exception {
// Mock das Service, um das PrimarySkill zu speichern
Mockito.when(primarySkillService.savePrimarySkill(ArgumentMatchers.any(PrimarySkill.class))).thenReturn(primarySkill);
Mockito.when(primarySkillActions.savePrimarySkill(any(PrimarySkill.class))).thenReturn(primarySkill);
// Führe die PUT-Anfrage aus
mockMvc.perform(MockMvcRequestBuilders.put("/api/primary-skill")
@ -107,34 +113,34 @@ public class PrimarySkillControllerTest {
.andExpect(MockMvcResultMatchers.jsonPath("$.description").value("Test Primary Skill"));
// Verifiziere, dass der Service aufgerufen wurde
Mockito.verify(primarySkillService, Mockito.times(1)).savePrimarySkill(ArgumentMatchers.any(PrimarySkill.class));
Mockito.verify(primarySkillActions, Mockito.times(1)).savePrimarySkill(any(PrimarySkill.class));
}
@Test
public void testDeletePrimarySkill_Success() throws Exception {
// Mock das Service, um das PrimarySkill zu liefern
Mockito.when(primarySkillService.getPrimarySkillByPsid(1L)).thenReturn(Optional.of(primarySkill));
Mockito.when(primarySkillDetails.getPrimarySkillByPsid(1L)).thenReturn(Optional.of(primarySkill));
// Führe die DELETE-Anfrage aus
mockMvc.perform(MockMvcRequestBuilders.delete("/api/primary-skill")
.param("id", "1"))
.param("pid", "1"))
.andExpect(MockMvcResultMatchers.status().isNoContent());
// Verifiziere, dass der Service aufgerufen wurde
Mockito.verify(primarySkillService, Mockito.times(1)).deletePrimarySkill(1L);
Mockito.verify(primarySkillActions, Mockito.times(1)).deletePrimarySkill(1L);
}
@Test
public void testDeletePrimarySkill_NotFound() throws Exception {
// Mock das Service, um das PrimarySkill nicht zu finden
Mockito.when(primarySkillService.getPrimarySkillByPsid(1L)).thenReturn(Optional.empty());
Mockito.when(primarySkillDetails.getPrimarySkillByPsid(1L)).thenReturn(Optional.empty());
// Führe die DELETE-Anfrage aus
mockMvc.perform(MockMvcRequestBuilders.delete("/api/primary-skill")
.param("id", "1"))
.param("pid", "1"))
.andExpect(MockMvcResultMatchers.status().isNotFound());
// Verifiziere, dass der Service nicht aufgerufen wurde
Mockito.verify(primarySkillService, Mockito.never()).deletePrimarySkill(1L);
Mockito.verify(primarySkillActions, Mockito.never()).deletePrimarySkill(1L);
}
}

View File

@ -1,9 +1,10 @@
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 com.maradona.backend.services.details.ProjectDetails;
import com.maradona.backend.services.actions.ProjectActions;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
@ -15,24 +16,24 @@ 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;
private ProjectDetails projectService;
@MockBean
private ProjectActions projectActions;
@Test
public void testGetProjectById() throws Exception {
//Arrange: Mock an project
Project project = new Project();
project.setPid(1L);
project.setName("Skillmanagementsystem erstellen");
@ -41,16 +42,13 @@ public class ProjectControllerTest {
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.getProjectByPid(1L)).thenReturn(Optional.of(project));
Mockito.when(projectService.getProjectByPid(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")
.param("psid", "1")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$.id").value(1))
.andExpect(jsonPath("$.pid").value(1))
.andExpect(jsonPath("$.name").value("Skillmanagementsystem erstellen"))
.andExpect(jsonPath("$.startDate").value("2024-11-08"))
.andExpect(jsonPath("$.endDate").value("2024-11-20"))
@ -60,14 +58,13 @@ public class ProjectControllerTest {
@Test
public void testGetAllProjects() throws Exception {
//Arrange: Creat a list of mock projects
Project project = new Project();
project.setPid(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 project1 = new Project();
project1.setPid(1L);
project1.setName("Skillmanagementsystem erstellen");
project1.setStartDate(LocalDate.of(2024, 11, 8));
project1.setEndDate(LocalDate.of(2024, 11, 20));
project1.setWorkload(12);
project1.setDescription("Skillmanagementsystem erstellen für die Firma");
Project project2 = new Project();
project2.setPid(2L);
@ -77,20 +74,18 @@ public class ProjectControllerTest {
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));
Mockito.when(projectService.getAllProjects()).thenReturn(Arrays.asList(project1, 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].pid").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].pid").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"))
@ -98,76 +93,23 @@ public class ProjectControllerTest {
.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.setPid(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.setPid(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 a specific project when each ID is requested
when(projectService.getProjectByPid(1L)).thenReturn(Optional.of(project1));
when(projectService.getProjectByPid(2L)).thenReturn(Optional.of(project2));
// Act & Assert: Send GET requests for each project and verify the response
mockMvc.perform(get("/api/project")
.param("id", "1")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$.pid").value(1))
.andExpect(jsonPath("$.name").value("Skill Management System"))
.andExpect(jsonPath("$.startDate").value("2024-11-08"))
.andExpect(jsonPath("$.endDate").value("2024-11-20"))
.andExpect(jsonPath("$.workload").value(12))
.andExpect(jsonPath("$.description").value("Create a skill management system for the company"));
mockMvc.perform(get("/api/project")
.param("id", "2")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$.pid").value(2))
.andExpect(jsonPath("$.name").value("Project Management Tool"))
.andExpect(jsonPath("$.startDate").value("2024-12-01"))
.andExpect(jsonPath("$.endDate").value("2024-12-15"))
.andExpect(jsonPath("$.workload").value(10))
.andExpect(jsonPath("$.description").value("Develop a project management tool"));
}
@Test
public void testCreateProject() throws Exception {
// Arrange: Create a new project and set all required fields
Project savedProject = new Project();
savedProject.setPid(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");
Project project = new Project();
project.setPid(1L);
project.setName("New Project");
project.setStartDate(LocalDate.of(2024, 11, 10));
project.setEndDate(LocalDate.of(2024, 12, 10));
project.setWorkload(15);
project.setDescription("A new project for testing");
// Mocking the saveProject method to return this specific project
when(projectService.saveProject(any(Project.class))).thenReturn(savedProject);
Mockito.when(projectActions.saveProject(any(Project.class))).thenReturn(project);
// 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("$.pid").value(1))
.andExpect(jsonPath("$.name").value("New Project"))
.andExpect(jsonPath("$.workload").value(15))
.andExpect(jsonPath("$.description").value("A new project for testing"));
@ -175,7 +117,6 @@ public class ProjectControllerTest {
@Test
public void testUpdateProject() throws Exception {
// Arrange: Create an existing project
Project project = new Project();
project.setPid(1L);
project.setName("Updated Project");
@ -184,15 +125,13 @@ public class ProjectControllerTest {
project.setWorkload(20);
project.setDescription("An updated project description");
// Mock the ProjectService to return the updated project
when(projectService.saveProject(project)).thenReturn(project);
Mockito.when(projectActions.saveProject(any(Project.class))).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\"}"))
.content("{\"pid\":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("$.pid").value(1))
.andExpect(jsonPath("$.name").value("Updated Project"))
.andExpect(jsonPath("$.workload").value(20))
.andExpect(jsonPath("$.description").value("An updated project description"));
@ -200,15 +139,13 @@ public class ProjectControllerTest {
@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))
.param("pid", String.valueOf(projectId))
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isNoContent());
Mockito.verify(projectActions, Mockito.times(1)).deleteProject(projectId);
}
}

View File

@ -1,20 +1,26 @@
package com.maradona.backend.controllers.api;
import com.maradona.backend.entities.SecondarySkill;
import com.maradona.backend.services.SecondarySkillService;
import com.maradona.backend.services.actions.SecondarySkillActions;
import com.maradona.backend.services.details.SecondarySkillDetails;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.junit.jupiter.api.BeforeEach;
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 org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
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;
@ -25,7 +31,15 @@ public class SecondarySkillControllerTest {
private MockMvc mockMvc;
@MockBean
private SecondarySkillService secondarySkillService;
private SecondarySkillDetails secondarySkillService;
@MockBean
private SecondarySkillActions secondarySkillActions;
@BeforeEach
public void setup() {
MockitoAnnotations.openMocks(this);
}
@Test
public void testGetSecondarySkillById() throws Exception {
@ -35,8 +49,8 @@ public class SecondarySkillControllerTest {
when(secondarySkillService.getSecondarySkillBySsid(1L)).thenReturn(Optional.of(skill));
mockMvc.perform(get("/api/secondary-skill")
.param("id", "1")
mockMvc.perform(MockMvcRequestBuilders.get("/api/secondary-skill")
.param("ssid", "1")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$.ssid").value(1))
@ -55,13 +69,13 @@ public class SecondarySkillControllerTest {
when(secondarySkillService.getAllSecondarySkills()).thenReturn(Arrays.asList(skill1, skill2));
mockMvc.perform(get("/api/secondary-skill/all")
mockMvc.perform(MockMvcRequestBuilders.get("/api/secondary-skill/all")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$[1].ssid").value(1))
.andExpect(jsonPath("$[1].description").value("Java Programming"))
.andExpect(jsonPath("$[2].ssid").value(2))
.andExpect(jsonPath("$[2].description").value("Python Programming"));
.andExpect(jsonPath("$[0].ssid").value(1))
.andExpect(jsonPath("$[0].description").value("Java Programming"))
.andExpect(jsonPath("$[1].ssid").value(2))
.andExpect(jsonPath("$[1].description").value("Python Programming"));
}
@Test
@ -76,14 +90,14 @@ public class SecondarySkillControllerTest {
when(secondarySkillService.getSecondarySkillsByPrimarySkillId(100L)).thenReturn(Arrays.asList(skill1, skill2));
mockMvc.perform(get("/api/secondary-skill/from-primary-skill")
.param("primarySkillId", "100")
mockMvc.perform(MockMvcRequestBuilders.get("/api/secondary-skill/from-primary-skill")
.param("psid", "100")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$[1].ssid").value(1))
.andExpect(jsonPath("$[1].description").value("Data Analysis"))
.andExpect(jsonPath("$[2].ssid").value(2))
.andExpect(jsonPath("$[2].description").value("Data Visualization"));
.andExpect(jsonPath("$[0].ssid").value(1))
.andExpect(jsonPath("$[0].description").value("Data Analysis"))
.andExpect(jsonPath("$[1].ssid").value(2))
.andExpect(jsonPath("$[1].description").value("Data Visualization"));
}
@Test
@ -92,11 +106,11 @@ public class SecondarySkillControllerTest {
skill.setSsid(1L);
skill.setDescription("Machine Learning");
when(secondarySkillService.saveSecondarySkill(any(SecondarySkill.class))).thenReturn(skill);
when(secondarySkillActions.saveSecondarySkill(any(SecondarySkill.class))).thenReturn(skill);
mockMvc.perform(post("/api/secondary-skill")
mockMvc.perform(MockMvcRequestBuilders.post("/api/secondary-skill")
.contentType(MediaType.APPLICATION_JSON)
.content("{\"name\":\"Machine Learning\"}"))
.content("{\"description\":\"Machine Learning\"}"))
.andExpect(status().isCreated())
.andExpect(jsonPath("$.ssid").value(1))
.andExpect(jsonPath("$.description").value("Machine Learning"));
@ -108,9 +122,9 @@ public class SecondarySkillControllerTest {
updatedSkill.setSsid(1L);
updatedSkill.setDescription("Advanced Machine Learning");
when(secondarySkillService.saveSecondarySkill(any(SecondarySkill.class))).thenReturn(updatedSkill);
when(secondarySkillActions.saveSecondarySkill(any(SecondarySkill.class))).thenReturn(updatedSkill);
mockMvc.perform(put("/api/secondary-skill")
mockMvc.perform(MockMvcRequestBuilders.put("/api/secondary-skill")
.contentType(MediaType.APPLICATION_JSON)
.content("{\"ssid\":1,\"description\":\"Advanced Machine Learning\"}"))
.andExpect(status().isOk())
@ -122,8 +136,8 @@ public class SecondarySkillControllerTest {
public void testDeleteSecondarySkill() throws Exception {
Long skillId = 1L;
mockMvc.perform(delete("/api/secondary-skill")
.param("id", String.valueOf(skillId))
mockMvc.perform(MockMvcRequestBuilders.delete("/api/secondary-skill")
.param("ssid", String.valueOf(skillId))
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isNoContent());
}