diff --git a/build.gradle b/build.gradle index e3e5de5..f01e106 100644 --- a/build.gradle +++ b/build.gradle @@ -22,6 +22,9 @@ repositories { dependencies { implementation 'org.springframework.boot:spring-boot-starter-thymeleaf' implementation 'org.springframework.boot:spring-boot-starter-web' + implementation 'org.springframework.boot:spring-boot-starter-validation' //added + implementation 'org.springframework.boot:spring-boot-starter-data-jpa' + implementation 'mysql:mysql-connector-java' developmentOnly 'org.springframework.boot:spring-boot-devtools' testImplementation('org.springframework.boot:spring-boot-starter-test') { exclude group: 'org.junit.vintage', module: 'junit-vintage-engine' diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar index 29953ea..cc4fdc2 100644 Binary files a/gradle/wrapper/gradle-wrapper.jar and b/gradle/wrapper/gradle-wrapper.jar differ diff --git a/gradlew b/gradlew index cccdd3d..2fe81a7 100755 --- a/gradlew +++ b/gradlew @@ -1,5 +1,21 @@ #!/usr/bin/env sh +# +# Copyright 2015 the original author or authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + ############################################################################## ## ## Gradle start up script for UN*X @@ -28,7 +44,7 @@ APP_NAME="Gradle" APP_BASE_NAME=`basename "$0"` # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. -DEFAULT_JVM_OPTS="" +DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' # Use the maximum available, or set MAX_FD != -1 to use that value. MAX_FD="maximum" @@ -109,8 +125,8 @@ if $darwin; then GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" fi -# For Cygwin, switch paths to Windows format before running java -if $cygwin ; then +# For Cygwin or MSYS, switch paths to Windows format before running java +if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then APP_HOME=`cygpath --path --mixed "$APP_HOME"` CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` JAVACMD=`cygpath --unix "$JAVACMD"` @@ -138,19 +154,19 @@ if $cygwin ; then else eval `echo args$i`="\"$arg\"" fi - i=$((i+1)) + i=`expr $i + 1` done case $i in - (0) set -- ;; - (1) set -- "$args0" ;; - (2) set -- "$args0" "$args1" ;; - (3) set -- "$args0" "$args1" "$args2" ;; - (4) set -- "$args0" "$args1" "$args2" "$args3" ;; - (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; - (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; - (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; - (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; - (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; + 0) set -- ;; + 1) set -- "$args0" ;; + 2) set -- "$args0" "$args1" ;; + 3) set -- "$args0" "$args1" "$args2" ;; + 4) set -- "$args0" "$args1" "$args2" "$args3" ;; + 5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; + 6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; + 7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; + 8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; + 9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; esac fi @@ -159,14 +175,9 @@ save () { for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done echo " " } -APP_ARGS=$(save "$@") +APP_ARGS=`save "$@"` # Collect all arguments for the java command, following the shell quoting and substitution rules eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" -# by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong -if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then - cd "$(dirname "$0")" -fi - exec "$JAVACMD" "$@" diff --git a/gradlew.bat b/gradlew.bat index f955316..9618d8d 100644 --- a/gradlew.bat +++ b/gradlew.bat @@ -1,3 +1,19 @@ +@rem +@rem Copyright 2015 the original author or authors. +@rem +@rem Licensed under the Apache License, Version 2.0 (the "License"); +@rem you may not use this file except in compliance with the License. +@rem You may obtain a copy of the License at +@rem +@rem https://www.apache.org/licenses/LICENSE-2.0 +@rem +@rem Unless required by applicable law or agreed to in writing, software +@rem distributed under the License is distributed on an "AS IS" BASIS, +@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +@rem See the License for the specific language governing permissions and +@rem limitations under the License. +@rem + @if "%DEBUG%" == "" @echo off @rem ########################################################################## @rem @@ -14,7 +30,7 @@ set APP_BASE_NAME=%~n0 set APP_HOME=%DIRNAME% @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. -set DEFAULT_JVM_OPTS= +set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" @rem Find java.exe if defined JAVA_HOME goto findJavaFromJavaHome diff --git a/queries.sql b/queries.sql index 972c068..a16f58e 100644 --- a/queries.sql +++ b/queries.sql @@ -1,7 +1,13 @@ -- Part 1: Test it with SQL -- Part 2: Test it with SQL +SELECT name FROM employer WHERE location = "St. Louis City"; -- Part 3: Test it with SQL +DROP TABLE job; --- Part 4: Test it with SQL \ No newline at end of file +-- Part 4: Test it with SQL +SELECT * FROM skill + LEFT JOIN job_skills ON skill.id = job_skills.skills_id + WHERE job_skills.jobs_id IS NOT NULL + ORDER BY name ASC; \ No newline at end of file diff --git a/src/main/java/org/launchcode/techjobs/persistent/controllers/EmployerController.java b/src/main/java/org/launchcode/techjobs/persistent/controllers/EmployerController.java index b297996..8684be2 100644 --- a/src/main/java/org/launchcode/techjobs/persistent/controllers/EmployerController.java +++ b/src/main/java/org/launchcode/techjobs/persistent/controllers/EmployerController.java @@ -1,6 +1,8 @@ package org.launchcode.techjobs.persistent.controllers; import org.launchcode.techjobs.persistent.models.Employer; +import org.launchcode.techjobs.persistent.models.data.EmployerRepository; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.validation.Errors; @@ -13,11 +15,20 @@ @RequestMapping("employers") public class EmployerController { + @Autowired + private EmployerRepository employerRepository; + // add index method + @GetMapping("") + public String index(Model model){ + model.addAttribute("employers", employerRepository.findAll()); + return "employers/index"; + } @GetMapping("add") public String displayAddEmployerForm(Model model) { model.addAttribute(new Employer()); return "employers/add"; + } @PostMapping("add") @@ -27,6 +38,8 @@ public String processAddEmployerForm(@ModelAttribute @Valid Employer newEmployer if (errors.hasErrors()) { return "employers/add"; } + //add method to add to the employerRepository + employerRepository.save(newEmployer); return "redirect:"; } @@ -34,7 +47,7 @@ public String processAddEmployerForm(@ModelAttribute @Valid Employer newEmployer @GetMapping("view/{employerId}") public String displayViewEmployer(Model model, @PathVariable int employerId) { - Optional optEmployer = null; + Optional optEmployer = employerRepository.findById(employerId); if (optEmployer.isPresent()) { Employer employer = (Employer) optEmployer.get(); model.addAttribute("employer", employer); 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 a3e5593..c69ba42 100644 --- a/src/main/java/org/launchcode/techjobs/persistent/controllers/HomeController.java +++ b/src/main/java/org/launchcode/techjobs/persistent/controllers/HomeController.java @@ -1,6 +1,12 @@ package org.launchcode.techjobs.persistent.controllers; +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; import org.springframework.validation.Errors; @@ -8,17 +14,26 @@ import javax.validation.Valid; import java.util.List; +import java.util.Optional; /** * Created by LaunchCode */ @Controller public class HomeController { + //section 3 + @Autowired + private EmployerRepository employerRepository; + @Autowired + private SkillRepository skillRepository; + + @Autowired + private JobRepository jobRepository; @RequestMapping("") public String index(Model model) { - model.addAttribute("title", "My Jobs"); + model.addAttribute("jobs", jobRepository.findAll()); return "index"; } @@ -27,25 +42,42 @@ public String index(Model model) { public String displayAddJobForm(Model model) { model.addAttribute("title", "Add Job"); model.addAttribute(new Job()); + model.addAttribute("employers", employerRepository.findAll()); + model.addAttribute("skills", skillRepository.findAll()); return "add"; } @PostMapping("add") public String processAddJobForm(@ModelAttribute @Valid Job newJob, Errors errors, Model model, @RequestParam int employerId, @RequestParam List skills) { - if (errors.hasErrors()) { model.addAttribute("title", "Add Job"); return "add"; } + Optional optEmployer = employerRepository.findById(employerId); + if (optEmployer.isPresent()) { + Employer employer = optEmployer.get(); + newJob.setEmployer(employer); + + } + List optionalSkills = (List) skillRepository.findAllById(skills); + newJob.setSkills(optionalSkills); + jobRepository.save(newJob); return "redirect:"; } @GetMapping("view/{jobId}") public String displayViewJob(Model model, @PathVariable int jobId) { + Optional optionalJob = jobRepository.findById(jobId); + if (optionalJob.isPresent()) { + Job job = optionalJob.get(); + model.addAttribute("job", job); + return "view"; + } else { + return "redirect:/"; + } - return "view"; } diff --git a/src/main/java/org/launchcode/techjobs/persistent/controllers/ListController.java b/src/main/java/org/launchcode/techjobs/persistent/controllers/ListController.java index 6e6bea9..896e076 100644 --- a/src/main/java/org/launchcode/techjobs/persistent/controllers/ListController.java +++ b/src/main/java/org/launchcode/techjobs/persistent/controllers/ListController.java @@ -1,8 +1,10 @@ package org.launchcode.techjobs.persistent.controllers; import org.launchcode.techjobs.persistent.models.Job; +import org.launchcode.techjobs.persistent.models.data.EmployerRepository; import org.launchcode.techjobs.persistent.models.data.JobRepository; import org.launchcode.techjobs.persistent.models.JobData; +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; @@ -21,6 +23,13 @@ public class ListController { @Autowired private JobRepository jobRepository; + @Autowired + private SkillRepository skillRepository; + + @Autowired + private EmployerRepository employerRepository; + + static HashMap columnChoices = new HashMap<>(); public ListController () { @@ -33,7 +42,8 @@ public ListController () { @RequestMapping("") public String list(Model model) { - + model.addAttribute("employers", employerRepository.findAll()); + model.addAttribute("skills" , skillRepository.findAll()); return "list"; } diff --git a/src/main/java/org/launchcode/techjobs/persistent/controllers/SkillController.java b/src/main/java/org/launchcode/techjobs/persistent/controllers/SkillController.java new file mode 100644 index 0000000..d2ef5a2 --- /dev/null +++ b/src/main/java/org/launchcode/techjobs/persistent/controllers/SkillController.java @@ -0,0 +1,61 @@ +package org.launchcode.techjobs.persistent.controllers; + +import org.launchcode.techjobs.persistent.models.Employer; +import org.launchcode.techjobs.persistent.models.Skill; +import org.launchcode.techjobs.persistent.models.data.EmployerRepository; +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; +import org.springframework.validation.Errors; +import org.springframework.web.bind.annotation.*; + +import javax.validation.Valid; +import java.util.Optional; +@Controller +@RequestMapping("skills") +public class SkillController { + @Autowired + private SkillRepository skillRepository; + + // add index method + @GetMapping("") + public String index(Model model) { + model.addAttribute("skills", skillRepository.findAll()); + return "skills/index"; + } + + @GetMapping("add") + public String displayAddSkillForm(Model model) { + model.addAttribute(new Employer()); + return "skills/add"; + + } + + @PostMapping("add") + public String processAddSkillForm(@ModelAttribute @Valid Skill newSkill, + Errors errors, Model model) { + + if (errors.hasErrors()) { + return "skills/add"; + } + //add method to add to the employerRepository + skillRepository.save(newSkill); + + return "redirect:"; + } + + @GetMapping("view/{skillId}") + public String displayViewSkill(Model model, @PathVariable int skillId) { + + Optional optSkill = skillRepository.findById(skillId); + if (optSkill.isPresent()) { + Skill skill = (Skill) optSkill.get(); + model.addAttribute("skill", skill); + return "skills/view"; + } else { + return "redirect:../"; + } + } + } + diff --git a/src/main/java/org/launchcode/techjobs/persistent/models/AbstractEntity.java b/src/main/java/org/launchcode/techjobs/persistent/models/AbstractEntity.java index 1ad0722..b66ed4c 100644 --- a/src/main/java/org/launchcode/techjobs/persistent/models/AbstractEntity.java +++ b/src/main/java/org/launchcode/techjobs/persistent/models/AbstractEntity.java @@ -1,14 +1,22 @@ package org.launchcode.techjobs.persistent.models; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.MappedSuperclass; +import javax.validation.constraints.NotBlank; +import javax.validation.constraints.Size; import java.util.Objects; - +@MappedSuperclass public abstract class AbstractEntity { - + @Id + @GeneratedValue private int id; - + @NotBlank + @Size(max = 50, min = 4) private String name; + public int getId() { return id; } diff --git a/src/main/java/org/launchcode/techjobs/persistent/models/Employer.java b/src/main/java/org/launchcode/techjobs/persistent/models/Employer.java index 857397a..7395233 100644 --- a/src/main/java/org/launchcode/techjobs/persistent/models/Employer.java +++ b/src/main/java/org/launchcode/techjobs/persistent/models/Employer.java @@ -1,6 +1,44 @@ package org.launchcode.techjobs.persistent.models; +import javax.persistence.Entity; +import javax.persistence.JoinColumn; +import javax.persistence.OneToMany; +import javax.validation.constraints.NotBlank; +import javax.validation.constraints.Size; +import java.util.List;import java.util.ArrayList; + + +@Entity public class Employer extends AbstractEntity { + @NotBlank (message = "You must enter a location") + @Size(max = 50, min = 4) + private String location; + + + + @OneToMany + @JoinColumn(name="employer_id") + private List jobs= new ArrayList<>(); + + public Employer(String location){ + this.location = location; + } + public Employer(){} + + public String getLocation() { + + return location; + } + public void setLocation(String location) { + this.location = location; + } + public List getJobs() { + return jobs; + } + + public void setJobs(List jobs) { + this.jobs = jobs; + } } 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 56d215c..b96b9c2 100644 --- a/src/main/java/org/launchcode/techjobs/persistent/models/Job.java +++ b/src/main/java/org/launchcode/techjobs/persistent/models/Job.java @@ -1,51 +1,44 @@ package org.launchcode.techjobs.persistent.models; import javax.persistence.*; +import java.util.List; +//above added @Entity -public class Job{ - - @Id - @GeneratedValue - private int id; - - private String name; - - private String employer; - private String skills; +//section 3 + public class Job extends AbstractEntity { + @ManyToOne + private Employer employer; + @ManyToMany + private List skills; public Job() { } +//Replace Employer below? +// public Job(Employer anEmployer, String someSkills) { - public Job(String anEmployer, String someSkills) { +// section 3 + public Job(Employer anEmployer, List someSkills) { super(); this.employer = anEmployer; + //section 4 this.skills = someSkills; } + //Getters and Setters - // Getters and setters. - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public String getEmployer() { + public Employer getEmployer(){ return employer; } - public void setEmployer(String employer) { + public void setEmployer(Employer employer) { this.employer = employer; } - public String getSkills() { - return skills; - } + public List getSkills (){ + return skills; + } + public void setSkills(List skills){ + this.skills = skills; + } - public void setSkills(String skills) { - this.skills = skills; - } } diff --git a/src/main/java/org/launchcode/techjobs/persistent/models/JobData.java b/src/main/java/org/launchcode/techjobs/persistent/models/JobData.java index 64ef349..f171d44 100644 --- a/src/main/java/org/launchcode/techjobs/persistent/models/JobData.java +++ b/src/main/java/org/launchcode/techjobs/persistent/models/JobData.java @@ -51,7 +51,8 @@ public static String getFieldValue(Job job, String fieldName){ theValue = job.getName(); } else if (fieldName.equals("employer")){ theValue = job.getEmployer().toString(); - } else { + } + else { theValue = job.getSkills().toString(); } 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 acf49e8..eb79e25 100644 --- a/src/main/java/org/launchcode/techjobs/persistent/models/Skill.java +++ b/src/main/java/org/launchcode/techjobs/persistent/models/Skill.java @@ -1,5 +1,37 @@ package org.launchcode.techjobs.persistent.models; + +import javax.persistence.Entity; +import javax.persistence.ManyToMany; +import javax.validation.constraints.NotBlank; +import javax.validation.constraints.Size; +import java.util.ArrayList; +import java.util.List; + +@Entity public class Skill extends AbstractEntity { + @NotBlank(message = "You must enter a description") + @Size(max = 50, min = 4) + private String description; + @ManyToMany(mappedBy = "skills") + private List jobs = new ArrayList<>(); + + public Skill () { + + } + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + public List getJobs() { + return jobs; + } + + public void setJobs(List jobs) { + this.jobs = jobs; + } } \ No newline at end of file diff --git a/src/main/java/org/launchcode/techjobs/persistent/models/data/EmployerRepository.java b/src/main/java/org/launchcode/techjobs/persistent/models/data/EmployerRepository.java new file mode 100644 index 0000000..c7444c6 --- /dev/null +++ b/src/main/java/org/launchcode/techjobs/persistent/models/data/EmployerRepository.java @@ -0,0 +1,13 @@ +package org.launchcode.techjobs.persistent.models.data; + +import org.launchcode.techjobs.persistent.models.Employer; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.repository.CrudRepository; +import org.springframework.stereotype.Repository; + + + +@Repository +public interface EmployerRepository extends CrudRepository { + +} diff --git a/src/main/java/org/launchcode/techjobs/persistent/models/data/SkillRepository.java b/src/main/java/org/launchcode/techjobs/persistent/models/data/SkillRepository.java new file mode 100644 index 0000000..c0efb72 --- /dev/null +++ b/src/main/java/org/launchcode/techjobs/persistent/models/data/SkillRepository.java @@ -0,0 +1,11 @@ +package org.launchcode.techjobs.persistent.models.data; + + + +import org.launchcode.techjobs.persistent.models.Skill; +import org.springframework.data.repository.CrudRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface SkillRepository extends CrudRepository { +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 8b13789..2c25015 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1 +1,18 @@ +# Database connection settings +spring.datasource.url=jdbc:mysql://localhost:3306/techjobs +spring.datasource.username=techjobs +spring.datasource.password=techjobs +# Specify the DBMS +spring.jpa.database = MYSQL + + +# Show or not log for each sql query +spring.jpa.show-sql = false + +# Hibernate ddl auto (create, create-drop, update) +spring.jpa.hibernate.ddl-auto = update + +# Use spring.jpa.properties.* for Hibernate native properties (the prefix is +# stripped before adding them to the entity manager) +spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL8Dialect diff --git a/src/test/java/org/launchcode/techjobs/persistent/TestTaskFour.java b/src/test/java/org/launchcode/techjobs/persistent/TestTaskFour.java index a08eb6f..593a475 100644 --- a/src/test/java/org/launchcode/techjobs/persistent/TestTaskFour.java +++ b/src/test/java/org/launchcode/techjobs/persistent/TestTaskFour.java @@ -9,7 +9,9 @@ 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; @@ -109,110 +111,110 @@ public void testJobSkillsRefactoring () throws ClassNotFoundException, NoSuchMet /* * Verifies that HomeController has an @Autowired skillRepository field * */ -// @Test -// public void testHomeControllerHasSkillRepository () throws ClassNotFoundException { -// Class homeControllerClass = getClassByName("controllers.HomeController"); -// Field skillRepositoryField = null; -// try { -// skillRepositoryField = homeControllerClass.getDeclaredField("skillRepository"); -// } catch (NoSuchFieldException e) { -// fail("HomeController should have a skillRepository field"); -// } -// -// assertEquals(SkillRepository.class, skillRepositoryField.getType(), "skillRepository is of incorrect type"); -// assertNotNull(skillRepositoryField.getAnnotation(Autowired.class), "skillRepository must be @Autowired"); -// } + @Test + public void testHomeControllerHasSkillRepository () throws ClassNotFoundException { + Class homeControllerClass = getClassByName("controllers.HomeController"); + Field skillRepositoryField = null; + try { + skillRepositoryField = homeControllerClass.getDeclaredField("skillRepository"); + } catch (NoSuchFieldException e) { + fail("HomeController should have a skillRepository field"); + } + + assertEquals(SkillRepository.class, skillRepositoryField.getType(), "skillRepository is of incorrect type"); + assertNotNull(skillRepositoryField.getAnnotation(Autowired.class), "skillRepository must be @Autowired"); + } /* * 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 * */ -// @Test -// public void testListControllerHasAutowiredRepositories () throws ClassNotFoundException { -// Class listControllerClass = getClassByName("controllers.ListController"); -// Field employerRepositoryField = null; -// Field skillRepositoryField = null; -// -// try { -// employerRepositoryField = listControllerClass.getDeclaredField("employerRepository"); -// } catch (NoSuchFieldException e) { -// fail("ListController must have an employerRepository field"); -// } -// -// assertEquals(EmployerRepository.class, employerRepositoryField.getType()); -// assertNotNull(employerRepositoryField.getAnnotation(Autowired.class)); -// -// try { -// skillRepositoryField = listControllerClass.getDeclaredField("skillRepository"); -// } catch (NoSuchFieldException e) { -// fail("ListController must have a skillRepository field"); -// } -// -// assertEquals(SkillRepository.class, skillRepositoryField.getType()); -// assertNotNull(skillRepositoryField.getAnnotation(Autowired.class)); -// } + @Test + public void testListControllerHasAutowiredRepositories () throws ClassNotFoundException { + Class listControllerClass = getClassByName("controllers.ListController"); + Field employerRepositoryField = null; + Field skillRepositoryField = null; + + try { + employerRepositoryField = listControllerClass.getDeclaredField("employerRepository"); + } catch (NoSuchFieldException e) { + fail("ListController must have an employerRepository field"); + } + + assertEquals(EmployerRepository.class, employerRepositoryField.getType()); + assertNotNull(employerRepositoryField.getAnnotation(Autowired.class)); + + try { + skillRepositoryField = listControllerClass.getDeclaredField("skillRepository"); + } catch (NoSuchFieldException e) { + fail("ListController must have a skillRepository field"); + } + + assertEquals(SkillRepository.class, skillRepositoryField.getType()); + assertNotNull(skillRepositoryField.getAnnotation(Autowired.class)); + } /* * Verifies that ListController.list sets the correct model attributes using skill/employerRepository objects * */ -// @Test -// public void testListControllerListMethodSetsFormFieldData (@Mocked Model model, @Mocked SkillRepository skillRepository, @Mocked EmployerRepository employerRepository) throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException { -// Class listControllerClass = getClassByName("controllers.ListController"); -// ListController listController = new ListController(); -// -// new Expectations() {{ -// model.addAttribute("employers", any); -// model.addAttribute("skills", any); -// skillRepository.findAll(); -// employerRepository.findAll(); -// }}; -// -// Field skillRepositoryField = listControllerClass.getDeclaredField("skillRepository"); -// skillRepositoryField.setAccessible(true); -// skillRepositoryField.set(listController, skillRepository); -// -// Field employerRepositoryField = listControllerClass.getDeclaredField("employerRepository"); -// employerRepositoryField.setAccessible(true); -// employerRepositoryField.set(listController, employerRepository); -// -// listController.list(model); -// } + @Test + public void testListControllerListMethodSetsFormFieldData (@Mocked Model model, @Mocked SkillRepository skillRepository, @Mocked EmployerRepository employerRepository) throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException { + Class listControllerClass = getClassByName("controllers.ListController"); + ListController listController = new ListController(); + + new Expectations() {{ + model.addAttribute("employers", any); + model.addAttribute("skills", any); + skillRepository.findAll(); + employerRepository.findAll(); + }}; + + Field skillRepositoryField = listControllerClass.getDeclaredField("skillRepository"); + skillRepositoryField.setAccessible(true); + skillRepositoryField.set(listController, skillRepository); + + Field employerRepositoryField = listControllerClass.getDeclaredField("employerRepository"); + employerRepositoryField.setAccessible(true); + employerRepositoryField.set(listController, employerRepository); + + listController.list(model); + } @Test public void testSqlQuery () throws IOException { diff --git a/src/test/java/org/launchcode/techjobs/persistent/TestTaskThree.java b/src/test/java/org/launchcode/techjobs/persistent/TestTaskThree.java index 6a74962..9215914 100644 --- a/src/test/java/org/launchcode/techjobs/persistent/TestTaskThree.java +++ b/src/test/java/org/launchcode/techjobs/persistent/TestTaskThree.java @@ -7,6 +7,8 @@ import org.launchcode.techjobs.persistent.models.AbstractEntity; import org.launchcode.techjobs.persistent.models.Employer; import org.launchcode.techjobs.persistent.models.Job; +import org.launchcode.techjobs.persistent.models.data.EmployerRepository; +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; @@ -149,34 +151,34 @@ public void testHomeControllerUsesEmployerRepository() throws ClassNotFoundExcep /* * Verifies that HomeController.displayAddJobForm calls employerRepository.findAll() * */ -// @Test -// public void testHomeControllerFetchesEmployers(@Mocked EmployerRepository employerRepository, @Mocked SkillRepository skillRepository) throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException { -// Class homeControllerClass = getClassByName("controllers.HomeController"); -// HomeController homeController = new HomeController(); -// -// Field employerRepositoryField = homeControllerClass.getDeclaredField("employerRepository"); -// employerRepositoryField.setAccessible(true); -// employerRepositoryField.set(homeController, employerRepository); -// -// // not needed for verification, but necessary to make sure calling the controller -// // method doesn't throw a NullPointerException -// Field skillRepositoryField = null; -// try { -// skillRepositoryField = homeControllerClass.getDeclaredField("skillRepository"); -// skillRepositoryField.setAccessible(true); -// skillRepositoryField.set(homeController, skillRepository); -// } catch (NoSuchFieldException e) { -// // do nothing -// } -// -// Model model = new ExtendedModelMap(); -// -// new Expectations() {{ -// employerRepository.findAll(); -// }}; -// -// homeController.displayAddJobForm(model); -// } + @Test + public void testHomeControllerFetchesEmployers(@Mocked EmployerRepository employerRepository, @Mocked SkillRepository skillRepository) throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException { + Class homeControllerClass = getClassByName("controllers.HomeController"); + HomeController homeController = new HomeController(); + + Field employerRepositoryField = homeControllerClass.getDeclaredField("employerRepository"); + employerRepositoryField.setAccessible(true); + employerRepositoryField.set(homeController, employerRepository); + + // not needed for verification, but necessary to make sure calling the controller + // method doesn't throw a NullPointerException + Field skillRepositoryField = null; + try { + skillRepositoryField = homeControllerClass.getDeclaredField("skillRepository"); + skillRepositoryField.setAccessible(true); + skillRepositoryField.set(homeController, skillRepository); + } catch (NoSuchFieldException e) { + // do nothing + } + + Model model = new ExtendedModelMap(); + + new Expectations() {{ + employerRepository.findAll(); + }}; + + homeController.displayAddJobForm(model); + } /* * Tests SQL query for task 3 diff --git a/src/test/java/org/launchcode/techjobs/persistent/TestTaskTwo.java b/src/test/java/org/launchcode/techjobs/persistent/TestTaskTwo.java index c8164b4..2e90b55 100644 --- a/src/test/java/org/launchcode/techjobs/persistent/TestTaskTwo.java +++ b/src/test/java/org/launchcode/techjobs/persistent/TestTaskTwo.java @@ -6,6 +6,9 @@ import org.launchcode.techjobs.persistent.controllers.EmployerController; import org.launchcode.techjobs.persistent.models.Employer; import org.launchcode.techjobs.persistent.models.Skill; +import org.launchcode.techjobs.persistent.controllers.SkillController; +import org.launchcode.techjobs.persistent.models.data.EmployerRepository; +import org.launchcode.techjobs.persistent.models.data.SkillRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.repository.CrudRepository; import org.springframework.stereotype.Repository; @@ -330,213 +333,213 @@ public void testSkillRepositoryHasRepositoryAnnotation () throws ClassNotFoundEx /* * Verifies that the employerRepository field is correctly defined * */ -// @Test -// public void testEmployerRepositoryDefinition () throws ClassNotFoundException { -// Class employerController = getClassByName("controllers.EmployerController"); -// Field employerRepositoryField = null; -// -// try { -// employerRepositoryField = employerController.getDeclaredField("employerRepository"); -// } catch (NoSuchFieldException e) { -// fail("EmployerController does not have an employerRepository field"); -// } -// -// assertEquals(EmployerRepository.class, employerRepositoryField.getType(), "employerRepository must be of type EmployerRepository"); -// assertNotNull(employerRepositoryField.getAnnotation(Autowired.class), "employerRepository must have the @Autowired annotation"); -// } + @Test + public void testEmployerRepositoryDefinition () throws ClassNotFoundException { + Class employerController = getClassByName("controllers.EmployerController"); + Field employerRepositoryField = null; + + try { + employerRepositoryField = employerController.getDeclaredField("employerRepository"); + } catch (NoSuchFieldException e) { + fail("EmployerController does not have an employerRepository field"); + } + + assertEquals(EmployerRepository.class, employerRepositoryField.getType(), "employerRepository must be of type EmployerRepository"); + assertNotNull(employerRepositoryField.getAnnotation(Autowired.class), "employerRepository must have the @Autowired annotation"); + } /* * Verifies that EmployerController.index is properly defined * */ -// @Test -// public void testEmployerControllerIndexMethodDefinition (@Mocked EmployerRepository employerRepository) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException, NoSuchFieldException { -// Class employerControllerClass = getClassByName("controllers.EmployerController"); -// Method indexMethod = null; -// + @Test + public void testEmployerControllerIndexMethodDefinition (@Mocked EmployerRepository employerRepository) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException, NoSuchFieldException { + Class employerControllerClass = getClassByName("controllers.EmployerController"); + Method indexMethod = null; + // // Verify that the index method exists -// try { -// indexMethod = employerControllerClass.getMethod("index", Model.class); -// } catch (NoSuchMethodException e) { -// fail("EmployerController must have an index method that takes a parameter of type Model"); -// } -// -// Annotation annotation = indexMethod.getDeclaredAnnotation(RequestMapping.class); -// + try { + indexMethod = employerControllerClass.getMethod("index", Model.class); + } catch (NoSuchMethodException e) { + fail("EmployerController must have an index method that takes a parameter of type Model"); + } + + Annotation annotation = indexMethod.getDeclaredAnnotation(RequestMapping.class); + // // Verify that index has a routing annotation. We need to accommodate // // both @RequestMapping and @GetMapping. -// if (annotation == null) { -// annotation = indexMethod.getDeclaredAnnotation(GetMapping.class); -// } -// -// assertNotNull(annotation, "index method must have a routing annotation"); -// -// Method annotationValueMethod = annotation.getClass().getMethod("value"); -// String[] values = ((String[]) annotationValueMethod.invoke(annotation)); -// assertEquals(1, values.length, "The routing annotation for index must have a value"); -// assertEquals("", values[0], "The value parameter for the routing annotation must be the empty string"); -// + if (annotation == null) { + annotation = indexMethod.getDeclaredAnnotation(GetMapping.class); + } + + assertNotNull(annotation, "index method must have a routing annotation"); + + Method annotationValueMethod = annotation.getClass().getMethod("value"); + String[] values = ((String[]) annotationValueMethod.invoke(annotation)); + assertEquals(1, values.length, "The routing annotation for index must have a value"); + assertEquals("", values[0], "The value parameter for the routing annotation must be the empty string"); + // // Verify that index calls employerRepository.findAll() -// new Expectations() {{ -// employerRepository.findAll(); -// }}; -// -// Model model = new ExtendedModelMap(); -// EmployerController employerController = new EmployerController(); -// Field employerRepositoryField = employerControllerClass.getDeclaredField("employerRepository"); -// employerRepositoryField.setAccessible(true); -// employerRepositoryField.set(employerController, employerRepository); -// indexMethod.invoke(employerController, model); -// } + new Expectations() {{ + employerRepository.findAll(); + }}; + + Model model = new ExtendedModelMap(); + EmployerController employerController = new EmployerController(); + Field employerRepositoryField = employerControllerClass.getDeclaredField("employerRepository"); + employerRepositoryField.setAccessible(true); + employerRepositoryField.set(employerController, employerRepository); + indexMethod.invoke(employerController, model); + } /* * Verify that processAddEmployerForm saves a new employer to the database * */ -// @Test -// public void testNewEmployerIsSaved (@Mocked EmployerRepository employerRepository, @Mocked Errors errors) throws ClassNotFoundException, NoSuchMethodException, NoSuchFieldException, IllegalAccessException, InvocationTargetException { -// Class employerControllerClass = getClassByName("controllers.EmployerController"); -// Method processAddEmployerFormMethod = employerControllerClass.getMethod("processAddEmployerForm", Employer.class, Errors.class, Model.class); -// Method saveMethod = EmployerRepository.class.getMethod("save", Object.class); -// -// Employer employer = new Employer(); -// employer.setLocation("Saint Louis"); -// employer.setName("LaunchCode"); -// -// new Expectations() {{ -// saveMethod.invoke(employerRepository, employer); -// }}; -// -// Model model = new ExtendedModelMap(); -// EmployerController employerController = new EmployerController(); -// Field employerRepositoryField = employerControllerClass.getDeclaredField("employerRepository"); -// employerRepositoryField.setAccessible(true); -// employerRepositoryField.set(employerController, employerRepository); -// processAddEmployerFormMethod.invoke(employerController, employer, errors, model); -// } + @Test + public void testNewEmployerIsSaved (@Mocked EmployerRepository employerRepository, @Mocked Errors errors) throws ClassNotFoundException, NoSuchMethodException, NoSuchFieldException, IllegalAccessException, InvocationTargetException { + Class employerControllerClass = getClassByName("controllers.EmployerController"); + Method processAddEmployerFormMethod = employerControllerClass.getMethod("processAddEmployerForm", Employer.class, Errors.class, Model.class); + Method saveMethod = EmployerRepository.class.getMethod("save", Object.class); + + Employer employer = new Employer(); + employer.setLocation("Saint Louis"); + employer.setName("LaunchCode"); + + new Expectations() {{ + saveMethod.invoke(employerRepository, employer); + }}; + + Model model = new ExtendedModelMap(); + EmployerController employerController = new EmployerController(); + Field employerRepositoryField = employerControllerClass.getDeclaredField("employerRepository"); + employerRepositoryField.setAccessible(true); + employerRepositoryField.set(employerController, employerRepository); + processAddEmployerFormMethod.invoke(employerController, employer, errors, model); + } /* * Verifies that displayViewEmployer calls findById to retrieve an employer object * */ -// @Test -// public void testDisplayViewEmployerCallsFindById (@Mocked EmployerRepository employerRepository) throws ClassNotFoundException, NoSuchMethodException, NoSuchFieldException, IllegalAccessException, InvocationTargetException { -// Class employerControllerClass = getClassByName("controllers.EmployerController"); -// Method displayViewEmployerMethod = employerControllerClass.getMethod("displayViewEmployer", Model.class, int.class); -// -// new Expectations() {{ -// employerRepository.findById(1); -// }}; -// -// Model model = new ExtendedModelMap(); -// EmployerController employerController = new EmployerController(); -// Field employerRepositoryField = employerControllerClass.getDeclaredField("employerRepository"); -// employerRepositoryField.setAccessible(true); -// employerRepositoryField.set(employerController, employerRepository); -// displayViewEmployerMethod.invoke(employerController, model, 1); -// } + @Test + public void testDisplayViewEmployerCallsFindById (@Mocked EmployerRepository employerRepository) throws ClassNotFoundException, NoSuchMethodException, NoSuchFieldException, IllegalAccessException, InvocationTargetException { + Class employerControllerClass = getClassByName("controllers.EmployerController"); + Method displayViewEmployerMethod = employerControllerClass.getMethod("displayViewEmployer", Model.class, int.class); + + new Expectations() {{ + employerRepository.findById(1); + }}; + + Model model = new ExtendedModelMap(); + EmployerController employerController = new EmployerController(); + Field employerRepositoryField = employerControllerClass.getDeclaredField("employerRepository"); + employerRepositoryField.setAccessible(true); + employerRepositoryField.set(employerController, employerRepository); + displayViewEmployerMethod.invoke(employerController, model, 1); + } /* * Verifies that the skillRepository field is correctly defined * */ -// @Test -// public void testSkillRepositoryDefinition () throws ClassNotFoundException { -// Class skillController = getClassByName("controllers.SkillController"); -// Field skillRepositoryField = null; -// -// try { -// skillRepositoryField = skillController.getDeclaredField("skillRepository"); -// } catch (NoSuchFieldException e) { -// fail("SkillController does not have an skillRepository field"); -// } -// -// assertEquals(SkillRepository.class, skillRepositoryField.getType(), "skillRepository must be of type SkillRepository"); -// assertNotNull(skillRepositoryField.getAnnotation(Autowired.class), "skillRepository must have the @Autowired annotation"); -// } + @Test + public void testSkillRepositoryDefinition () throws ClassNotFoundException { + Class skillController = getClassByName("controllers.SkillController"); + Field skillRepositoryField = null; + + try { + skillRepositoryField = skillController.getDeclaredField("skillRepository"); + } catch (NoSuchFieldException e) { + fail("SkillController does not have an skillRepository field"); + } + + assertEquals(SkillRepository.class, skillRepositoryField.getType(), "skillRepository must be of type SkillRepository"); + assertNotNull(skillRepositoryField.getAnnotation(Autowired.class), "skillRepository must have the @Autowired annotation"); + } /* * Verifies that SkillController.index is properly defined * */ -// @Test -// public void testSkillControllerIndexMethodDefinition (@Mocked SkillRepository skillRepository) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException, NoSuchFieldException { -// Class skillControllerClass = getClassByName("controllers.SkillController"); -// Method indexMethod = null; -// + @Test + public void testSkillControllerIndexMethodDefinition (@Mocked SkillRepository skillRepository) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException, NoSuchFieldException { + Class skillControllerClass = getClassByName("controllers.SkillController"); + Method indexMethod = null; + // // Verify that the index method exists -// try { -// indexMethod = skillControllerClass.getMethod("index", Model.class); -// } catch (NoSuchMethodException e) { -// fail("SkillController must have an index method that takes a parameter of type Model"); -// } -// -// Annotation annotation = indexMethod.getDeclaredAnnotation(RequestMapping.class); -// + try { + indexMethod = skillControllerClass.getMethod("index", Model.class); + } catch (NoSuchMethodException e) { + fail("SkillController must have an index method that takes a parameter of type Model"); + } + + Annotation annotation = indexMethod.getDeclaredAnnotation(RequestMapping.class); + // // Verify that index has a routing annotation. We need to accommodate // // both @RequestMapping and @GetMapping. -// if (annotation == null) { -// annotation = indexMethod.getDeclaredAnnotation(GetMapping.class); -// } -// -// assertNotNull(annotation, "index method must have a routing annotation"); -// -// Method annotationValueMethod = annotation.getClass().getMethod("value"); -// String[] values = ((String[]) annotationValueMethod.invoke(annotation)); -// assertEquals(1, values.length, "The routing annotation for index must have a value"); -// assertEquals("", values[0], "The value parameter for the routing annotation must be the empty string"); -// + if (annotation == null) { + annotation = indexMethod.getDeclaredAnnotation(GetMapping.class); + } + + assertNotNull(annotation, "index method must have a routing annotation"); + + Method annotationValueMethod = annotation.getClass().getMethod("value"); + String[] values = ((String[]) annotationValueMethod.invoke(annotation)); + assertEquals(1, values.length, "The routing annotation for index must have a value"); + assertEquals("", values[0], "The value parameter for the routing annotation must be the empty string"); + // // Verify that index calls skillRepository.findAll() -// new Expectations() {{ -// skillRepository.findAll(); -// }}; -// -// Model model = new ExtendedModelMap(); -// SkillController skillController = new SkillController(); -// Field skillRepositoryField = skillControllerClass.getDeclaredField("skillRepository"); -// skillRepositoryField.setAccessible(true); -// skillRepositoryField.set(skillController, skillRepository); -// indexMethod.invoke(skillController, model); -// } + new Expectations() {{ + skillRepository.findAll(); + }}; + + Model model = new ExtendedModelMap(); + SkillController skillController = new SkillController(); + Field skillRepositoryField = skillControllerClass.getDeclaredField("skillRepository"); + skillRepositoryField.setAccessible(true); + skillRepositoryField.set(skillController, skillRepository); + indexMethod.invoke(skillController, model); + } /* * Verify that processAddSkillForm saves a new skill to the database * */ -// @Test -// public void testNewSkillIsSaved (@Mocked SkillRepository skillRepository, @Mocked Errors errors) throws ClassNotFoundException, NoSuchMethodException, NoSuchFieldException, IllegalAccessException, InvocationTargetException { -// Class skillControllerClass = getClassByName("controllers.SkillController"); -// Method processAddSkillFormMethod = skillControllerClass.getMethod("processAddSkillForm", Skill.class, Errors.class, Model.class); -// Method saveMethod = SkillRepository.class.getMethod("save", Object.class); -// -// Skill skill = new Skill(); -// skill.setName("Java"); -// -// new Expectations() {{ -// saveMethod.invoke(skillRepository, skill); -// }}; -// -// Model model = new ExtendedModelMap(); -// SkillController skillController = new SkillController(); -// Field skillRepositoryField = skillControllerClass.getDeclaredField("skillRepository"); -// skillRepositoryField.setAccessible(true); -// skillRepositoryField.set(skillController, skillRepository); -// processAddSkillFormMethod.invoke(skillController, skill, errors, model); -// } + @Test + public void testNewSkillIsSaved (@Mocked SkillRepository skillRepository, @Mocked Errors errors) throws ClassNotFoundException, NoSuchMethodException, NoSuchFieldException, IllegalAccessException, InvocationTargetException { + Class skillControllerClass = getClassByName("controllers.SkillController"); + Method processAddSkillFormMethod = skillControllerClass.getMethod("processAddSkillForm", Skill.class, Errors.class, Model.class); + Method saveMethod = SkillRepository.class.getMethod("save", Object.class); + + Skill skill = new Skill(); + skill.setName("Java"); + + new Expectations() {{ + saveMethod.invoke(skillRepository, skill); + }}; + + Model model = new ExtendedModelMap(); + SkillController skillController = new SkillController(); + Field skillRepositoryField = skillControllerClass.getDeclaredField("skillRepository"); + skillRepositoryField.setAccessible(true); + skillRepositoryField.set(skillController, skillRepository); + processAddSkillFormMethod.invoke(skillController, skill, errors, model); + } /* * Verifies that displayViewSkill calls findById to retrieve an skill object * */ -// @Test -// public void testDisplayViewSkillCallsFindById (@Mocked SkillRepository skillRepository) throws ClassNotFoundException, NoSuchMethodException, NoSuchFieldException, IllegalAccessException, InvocationTargetException { -// Class skillControllerClass = getClassByName("controllers.SkillController"); -// Method displayViewSkillMethod = skillControllerClass.getMethod("displayViewSkill", Model.class, int.class); -// -// new Expectations() {{ -// skillRepository.findById(1); -// }}; -// -// Model model = new ExtendedModelMap(); -// SkillController skillController = new SkillController(); -// Field skillRepositoryField = skillControllerClass.getDeclaredField("skillRepository"); -// skillRepositoryField.setAccessible(true); -// skillRepositoryField.set(skillController, skillRepository); -// displayViewSkillMethod.invoke(skillController, model, 1); -// } + @Test + public void testDisplayViewSkillCallsFindById (@Mocked SkillRepository skillRepository) throws ClassNotFoundException, NoSuchMethodException, NoSuchFieldException, IllegalAccessException, InvocationTargetException { + Class skillControllerClass = getClassByName("controllers.SkillController"); + Method displayViewSkillMethod = skillControllerClass.getMethod("displayViewSkill", Model.class, int.class); + + new Expectations() {{ + skillRepository.findById(1); + }}; + + Model model = new ExtendedModelMap(); + SkillController skillController = new SkillController(); + Field skillRepositoryField = skillControllerClass.getDeclaredField("skillRepository"); + skillRepositoryField.setAccessible(true); + skillRepositoryField.set(skillController, skillRepository); + displayViewSkillMethod.invoke(skillController, model, 1); + } // --- END CONTROLLER TESTS --- //