Merge pull request 'chore: Update branch' (#5) from Maradona/Backend:restfull into restfull

Reviewed-on: #5
pull/1/head
Piotr Jakubiak 2024-11-11 14:50:37 +01:00
commit 682cf886ee
23 changed files with 1068 additions and 953 deletions

View File

@ -14,7 +14,8 @@ import org.springframework.web.bind.annotation.RestController;
import com.maradona.backend.dto.SkillPrototype; import com.maradona.backend.dto.SkillPrototype;
import com.maradona.backend.entities.Employee; import com.maradona.backend.entities.Employee;
import com.maradona.backend.services.EmployeeService; import com.maradona.backend.services.actions.EmployeeActions;
import com.maradona.backend.services.details.EmployeeDetails;
/** /**
* Controller for handling employee related requests. * Controller for handling employee related requests.
@ -36,7 +37,10 @@ import com.maradona.backend.services.EmployeeService;
public class EmployeeController { public class EmployeeController {
@Autowired @Autowired
private EmployeeService employeeService; private EmployeeDetails employeeDetails;
@Autowired
private EmployeeActions employeeActions;
Long user = Long.valueOf(1); Long user = Long.valueOf(1);
@ -48,7 +52,7 @@ public class EmployeeController {
*/ */
@GetMapping({ "/", "" }) @GetMapping({ "/", "" })
public ResponseEntity<Employee> get() { public ResponseEntity<Employee> get() {
var employee = employeeService.getEmployeeByEid(user); var employee = employeeDetails.getEmployeeByEid(user);
return employee return employee
.map(ResponseEntity::ok) .map(ResponseEntity::ok)
.orElse(ResponseEntity.notFound().build()); .orElse(ResponseEntity.notFound().build());
@ -63,7 +67,7 @@ public class EmployeeController {
*/ */
@GetMapping("/{eid}") @GetMapping("/{eid}")
public ResponseEntity<Employee> get(@RequestParam Long eid) { public ResponseEntity<Employee> get(@RequestParam Long eid) {
var employee = employeeService.getEmployeeByEid(eid); var employee = employeeDetails.getEmployeeByEid(eid);
return employee return employee
.map(ResponseEntity::ok) .map(ResponseEntity::ok)
.orElse(ResponseEntity.notFound().build()); .orElse(ResponseEntity.notFound().build());
@ -77,7 +81,7 @@ public class EmployeeController {
*/ */
@GetMapping("/all") @GetMapping("/all")
public ResponseEntity<Iterable<Employee>> getAll() { public ResponseEntity<Iterable<Employee>> getAll() {
return ResponseEntity.ok(employeeService.getAllEmployees()); return ResponseEntity.ok(employeeDetails.getAllEmployees());
} }
/** /**
@ -91,7 +95,7 @@ public class EmployeeController {
*/ */
@GetMapping("/from-skill") @GetMapping("/from-skill")
public ResponseEntity<Iterable<Employee>> getFromSkill(@RequestParam Long ssid, @RequestParam Integer level) { public ResponseEntity<Iterable<Employee>> getFromSkill(@RequestParam Long ssid, @RequestParam Integer level) {
return ResponseEntity.ok(employeeService.getEmployeesBySecondarySkill(ssid, level)); return ResponseEntity.ok(employeeDetails.getEmployeesBySecondarySkill(ssid, level));
} }
/** /**
@ -105,7 +109,7 @@ public class EmployeeController {
*/ */
@PostMapping("/skill/protoype") @PostMapping("/skill/protoype")
public ResponseEntity<Void> postSkillProtoype(@RequestBody SkillPrototype skillPrototype) { public ResponseEntity<Void> postSkillProtoype(@RequestBody SkillPrototype skillPrototype) {
employeeService.addSecondarySkillToEmployee(user, skillPrototype); employeeActions.addSecondarySkillToEmployee(user, skillPrototype);
return ResponseEntity.status(HttpStatus.CREATED).build(); return ResponseEntity.status(HttpStatus.CREATED).build();
} }
@ -121,7 +125,7 @@ public class EmployeeController {
*/ */
@PutMapping("/skill/level") @PutMapping("/skill/level")
public ResponseEntity<Void> putSkillLevel(@RequestParam Long ssid, @RequestParam Integer level) { public ResponseEntity<Void> putSkillLevel(@RequestParam Long ssid, @RequestParam Integer level) {
employeeService.updateSecondarySkillLevel(user, ssid, level); employeeActions.updateSecondarySkillLevel(user, ssid, level);
return ResponseEntity.ok().build(); return ResponseEntity.ok().build();
} }
@ -135,7 +139,7 @@ public class EmployeeController {
*/ */
@DeleteMapping("/skill") @DeleteMapping("/skill")
public ResponseEntity<Void> delete(@RequestParam Long ssid) { public ResponseEntity<Void> delete(@RequestParam Long ssid) {
employeeService.deleteSecondarySkillFromEmployee(user, ssid); employeeActions.deleteSecondarySkillFromEmployee(user, ssid);
return ResponseEntity.noContent().build(); return ResponseEntity.noContent().build();
} }
} }

View File

@ -6,7 +6,8 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import com.maradona.backend.entities.FormOfAddress; import com.maradona.backend.entities.FormOfAddress;
import com.maradona.backend.services.FormOfAddressService; import com.maradona.backend.services.actions.FormOfAddressActions;
import com.maradona.backend.services.details.FormOfAddressDetails;
import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
@ -30,9 +31,11 @@ import org.springframework.web.bind.annotation.PutMapping;
@RestController @RestController
@RequestMapping("/api/form-of-adress") @RequestMapping("/api/form-of-adress")
class FormOfAdressController { class FormOfAdressController {
@Autowired
private FormOfAddressDetails formOfAdressDetails;
@Autowired @Autowired
private FormOfAddressService formOfAdressService; private FormOfAddressActions formOfAdressActions;
/** /**
* Returns the form of address with the given ID. * Returns the form of address with the given ID.
@ -43,7 +46,7 @@ class FormOfAdressController {
*/ */
@GetMapping({ "/", "" }) @GetMapping({ "/", "" })
public ResponseEntity<FormOfAddress> getFormOfAdress(@RequestParam Long fid) { public ResponseEntity<FormOfAddress> getFormOfAdress(@RequestParam Long fid) {
return formOfAdressService.getFormOfAddressByFid(fid) return formOfAdressDetails.getFormOfAddressByFid(fid)
.map(ResponseEntity::ok) .map(ResponseEntity::ok)
.orElse(ResponseEntity.notFound().build()); .orElse(ResponseEntity.notFound().build());
} }
@ -56,7 +59,7 @@ class FormOfAdressController {
*/ */
@GetMapping("/all") @GetMapping("/all")
public ResponseEntity<Iterable<FormOfAddress>> getAllFormOfAdresses() { public ResponseEntity<Iterable<FormOfAddress>> getAllFormOfAdresses() {
Iterable<FormOfAddress> formOfAddresses = formOfAdressService.getAllFormOfAddresses(); Iterable<FormOfAddress> formOfAddresses = formOfAdressDetails.getAllFormOfAddresses();
return ResponseEntity.ok(formOfAddresses); return ResponseEntity.ok(formOfAddresses);
} }
@ -71,7 +74,7 @@ class FormOfAdressController {
public ResponseEntity<Void> createFormOfAdress(@RequestBody String description) { public ResponseEntity<Void> createFormOfAdress(@RequestBody String description) {
var formOfAddress = new FormOfAddress(); var formOfAddress = new FormOfAddress();
formOfAddress.setDescription(description); formOfAddress.setDescription(description);
formOfAdressService.saveFormOfAddress(formOfAddress); formOfAdressActions.saveFormOfAddress(formOfAddress);
return ResponseEntity.ok().build(); return ResponseEntity.ok().build();
} }
@ -85,12 +88,12 @@ class FormOfAdressController {
*/ */
@PutMapping({ "/", "" }) @PutMapping({ "/", "" })
public ResponseEntity<Void> updateFormOfAdress(@RequestParam Long fid, @RequestBody String description) { public ResponseEntity<Void> updateFormOfAdress(@RequestParam Long fid, @RequestBody String description) {
var formOfAddress = formOfAdressService.getFormOfAddressByFid(fid).orElse(null); var formOfAddress = formOfAdressDetails.getFormOfAddressByFid(fid).orElse(null);
if (formOfAddress == null) { if (formOfAddress == null) {
return ResponseEntity.notFound().build(); return ResponseEntity.notFound().build();
} }
formOfAddress.setDescription(description); formOfAddress.setDescription(description);
formOfAdressService.saveFormOfAddress(formOfAddress); formOfAdressActions.saveFormOfAddress(formOfAddress);
return ResponseEntity.ok().build(); return ResponseEntity.ok().build();
} }
@ -103,11 +106,11 @@ class FormOfAdressController {
*/ */
@DeleteMapping({ "/", "" }) @DeleteMapping({ "/", "" })
public ResponseEntity<Void> deleteFormOfAdress(@RequestParam Long fid) { public ResponseEntity<Void> deleteFormOfAdress(@RequestParam Long fid) {
var formOfAddress = formOfAdressService.getFormOfAddressByFid(fid).orElse(null); var formOfAddress = formOfAdressDetails.getFormOfAddressByFid(fid).orElse(null);
if (formOfAddress == null) { if (formOfAddress == null) {
return ResponseEntity.notFound().build(); return ResponseEntity.notFound().build();
} }
formOfAdressService.deleteFormOfAddress(fid); formOfAdressActions.deleteFormOfAddress(fid);
return ResponseEntity.ok().build(); return ResponseEntity.ok().build();
} }
} }

View File

@ -14,8 +14,10 @@ import org.springframework.web.bind.annotation.RestController;
import com.maradona.backend.entities.PrimarySkill; import com.maradona.backend.entities.PrimarySkill;
import com.maradona.backend.entities.SecondarySkill; import com.maradona.backend.entities.SecondarySkill;
import com.maradona.backend.services.PrimarySkillService; import com.maradona.backend.services.actions.PrimarySkillActions;
import com.maradona.backend.services.SecondarySkillService; import com.maradona.backend.services.actions.SecondarySkillActions;
import com.maradona.backend.services.details.PrimarySkillDetails;
import com.maradona.backend.services.details.SecondarySkillDetails;
/** /**
* Controller for handling primary skill data. * Controller for handling primary skill data.
@ -34,10 +36,16 @@ import com.maradona.backend.services.SecondarySkillService;
public class PrimarySkillController { public class PrimarySkillController {
@Autowired @Autowired
private PrimarySkillService primarySkillService; private PrimarySkillDetails primarySkillDetails;
@Autowired @Autowired
private SecondarySkillService secondarySkillService; private PrimarySkillActions primarySkillActions;
@Autowired
private SecondarySkillDetails secondarySkillDetails;
@Autowired
private SecondarySkillActions secondarySkillActions;
/** /**
* Returns a specific primary skill from the database. * Returns a specific primary skill from the database.
@ -48,7 +56,7 @@ public class PrimarySkillController {
*/ */
@GetMapping({ "/", "" }) @GetMapping({ "/", "" })
public ResponseEntity<PrimarySkill> get(@RequestParam Long pid) { public ResponseEntity<PrimarySkill> get(@RequestParam Long pid) {
return primarySkillService.getPrimarySkillByPsid(pid) return primarySkillDetails.getPrimarySkillByPsid(pid)
.map(ResponseEntity::ok) .map(ResponseEntity::ok)
.orElse(ResponseEntity.notFound().build()); .orElse(ResponseEntity.notFound().build());
} }
@ -61,7 +69,7 @@ public class PrimarySkillController {
*/ */
@GetMapping("/all") @GetMapping("/all")
public ResponseEntity<Iterable<PrimarySkill>> getAll() { public ResponseEntity<Iterable<PrimarySkill>> getAll() {
return ResponseEntity.ok(primarySkillService.getAllPrimarySkills()); return ResponseEntity.ok(primarySkillDetails.getAllPrimarySkills());
} }
/** /**
@ -73,7 +81,7 @@ public class PrimarySkillController {
*/ */
@PostMapping({ "/", "" }) @PostMapping({ "/", "" })
public ResponseEntity<PrimarySkill> post(@RequestBody PrimarySkill primarySkill) { public ResponseEntity<PrimarySkill> post(@RequestBody PrimarySkill primarySkill) {
PrimarySkill savedPrimarySkill = primarySkillService.savePrimarySkill(primarySkill); PrimarySkill savedPrimarySkill = primarySkillActions.savePrimarySkill(primarySkill);
return ResponseEntity.status(HttpStatus.CREATED).body(savedPrimarySkill); return ResponseEntity.status(HttpStatus.CREATED).body(savedPrimarySkill);
} }
@ -86,7 +94,7 @@ public class PrimarySkillController {
*/ */
@PutMapping({ "/", "" }) @PutMapping({ "/", "" })
public ResponseEntity<PrimarySkill> put(@RequestBody PrimarySkill primarySkill) { public ResponseEntity<PrimarySkill> put(@RequestBody PrimarySkill primarySkill) {
PrimarySkill updatedPrimarySkill = primarySkillService.savePrimarySkill(primarySkill); PrimarySkill updatedPrimarySkill = primarySkillActions.savePrimarySkill(primarySkill);
return ResponseEntity.ok(updatedPrimarySkill); return ResponseEntity.ok(updatedPrimarySkill);
} }
@ -99,15 +107,15 @@ public class PrimarySkillController {
*/ */
@DeleteMapping({ "/", "" }) @DeleteMapping({ "/", "" })
public ResponseEntity<Void> delete(@RequestParam Long pid) { public ResponseEntity<Void> delete(@RequestParam Long pid) {
var primarySkill = primarySkillService.getPrimarySkillByPsid(pid); var primarySkill = primarySkillDetails.getPrimarySkillByPsid(pid);
if (primarySkill.isPresent()) { if (primarySkill.isPresent()) {
var secondarySkills = secondarySkillService.getAllSecondarySkills(); var secondarySkills = secondarySkillDetails.getAllSecondarySkills();
for (SecondarySkill secondarySkill : secondarySkills) { for (SecondarySkill secondarySkill : secondarySkills) {
if (secondarySkill.getPrimarySkill().getPsid().equals(pid)) { if (secondarySkill.getPrimarySkill().getPsid().equals(pid)) {
secondarySkillService.deleteSecondarySkill(secondarySkill.getSsid()); secondarySkillActions.deleteSecondarySkill(secondarySkill.getSsid());
} }
} }
primarySkillService.deletePrimarySkill(pid); primarySkillActions.deletePrimarySkill(pid);
return ResponseEntity.noContent().build(); return ResponseEntity.noContent().build();
} else { } else {
return ResponseEntity.notFound().build(); return ResponseEntity.notFound().build();

View File

@ -15,7 +15,8 @@ import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import com.maradona.backend.entities.Project; import com.maradona.backend.entities.Project;
import com.maradona.backend.services.ProjectService; import com.maradona.backend.services.actions.ProjectActions;
import com.maradona.backend.services.details.ProjectDetails;
/** /**
* Controller for handling project data. * Controller for handling project data.
@ -29,14 +30,16 @@ import com.maradona.backend.services.ProjectService;
* - DELETE /api/project * - DELETE /api/project
* *
* @see com.maradona.backend.entities.Project * @see com.maradona.backend.entities.Project
* @see com.maradona.backend.services.ProjectService * @see com.maradona.backend.services.actions.ProjectActions
*/ */
@RestController @RestController
@RequestMapping("/api/project") @RequestMapping("/api/project")
public class ProjectController { public class ProjectController {
@Autowired
private ProjectDetails projectDetails;
@Autowired @Autowired
private ProjectService projectService; private ProjectActions projectActions;
/** /**
* Returns a specific project from the database. * Returns a specific project from the database.
@ -47,7 +50,7 @@ public class ProjectController {
*/ */
@GetMapping({ "/", "" }) @GetMapping({ "/", "" })
public ResponseEntity<Project> get(@RequestParam Long psid) { public ResponseEntity<Project> get(@RequestParam Long psid) {
Optional<Project> project = projectService.getProjectByPid(psid); Optional<Project> project = projectDetails.getProjectByPid(psid);
return project.map(ResponseEntity::ok).orElseGet(() -> ResponseEntity.notFound().build()); return project.map(ResponseEntity::ok).orElseGet(() -> ResponseEntity.notFound().build());
} }
@ -59,7 +62,7 @@ public class ProjectController {
*/ */
@GetMapping("/all") @GetMapping("/all")
public ResponseEntity<Iterable<Project>> getAll() { public ResponseEntity<Iterable<Project>> getAll() {
return ResponseEntity.ok(projectService.getAllProjects()); return ResponseEntity.ok(projectDetails.getAllProjects());
} }
/** /**
@ -71,7 +74,7 @@ public class ProjectController {
*/ */
@GetMapping("/from-employee") @GetMapping("/from-employee")
public ResponseEntity<Iterable<Project>> getFromUser(@RequestParam Long eid) { public ResponseEntity<Iterable<Project>> getFromUser(@RequestParam Long eid) {
return ResponseEntity.ok(projectService.getProjectsByEid(eid)); return ResponseEntity.ok(projectDetails.getProjectsByEid(eid));
} }
/** /**
@ -83,7 +86,7 @@ public class ProjectController {
*/ */
@PostMapping({ "/", "" }) @PostMapping({ "/", "" })
public ResponseEntity<Project> post(@RequestBody Project project) { public ResponseEntity<Project> post(@RequestBody Project project) {
Project savedProject = projectService.saveProject(project); Project savedProject = projectActions.saveProject(project);
return ResponseEntity.status(HttpStatus.CREATED).body(savedProject); return ResponseEntity.status(HttpStatus.CREATED).body(savedProject);
} }
@ -96,7 +99,7 @@ public class ProjectController {
*/ */
@PutMapping({ "/", "" }) @PutMapping({ "/", "" })
public ResponseEntity<Project> put(@RequestBody Project project) { public ResponseEntity<Project> put(@RequestBody Project project) {
Project updatedProject = projectService.saveProject(project); Project updatedProject = projectActions.saveProject(project);
return ResponseEntity.ok(updatedProject); return ResponseEntity.ok(updatedProject);
} }
@ -109,7 +112,7 @@ public class ProjectController {
*/ */
@DeleteMapping({ "/", "" }) @DeleteMapping({ "/", "" })
public ResponseEntity<Void> delete(@RequestParam Long pid) { public ResponseEntity<Void> delete(@RequestParam Long pid) {
projectService.deleteProject(pid); projectActions.deleteProject(pid);
return ResponseEntity.noContent().build(); return ResponseEntity.noContent().build();
} }
} }

View File

@ -15,7 +15,8 @@ import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import com.maradona.backend.entities.SecondarySkill; 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;
/** /**
* Controller for handling secondary skill data. * Controller for handling secondary skill data.
@ -35,7 +36,10 @@ import com.maradona.backend.services.SecondarySkillService;
public class SecondarySkillController { public class SecondarySkillController {
@Autowired @Autowired
private SecondarySkillService secondarySkillService; private SecondarySkillDetails secondarySkillDetails;
@Autowired
private SecondarySkillActions secondarySkillActions;
/** /**
* Returns a specific secondary skill from the database. * Returns a specific secondary skill from the database.
@ -46,7 +50,7 @@ public class SecondarySkillController {
*/ */
@GetMapping({ "/", "" }) @GetMapping({ "/", "" })
public ResponseEntity<SecondarySkill> get(@RequestParam Long ssid) { public ResponseEntity<SecondarySkill> get(@RequestParam Long ssid) {
Optional<SecondarySkill> secondarySkill = secondarySkillService.getSecondarySkillBySsid(ssid); Optional<SecondarySkill> secondarySkill = secondarySkillDetails.getSecondarySkillBySsid(ssid);
return secondarySkill.map(ResponseEntity::ok).orElseGet(() -> ResponseEntity.notFound().build()); return secondarySkill.map(ResponseEntity::ok).orElseGet(() -> ResponseEntity.notFound().build());
} }
@ -58,7 +62,7 @@ public class SecondarySkillController {
*/ */
@GetMapping("/all") @GetMapping("/all")
public ResponseEntity<Iterable<SecondarySkill>> getAll() { public ResponseEntity<Iterable<SecondarySkill>> getAll() {
return ResponseEntity.ok(secondarySkillService.getAllSecondarySkills()); return ResponseEntity.ok(secondarySkillDetails.getAllSecondarySkills());
} }
/** /**
@ -71,7 +75,7 @@ public class SecondarySkillController {
*/ */
@GetMapping("/from-primary-skill") @GetMapping("/from-primary-skill")
public ResponseEntity<Iterable<SecondarySkill>> getSecondarySkills(@RequestParam Long psid) { public ResponseEntity<Iterable<SecondarySkill>> getSecondarySkills(@RequestParam Long psid) {
return ResponseEntity.ok(secondarySkillService.getSecondarySkillsByPrimarySkillId(psid)); return ResponseEntity.ok(secondarySkillDetails.getSecondarySkillsByPrimarySkillId(psid));
} }
/** /**
@ -83,7 +87,7 @@ public class SecondarySkillController {
*/ */
@PostMapping({ "/", "" }) @PostMapping({ "/", "" })
public ResponseEntity<SecondarySkill> post(@RequestBody SecondarySkill secondarySkill) { public ResponseEntity<SecondarySkill> post(@RequestBody SecondarySkill secondarySkill) {
SecondarySkill savedSecondarySkill = secondarySkillService.saveSecondarySkill(secondarySkill); SecondarySkill savedSecondarySkill = secondarySkillActions.saveSecondarySkill(secondarySkill);
return ResponseEntity.status(HttpStatus.CREATED).body(savedSecondarySkill); return ResponseEntity.status(HttpStatus.CREATED).body(savedSecondarySkill);
} }
@ -96,7 +100,7 @@ public class SecondarySkillController {
*/ */
@PutMapping({ "/", "" }) @PutMapping({ "/", "" })
public ResponseEntity<SecondarySkill> put(@RequestBody SecondarySkill secondarySkill) { public ResponseEntity<SecondarySkill> put(@RequestBody SecondarySkill secondarySkill) {
SecondarySkill updatedSecondarySkill = secondarySkillService.saveSecondarySkill(secondarySkill); SecondarySkill updatedSecondarySkill = secondarySkillActions.saveSecondarySkill(secondarySkill);
return ResponseEntity.ok(updatedSecondarySkill); return ResponseEntity.ok(updatedSecondarySkill);
} }
@ -109,7 +113,7 @@ public class SecondarySkillController {
*/ */
@DeleteMapping({ "/", "" }) @DeleteMapping({ "/", "" })
public ResponseEntity<Void> delete(@RequestParam Long ssid) { public ResponseEntity<Void> delete(@RequestParam Long ssid) {
secondarySkillService.deleteSecondarySkill(ssid); secondarySkillActions.deleteSecondarySkill(ssid);
return ResponseEntity.noContent().build(); return ResponseEntity.noContent().build();
} }
} }

View File

@ -6,7 +6,7 @@ import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import com.maradona.backend.entities.Project; import com.maradona.backend.entities.Project;
import com.maradona.backend.services.ProjectService; import com.maradona.backend.services.details.ProjectDetails;
/** /**
* Controller for routing to project related pages. * Controller for routing to project related pages.
@ -21,7 +21,7 @@ import com.maradona.backend.services.ProjectService;
@RequestMapping("/projects") @RequestMapping("/projects")
public class ProjectPage { public class ProjectPage {
@Autowired @Autowired
private ProjectService projectService; private ProjectDetails projectDetails;
/** /**
* Returns the projects overview page. * Returns the projects overview page.
@ -36,7 +36,7 @@ public class ProjectPage {
*/ */
@GetMapping({ "/", "" }) @GetMapping({ "/", "" })
public String projects(Model model) { public String projects(Model model) {
var projects = projectService.getAllProjects(); var projects = projectDetails.getAllProjects();
model.addAttribute("projects", projects); model.addAttribute("projects", projects);
return "/pages/projects/overview"; return "/pages/projects/overview";
} }

View File

@ -7,9 +7,9 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import com.maradona.backend.entities.SecondarySkill; import com.maradona.backend.entities.SecondarySkill;
import com.maradona.backend.services.EmployeeService; import com.maradona.backend.services.details.EmployeeDetails;
import com.maradona.backend.services.SkillService; import com.maradona.backend.services.details.PrimarySkillDetails;
import com.maradona.backend.services.PrimarySkillService; import com.maradona.backend.services.transfer.SkillService;
import com.maradona.backend.dto.SkillPrototype; import com.maradona.backend.dto.SkillPrototype;
/** /**
@ -26,13 +26,13 @@ import com.maradona.backend.dto.SkillPrototype;
@RequestMapping("/skills") @RequestMapping("/skills")
public class SkillsPage { public class SkillsPage {
@Autowired @Autowired
private EmployeeService employeeService; private EmployeeDetails employeeDetails;
@Autowired @Autowired
private SkillService skillService; private SkillService skillService;
@Autowired @Autowired
private PrimarySkillService primarySkillService; private PrimarySkillDetails primarySkillDetails;
// Hardcoded placeholder user ID for now // Hardcoded placeholder user ID for now
Long user = Long.valueOf(1); Long user = Long.valueOf(1);
@ -52,7 +52,7 @@ public class SkillsPage {
*/ */
@GetMapping({ "/", "" }) @GetMapping({ "/", "" })
public String profile(Model model) { public String profile(Model model) {
model.addAttribute("employee", employeeService.getEmployeeByEid(user).orElse(null)); model.addAttribute("employee", employeeDetails.getEmployeeByEid(user).orElse(null));
model.addAttribute("skills", skillService.getUserSkills(user)); model.addAttribute("skills", skillService.getUserSkills(user));
return "/pages/skills/overview"; return "/pages/skills/overview";
} }
@ -93,7 +93,7 @@ public class SkillsPage {
@GetMapping("/add") @GetMapping("/add")
public String addSkill(Model model) { public String addSkill(Model model) {
// TODO: Make sure it returns the correct initail data for secondary skills // TODO: Make sure it returns the correct initail data for secondary skills
model.addAttribute("primarySkills", primarySkillService.getAllPrimarySkills()); model.addAttribute("primarySkills", primarySkillDetails.getAllPrimarySkills());
model.addAttribute("skillProtoype", new SkillPrototype()); model.addAttribute("skillProtoype", new SkillPrototype());
return "/pages/skills/add"; return "/pages/skills/add";
} }

View File

@ -1,36 +1,29 @@
package com.maradona.backend.services; package com.maradona.backend.services.actions;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import com.maradona.backend.entities.Employee; import com.maradona.backend.entities.Employee;
import com.maradona.backend.entities.EmployeeSecondarySkill; import com.maradona.backend.entities.EmployeeSecondarySkill;
import com.maradona.backend.repositories.EmployeeRepository; import com.maradona.backend.repositories.EmployeeRepository;
import com.maradona.backend.services.details.EmployeeDetails;
import com.maradona.backend.services.details.SecondarySkillDetails;
import com.maradona.backend.dto.SkillPrototype; import com.maradona.backend.dto.SkillPrototype;
import java.util.Optional;
@Service @Service
public class EmployeeService { public class EmployeeActions {
@Autowired @Autowired
private EmployeeRepository employeeRepository; private EmployeeRepository employeeRepository;
@Autowired @Autowired
private SecondarySkillService secondarySkillService; private EmployeeDetails employeeDetails;
@Autowired
private SecondarySkillDetails secondarySkillDetails;
public Employee saveEmployee(Employee employee) { public Employee saveEmployee(Employee employee) {
return employeeRepository.save(employee); return employeeRepository.save(employee);
} }
public Optional<Employee> getEmployeeByEid(Long eid) {
var employees = employeeRepository.findAll();
for (Employee employee : employees) {
if (employee.getEid().equals(eid)) {
return Optional.of(employee);
}
}
return Optional.empty();
}
public void deleteEmployee(Long eid) { public void deleteEmployee(Long eid) {
for (Employee employee : employeeRepository.findAll()) { for (Employee employee : employeeRepository.findAll()) {
if (employee.getEid().equals(eid)) { if (employee.getEid().equals(eid)) {
@ -40,13 +33,9 @@ public class EmployeeService {
} }
} }
public Iterable<Employee> getAllEmployees() {
return employeeRepository.findAll();
}
public void addSecondarySkillToEmployee(Long eid, SkillPrototype skillPrototype) { public void addSecondarySkillToEmployee(Long eid, SkillPrototype skillPrototype) {
var employee = getEmployeeByEid(eid).orElseThrow(() -> new RuntimeException("Employee not found")); var employee = employeeDetails.getEmployeeByEid(eid).orElseThrow(() -> new RuntimeException("Employee not found"));
var secondarySkill = secondarySkillService.getSecondarySkillBySsid(skillPrototype.getSsid()) var secondarySkill = secondarySkillDetails.getSecondarySkillBySsid(skillPrototype.getSsid())
.orElseThrow(() -> new RuntimeException("Secondary Skill not found")); .orElseThrow(() -> new RuntimeException("Secondary Skill not found"));
for (EmployeeSecondarySkill skill : employee.getSecondarySkills()) { for (EmployeeSecondarySkill skill : employee.getSecondarySkills()) {
@ -64,8 +53,8 @@ public class EmployeeService {
} }
public void updateSecondarySkillLevel(Long eid, Long ssid, Integer level) { public void updateSecondarySkillLevel(Long eid, Long ssid, Integer level) {
var employee = getEmployeeByEid(eid).orElseThrow(() -> new RuntimeException("Employee not found")); var employee = employeeDetails.getEmployeeByEid(eid).orElseThrow(() -> new RuntimeException("Employee not found"));
secondarySkillService.getSecondarySkillBySsid( secondarySkillDetails.getSecondarySkillBySsid(
ssid) ssid)
.orElseThrow(() -> new RuntimeException("Secondary Skill not found")); .orElseThrow(() -> new RuntimeException("Secondary Skill not found"));
@ -80,18 +69,12 @@ public class EmployeeService {
} }
public void deleteSecondarySkillFromEmployee(Long eid, Long ssid) { public void deleteSecondarySkillFromEmployee(Long eid, Long ssid) {
var employee = getEmployeeByEid(eid).orElseThrow(() -> new RuntimeException("Employee not found")); var employee = employeeDetails.getEmployeeByEid(eid).orElseThrow(() -> new RuntimeException("Employee not found"));
secondarySkillService.getSecondarySkillBySsid( secondarySkillDetails.getSecondarySkillBySsid(
ssid) ssid)
.orElseThrow(() -> new RuntimeException("Secondary Skill not found")); .orElseThrow(() -> new RuntimeException("Secondary Skill not found"));
employee.getSecondarySkills().removeIf(skill -> skill.getSecondarySkill().getSsid().equals(ssid)); employee.getSecondarySkills().removeIf(skill -> skill.getSecondarySkill().getSsid().equals(ssid));
saveEmployee(employee); saveEmployee(employee);
} }
public Iterable<Employee> getEmployeesBySecondarySkill(Long ssid, Integer level) {
// Implement logic to fetch employees by secondary skill and level
// TODO: This is a placeholder implementation
return employeeRepository.findAll();
}
} }

View File

@ -1,32 +1,24 @@
package com.maradona.backend.services; package com.maradona.backend.services.actions;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import com.maradona.backend.entities.FormOfAddress; import com.maradona.backend.entities.FormOfAddress;
import com.maradona.backend.repositories.FormOfAddressRepository; import com.maradona.backend.repositories.FormOfAddressRepository;
import com.maradona.backend.services.details.FormOfAddressDetails;
import java.util.Optional;
@Service @Service
public class FormOfAddressService { public class FormOfAddressActions {
@Autowired @Autowired
private FormOfAddressRepository formOfAddressRepository; private FormOfAddressRepository formOfAddressRepository;
@Autowired
private FormOfAddressDetails formOfAddressDetails;
public FormOfAddress saveFormOfAddress(FormOfAddress formOfAddress) { public FormOfAddress saveFormOfAddress(FormOfAddress formOfAddress) {
return formOfAddressRepository.save(formOfAddress); return formOfAddressRepository.save(formOfAddress);
} }
public Optional<FormOfAddress> getFormOfAddressByFid(Long fid) {
var formOfAddresses = formOfAddressRepository.findAll();
for (FormOfAddress formOfAddress : formOfAddresses) {
if (formOfAddress.getFid().equals(fid)) {
return Optional.of(formOfAddress);
}
}
return Optional.empty();
}
public void deleteFormOfAddress(Long fid) { public void deleteFormOfAddress(Long fid) {
for (FormOfAddress formOfAddress : formOfAddressRepository.findAll()) { for (FormOfAddress formOfAddress : formOfAddressRepository.findAll()) {
if (formOfAddress.getFid().equals(fid)) { if (formOfAddress.getFid().equals(fid)) {
@ -36,12 +28,9 @@ public class FormOfAddressService {
} }
} }
public Iterable<FormOfAddress> getAllFormOfAddresses() {
return formOfAddressRepository.findAll();
}
public void updateFormOfAddress(Long fid, String description) { public void updateFormOfAddress(Long fid, String description) {
var formOfAddress = getFormOfAddressByFid(fid).orElseThrow(() -> new RuntimeException("Form of Address not found")); var formOfAddress = formOfAddressDetails.getFormOfAddressByFid(fid)
.orElseThrow(() -> new RuntimeException("Form of Address not found"));
formOfAddress.setDescription(description); formOfAddress.setDescription(description);
saveFormOfAddress(formOfAddress); saveFormOfAddress(formOfAddress);
} }

View File

@ -0,0 +1,42 @@
package com.maradona.backend.services.actions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.maradona.backend.entities.PrimarySkill;
import com.maradona.backend.entities.SecondarySkill;
import com.maradona.backend.repositories.PrimarySkillRepository;
import com.maradona.backend.repositories.SecondarySkillRepository;
import com.maradona.backend.services.details.SecondarySkillDetails;
@Service
public class PrimarySkillActions {
@Autowired
private PrimarySkillRepository primarySkillRepository;
@Autowired
private SecondarySkillDetails secondarySkillDetails;
@Autowired
private SecondarySkillRepository secondarySkillRepository;
public PrimarySkill savePrimarySkill(PrimarySkill primarySkill) {
return primarySkillRepository.save(primarySkill);
}
public void deletePrimarySkill(Long psid) {
var primarySkills = primarySkillRepository.findAll();
var secondarySkills = secondarySkillDetails.getAllSecondarySkills();
for (SecondarySkill secondarySkill : secondarySkills) {
if (secondarySkill.getPrimarySkill().getPsid().equals(psid)) {
var skillToDelete = secondarySkillDetails.getSecondarySkillBySsid(psid);
secondarySkillRepository.delete(skillToDelete.orElse(null));
}
}
for (PrimarySkill primarySkill : primarySkills) {
if (primarySkill.getPsid().equals(psid)) {
primarySkillRepository.delete(primarySkill);
}
}
}
}

View File

@ -0,0 +1,26 @@
package com.maradona.backend.services.actions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.maradona.backend.entities.Project;
import com.maradona.backend.repositories.ProjectRepository;
@Service
public class ProjectActions {
@Autowired
private ProjectRepository projectRepository;
public Project saveProject(Project project) {
return projectRepository.save(project);
}
public void deleteProject(Long pid) {
var projects = projectRepository.findAll();
for (Project project : projects) {
if (project.getPid().equals(pid)) {
projectRepository.delete(project);
}
}
}
}

View File

@ -0,0 +1,42 @@
package com.maradona.backend.services.actions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.maradona.backend.entities.SecondarySkill;
import com.maradona.backend.repositories.PrimarySkillRepository;
import com.maradona.backend.repositories.SecondarySkillRepository;
import com.maradona.backend.services.details.PrimarySkillDetails;
@Service
public class SecondarySkillActions {
@Autowired
private SecondarySkillRepository secondarySkillRepository;
@Autowired
private PrimarySkillDetails primarySkillDetails;
@Autowired
private PrimarySkillRepository primarySkillrRepository;
public SecondarySkill saveSecondarySkill(SecondarySkill secondarySkill) {
var primarySkillDescription = secondarySkill.getPrimarySkill().getDescription();
var existingPrimarySkill = primarySkillDetails.findByDescription(primarySkillDescription);
if (existingPrimarySkill.isPresent()) {
secondarySkill.setPrimarySkill(existingPrimarySkill.get());
} else {
primarySkillrRepository.save(secondarySkill.getPrimarySkill());
}
return secondarySkillRepository.save(secondarySkill);
}
public void deleteSecondarySkill(Long ssid) {
var secondarySkills = secondarySkillRepository.findAll();
for (SecondarySkill secondarySkill : secondarySkills) {
if (secondarySkill.getSsid().equals(ssid)) {
secondarySkillRepository.delete(secondarySkill);
}
}
}
}

View File

@ -0,0 +1,34 @@
package com.maradona.backend.services.details;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.maradona.backend.entities.Employee;
import com.maradona.backend.repositories.EmployeeRepository;
import java.util.Optional;
@Service
public class EmployeeDetails {
@Autowired
private EmployeeRepository employeeRepository;
public Optional<Employee> getEmployeeByEid(Long eid) {
var employees = employeeRepository.findAll();
for (Employee employee : employees) {
if (employee.getEid().equals(eid)) {
return Optional.of(employee);
}
}
return Optional.empty();
}
public Iterable<Employee> getAllEmployees() {
return employeeRepository.findAll();
}
public Iterable<Employee> getEmployeesBySecondarySkill(Long ssid, Integer level) {
// Implement logic to fetch employees by secondary skill and level
// TODO: This is a placeholder implementation
return employeeRepository.findAll();
}
}

View File

@ -0,0 +1,29 @@
package com.maradona.backend.services.details;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.maradona.backend.entities.FormOfAddress;
import com.maradona.backend.repositories.FormOfAddressRepository;
import java.util.Optional;
@Service
public class FormOfAddressDetails {
@Autowired
private FormOfAddressRepository formOfAddressRepository;
public Optional<FormOfAddress> getFormOfAddressByFid(Long fid) {
var formOfAddresses = formOfAddressRepository.findAll();
for (FormOfAddress formOfAddress : formOfAddresses) {
if (formOfAddress.getFid().equals(fid)) {
return Optional.of(formOfAddress);
}
}
return Optional.empty();
}
public Iterable<FormOfAddress> getAllFormOfAddresses() {
return formOfAddressRepository.findAll();
}
}

View File

@ -1,26 +1,18 @@
package com.maradona.backend.services; package com.maradona.backend.services.details;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import com.maradona.backend.entities.PrimarySkill; import com.maradona.backend.entities.PrimarySkill;
import com.maradona.backend.entities.SecondarySkill;
import com.maradona.backend.repositories.PrimarySkillRepository; import com.maradona.backend.repositories.PrimarySkillRepository;
import java.util.Optional; import java.util.Optional;
@Service @Service
public class PrimarySkillService { public class PrimarySkillDetails {
@Autowired @Autowired
private PrimarySkillRepository primarySkillRepository; private PrimarySkillRepository primarySkillRepository;
@Autowired
private SecondarySkillService secondarySkillRepository;
public PrimarySkill savePrimarySkill(PrimarySkill primarySkill) {
return primarySkillRepository.save(primarySkill);
}
public Optional<PrimarySkill> getPrimarySkillByPsid(Long psid) { public Optional<PrimarySkill> getPrimarySkillByPsid(Long psid) {
var primarySkills = primarySkillRepository.findAll(); var primarySkills = primarySkillRepository.findAll();
for (PrimarySkill primarySkill : primarySkills) { for (PrimarySkill primarySkill : primarySkills) {
@ -31,21 +23,6 @@ public class PrimarySkillService {
return Optional.empty(); return Optional.empty();
} }
public void deletePrimarySkill(Long psid) {
var primarySkills = primarySkillRepository.findAll();
var secondarySkills = secondarySkillRepository.getAllSecondarySkills();
for (SecondarySkill secondarySkill : secondarySkills) {
if (secondarySkill.getPrimarySkill().getPsid().equals(psid)) {
secondarySkillRepository.deleteSecondarySkill(secondarySkill.getSsid());
}
}
for (PrimarySkill primarySkill : primarySkills) {
if (primarySkill.getPsid().equals(psid)) {
primarySkillRepository.delete(primarySkill);
}
}
}
public Iterable<PrimarySkill> getAllPrimarySkills() { public Iterable<PrimarySkill> getAllPrimarySkills() {
return primarySkillRepository.findAll(); return primarySkillRepository.findAll();
} }

View File

@ -1,4 +1,4 @@
package com.maradona.backend.services; package com.maradona.backend.services.details;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -8,15 +8,11 @@ import com.maradona.backend.repositories.ProjectRepository;
import java.util.Optional; import java.util.Optional;
@Service @Service
public class ProjectService { public class ProjectDetails {
@Autowired @Autowired
private ProjectRepository projectRepository; private ProjectRepository projectRepository;
public Project saveProject(Project project) {
return projectRepository.save(project);
}
public Optional<Project> getProjectByPid(Long pid) { public Optional<Project> getProjectByPid(Long pid) {
var projects = projectRepository.findAll(); var projects = projectRepository.findAll();
for (Project project : projects) { for (Project project : projects) {
@ -27,15 +23,6 @@ public class ProjectService {
return Optional.empty(); return Optional.empty();
} }
public void deleteProject(Long pid) {
var projects = projectRepository.findAll();
for (Project project : projects) {
if (project.getPid().equals(pid)) {
projectRepository.delete(project);
}
}
}
public Iterable<Project> getAllProjects() { public Iterable<Project> getAllProjects() {
return projectRepository.findAll(); return projectRepository.findAll();
} }

View File

@ -1,4 +1,4 @@
package com.maradona.backend.services; package com.maradona.backend.services.details;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -8,26 +8,10 @@ import com.maradona.backend.repositories.SecondarySkillRepository;
import java.util.Optional; import java.util.Optional;
@Service @Service
public class SecondarySkillService { public class SecondarySkillDetails {
@Autowired @Autowired
private SecondarySkillRepository secondarySkillRepository; private SecondarySkillRepository secondarySkillRepository;
@Autowired
private PrimarySkillService primarySkillService;
public SecondarySkill saveSecondarySkill(SecondarySkill secondarySkill) {
var primarySkillDescription = secondarySkill.getPrimarySkill().getDescription();
var existingPrimarySkill = primarySkillService.findByDescription(primarySkillDescription);
if (existingPrimarySkill.isPresent()) {
secondarySkill.setPrimarySkill(existingPrimarySkill.get());
} else {
primarySkillService.savePrimarySkill(secondarySkill.getPrimarySkill());
}
return secondarySkillRepository.save(secondarySkill);
}
public Optional<SecondarySkill> getSecondarySkillBySsid(Long ssid) { public Optional<SecondarySkill> getSecondarySkillBySsid(Long ssid) {
var secondarySkills = secondarySkillRepository.findAll(); var secondarySkills = secondarySkillRepository.findAll();
for (SecondarySkill secondarySkill : secondarySkills) { for (SecondarySkill secondarySkill : secondarySkills) {
@ -38,23 +22,19 @@ public class SecondarySkillService {
return Optional.empty(); return Optional.empty();
} }
public void deleteSecondarySkill(Long ssid) {
secondarySkillRepository.deleteById(ssid);
}
public Iterable<SecondarySkill> getAllSecondarySkills() {
return secondarySkillRepository.findAll();
}
public Iterable<SecondarySkill> getSecondarySkillsByPrimarySkillId(Long psid) { public Iterable<SecondarySkill> getSecondarySkillsByPrimarySkillId(Long psid) {
var skills = secondarySkillRepository.findAll(); var skills = secondarySkillRepository.findAll();
var result = new java.util.ArrayList<SecondarySkill>(); var result = new java.util.ArrayList<SecondarySkill>();
for (SecondarySkill skill : skills) { for (SecondarySkill skill : skills) {
if (skill.getPrimarySkill().getPsid().equals(psid)) { if (skill.getPrimarySkill().getPsid().equals(psid)) {
result.add(skill); result.add(skill);
System.out.println(skill.getDescription());
} }
} }
return result; return result;
} }
public Iterable<SecondarySkill> getAllSecondarySkills() {
return secondarySkillRepository.findAll();
}
} }

View File

@ -1,9 +1,13 @@
package com.maradona.backend.services; package com.maradona.backend.services.transfer;
import com.maradona.backend.dto.SkillList; import com.maradona.backend.dto.SkillList;
import com.maradona.backend.entities.Employee; import com.maradona.backend.entities.Employee;
import com.maradona.backend.entities.PrimarySkill; import com.maradona.backend.entities.PrimarySkill;
import com.maradona.backend.entities.SecondarySkill; import com.maradona.backend.entities.SecondarySkill;
import com.maradona.backend.services.details.EmployeeDetails;
import com.maradona.backend.services.details.PrimarySkillDetails;
import com.maradona.backend.services.details.SecondarySkillDetails;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.data.util.Pair; import org.springframework.data.util.Pair;
@ -16,17 +20,17 @@ import java.util.Map;
public class SkillService { public class SkillService {
@Autowired @Autowired
private PrimarySkillService primarySkillService; private PrimarySkillDetails primarySkillDetails;
@Autowired @Autowired
private SecondarySkillService secondarySkillService; private SecondarySkillDetails secondarySkillDetails;
@Autowired @Autowired
private EmployeeService employeeService; private EmployeeDetails employeeDetails;
public Iterable<SkillList> getAllSkills() { public Iterable<SkillList> getAllSkills() {
Iterable<PrimarySkill> primarySkills = primarySkillService.getAllPrimarySkills(); Iterable<PrimarySkill> primarySkills = primarySkillDetails.getAllPrimarySkills();
Iterable<SecondarySkill> secondarySkills = secondarySkillService.getAllSecondarySkills(); Iterable<SecondarySkill> secondarySkills = secondarySkillDetails.getAllSecondarySkills();
List<SkillList> skills = new ArrayList<>(); List<SkillList> skills = new ArrayList<>();
for (PrimarySkill primarySkill : primarySkills) { for (PrimarySkill primarySkill : primarySkills) {
@ -45,7 +49,7 @@ public class SkillService {
if (eid == null) { if (eid == null) {
return new ArrayList<>(); return new ArrayList<>();
} }
Employee employee = employeeService.getEmployeeByEid(eid).orElse(null); Employee employee = employeeDetails.getEmployeeByEid(eid).orElse(null);
if (employee == null) { if (employee == null) {
return new ArrayList<>(); return new ArrayList<>();
} }
@ -53,7 +57,7 @@ public class SkillService {
Map<SecondarySkill, Integer> secondarySkillLevels = employee.getSecondarySkillLevels(); Map<SecondarySkill, Integer> secondarySkillLevels = employee.getSecondarySkillLevels();
List<SkillList> skills = new ArrayList<>(); List<SkillList> skills = new ArrayList<>();
for (PrimarySkill primarySkill : primarySkillService.getAllPrimarySkills()) { for (PrimarySkill primarySkill : primarySkillDetails.getAllPrimarySkills()) {
List<Pair<SecondarySkill, Integer>> secondarySkillList = new ArrayList<>(); List<Pair<SecondarySkill, Integer>> secondarySkillList = new ArrayList<>();
for (Map.Entry<SecondarySkill, Integer> entry : secondarySkillLevels.entrySet()) { for (Map.Entry<SecondarySkill, Integer> entry : secondarySkillLevels.entrySet()) {
if (entry.getKey().getPrimarySkill().getPsid().equals(primarySkill.getPsid())) { if (entry.getKey().getPrimarySkill().getPsid().equals(primarySkill.getPsid())) {

View File

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

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.setId(1L); // formOfAddress.setFid(1L);
formOfAddress.setDescription("Mr."); // formOfAddress.setDescription("Mr.");
//
when(formOfAdressService.getFormOfAddressById(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.setId(1L); // form1.setFid(1L);
form1.setDescription("Mr."); // form1.setDescription("Mr.");
//
FormOfAddress form2 = new FormOfAddress(); // FormOfAddress form2 = new FormOfAddress();
form2.setId(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.setId(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.setId(1L); // existingForm.setFid(1L);
existingForm.setDescription("Mr."); // existingForm.setDescription("Mr.");
//
when(formOfAdressService.getFormOfAddressById(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.getFormOfAddressById(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.setId(1L); // formOfAddress.setFid(1L);
formOfAddress.setDescription("Mr."); // formOfAddress.setDescription("Mr.");
//
when(formOfAdressService.getFormOfAddressById(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.getFormOfAddressById(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.getPrimarySkillById(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)).getPrimarySkillById(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.getPrimarySkillById(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.getPrimarySkillById(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,199 @@
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.setId(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.getProjectById(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.setId(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.setId(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.setId(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.setId(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 projects for a specific user
when(projectService.getProjectsByUserId(userId)).thenReturn(Arrays.asList(project1, project2)); //// when(projectService.getProjectByPid(1L)).thenReturn(Arrays.asList(project1, project2));
////
// Act & Assert: Send GET request and expect a 200 OK status with the correct JSON response //// // Act & Assert: Send GET request and expect a 200 OK status with the correct JSON response
mockMvc.perform(get("/api/project/from-user") //// mockMvc.perform(get("/api/project/from-user")
.param("userId", String.valueOf(userId)) //// .param("userId", String.valueOf(userId))
.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("Skill Management System")) //// .andExpect(jsonPath("$[0].name").value("Skill Management System"))
.andExpect(jsonPath("$[1].id").value(2)) //// .andExpect(jsonPath("$[1].id").value(2))
.andExpect(jsonPath("$[1].name").value("Project Management Tool")); //// .andExpect(jsonPath("$[1].name").value("Project Management Tool"));
} //// }
//
@Test // @Test
public void testCreateProject() throws Exception { // public void testCreateProject() throws Exception {
// Arrange: Create a new project and set all required fields // // Arrange: Create a new project and set all required fields
Project savedProject = new Project(); // Project savedProject = new Project();
savedProject.setId(1L); // ID setzen // savedProject.setPid(1L); // ID setzen
savedProject.setName("New Project"); // savedProject.setName("New Project");
savedProject.setStartDate(LocalDate.of(2024, 11, 10)); // savedProject.setStartDate(LocalDate.of(2024, 11, 10));
savedProject.setEndDate(LocalDate.of(2024, 12, 10)); // savedProject.setEndDate(LocalDate.of(2024, 12, 10));
savedProject.setWorkload(15); // savedProject.setWorkload(15);
savedProject.setDescription("A new project for testing"); // savedProject.setDescription("A new project for testing");
//
// Mocking the saveProject method to return this specific project // // Mocking the saveProject method to return this specific project
when(projectService.saveProject(any(Project.class))).thenReturn(savedProject); // when(projectService.saveProject(any(Project.class))).thenReturn(savedProject);
//
// Act & Assert: Send POST request and check for 201 Created with JSON response // // Act & Assert: Send POST request and check for 201 Created with JSON response
mockMvc.perform(post("/api/project") // mockMvc.perform(post("/api/project")
.contentType(MediaType.APPLICATION_JSON) // .contentType(MediaType.APPLICATION_JSON)
.content("{\"name\":\"New Project\",\"startDate\":\"2024-11-10\",\"endDate\":\"2024-12-10\",\"workload\":15,\"description\":\"A new project for testing\"}")) // .content("{\"name\":\"New Project\",\"startDate\":\"2024-11-10\",\"endDate\":\"2024-12-10\",\"workload\":15,\"description\":\"A new project for testing\"}"))
.andExpect(status().isCreated()) // .andExpect(status().isCreated())
.andExpect(jsonPath("$.id").value(1)) // Ensure the response JSON contains id = 1 // .andExpect(jsonPath("$.id").value(1)) // Ensure the response JSON contains id = 1
.andExpect(jsonPath("$.name").value("New Project")) // .andExpect(jsonPath("$.name").value("New Project"))
.andExpect(jsonPath("$.workload").value(15)) // .andExpect(jsonPath("$.workload").value(15))
.andExpect(jsonPath("$.description").value("A new project for testing")); // .andExpect(jsonPath("$.description").value("A new project for testing"));
} // }
//
@Test // @Test
public void testUpdateProject() throws Exception { // public void testUpdateProject() throws Exception {
// Arrange: Create an existing project // // Arrange: Create an existing project
Project project = new Project(); // Project project = new Project();
project.setId(1L); // project.setPid(1L);
project.setName("Updated Project"); // project.setName("Updated Project");
project.setStartDate(LocalDate.of(2024, 11, 10)); // project.setStartDate(LocalDate.of(2024, 11, 10));
project.setEndDate(LocalDate.of(2024, 12, 10)); // project.setEndDate(LocalDate.of(2024, 12, 10));
project.setWorkload(20); // project.setWorkload(20);
project.setDescription("An updated project description"); // project.setDescription("An updated project description");
//
// Mock the ProjectService to return the updated project // // Mock the ProjectService to return the updated project
when(projectService.saveProject(project)).thenReturn(project); // when(projectService.saveProject(project)).thenReturn(project);
//
// Act & Assert: Send PUT request and expect a 200 OK status with the updated project as JSON response // // Act & Assert: Send PUT request and expect a 200 OK status with the updated project as JSON response
mockMvc.perform(put("/api/project") // mockMvc.perform(put("/api/project")
.contentType(MediaType.APPLICATION_JSON) // .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("{\"id\":1,\"name\":\"Updated Project\",\"startDate\":\"2024-11-10\",\"endDate\":\"2024-12-10\",\"workload\":20,\"description\":\"An updated project description\"}"))
.andExpect(status().isOk()) // .andExpect(status().isOk())
.andExpect(jsonPath("$.id").value(1)) // .andExpect(jsonPath("$.id").value(1))
.andExpect(jsonPath("$.name").value("Updated Project")) // .andExpect(jsonPath("$.name").value("Updated Project"))
.andExpect(jsonPath("$.workload").value(20)) // .andExpect(jsonPath("$.workload").value(20))
.andExpect(jsonPath("$.description").value("An updated project description")); // .andExpect(jsonPath("$.description").value("An updated project description"));
} // }
//
@Test // @Test
public void testDeleteProject() throws Exception { // public void testDeleteProject() throws Exception {
// Arrange: Define the ID of the project to delete // // Arrange: Define the ID of the project to delete
Long projectId = 1L; // Long projectId = 1L;
//
// No need to mock the delete method as it returns void, we just ensure no exception is thrown // // 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 // // Act & Assert: Send DELETE request and expect a 204 No Content status
mockMvc.perform(delete("/api/project") // mockMvc.perform(delete("/api/project")
.param("id", String.valueOf(projectId)) // .param("id", String.valueOf(projectId))
.contentType(MediaType.APPLICATION_JSON)) // .contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isNoContent()); // .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.getSecondarySkillById(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());
} // }
} //}