-
Notifications
You must be signed in to change notification settings - Fork 522
Expand file tree
/
Copy pathTestTaskTwo.java
More file actions
556 lines (507 loc) · 23.6 KB
/
TestTaskTwo.java
File metadata and controls
556 lines (507 loc) · 23.6 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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
package org.launchcode.techjobs.persistent;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.MappedSuperclass;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import mockit.Expectations;
import mockit.Mocked;
import org.junit.jupiter.api.Test;
import org.launchcode.techjobs.persistent.controllers.EmployerController;
import org.launchcode.techjobs.persistent.models.Employer;
import org.launchcode.techjobs.persistent.models.Skill;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import org.springframework.ui.ExtendedModelMap;
import org.springframework.ui.Model;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.*;
import java.util.Arrays;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static org.junit.jupiter.api.Assertions.*;
/**
* Created by LaunchCode
*/
public class TestTaskTwo extends AbstractTest {
// --- BEGIN AbstractEntity TESTS --- //
/*
* Verifies that AbstractEntity has @MappedSuperclass
* */
@Test
public void testAbstractEntityHasCorrectAnnotation () throws ClassNotFoundException {
Class abstractEntityClass = getClassByName("models.AbstractEntity");
Annotation annotation = abstractEntityClass.getAnnotation(MappedSuperclass.class);
assertNotNull(annotation, "AbstractEntity must have @MappedSuperclass annotation");
}
/*
* Verifies that AbstractEntity.id has correct annotations
* */
@Test
public void testIdFieldHasCorrectAnnotations () throws ClassNotFoundException {
Class abstractEntityClass = getClassByName("models.AbstractEntity");
Field idField = null;
try {
idField = abstractEntityClass.getDeclaredField("id");
} catch (NoSuchFieldException e) {
fail("AbstractEntity does not have an id field");
}
Annotation idAnnotation = idField.getAnnotation(Id.class);
assertNotNull(idAnnotation, "id field must have @Id annotation");
Annotation generatedValueAnnotation = idField.getAnnotation(GeneratedValue.class);
assertNotNull(generatedValueAnnotation, "id field must have @GeneratedValue annotation");
}
/*
* Verifies that AbstractEntity.name has correct annotations
* */
@Test
public void testNameFieldHasCorrectAnnotations () throws ClassNotFoundException {
Class abstractEntityClass = getClassByName("models.AbstractEntity");
Field nameField = null;
try {
nameField = abstractEntityClass.getDeclaredField("name");
} catch (NoSuchFieldException e) {
fail("AbstractEntity does not have a name field");
}
Annotation sizeAnnotation = nameField.getAnnotation(Size.class);
assertNotNull(sizeAnnotation, "name field must use @Size to validate input");
// we allow for either @NotBlank or @NotNull to ensure the field is not empty
Annotation notEmptyAnnotation = nameField.getAnnotation(NotNull.class);
if (notEmptyAnnotation == null) {
notEmptyAnnotation = nameField.getAnnotation(NotBlank.class);
}
assertNotNull(notEmptyAnnotation, "name must have an annotation to ensure the field is not empty");
}
// --- END AbstractEntity TESTS --- //
// --- BEGIN Employer TESTS --- //
/*
* Verifies that Employer has a location field
* */
@Test
public void testEmployerHasLocationField () throws ClassNotFoundException {
Class employerClass = getClassByName("models.Employer");
Field locationField = null;
try {
locationField = employerClass.getDeclaredField("location");
} catch (NoSuchFieldException e) {
fail("Employer class has no location field");
}
Class locationClass = locationField.getType();
assertEquals(String.class, locationClass);
}
/*
* Verifies that Employer.location has public accessors
* */
@Test
public void testLocationFieldHasPublicAccessors () throws ClassNotFoundException, NoSuchFieldException {
Class employerClass = getClassByName("models.Employer");
Field locationField = employerClass.getDeclaredField("location");
Method getLocationMethod = null;
try {
getLocationMethod = employerClass.getDeclaredMethod("getLocation");
} catch (NoSuchMethodException e) {
fail("Employer class has no getLocation method");
}
int getLocationModifier = getLocationMethod.getModifiers();
assertEquals(Modifier.PUBLIC, getLocationModifier, "getLocation must be public");
Method setLocationMethod = null;
try {
setLocationMethod = employerClass.getDeclaredMethod("setLocation", String.class);
} catch (NoSuchMethodException e) {
fail("Employer class has no setLocation method");
}
int setLocationModifier = setLocationMethod.getModifiers();
assertEquals(Modifier.PUBLIC, setLocationModifier, "setLocation must be public");
}
/*
* Verifies that Employer.location has correct validation annotations
* */
@Test
public void testLocationHasCorrectValidationAnnotations () throws ClassNotFoundException, NoSuchFieldException {
Class employerClass = getClassByName("models.Employer");
Field locationField = employerClass.getDeclaredField("location");
Annotation sizeAnnotation = locationField.getAnnotation(Size.class);
assertNotNull(sizeAnnotation, "location field must use @Size to validate input");
// we allow for either @NotBlank or @NotNull to ensure the field is not empty
Annotation notEmptyAnnotation = locationField.getAnnotation(NotNull.class);
if (notEmptyAnnotation == null) {
notEmptyAnnotation = locationField.getAnnotation(NotBlank.class);
}
assertNotNull(notEmptyAnnotation, "location must have an annotation to ensure the field is not empty");
}
/*
* Verifies that Employer has the persistence annotation
* */
@Test
public void testEmployerHasPersistenceAnnotation () throws ClassNotFoundException {
Class employerClass = getClassByName("models.Employer");
Annotation entityAnnotation = employerClass.getAnnotation(Entity.class);
assertNotNull(entityAnnotation, "Employer must have the @Entity persistence annotation");
}
/*
* Verifies that Employer has a no-arg/default constructor
* */
@Test
public void testEmployerHasDefaultConstructor () throws ClassNotFoundException {
Class employerClass = getClassByName("models.Employer");
try {
Constructor defaultConstructor = employerClass.getDeclaredConstructor();
} catch (NoSuchMethodException e) {
fail("Employer has no no-arg/default constructor");
}
}
// --- END Employer TESTS --- //
// --- START Skill TESTS --- //
/*
* Verifies that Skill has a description field
* */
@Test
public void testSkillHasDescriptionField () throws ClassNotFoundException {
Class skillClass = getClassByName("models.Skill");
Field descriptionField = null;
try {
descriptionField = skillClass.getDeclaredField("description");
} catch (NoSuchFieldException e) {
fail("Skill class has no description field");
}
Class descriptionClass = descriptionField.getType();
assertEquals(String.class, descriptionClass);
}
/*
* Verifies that Skill.description has public accessors
* */
@Test
public void testDescriptionFieldHasPublicAccessors () throws ClassNotFoundException, NoSuchFieldException {
Class skillClass = getClassByName("models.Skill");
Field descriptionField = skillClass.getDeclaredField("description");
Method getDescriptionMethod = null;
try {
getDescriptionMethod = skillClass.getDeclaredMethod("getDescription");
} catch (NoSuchMethodException e) {
fail("Skill class has no getDescription method");
}
int getDescriptionModifier = getDescriptionMethod.getModifiers();
assertEquals(Modifier.PUBLIC, getDescriptionModifier, "getDescription must be public");
Method setDescriptionMethod = null;
try {
setDescriptionMethod = skillClass.getDeclaredMethod("setDescription", String.class);
} catch (NoSuchMethodException e) {
fail("Skill class has no setDescription method");
}
int setDescriptionModifier = setDescriptionMethod.getModifiers();
assertEquals(Modifier.PUBLIC, setDescriptionModifier, "setDescription must be public");
}
/*
* Verifies that Skill has the persistence annotation
* */
@Test
public void testSkillHasPersistenceAnnotation () throws ClassNotFoundException {
Class skillClass = getClassByName("models.Skill");
Annotation entityAnnotation = skillClass.getAnnotation(Entity.class);
assertNotNull(entityAnnotation, "Skill must have the @Entity persistence annotation");
}
/*
* Verifies that Skill has a no-arg/default constructor
* */
@Test
public void testSkillHasDefaultConstructor () throws ClassNotFoundException {
Class skillClass = getClassByName("models.Skill");
try {
Constructor defaultConstructor = skillClass.getDeclaredConstructor();
} catch (NoSuchMethodException e) {
fail("Skill has no no-arg/default constructor");
}
}
// --- END Skill TESTS --- //
// --- BEGIN DATA LAYER TESTS --- //
/*
* Verifies that EmployerRepository exists
* */
@Test
public void testEmployerRepositoryExists () {
try {
Class employerRepositoryClass = getClassByName("models.data.EmployerRepository");
} catch (ClassNotFoundException e) {
fail("EmployerRepository does not exist");
}
}
/*
* Verifies that EmployerRepository implements CrudRepository
* */
@Test
public void testEmployerRepositoryImplementsJpaInterface () throws ClassNotFoundException {
Class employerRepositoryClass = getClassByName("models.data.EmployerRepository");
Class[] interfaces = employerRepositoryClass.getInterfaces();
assertTrue(Arrays.asList(interfaces).contains(CrudRepository.class), "EmployerRepository must implement CrudRepository");
}
/*
* Verifies that EmployerRepository has @Repository
* */
@Test
public void testEmployerRepositoryHasRepositoryAnnotation () throws ClassNotFoundException {
Class employerRepositoryClass = getClassByName("models.data.EmployerRepository");
Annotation annotation = employerRepositoryClass.getAnnotation(Repository.class);
}
/*
* Verifies that SkillRepository exists
* */
@Test
public void testSkillRepositoryExists () {
try {
Class skillRepositoryClass = getClassByName("models.data.SkillRepository");
} catch (ClassNotFoundException e) {
fail("SkillRepository does not exist");
}
}
/*
* Verifies that SkillRepository implements CrudRepository
* */
@Test
public void testSkillRepositoryImplementsJpaInterface () throws ClassNotFoundException {
Class skillRepositoryClass = getClassByName("models.data.SkillRepository");
Class[] interfaces = skillRepositoryClass.getInterfaces();
assertTrue(Arrays.asList(interfaces).contains(CrudRepository.class), "SkillRepository must implement CrudRepository");
}
/*
* Verifies that SkillRepository has @Repository
* */
@Test
public void testSkillRepositoryHasRepositoryAnnotation () throws ClassNotFoundException {
Class skillRepositoryClass = getClassByName("models.data.SkillRepository");
Annotation annotation = skillRepositoryClass.getAnnotation(Repository.class);
}
// --- END DATA LAYER TESTS --- //
// --- BEGIN CONTROLLER TESTS --- //
/*
* 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");
// }
//
// /*
// * 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;
//
// // 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);
//
// // 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");
//
// // 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);
// }
//
// /*
// * 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);
// }
//
// /*
// * 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);
// }
//
// /*
// * 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");
// }
//
// /*
// * 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;
//
// // 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);
//
// // 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");
//
// // 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);
// }
//
// /*
// * 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);
// }
//
// /*
// * 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);
// }
//
// // --- END CONTROLLER TESTS --- //
//
// /*
// * Tests SQL query for task 2
// * */
// @Test
// public void testSqlQuery() throws IOException {
// String queryFileContents = getFileContents("queries.sql");
//
// Pattern queryPattern = Pattern.compile("SELECT\\s+name\\s+FROM\\s+employer\\s+WHERE\\s+location\\s*=\\s*\"St.\\s+Louis\\s+City\";", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);
// Matcher queryMatcher = queryPattern.matcher(queryFileContents);
// boolean queryFound = queryMatcher.find();
// assertTrue(queryFound, "Task 2 SQL query is incorrect. Test your query against your database to find the error.");
// }
}