-
Notifications
You must be signed in to change notification settings - Fork 522
Expand file tree
/
Copy pathTestTaskFour.java
More file actions
230 lines (214 loc) · 10.1 KB
/
TestTaskFour.java
File metadata and controls
230 lines (214 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package org.launchcode.techjobs.persistent;
import jakarta.persistence.ManyToMany;
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.JobRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ExtendedModelMap;
import org.springframework.ui.Model;
import org.springframework.validation.Errors;
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;
import static org.junit.jupiter.api.Assertions.*;
/**
* Created by LaunchCode
*/
public class TestTaskFour extends AbstractTest {
/*
* Verifies that Skill.jobs exists
* */
@Test
public void testSkillClassHasJobsField () throws ClassNotFoundException {
Class skillClass = getClassByName("models.Skill");
Field jobsField = null;
try {
jobsField = skillClass.getDeclaredField("jobs");
} catch (NoSuchFieldException e) {
fail("Skill class does not have a jobs field");
}
}
/*
* Verifies that Skill.jobs is of type List (or a subclass of List)
* */
@Test
public void testSkillJobsFieldHasCorrectType () throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {
Class skillClass = getClassByName("models.Skill");
Method getJobsMethod = skillClass.getMethod("getJobs");
Skill skill = new Skill();
Object jobsObj = getJobsMethod.invoke(skill);
assertTrue(jobsObj instanceof List);
}
/*
* Verifies that Skill.jobs has @ManyToMany with correct mappedBy value
* */
@Test
public void testSkillJobsFieldHasCorrectAnnotation () throws ClassNotFoundException, NoSuchFieldException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {
Class skillClass = getClassByName("models.Skill");
Field jobsField = skillClass.getDeclaredField("jobs");
Annotation annotation = jobsField.getDeclaredAnnotation(ManyToMany.class);
assertNotNull(annotation);
Method mappedByMethod = annotation.getClass().getMethod("mappedBy");
assertEquals("skills", mappedByMethod.invoke(annotation));
}
/*
* Verifies that Job.skills has been refactored to be of the correct type, with mapping annotation
* */
@Test
public void testJobSkillsHasCorrectTypeAndAnnotation () throws ClassNotFoundException, NoSuchFieldException {
Class jobClass = getClassByName("models.Job");
Field skillsField = jobClass.getDeclaredField("skills");
Type skillsFieldType = skillsField.getType();
assertEquals(List.class, skillsFieldType, "Job.skills should be of type List<Skills>");
assertNotNull(skillsField.getAnnotation(ManyToMany.class), "Job.skills is missing the correct mapping annotation");
}
/*
* Verifies that after refactoring Job.skills, the non-default constructor
* and accessors have been updated
* */
@Test
public void testJobSkillsRefactoring () throws ClassNotFoundException, NoSuchMethodException {
Class jobClass = getClassByName("models.Job");
try {
Constructor nonDefaultConstructor = jobClass.getConstructor(Employer.class, List.class);
} catch (NoSuchMethodException e) {
fail("The non-default constructor has not been refactored to handle the new skills field type");
}
Method getSkillsMethod = jobClass.getMethod("getSkills");
getSkillsMethod.getReturnType().isInstance(List.class);
try {
jobClass.getMethod("setSkills", List.class);
} catch (NoSuchMethodException e) {
fail("Job.setSkills has not been refactoring to handle the new skills field type");
}
}
/*
* 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");
// }
//
// /*
// * 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<Integer>) any);
// job.setSkills((List<Skill>) 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<Skill>());
// }
//
// /*
// * 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));
// }
//
// /*
// * 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 testSqlQuery () throws IOException {
String queryFileContents = getFileContents("queries.sql");
Pattern queryPattern = Pattern.compile("(?=[^()]*(\\([^()]+\\)[^()]*)*$)" +
"SELECT\\s+\\*\\s+FROM\\s+skill" +
"\\s*(LEFT|INNER)?\\s+JOIN\\s+job_skills\\s+ON\\s+\\(?(skill.id\\s*=\\s*job_skills.skills_id|job_skills.skills_id\\s*=\\s*skill.id)\\)?" +
"(\\s*WHERE\\s+\\(?job_skills.jobs_id\\s+IS\\s+NOT\\s+NULL\\)?)?" +
"\\s*ORDER\\s+BY\\s+name\\s+ASC;", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);
Matcher queryMatcher = queryPattern.matcher(queryFileContents);
boolean queryFound = queryMatcher.find();
assertTrue(queryFound, "Task 4 SQL query is incorrect. Test your query against your database to find the error.");
}
}