From 53a91861737c42b5191eea06d9f732aaccaf6d03 Mon Sep 17 00:00:00 2001 From: JennGoldring Date: Mon, 12 Jun 2023 11:41:46 -0500 Subject: [PATCH] Part 4 half way --- .../controllers/HomeController.java | 22 +++++- .../techjobs/persistent/models/Job.java | 11 +-- .../techjobs/persistent/models/Skill.java | 5 +- .../techjobs/persistent/TestTaskFour.java | 72 ++++++++++--------- 4 files changed, 68 insertions(+), 42 deletions(-) diff --git a/src/main/java/org/launchcode/techjobs/persistent/controllers/HomeController.java b/src/main/java/org/launchcode/techjobs/persistent/controllers/HomeController.java index b92335e..585f5bd 100644 --- a/src/main/java/org/launchcode/techjobs/persistent/controllers/HomeController.java +++ b/src/main/java/org/launchcode/techjobs/persistent/controllers/HomeController.java @@ -2,7 +2,10 @@ import org.launchcode.techjobs.persistent.models.Employer; import org.launchcode.techjobs.persistent.models.Job; +import org.launchcode.techjobs.persistent.models.Skill; import org.launchcode.techjobs.persistent.models.data.EmployerRepository; +import org.launchcode.techjobs.persistent.models.data.JobRepository; +import org.launchcode.techjobs.persistent.models.data.SkillRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; @@ -10,6 +13,7 @@ import org.springframework.web.bind.annotation.*; import javax.validation.Valid; +import java.util.List; /** * Created by LaunchCode @@ -20,6 +24,11 @@ public class HomeController { @Autowired private EmployerRepository employerRepository; + @Autowired + private SkillRepository skillRepository; + @Autowired + private JobRepository jobRepository; + public HomeController() { } @@ -34,30 +43,37 @@ public String displayAddJobForm(Model model) { model.addAttribute("job", new Job()); // List employers = (List) employerRepository.findAll(); model.addAttribute("employers", employerRepository.findAll()); + model.addAttribute("skills", skillRepository.findAll()); // model.addAttribute(new Job()); return "add"; } @PostMapping("add") public String processAddJobForm(@ModelAttribute @Valid Job newJob, - Errors errors, Model model, @RequestParam int employerId) { + Errors errors, Model model, @RequestParam int employerId, @RequestParam List skills) { if (errors.hasErrors()) { model.addAttribute("title", "Add Job"); model.addAttribute("employers", employerRepository.findAll()); + model.addAttribute("skills", skillRepository.findAll()); return "add"; } Employer employer = employerRepository.findById(employerId).orElse(null); newJob.setEmployer(employer); // Process skills - + if (skills != null && !skills.isEmpty()) { + List skillObjs = (List) skillRepository.findAllById(skills); + newJob.setSkills(skillObjs); + } return "redirect:/"; } - @GetMapping("view/{jobId}") + @GetMapping("/view/{jobId}") public String displayViewJob(Model model, @PathVariable int jobId) { + Job job = jobRepository.findById(jobId).orElse(null); + model.addAttribute("job", job); return "view"; } diff --git a/src/main/java/org/launchcode/techjobs/persistent/models/Job.java b/src/main/java/org/launchcode/techjobs/persistent/models/Job.java index 969a677..b478524 100644 --- a/src/main/java/org/launchcode/techjobs/persistent/models/Job.java +++ b/src/main/java/org/launchcode/techjobs/persistent/models/Job.java @@ -1,9 +1,7 @@ package org.launchcode.techjobs.persistent.models; import javax.persistence.Entity; -import javax.persistence.JoinColumn; -import javax.persistence.ManyToMany; -import javax.persistence.ManyToOne; +import javax.persistence.*; import java.util.List; @Entity @@ -16,10 +14,15 @@ public class Job extends AbstractEntity { private Employer employer; @ManyToMany + @JoinTable( + name = "skill", + joinColumns = @JoinColumn(name = "job_id"), + inverseJoinColumns = @JoinColumn(name = "skill_id") + ) private List skills; public Job() { - super(); +// super(); // no-arg constructor } diff --git a/src/main/java/org/launchcode/techjobs/persistent/models/Skill.java b/src/main/java/org/launchcode/techjobs/persistent/models/Skill.java index 2c68430..4501ccd 100644 --- a/src/main/java/org/launchcode/techjobs/persistent/models/Skill.java +++ b/src/main/java/org/launchcode/techjobs/persistent/models/Skill.java @@ -5,6 +5,7 @@ import javax.persistence.Id; import javax.persistence.ManyToMany; import javax.validation.constraints.NotBlank; +import java.util.ArrayList; import java.util.List; @Entity @@ -18,9 +19,9 @@ public class Skill extends AbstractEntity { private String description; @ManyToMany(mappedBy = "skills") - private List jobs; + private List jobs = new ArrayList<>(); - public Skill () { + public Skill() { } public List getJobs() { diff --git a/src/test/java/org/launchcode/techjobs/persistent/TestTaskFour.java b/src/test/java/org/launchcode/techjobs/persistent/TestTaskFour.java index 73c5bd6..779a00d 100644 --- a/src/test/java/org/launchcode/techjobs/persistent/TestTaskFour.java +++ b/src/test/java/org/launchcode/techjobs/persistent/TestTaskFour.java @@ -3,18 +3,24 @@ import mockit.Expectations; import mockit.Mocked; import org.junit.jupiter.api.Test; +import org.launchcode.techjobs.persistent.controllers.HomeController; import org.launchcode.techjobs.persistent.controllers.ListController; import org.launchcode.techjobs.persistent.models.Employer; +import org.launchcode.techjobs.persistent.models.Job; import org.launchcode.techjobs.persistent.models.Skill; import org.launchcode.techjobs.persistent.models.data.EmployerRepository; +import org.launchcode.techjobs.persistent.models.data.JobRepository; import org.launchcode.techjobs.persistent.models.data.SkillRepository; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.ui.ExtendedModelMap; import org.springframework.ui.Model; +import org.springframework.validation.Errors; import javax.persistence.ManyToMany; import java.io.IOException; import java.lang.annotation.Annotation; import java.lang.reflect.*; +import java.util.ArrayList; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -121,39 +127,39 @@ public void testHomeControllerHasSkillRepository () throws ClassNotFoundExceptio /* * Verifies that HomeController.processAddJobForm queries skillRepository and sets skills properly * */ -// @Test -// public void testProcessAddJobFormHandlesSkillsProperly ( -// @Mocked SkillRepository skillRepository, -// @Mocked EmployerRepository employerRepository, -// @Mocked JobRepository jobRepository, -// @Mocked Job job, -// @Mocked Errors errors) -// throws ClassNotFoundException, NoSuchMethodException, NoSuchFieldException, IllegalAccessException, InvocationTargetException { -// Class homeControllerClass = getClassByName("controllers.HomeController"); -// Method processAddJobFormMethod = homeControllerClass.getMethod("processAddJobForm", Job.class, Errors.class, Model.class, int.class, List.class); -// -// new Expectations() {{ -// skillRepository.findAllById((Iterable) any); -// job.setSkills((List) any); -// }}; -// -// Model model = new ExtendedModelMap(); -// HomeController homeController = new HomeController(); -// -// Field skillRepositoryField = homeControllerClass.getDeclaredField("skillRepository"); -// skillRepositoryField.setAccessible(true); -// skillRepositoryField.set(homeController, skillRepository); -// -// Field employerRepositoryField = homeControllerClass.getDeclaredField("employerRepository"); -// employerRepositoryField.setAccessible(true); -// employerRepositoryField.set(homeController, employerRepository); -// -// Field jobRepositoryField = homeControllerClass.getDeclaredField("jobRepository"); -// jobRepositoryField.setAccessible(true); -// jobRepositoryField.set(homeController, jobRepository); -// -// processAddJobFormMethod.invoke(homeController, job, errors, model, 0, new ArrayList()); -// } + @Test + public void testProcessAddJobFormHandlesSkillsProperly ( + @Mocked SkillRepository skillRepository, + @Mocked EmployerRepository employerRepository, + @Mocked JobRepository jobRepository, + @Mocked Job job, + @Mocked Errors errors) + throws ClassNotFoundException, NoSuchMethodException, NoSuchFieldException, IllegalAccessException, InvocationTargetException { + Class homeControllerClass = getClassByName("controllers.HomeController"); + Method processAddJobFormMethod = homeControllerClass.getMethod("processAddJobForm", Job.class, Errors.class, Model.class, int.class, List.class); + + new Expectations() {{ + skillRepository.findAllById((Iterable) any); + job.setSkills((List) any); + }}; + + Model model = new ExtendedModelMap(); + HomeController homeController = new HomeController(); + + Field skillRepositoryField = homeControllerClass.getDeclaredField("skillRepository"); + skillRepositoryField.setAccessible(true); + skillRepositoryField.set(homeController, skillRepository); + + Field employerRepositoryField = homeControllerClass.getDeclaredField("employerRepository"); + employerRepositoryField.setAccessible(true); + employerRepositoryField.set(homeController, employerRepository); + + Field jobRepositoryField = homeControllerClass.getDeclaredField("jobRepository"); + jobRepositoryField.setAccessible(true); + jobRepositoryField.set(homeController, jobRepository); + + processAddJobFormMethod.invoke(homeController, job, errors, model, 0, new ArrayList()); + } /* * Verifies that skillRepository and employerRepository fields have been added to ListController