test: Adding new test for the controller

pull/1/head
mohammad 2024-11-11 14:41:01 +01:00
parent b869327381
commit f1ba3228e2
5 changed files with 1283 additions and 765 deletions

View File

@ -1,171 +1,674 @@
//package com.maradona.backend.controllers.api; package com.maradona.backend.controllers.api;
//
//import com.maradona.backend.controllers.api.EmployeeController; import com.maradona.backend.controllers.api.EmployeeController;
//import com.maradona.backend.entities.Employee; import com.maradona.backend.entities.Employee;
//import com.maradona.backend.services.EmployeeService; import com.maradona.backend.services.EmployeeService;
//import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
//import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
//import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.boot.test.mock.mockito.MockBean;
//import org.springframework.http.MediaType; import org.springframework.http.MediaType;
//import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MockMvc;
//
//import java.time.LocalTime; import java.time.LocalTime;
//import java.util.List; import java.util.List;
//import java.util.Optional; import java.util.Optional;
//
//import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
//import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; 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.status;
//import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
//
//@WebMvcTest(EmployeeController.class) @WebMvcTest(EmployeeController.class)
//public class EmployeeControllerTest { public class EmployeeControllerTest {
//
// @Autowired @Autowired
// private MockMvc mockMvc; private MockMvc mockMvc;
//
// @MockBean @MockBean
// private EmployeeService employeeService; private EmployeeService employeeService;
//
@Test
public void testGetEmployeeById() throws Exception {
// Arrange: Mock an employee
Employee employee = new Employee();
employee.setEid(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.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("$.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"));
}
//Test if an employee is not found (404 Not Found)
@Test
public void testGetEmployeeByIdNotFound() throws Exception {
//Arrange
when(employeeService.getEmployeeByEid(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 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.setEid(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 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());
}
// Test GET /api/employee with a valid ID but the employee has no secondary skills
@Test
public void testGetEmployeeById_NoSecondarySkills() throws Exception {
Employee employee = new Employee();
employee.setEid(1L);
employee.setEmployeeNr(123);
employee.setFirstName("John");
employee.setLastName("Doe");
when(employeeService.getEmployeeByEid(1L)).thenReturn(Optional.of(employee));
mockMvc.perform(get("/api/employee")
.param("id", "1")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$.eid").value(1))
.andExpect(jsonPath("$.employeeNr").value(123))
.andExpect(jsonPath("$.firstName").value("John"))
.andExpect(jsonPath("$.lastName").value("Doe"))
.andExpect(jsonPath("$.secondarySkills").doesNotExist());
}
// Test GET /api/employee/all with employees having varied skill levels
@Test
public void testGetAllEmployees_WithVariedSkills() throws Exception {
Employee employee1 = new Employee();
employee1.setEid(1L);
employee1.setEmployeeNr(123);
employee1.setFirstName("Alice");
employee1.setLastName("Smith");
Employee employee2 = new Employee();
employee2.setEid(2L);
employee2.setEmployeeNr(124);
employee2.setFirstName("Bob");
employee2.setLastName("Johnson");
when(employeeService.getAllEmployees()).thenReturn(List.of(employee1, employee2));
mockMvc.perform(get("/api/employee/all")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$[0].eid").value(1))
.andExpect(jsonPath("$[0].firstName").value("Alice"))
.andExpect(jsonPath("$[1].eid").value(2))
.andExpect(jsonPath("$[1].firstName").value("Bob"));
}
// Test PUT /api/employee/skill/level to ensure updating to the minimum level
@Test
public void testPutSkillLevel_MinLevel() throws Exception {
mockMvc.perform(put("/api/employee/skill/level")
.param("skillId", "1")
.param("level", "1")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk());
}
// Test DELETE /api/employee/skill by calling delete on an existing skill
@Test
public void testDeleteExistingSecondarySkill() throws Exception {
mockMvc.perform(delete("/api/employee/skill")
.param("id", "1")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isNoContent());
}
// Test GET /api/employee/from-skill with employees at boundary skill levels
@Test
public void testGetEmployeesBySecondarySkill_BoundaryLevels() throws Exception {
Employee employee = new Employee();
employee.setEid(1L);
employee.setEmployeeNr(123);
employee.setFirstName("John");
employee.setLastName("Doe");
when(employeeService.getEmployeesBySecondarySkill(1L, 1)).thenReturn(List.of(employee));
when(employeeService.getEmployeesBySecondarySkill(1L, 5)).thenReturn(List.of(employee));
// Test with minimum level
mockMvc.perform(get("/api/employee/from-skill")
.param("skillId", "1")
.param("level", "1")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$[0].eid").value(1))
.andExpect(jsonPath("$[0].employeeNr").value(123));
// Test with maximum level
mockMvc.perform(get("/api/employee/from-skill")
.param("skillId", "1")
.param("level", "5")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$[0].eid").value(1))
.andExpect(jsonPath("$[0].employeeNr").value(123));
}
// Test GET /api/employee with an ID at the upper limit of Long values
@Test
public void testGetEmployeeById_MaxLongValue() throws Exception {
Employee employee = new Employee();
employee.setEid(Long.MAX_VALUE);
employee.setEmployeeNr(99999);
employee.setFirstName("Max");
employee.setLastName("Value");
when(employeeService.getEmployeeByEid(Long.MAX_VALUE)).thenReturn(Optional.of(employee));
mockMvc.perform(get("/api/employee")
.param("id", String.valueOf(Long.MAX_VALUE))
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$.eid").value(Long.MAX_VALUE))
.andExpect(jsonPath("$.firstName").value("Max"))
.andExpect(jsonPath("$.lastName").value("Value"));
}
// 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());
mockMvc.perform(get("/api/employee")
.param("id", String.valueOf(Long.MAX_VALUE))
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isNotFound());
}
// // Test GET /api/employee/all mit einer großen Anzahl von Mitarbeitern
// @Test // @Test
// public void testGetEmployeeById() throws Exception { // public void testGetAllEmployees_LargeDataset() throws Exception {
// // Arrange: Mock an employee
// Employee employee = new Employee();
// employee.setEid(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.getEmployeeByEid(1L)).thenReturn(Optional.of(employee));
// //
// // Act & Assert: Send GET request and expect a 200 OK status and JSON response // when(employeeService.getAllEmployees()).thenReturn(largeEmployeeList);
// mockMvc.perform(get("/api/employee") //
// .param("id", "1") // mockMvc.perform(get("/api/employee/all")
// .contentType(MediaType.APPLICATION_JSON)) // .contentType(MediaType.APPLICATION_JSON))
// .andExpect(status().isOk()) // .andExpect(status().isOk())
// .andExpect(jsonPath("$.id").value(1)) // .andExpect(jsonPath("$.length()").value(largeEmployeeList.size()));
// .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"));
// } // }
//
// //Test if an employee is not found (404 Not Found) // Test GET /api/employee/from-skill ohne level Parameter
// @Test @Test
// public void testGetEmployeeByIdNotFound() throws Exception { public void testGetEmployeesBySecondarySkill_MissingLevelParameter() throws Exception {
// //Arrange mockMvc.perform(get("/api/employee/from-skill")
// when(employeeService.getEmployeeByEid(999L)).thenReturn(Optional.empty()); .param("skillId", "1")
// .contentType(MediaType.APPLICATION_JSON))
// //Act & Assert: Send GET request and expect a 404 Not Found status .andExpect(status().isBadRequest());
// mockMvc.perform(get("/api/employee") }
// .param("id", "999")
// .contentType(MediaType.APPLICATION_JSON))
// .andExpect(status().isNotFound());
// } // Test PUT /api/employee/skill/level mit nicht numerischem Skill ID-Wert
// @Test
// public void testPutSkillLevel_InvalidSkillIdFormat() throws Exception {
// //Testing the getAll method for all employees mockMvc.perform(put("/api/employee/skill/level")
// @Test .param("skillId", "abc") // ungültiger Skill ID-Wert
// public void testGetAllEmployees() throws Exception { .param("level", "3")
// //Arrange: Mock a list of employees .contentType(MediaType.APPLICATION_JSON))
// Employee employee1 = new Employee(); .andExpect(status().isBadRequest());
// employee1.setEid(1L); }
// employee1.setEmployeeNr(123);
// employee1.setFirstName("Mohammad"); // Test DELETE /api/employee/skill ohne id Parameter
// employee1.setLastName("Hawrami"); @Test
// public void testDeleteSecondarySkill_MissingIdParameter() throws Exception {
// Employee employee2 = new Employee(); mockMvc.perform(delete("/api/employee/skill")
// employee2.setEid(2L); .contentType(MediaType.APPLICATION_JSON))
// employee2.setEmployeeNr(124); .andExpect(status().isBadRequest());
// employee2.setFirstName("Tarik"); }
// employee2.setLastName("Gökmen");
//
// when(employeeService.getAllEmployees()).thenReturn(List.of(employee1, employee2));
// // Test PUT /api/employee/skill/level mit maximal gültigem Level
// //Act & Assert: Send GET request and expect a 200 OK status and JSON array response @Test
// mockMvc.perform(get("/api/employees/all") public void testPutSkillLevel_MaxLevel() throws Exception {
// .contentType(MediaType.APPLICATION_JSON)) mockMvc.perform(put("/api/employee/skill/level")
// .andExpect(status().isOk()) .param("skillId", "1")
// .andExpect(jsonPath("$[0].employeeNr").value(1)) .param("level", "5") // angenommen, 5 ist das maximale Level
// .andExpect(jsonPath("$[1].employeeNr").value(2)); .contentType(MediaType.APPLICATION_JSON))
// } .andExpect(status().isOk());
// }
//
// //Testing the getFromSkill method with a specific Skilld and level // Test GET /api/employee/all mit leeren Rückgabedaten
// @Test @Test
// public void testGetEmployeesBySecondarySkill() throws Exception { public void testGetAllEmployees_EmptyList() throws Exception {
// //Arrange: Mock a list of employees with specific skills when(employeeService.getAllEmployees()).thenReturn(List.of());
// Employee employee = new Employee();
// employee.setEid(1L); mockMvc.perform(get("/api/employee/all")
// employee.setEmployeeNr(123); .contentType(MediaType.APPLICATION_JSON))
// employee.setFirstName("Mohammad"); .andExpect(status().isOk())
// employee.setLastName("Hawrami"); .andExpect(jsonPath("$").isEmpty());
// }
// when(employeeService.getEmployeesBySecondarySkill(1L,2)).thenReturn(List.of(employee));
//
// //Act & Assert: Send GET request and expect a 200 OK status and JSON array response // Test GET /api/employee mit einem Mitarbeiter mit vollständigen Details und spezifischen Arbeitszeiten
// mockMvc.perform(get("/api/employee/from-skill") @Test
// .param("skillId", "1") public void testGetEmployeeById_WithFullDetails() throws Exception {
// .param("level", "2") Employee employee = new Employee();
// .contentType(MediaType.APPLICATION_JSON)) employee.setEid(10L);
// .andExpect(status().isOk()) employee.setEmployeeNr(1010);
// .andExpect(jsonPath("$[0].id").value(1)) employee.setFirstName("Alice");
// .andExpect(jsonPath("$[0].employeeNr").value(123)); employee.setLastName("Wonderland");
// } employee.setDStart(LocalTime.of(8, 30));
// employee.setDEnd(LocalTime.of(16, 30));
//
// //Testing the postSkillPrototype method with a valid SkillPrototype object when(employeeService.getEmployeeByEid(10L)).thenReturn(Optional.of(employee));
// @Test
// public void testPostSkillPrototype() throws Exception { mockMvc.perform(get("/api/employee")
// //Arrange: Create a SkillPrototype JSON payload .param("id", "10")
// String skillPrototypeJson = "{\"skillId\":\"1\",\"level\":\"3\"}"; .contentType(MediaType.APPLICATION_JSON))
// .andExpect(status().isOk())
// //Act & Assert: Send POST request and expect a 201 Creat status .andExpect(jsonPath("$.eid").value(10))
// mockMvc.perform(post("/api/employee/skill/prototype") .andExpect(jsonPath("$.employeeNr").value(1010))
// .content(skillPrototypeJson) .andExpect(jsonPath("$.firstName").value("Alice"))
// .contentType(MediaType.APPLICATION_JSON)) .andExpect(jsonPath("$.lastName").value("Wonderland"))
// .andExpect(status().isCreated()); .andExpect(jsonPath("$.dStart").value("08:30:00"))
// } .andExpect(jsonPath("$.dEnd").value("16:30:00"));
// }
//
// //Testing the putSkillLevel method to update an existing level // Test GET /api/employee/all mit mehreren Einträgen
// @Test @Test
// public void testPutSkillLevel() throws Exception { public void testGetAllEmployees_WithMultipleEntries() throws Exception {
// // Act & Assert: Send PUT request and expect a 200 OK status Employee employee1 = new Employee();
// mockMvc.perform(put("/api/employee/skill/level") employee1.setEid(1L);
// .param("skillId", "1") employee1.setEmployeeNr(100);
// .param("level", "5") employee1.setFirstName("John");
// .contentType(MediaType.APPLICATION_JSON)) employee1.setLastName("Doe");
// .andExpect(status().isOk());
// } Employee employee2 = new Employee();
// employee2.setEid(2L);
// employee2.setEmployeeNr(101);
// //Testing the delete method for removing a secondary skill java employee2.setFirstName("Jane");
// @Test employee2.setLastName("Doe");
// public void testDeleteSecondarySkill() throws Exception {
// // Act & Assert: Send DELETE request and expect a 204 No Content status Employee employee3 = new Employee();
// mockMvc.perform(delete("/api/employee/skill") employee3.setEid(3L);
// .param("id", "1") employee3.setEmployeeNr(102);
// .contentType(MediaType.APPLICATION_JSON)) employee3.setFirstName("Jake");
// .andExpect(status().isNoContent()); employee3.setLastName("Smith");
// }
// when(employeeService.getAllEmployees()).thenReturn(List.of(employee1, employee2, employee3));
//
// //Testing the postSkillPrototype methode with invalid payload (e.g., no Skilld) mockMvc.perform(get("/api/employee/all")
// @Test .contentType(MediaType.APPLICATION_JSON))
// public void testPostSkillPrototype_BadRequest() throws Exception { .andExpect(status().isOk())
// // Arrange: Create an invalid JSON payload (missing skillId) .andExpect(jsonPath("$.length()").value(3))
// String invalidSkillPrototypeJson = "{\"level\":3}"; .andExpect(jsonPath("$[0].eid").value(1))
// .andExpect(jsonPath("$[1].eid").value(2))
// // Act & Assert: Send POST request and expect a 400 Bad Request status .andExpect(jsonPath("$[2].eid").value(3))
// mockMvc.perform(post("/api/employee/skill/prototype") .andExpect(jsonPath("$[0].firstName").value("John"))
// .content(invalidSkillPrototypeJson) .andExpect(jsonPath("$[1].firstName").value("Jane"))
// .contentType(MediaType.APPLICATION_JSON)) .andExpect(jsonPath("$[2].firstName").value("Jake"));
// .andExpect(status().isBadRequest()); }
// }
//} // Test POST /api/employee/skill/prototype für einen Prototyp mit hohem Levelwert
@Test
public void testPostSkillPrototype_HighLevel() throws Exception {
String skillPrototypeJson = "{\"skillId\":\"2\",\"level\":\"4\"}";
mockMvc.perform(post("/api/employee/skill/prototype")
.content(skillPrototypeJson)
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isCreated());
}
// Test GET /api/employee/from-skill mit einem spezifischen Skill-Level und mehreren Mitarbeitern
@Test
public void testGetEmployeesBySecondarySkill_WithMultipleMatchingEmployees() throws Exception {
Employee employee1 = new Employee();
employee1.setEid(4L);
employee1.setEmployeeNr(200);
employee1.setFirstName("Emma");
employee1.setLastName("Brown");
Employee employee2 = new Employee();
employee2.setEid(5L);
employee2.setEmployeeNr(201);
employee2.setFirstName("Liam");
employee2.setLastName("Wilson");
when(employeeService.getEmployeesBySecondarySkill(2L, 3)).thenReturn(List.of(employee1, employee2));
mockMvc.perform(get("/api/employee/from-skill")
.param("skillId", "2")
.param("level", "3")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$.length()").value(2))
.andExpect(jsonPath("$[0].eid").value(4))
.andExpect(jsonPath("$[1].eid").value(5))
.andExpect(jsonPath("$[0].firstName").value("Emma"))
.andExpect(jsonPath("$[1].firstName").value("Liam"));
}
// Test PUT /api/employee/skill/level mit minimalem Levelwert
@Test
public void testPutSkillLevel_MinimumLevel() throws Exception {
mockMvc.perform(put("/api/employee/skill/level")
.param("skillId", "3")
.param("level", "1")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk());
}
// Test DELETE /api/employee/skill für einen vorhandenen Skill
@Test
public void testDeleteSecondarySkill_WithValidSkillId() throws Exception {
mockMvc.perform(delete("/api/employee/skill")
.param("id", "2")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isNoContent());
}
// Test GET /api/employee für einen Mitarbeiter mit spezifischen Details
@Test
public void testGetEmployeeById_WithSpecificDetails() throws Exception {
Employee employee = new Employee();
employee.setEid(15L);
employee.setEmployeeNr(555);
employee.setFirstName("Evelyn");
employee.setLastName("Hall");
employee.setDStart(LocalTime.of(7, 45));
employee.setDEnd(LocalTime.of(15, 45));
when(employeeService.getEmployeeByEid(15L)).thenReturn(Optional.of(employee));
mockMvc.perform(get("/api/employee")
.param("id", "15")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$.eid").value(15))
.andExpect(jsonPath("$.employeeNr").value(555))
.andExpect(jsonPath("$.firstName").value("Evelyn"))
.andExpect(jsonPath("$.lastName").value("Hall"))
.andExpect(jsonPath("$.dStart").value("07:45:00"))
.andExpect(jsonPath("$.dEnd").value("15:45:00"));
}
// Test GET /api/employee/all mit einer einzelnen Eintragung
@Test
public void testGetAllEmployees_WithSingleEntry() throws Exception {
Employee employee = new Employee();
employee.setEid(20L);
employee.setEmployeeNr(202);
employee.setFirstName("Michael");
employee.setLastName("Jordan");
when(employeeService.getAllEmployees()).thenReturn(List.of(employee));
mockMvc.perform(get("/api/employee/all")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$.length()").value(1))
.andExpect(jsonPath("$[0].eid").value(20))
.andExpect(jsonPath("$[0].employeeNr").value(202))
.andExpect(jsonPath("$[0].firstName").value("Michael"))
.andExpect(jsonPath("$[0].lastName").value("Jordan"));
}
// Test GET /api/employee/from-skill für ein Skill-Level mit nur einem passenden Mitarbeiter
@Test
public void testGetEmployeesBySecondarySkill_SingleEmployee() throws Exception {
Employee employee = new Employee();
employee.setEid(30L);
employee.setEmployeeNr(303);
employee.setFirstName("Sophia");
employee.setLastName("Brown");
when(employeeService.getEmployeesBySecondarySkill(3L, 2)).thenReturn(List.of(employee));
mockMvc.perform(get("/api/employee/from-skill")
.param("skillId", "3")
.param("level", "2")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$.length()").value(1))
.andExpect(jsonPath("$[0].eid").value(30))
.andExpect(jsonPath("$[0].employeeNr").value(303))
.andExpect(jsonPath("$[0].firstName").value("Sophia"))
.andExpect(jsonPath("$[0].lastName").value("Brown"));
}
// Test PUT /api/employee/skill/level für eine existierende Fähigkeit und mittleres Level
@Test
public void testPutSkillLevel_MidLevel() throws Exception {
mockMvc.perform(put("/api/employee/skill/level")
.param("skillId", "7")
.param("level", "3")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk());
}
// Test DELETE /api/employee/skill für einen existierenden Skill, um sicherzustellen, dass der Skill gelöscht wird
@Test
public void testDeleteSecondarySkill_ValidSkill() throws Exception {
mockMvc.perform(delete("/api/employee/skill")
.param("id", "3")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isNoContent());
}
// Test GET /api/employee/all mit mehreren Einträgen, die verschiedene Abteilungen repräsentieren könnten
@Test
public void testGetAllEmployees_WithDepartmentVariety() throws Exception {
Employee employee1 = new Employee();
employee1.setEid(100L);
employee1.setEmployeeNr(300);
employee1.setFirstName("Lara");
employee1.setLastName("Croft");
Employee employee2 = new Employee();
employee2.setEid(101L);
employee2.setEmployeeNr(301);
employee2.setFirstName("Nathan");
employee2.setLastName("Drake");
Employee employee3 = new Employee();
employee3.setEid(102L);
employee3.setEmployeeNr(302);
employee3.setFirstName("Samus");
employee3.setLastName("Aran");
when(employeeService.getAllEmployees()).thenReturn(List.of(employee1, employee2, employee3));
mockMvc.perform(get("/api/employee/all")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$.length()").value(3))
.andExpect(jsonPath("$[0].eid").value(100))
.andExpect(jsonPath("$[1].eid").value(101))
.andExpect(jsonPath("$[2].eid").value(102))
.andExpect(jsonPath("$[0].firstName").value("Lara"))
.andExpect(jsonPath("$[1].firstName").value("Nathan"))
.andExpect(jsonPath("$[2].firstName").value("Samus"));
}
// Test GET /api/employee für einen Mitarbeiter mit speziellen Arbeitszeiten (z.B. Nachtarbeit)
@Test
public void testGetEmployeeById_NightShift() throws Exception {
Employee employee = new Employee();
employee.setEid(50L);
employee.setEmployeeNr(505);
employee.setFirstName("Tom");
employee.setLastName("Night");
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));
mockMvc.perform(get("/api/employee")
.param("id", "50")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$.eid").value(50))
.andExpect(jsonPath("$.employeeNr").value(505))
.andExpect(jsonPath("$.firstName").value("Tom"))
.andExpect(jsonPath("$.lastName").value("Night"))
.andExpect(jsonPath("$.dStart").value("22:00:00"))
.andExpect(jsonPath("$.dEnd").value("06:00:00"));
}
// Test GET /api/employee/all mit einer Kombination aus Vollzeit und Teilzeitmitarbeitern
@Test
public void testGetAllEmployees_FullAndPartTime() throws Exception {
Employee fullTimeEmployee = new Employee();
fullTimeEmployee.setEid(60L);
fullTimeEmployee.setEmployeeNr(606);
fullTimeEmployee.setFirstName("Alice");
fullTimeEmployee.setLastName("Fulltime");
fullTimeEmployee.setDStart(LocalTime.of(9, 0));
fullTimeEmployee.setDEnd(LocalTime.of(17, 0));
Employee partTimeEmployee = new Employee();
partTimeEmployee.setEid(61L);
partTimeEmployee.setEmployeeNr(607);
partTimeEmployee.setFirstName("Bob");
partTimeEmployee.setLastName("Parttime");
partTimeEmployee.setDStart(LocalTime.of(10, 0));
partTimeEmployee.setDEnd(LocalTime.of(14, 0));
when(employeeService.getAllEmployees()).thenReturn(List.of(fullTimeEmployee, partTimeEmployee));
mockMvc.perform(get("/api/employee/all")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$.length()").value(2))
.andExpect(jsonPath("$[0].eid").value(60))
.andExpect(jsonPath("$[1].eid").value(61))
.andExpect(jsonPath("$[0].firstName").value("Alice"))
.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 {
mockMvc.perform(put("/api/employee/skill/level")
.param("skillId", "10")
.param("level", "5") // Maximal erlaubtes Level
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk());
}
// Test GET /api/employee/from-skill mit mehreren Mitarbeitern und hohem Level
@Test
public void testGetEmployeesBySecondarySkill_HighLevelMultipleEmployees() throws Exception {
Employee employee1 = new Employee();
employee1.setEid(70L);
employee1.setEmployeeNr(700);
employee1.setFirstName("John");
employee1.setLastName("Highskill");
Employee employee2 = new Employee();
employee2.setEid(71L);
employee2.setEmployeeNr(701);
employee2.setFirstName("Jane");
employee2.setLastName("Expert");
when(employeeService.getEmployeesBySecondarySkill(5L, 5)).thenReturn(List.of(employee1, employee2));
mockMvc.perform(get("/api/employee/from-skill")
.param("skillId", "5")
.param("level", "5")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$.length()").value(2))
.andExpect(jsonPath("$[0].eid").value(70))
.andExpect(jsonPath("$[1].eid").value(71))
.andExpect(jsonPath("$[0].firstName").value("John"))
.andExpect(jsonPath("$[1].firstName").value("Jane"));
}
// Test DELETE /api/employee/skill für einen vorhandenen Skill mit Bestätigung der Löschung
@Test
public void testDeleteSecondarySkill_WithConfirmation() throws Exception {
mockMvc.perform(delete("/api/employee/skill")
.param("id", "5")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isNoContent());
}
}

View File

@ -1,131 +1,131 @@
//package com.maradona.backend.controllers.api; package com.maradona.backend.controllers.api;
//
//import com.maradona.backend.entities.FormOfAddress; import com.maradona.backend.entities.FormOfAddress;
//import com.maradona.backend.services.FormOfAddressService; import com.maradona.backend.services.FormOfAddressService;
//import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
//import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
//import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.boot.test.mock.mockito.MockBean;
//import org.springframework.http.MediaType; import org.springframework.http.MediaType;
//import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MockMvc;
//
//import java.util.Optional; import java.util.Optional;
//import java.util.Arrays; import java.util.Arrays;
//
//import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.any;
//import static org.mockito.ArgumentMatchers.eq; import static org.mockito.ArgumentMatchers.eq;
//import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
//import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; 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.status;
//import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
//
//@WebMvcTest(FormOfAdressController.class) @WebMvcTest(FormOfAdressController.class)
//public class FormOfAdressControllerTest { public class FormOfAdressControllerTest {
//
// @Autowired @Autowired
// private MockMvc mockMvc; private MockMvc mockMvc;
//
// @MockBean @MockBean
// private FormOfAddressService formOfAdressService; private FormOfAddressService formOfAdressService;
//
// @Test @Test
// public void testGetFormOfAdressById() throws Exception { public void testGetFormOfAdressById() throws Exception {
// FormOfAddress formOfAddress = new FormOfAddress(); FormOfAddress formOfAddress = new FormOfAddress();
// formOfAddress.setFid(1L); formOfAddress.setFid(1L);
// formOfAddress.setDescription("Mr."); formOfAddress.setDescription("Mr.");
//
// when(formOfAdressService.getFormOfAddressByFid(1L)).thenReturn(Optional.of(formOfAddress)); when(formOfAdressService.getFormOfAddressByFid(1L)).thenReturn(Optional.of(formOfAddress));
//
// mockMvc.perform(get("/api/form-of-adress") mockMvc.perform(get("/api/form-of-adress")
// .param("id", "1") .param("id", "1")
// .contentType(MediaType.APPLICATION_JSON)) .contentType(MediaType.APPLICATION_JSON))
// .andExpect(status().isOk()) .andExpect(status().isOk())
// .andExpect(jsonPath("$.id").value(1)) .andExpect(jsonPath("$.id").value(1))
// .andExpect(jsonPath("$.description").value("Mr.")); .andExpect(jsonPath("$.description").value("Mr."));
// } }
//
// @Test @Test
// public void testGetAllFormOfAdresses() throws Exception { public void testGetAllFormOfAdresses() throws Exception {
// FormOfAddress form1 = new FormOfAddress(); FormOfAddress form1 = new FormOfAddress();
// form1.setFid(1L); form1.setFid(1L);
// form1.setDescription("Mr."); form1.setDescription("Mr.");
//
// FormOfAddress form2 = new FormOfAddress(); FormOfAddress form2 = new FormOfAddress();
// form2.setFid(2L); form2.setFid(2L);
// form2.setDescription("Ms."); form2.setDescription("Ms.");
//
// when(formOfAdressService.getAllFormOfAddresses()).thenReturn(Arrays.asList(form1, form2)); when(formOfAdressService.getAllFormOfAddresses()).thenReturn(Arrays.asList(form1, form2));
//
// mockMvc.perform(get("/api/form-of-adress/all") mockMvc.perform(get("/api/form-of-adress/all")
// .contentType(MediaType.APPLICATION_JSON)) .contentType(MediaType.APPLICATION_JSON))
// .andExpect(status().isOk()) .andExpect(status().isOk())
// .andExpect(jsonPath("$[0].id").value(1)) .andExpect(jsonPath("$[0].id").value(1))
// .andExpect(jsonPath("$[0].description").value("Mr.")) .andExpect(jsonPath("$[0].description").value("Mr."))
// .andExpect(jsonPath("$[1].id").value(2)) .andExpect(jsonPath("$[1].id").value(2))
// .andExpect(jsonPath("$[1].description").value("Ms.")); .andExpect(jsonPath("$[1].description").value("Ms."));
// } }
//
// @Test @Test
// public void testCreateFormOfAdress() throws Exception { public void testCreateFormOfAdress() throws Exception {
// FormOfAddress formOfAddress = new FormOfAddress(); FormOfAddress formOfAddress = new FormOfAddress();
// formOfAddress.setFid(1L); formOfAddress.setFid(1L);
// formOfAddress.setDescription("Dr."); formOfAddress.setDescription("Dr.");
//
// when(formOfAdressService.saveFormOfAddress(any(FormOfAddress.class))).thenReturn(formOfAddress); when(formOfAdressService.saveFormOfAddress(any(FormOfAddress.class))).thenReturn(formOfAddress);
//
// mockMvc.perform(post("/api/form-of-adress") mockMvc.perform(post("/api/form-of-adress")
// .contentType(MediaType.APPLICATION_JSON) .contentType(MediaType.APPLICATION_JSON)
// .content("\"Dr.\"")) .content("\"Dr.\""))
// .andExpect(status().isOk()); .andExpect(status().isOk());
// } }
//
// @Test @Test
// public void testUpdateFormOfAdress() throws Exception { public void testUpdateFormOfAdress() throws Exception {
// FormOfAddress existingForm = new FormOfAddress(); FormOfAddress existingForm = new FormOfAddress();
// existingForm.setFid(1L); existingForm.setFid(1L);
// existingForm.setDescription("Mr."); existingForm.setDescription("Mr.");
//
// when(formOfAdressService.getFormOfAddressByFid(1L)).thenReturn(Optional.of(existingForm)); when(formOfAdressService.getFormOfAddressByFid(1L)).thenReturn(Optional.of(existingForm));
//
// mockMvc.perform(put("/api/form-of-adress") mockMvc.perform(put("/api/form-of-adress")
// .param("id", "1") .param("id", "1")
// .contentType(MediaType.APPLICATION_JSON) .contentType(MediaType.APPLICATION_JSON)
// .content("\"Prof.\"")) .content("\"Prof.\""))
// .andExpect(status().isOk()); .andExpect(status().isOk());
// } }
//
// @Test @Test
// public void testUpdateFormOfAdressNotFound() throws Exception { public void testUpdateFormOfAdressNotFound() throws Exception {
// when(formOfAdressService.getFormOfAddressByFid(1L)).thenReturn(Optional.empty()); when(formOfAdressService.getFormOfAddressByFid(1L)).thenReturn(Optional.empty());
//
// mockMvc.perform(put("/api/form-of-adress") mockMvc.perform(put("/api/form-of-adress")
// .param("id", "1") .param("id", "1")
// .contentType(MediaType.APPLICATION_JSON) .contentType(MediaType.APPLICATION_JSON)
// .content("\"Prof.\"")) .content("\"Prof.\""))
// .andExpect(status().isNotFound()); .andExpect(status().isNotFound());
// } }
//
// @Test @Test
// public void testDeleteFormOfAdress() throws Exception { public void testDeleteFormOfAdress() throws Exception {
// FormOfAddress formOfAddress = new FormOfAddress(); FormOfAddress formOfAddress = new FormOfAddress();
// formOfAddress.setFid(1L); formOfAddress.setFid(1L);
// formOfAddress.setDescription("Mr."); formOfAddress.setDescription("Mr.");
//
// when(formOfAdressService.getFormOfAddressByFid(1L)).thenReturn(Optional.of(formOfAddress)); when(formOfAdressService.getFormOfAddressByFid(1L)).thenReturn(Optional.of(formOfAddress));
//
// mockMvc.perform(delete("/api/form-of-adress") mockMvc.perform(delete("/api/form-of-adress")
// .param("id", "1") .param("id", "1")
// .contentType(MediaType.APPLICATION_JSON)) .contentType(MediaType.APPLICATION_JSON))
// .andExpect(status().isOk()); .andExpect(status().isOk());
// } }
//
// @Test @Test
// public void testDeleteFormOfAdressNotFound() throws Exception { public void testDeleteFormOfAdressNotFound() throws Exception {
// when(formOfAdressService.getFormOfAddressByFid(1L)).thenReturn(Optional.empty()); when(formOfAdressService.getFormOfAddressByFid(1L)).thenReturn(Optional.empty());
//
// mockMvc.perform(delete("/api/form-of-adress") mockMvc.perform(delete("/api/form-of-adress")
// .param("id", "1") .param("id", "1")
// .contentType(MediaType.APPLICATION_JSON)) .contentType(MediaType.APPLICATION_JSON))
// .andExpect(status().isNotFound()); .andExpect(status().isNotFound());
// } }
//} }

View File

@ -1,140 +1,140 @@
//package com.maradona.backend.controllers.api; package com.maradona.backend.controllers.api;
//
//import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
//import com.maradona.backend.entities.PrimarySkill; import com.maradona.backend.entities.PrimarySkill;
//import com.maradona.backend.services.PrimarySkillService; import com.maradona.backend.services.PrimarySkillService;
//import com.maradona.backend.services.SecondarySkillService; import com.maradona.backend.services.SecondarySkillService;
//import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
//import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
//import org.mockito.*; import org.mockito.*;
//import org.springframework.http.MediaType; import org.springframework.http.MediaType;
//import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MockMvc;
//import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
//import org.springframework.test.web.servlet.result.MockMvcResultMatchers; import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
//import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.test.web.servlet.setup.MockMvcBuilders;
//
//import java.util.List; import java.util.List;
//import java.util.Optional; import java.util.Optional;
//
//import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.any;
//
//public class PrimarySkillControllerTest { public class PrimarySkillControllerTest {
//
// private MockMvc mockMvc; private MockMvc mockMvc;
//
// @Mock @Mock
// private PrimarySkillService primarySkillService; private PrimarySkillService primarySkillService;
//
// @Mock @Mock
// private SecondarySkillService secondarySkillService; private SecondarySkillService secondarySkillService;
//
// @InjectMocks @InjectMocks
// private PrimarySkillController primarySkillController; private PrimarySkillController primarySkillController;
//
// private PrimarySkill primarySkill; private PrimarySkill primarySkill;
//
// @BeforeEach @BeforeEach
// public void setUp() { public void setUp() {
// MockitoAnnotations.openMocks(this); MockitoAnnotations.openMocks(this);
// mockMvc = MockMvcBuilders.standaloneSetup(primarySkillController).build(); mockMvc = MockMvcBuilders.standaloneSetup(primarySkillController).build();
//
// // Initialisieren eines Beispiels für PrimarySkill // Initialisieren eines Beispiels für PrimarySkill
// primarySkill = new PrimarySkill(); primarySkill = new PrimarySkill();
// primarySkill.setPsid(1L); primarySkill.setPsid(1L);
// primarySkill.setDescription("Test Primary Skill"); primarySkill.setDescription("Test Primary Skill");
// } }
//
// @Test @Test
// public void testGetPrimarySkillById_Success() throws Exception { public void testGetPrimarySkillById_Success() throws Exception {
// // Mock das Service, um ein PrimarySkill zu liefern // Mock das Service, um ein PrimarySkill zu liefern
// Mockito.when(primarySkillService.getPrimarySkillByPsid(1L)).thenReturn(Optional.of(primarySkill)); Mockito.when(primarySkillService.getPrimarySkillByPsid(1L)).thenReturn(Optional.of(primarySkill));
//
// // Führe die GET-Anfrage aus // Führe die GET-Anfrage aus
// mockMvc.perform(MockMvcRequestBuilders.get("/api/primary-skill") mockMvc.perform(MockMvcRequestBuilders.get("/api/primary-skill")
// .param("id", "1")) .param("id", "1"))
// .andExpect(MockMvcResultMatchers.status().isOk()) .andExpect(MockMvcResultMatchers.status().isOk())
// .andExpect(MockMvcResultMatchers.jsonPath("$.psid").value(1)) .andExpect(MockMvcResultMatchers.jsonPath("$.psid").value(1))
// .andExpect(MockMvcResultMatchers.jsonPath("$.description").value("Test Primary Skill")); .andExpect(MockMvcResultMatchers.jsonPath("$.description").value("Test Primary Skill"));
//
// // Verifiziere, dass der Service aufgerufen wurde // Verifiziere, dass der Service aufgerufen wurde
// Mockito.verify(primarySkillService, Mockito.times(1)).getPrimarySkillByPsid(1L); Mockito.verify(primarySkillService, Mockito.times(1)).getPrimarySkillByPsid(1L);
// } }
//
// @Test @Test
// public void testGetAllPrimarySkills() throws Exception { public void testGetAllPrimarySkills() throws Exception {
// // Mock das Service, um eine Liste von PrimarySkills zu liefern // Mock das Service, um eine Liste von PrimarySkills zu liefern
// Mockito.when(primarySkillService.getAllPrimarySkills()).thenReturn(List.of(primarySkill)); Mockito.when(primarySkillService.getAllPrimarySkills()).thenReturn(List.of(primarySkill));
//
// // Führe die GET-Anfrage aus // Führe die GET-Anfrage aus
// mockMvc.perform(MockMvcRequestBuilders.get("/api/primary-skill/all")) mockMvc.perform(MockMvcRequestBuilders.get("/api/primary-skill/all"))
// .andExpect(MockMvcResultMatchers.status().isOk()) .andExpect(MockMvcResultMatchers.status().isOk())
// .andExpect(MockMvcResultMatchers.jsonPath("$[0].psid").value(1)) .andExpect(MockMvcResultMatchers.jsonPath("$[0].psid").value(1))
// .andExpect(MockMvcResultMatchers.jsonPath("$[0].description").value("Test Primary Skill")); .andExpect(MockMvcResultMatchers.jsonPath("$[0].description").value("Test Primary Skill"));
//
// // Verifiziere, dass der Service aufgerufen wurde // Verifiziere, dass der Service aufgerufen wurde
// Mockito.verify(primarySkillService, Mockito.times(1)).getAllPrimarySkills(); Mockito.verify(primarySkillService, Mockito.times(1)).getAllPrimarySkills();
// } }
//
// @Test @Test
// public void testCreatePrimarySkill() throws Exception { public void testCreatePrimarySkill() throws Exception {
// // Mock das Service, um das PrimarySkill zu speichern // Mock das Service, um das PrimarySkill zu speichern
// Mockito.when(primarySkillService.savePrimarySkill(ArgumentMatchers.any(PrimarySkill.class))).thenReturn(primarySkill); Mockito.when(primarySkillService.savePrimarySkill(ArgumentMatchers.any(PrimarySkill.class))).thenReturn(primarySkill);
//
// // Führe die POST-Anfrage aus // Führe die POST-Anfrage aus
// mockMvc.perform(MockMvcRequestBuilders.post("/api/primary-skill") mockMvc.perform(MockMvcRequestBuilders.post("/api/primary-skill")
// .contentType(MediaType.APPLICATION_JSON) .contentType(MediaType.APPLICATION_JSON)
// .content(new ObjectMapper().writeValueAsString(primarySkill))) // Hier wird das PrimarySkill als JSON übermittelt .content(new ObjectMapper().writeValueAsString(primarySkill))) // Hier wird das PrimarySkill als JSON übermittelt
// .andExpect(MockMvcResultMatchers.status().isCreated()) // Erwartet den Statuscode 201 Created .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("$.psid").value(1)) // Überprüft das psid in der Antwort
// .andExpect(MockMvcResultMatchers.jsonPath("$.description").value("Test Primary Skill")); // Überprüft die Beschreibung .andExpect(MockMvcResultMatchers.jsonPath("$.description").value("Test Primary Skill")); // Überprüft die Beschreibung
//
// // Verifiziere, dass der Service aufgerufen wurde // Verifiziere, dass der Service aufgerufen wurde
// Mockito.verify(primarySkillService, Mockito.times(1)).savePrimarySkill(ArgumentMatchers.any(PrimarySkill.class)); Mockito.verify(primarySkillService, Mockito.times(1)).savePrimarySkill(ArgumentMatchers.any(PrimarySkill.class));
// } }
//
//
// @Test @Test
// public void testUpdatePrimarySkill() throws Exception { public void testUpdatePrimarySkill() throws Exception {
// // Mock das Service, um das PrimarySkill zu speichern // Mock das Service, um das PrimarySkill zu speichern
// Mockito.when(primarySkillService.savePrimarySkill(ArgumentMatchers.any(PrimarySkill.class))).thenReturn(primarySkill); Mockito.when(primarySkillService.savePrimarySkill(ArgumentMatchers.any(PrimarySkill.class))).thenReturn(primarySkill);
//
// // Führe die PUT-Anfrage aus // Führe die PUT-Anfrage aus
// mockMvc.perform(MockMvcRequestBuilders.put("/api/primary-skill") mockMvc.perform(MockMvcRequestBuilders.put("/api/primary-skill")
// .contentType(MediaType.APPLICATION_JSON) .contentType(MediaType.APPLICATION_JSON)
// .content(new ObjectMapper().writeValueAsString(primarySkill))) .content(new ObjectMapper().writeValueAsString(primarySkill)))
// .andExpect(MockMvcResultMatchers.status().isOk()) .andExpect(MockMvcResultMatchers.status().isOk())
// .andExpect(MockMvcResultMatchers.jsonPath("$.psid").value(1)) .andExpect(MockMvcResultMatchers.jsonPath("$.psid").value(1))
// .andExpect(MockMvcResultMatchers.jsonPath("$.description").value("Test Primary Skill")); .andExpect(MockMvcResultMatchers.jsonPath("$.description").value("Test Primary Skill"));
//
// // Verifiziere, dass der Service aufgerufen wurde // Verifiziere, dass der Service aufgerufen wurde
// Mockito.verify(primarySkillService, Mockito.times(1)).savePrimarySkill(ArgumentMatchers.any(PrimarySkill.class)); Mockito.verify(primarySkillService, Mockito.times(1)).savePrimarySkill(ArgumentMatchers.any(PrimarySkill.class));
// } }
//
// @Test @Test
// public void testDeletePrimarySkill_Success() throws Exception { public void testDeletePrimarySkill_Success() throws Exception {
// // Mock das Service, um das PrimarySkill zu liefern // Mock das Service, um das PrimarySkill zu liefern
// Mockito.when(primarySkillService.getPrimarySkillByPsid(1L)).thenReturn(Optional.of(primarySkill)); Mockito.when(primarySkillService.getPrimarySkillByPsid(1L)).thenReturn(Optional.of(primarySkill));
//
// // Führe die DELETE-Anfrage aus // Führe die DELETE-Anfrage aus
// mockMvc.perform(MockMvcRequestBuilders.delete("/api/primary-skill") mockMvc.perform(MockMvcRequestBuilders.delete("/api/primary-skill")
// .param("id", "1")) .param("id", "1"))
// .andExpect(MockMvcResultMatchers.status().isNoContent()); .andExpect(MockMvcResultMatchers.status().isNoContent());
//
// // Verifiziere, dass der Service aufgerufen wurde // Verifiziere, dass der Service aufgerufen wurde
// Mockito.verify(primarySkillService, Mockito.times(1)).deletePrimarySkill(1L); Mockito.verify(primarySkillService, Mockito.times(1)).deletePrimarySkill(1L);
// } }
//
// @Test @Test
// public void testDeletePrimarySkill_NotFound() throws Exception { public void testDeletePrimarySkill_NotFound() throws Exception {
// // Mock das Service, um das PrimarySkill nicht zu finden // Mock das Service, um das PrimarySkill nicht zu finden
// Mockito.when(primarySkillService.getPrimarySkillByPsid(1L)).thenReturn(Optional.empty()); Mockito.when(primarySkillService.getPrimarySkillByPsid(1L)).thenReturn(Optional.empty());
//
// // Führe die DELETE-Anfrage aus // Führe die DELETE-Anfrage aus
// mockMvc.perform(MockMvcRequestBuilders.delete("/api/primary-skill") mockMvc.perform(MockMvcRequestBuilders.delete("/api/primary-skill")
// .param("id", "1")) .param("id", "1"))
// .andExpect(MockMvcResultMatchers.status().isNotFound()); .andExpect(MockMvcResultMatchers.status().isNotFound());
//
// // Verifiziere, dass der Service nicht aufgerufen wurde // Verifiziere, dass der Service nicht aufgerufen wurde
// Mockito.verify(primarySkillService, Mockito.never()).deletePrimarySkill(1L); Mockito.verify(primarySkillService, Mockito.never()).deletePrimarySkill(1L);
// } }
//} }

View File

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

View File

@ -1,130 +1,130 @@
//package com.maradona.backend.controllers.api; package com.maradona.backend.controllers.api;
//
//import com.maradona.backend.entities.SecondarySkill; import com.maradona.backend.entities.SecondarySkill;
//import com.maradona.backend.services.SecondarySkillService; import com.maradona.backend.services.SecondarySkillService;
//import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
//import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
//import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.boot.test.mock.mockito.MockBean;
//import org.springframework.http.MediaType; import org.springframework.http.MediaType;
//import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MockMvc;
//
//import java.util.Arrays; import java.util.Arrays;
//import java.util.Optional; import java.util.Optional;
//
//import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.any;
//import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
//import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; 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.status;
//import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
//
//@WebMvcTest(SecondarySkillController.class) @WebMvcTest(SecondarySkillController.class)
//public class SecondarySkillControllerTest { public class SecondarySkillControllerTest {
//
// @Autowired @Autowired
// private MockMvc mockMvc; private MockMvc mockMvc;
//
// @MockBean @MockBean
// private SecondarySkillService secondarySkillService; private SecondarySkillService secondarySkillService;
//
// @Test @Test
// public void testGetSecondarySkillById() throws Exception { public void testGetSecondarySkillById() throws Exception {
// SecondarySkill skill = new SecondarySkill(); SecondarySkill skill = new SecondarySkill();
// skill.setSsid(1L); skill.setSsid(1L);
// skill.setDescription("Java Programming"); skill.setDescription("Java Programming");
//
// when(secondarySkillService.getSecondarySkillBySsid(1L)).thenReturn(Optional.of(skill)); when(secondarySkillService.getSecondarySkillBySsid(1L)).thenReturn(Optional.of(skill));
//
// mockMvc.perform(get("/api/secondary-skill") mockMvc.perform(get("/api/secondary-skill")
// .param("id", "1") .param("id", "1")
// .contentType(MediaType.APPLICATION_JSON)) .contentType(MediaType.APPLICATION_JSON))
// .andExpect(status().isOk()) .andExpect(status().isOk())
// .andExpect(jsonPath("$.ssid").value(1)) .andExpect(jsonPath("$.ssid").value(1))
// .andExpect(jsonPath("$.description").value("Java Programming")); .andExpect(jsonPath("$.description").value("Java Programming"));
// } }
//
// @Test @Test
// public void testGetAllSecondarySkills() throws Exception { public void testGetAllSecondarySkills() throws Exception {
// SecondarySkill skill1 = new SecondarySkill(); SecondarySkill skill1 = new SecondarySkill();
// skill1.setSsid(1L); skill1.setSsid(1L);
// skill1.setDescription("Java Programming"); skill1.setDescription("Java Programming");
//
// SecondarySkill skill2 = new SecondarySkill(); SecondarySkill skill2 = new SecondarySkill();
// skill2.setSsid(2L); skill2.setSsid(2L);
// skill2.setDescription("Python Programming"); skill2.setDescription("Python Programming");
//
// when(secondarySkillService.getAllSecondarySkills()).thenReturn(Arrays.asList(skill1, skill2)); when(secondarySkillService.getAllSecondarySkills()).thenReturn(Arrays.asList(skill1, skill2));
//
// mockMvc.perform(get("/api/secondary-skill/all") mockMvc.perform(get("/api/secondary-skill/all")
// .contentType(MediaType.APPLICATION_JSON)) .contentType(MediaType.APPLICATION_JSON))
// .andExpect(status().isOk()) .andExpect(status().isOk())
// .andExpect(jsonPath("$[1].ssid").value(1)) .andExpect(jsonPath("$[1].ssid").value(1))
// .andExpect(jsonPath("$[1].description").value("Java Programming")) .andExpect(jsonPath("$[1].description").value("Java Programming"))
// .andExpect(jsonPath("$[2].ssid").value(2)) .andExpect(jsonPath("$[2].ssid").value(2))
// .andExpect(jsonPath("$[2].description").value("Python Programming")); .andExpect(jsonPath("$[2].description").value("Python Programming"));
// } }
//
// @Test @Test
// public void testGetSecondarySkillsByPrimarySkillId() throws Exception { public void testGetSecondarySkillsByPrimarySkillId() throws Exception {
// SecondarySkill skill1 = new SecondarySkill(); SecondarySkill skill1 = new SecondarySkill();
// skill1.setSsid(1L); skill1.setSsid(1L);
// skill1.setDescription("Data Analysis"); skill1.setDescription("Data Analysis");
//
// SecondarySkill skill2 = new SecondarySkill(); SecondarySkill skill2 = new SecondarySkill();
// skill2.setSsid(2L); skill2.setSsid(2L);
// skill2.setDescription("Data Visualization"); skill2.setDescription("Data Visualization");
//
// when(secondarySkillService.getSecondarySkillsByPrimarySkillId(100L)).thenReturn(Arrays.asList(skill1, skill2)); when(secondarySkillService.getSecondarySkillsByPrimarySkillId(100L)).thenReturn(Arrays.asList(skill1, skill2));
//
// mockMvc.perform(get("/api/secondary-skill/from-primary-skill") mockMvc.perform(get("/api/secondary-skill/from-primary-skill")
// .param("primarySkillId", "100") .param("primarySkillId", "100")
// .contentType(MediaType.APPLICATION_JSON)) .contentType(MediaType.APPLICATION_JSON))
// .andExpect(status().isOk()) .andExpect(status().isOk())
// .andExpect(jsonPath("$[1].ssid").value(1)) .andExpect(jsonPath("$[1].ssid").value(1))
// .andExpect(jsonPath("$[1].description").value("Data Analysis")) .andExpect(jsonPath("$[1].description").value("Data Analysis"))
// .andExpect(jsonPath("$[2].ssid").value(2)) .andExpect(jsonPath("$[2].ssid").value(2))
// .andExpect(jsonPath("$[2].description").value("Data Visualization")); .andExpect(jsonPath("$[2].description").value("Data Visualization"));
// } }
//
// @Test @Test
// public void testCreateSecondarySkill() throws Exception { public void testCreateSecondarySkill() throws Exception {
// SecondarySkill skill = new SecondarySkill(); SecondarySkill skill = new SecondarySkill();
// skill.setSsid(1L); skill.setSsid(1L);
// skill.setDescription("Machine Learning"); skill.setDescription("Machine Learning");
//
// when(secondarySkillService.saveSecondarySkill(any(SecondarySkill.class))).thenReturn(skill); when(secondarySkillService.saveSecondarySkill(any(SecondarySkill.class))).thenReturn(skill);
//
// mockMvc.perform(post("/api/secondary-skill") mockMvc.perform(post("/api/secondary-skill")
// .contentType(MediaType.APPLICATION_JSON) .contentType(MediaType.APPLICATION_JSON)
// .content("{\"name\":\"Machine Learning\"}")) .content("{\"name\":\"Machine Learning\"}"))
// .andExpect(status().isCreated()) .andExpect(status().isCreated())
// .andExpect(jsonPath("$.ssid").value(1)) .andExpect(jsonPath("$.ssid").value(1))
// .andExpect(jsonPath("$.description").value("Machine Learning")); .andExpect(jsonPath("$.description").value("Machine Learning"));
// } }
//
// @Test @Test
// public void testUpdateSecondarySkill() throws Exception { public void testUpdateSecondarySkill() throws Exception {
// SecondarySkill updatedSkill = new SecondarySkill(); SecondarySkill updatedSkill = new SecondarySkill();
// updatedSkill.setSsid(1L); updatedSkill.setSsid(1L);
// updatedSkill.setDescription("Advanced Machine Learning"); updatedSkill.setDescription("Advanced Machine Learning");
//
// when(secondarySkillService.saveSecondarySkill(any(SecondarySkill.class))).thenReturn(updatedSkill); when(secondarySkillService.saveSecondarySkill(any(SecondarySkill.class))).thenReturn(updatedSkill);
//
// mockMvc.perform(put("/api/secondary-skill") mockMvc.perform(put("/api/secondary-skill")
// .contentType(MediaType.APPLICATION_JSON) .contentType(MediaType.APPLICATION_JSON)
// .content("{\"ssid\":1,\"description\":\"Advanced Machine Learning\"}")) .content("{\"ssid\":1,\"description\":\"Advanced Machine Learning\"}"))
// .andExpect(status().isOk()) .andExpect(status().isOk())
// .andExpect(jsonPath("$.ssid").value(1)) .andExpect(jsonPath("$.ssid").value(1))
// .andExpect(jsonPath("$.description").value("Advanced Machine Learning")); .andExpect(jsonPath("$.description").value("Advanced Machine Learning"));
// } }
//
// @Test @Test
// public void testDeleteSecondarySkill() throws Exception { public void testDeleteSecondarySkill() throws Exception {
// Long skillId = 1L; Long skillId = 1L;
//
// mockMvc.perform(delete("/api/secondary-skill") mockMvc.perform(delete("/api/secondary-skill")
// .param("id", String.valueOf(skillId)) .param("id", String.valueOf(skillId))
// .contentType(MediaType.APPLICATION_JSON)) .contentType(MediaType.APPLICATION_JSON))
// .andExpect(status().isNoContent()); .andExpect(status().isNoContent());
// } }
//} }