-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBILLTest.java
More file actions
599 lines (546 loc) · 15.6 KB
/
BILLTest.java
File metadata and controls
599 lines (546 loc) · 15.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
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
package test.java.edu.sc;
import static org.junit.Assert.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.google.gson.Gson;
import main.java.edu.sc.csce740.BILL;
import main.java.edu.sc.csce740.model.StudentRecord;
import main.java.edu.sc.csce740.model.StudentDemographics;
import main.java.edu.sc.csce740.model.Term;
import main.java.edu.sc.csce740.model.Course;
import main.java.edu.sc.csce740.model.InvalidUserIdException;
import main.java.edu.sc.csce740.model.AdminRightsException;
import main.java.edu.sc.csce740.model.Bill;
import main.java.edu.sc.csce740.model.GetRecordException;
import main.java.edu.sc.csce740.model.EditRecordException;
import main.java.edu.sc.csce740.model.InvalidPaymentException;
public class BILLTest {
static BILL testerClass;
/**
* Load users and students and logout before each test
*/
@Before
public void before() {
testerClass = new BILL();
// Load files
try {
testerClass.loadUsers("src/test/resources/users.txt");
} catch (FileNotFoundException | NullPointerException e) {
fail("yeah, this did not work.");
}
try {
testerClass.loadRecords("src/test/resources/students.txt");
} catch (FileNotFoundException | NullPointerException e) {
fail("yeah, this did not work.");
}
// Logout
testerClass.logOut();
}
/**
* Test the users ability to log out
*
* @result Account will be logged out without any errors
*/
@Test
public void testLoginLogout() {
// Login
try {
testerClass.logIn("mhunt");
} catch (InvalidUserIdException e) {
fail("Fail: InvalidUserIdException.");
}
}
/**
* Test that log in will throw an exception for invalid user id
*
* @result exception thrown
*/
@Test
public void testLoginInvalid() {
// Login
try {
testerClass.logIn("apples");
fail("Fail: We should have not got to this point.");
} catch (InvalidUserIdException e) {
} catch (Exception e) {
}
}
/**
* Test that log get user will work with no errors
*
* @result user is returned correctly
*/
@Test
public void testGetUser() {
// Login
try {
testerClass.logIn("mhunt");
} catch (InvalidUserIdException e) {
fail("Fail: InvalidUserIdException.");
}
String id = testerClass.getUser();
assertEquals("mhunt", id);
}
/**
* Test that an admin can obtain correct user ids
*
* @result correct student ids are returned
*/
@Test
public void testGetStudentIDsAdmin() {
// Login
try {
testerClass.logIn("rbob");
} catch (InvalidUserIdException e) {
fail("Fail: InvalidUserIdException.");
}
try {
List<String> studentIDs = testerClass.getStudentIDs();
assertEquals("ggay", studentIDs.get(0));
} catch (AdminRightsException e) {
fail("Fail: AdminRightsException.");
} catch (Exception e) {
fail("Fail: Exception.");
}
}
/**
* Test that a user that is not an admin cannot obtain a list of users
*
* @result exception is thrown because user is not an admin
*/
@Test
public void testGetStudentIDsNotAdmin() {
// Login
try {
testerClass.logIn("mhunt");
} catch (InvalidUserIdException e) {
fail("Fail: InvalidUserIdException.");
}
try {
testerClass.getStudentIDs();
fail("Fail: We should have not got to this point.");
} catch (AdminRightsException e) {
} catch (Exception e) {
}
}
/**
* Test that a user with correct permissions can obtain a student record
*
* @result student record is obtained with no errors
*/
@Test
public void testGetRecord() {
// Login
try {
testerClass.logIn("rbob");
} catch (InvalidUserIdException e) {
fail("Fail: InvalidUserIdException.");
}
try {
StudentRecord testStudent = testerClass.getRecord("ggay");
assertEquals("Apples", testStudent.getStudent().getFirstname());
assertEquals("ENGINEERING_AND_COMPUTING", testStudent.getCollege());
assertEquals(2016, testStudent.getTermBegan().getYear());
} catch (GetRecordException e) {
fail("Fail: GetRecordException.");
} catch (Exception e) {
fail("Fail: Exception.");
}
}
/**
* Test that a user without correct permissions cannot obtain a student
* record
*
* @result exception is thrown because user is attempting to access another
* users student record
*/
@Test
public void testGetRecordNoPermissionStudent() {
// Login
try {
testerClass.logIn("mhunt");
} catch (InvalidUserIdException e) {
fail("Fail: InvalidUserIdException.");
}
try {
testerClass.getRecord("ggay");
fail("Fail: We should have not got to this point.");
} catch (GetRecordException e) {
} catch (Exception e) {
}
}
/**
* Test that a user without correct permissions cannot obtain a student
* record
*
* @result exception is thrown because user is attempting to access another
* users student record
*/
@Test
public void testGetRecordNoPermissionAdmin() {
// Login
try {
testerClass.logIn("rbob");
} catch (InvalidUserIdException e) {
fail("Fail: InvalidUserIdException.");
}
try {
testerClass.getRecord("mhunt");
fail("Fail: We should have not got to this point.");
} catch (GetRecordException e) {
} catch (Exception e) {
}
}
/**
* Test that a record can be edited without it writing to file
*
* @result student record written in memory but not to file
*/
@Test
public void testEditRecordNotPermnanent() {
this.testEditRecord(false, "rbob", false);
}
/**
* Test that a record can be edited and written to file
*
* @result student record written in memory and also to file
*/
@Test
public void testEditRecordPermnanent() {
this.testEditRecord(true, "rbob", false);
}
/**
* Test that a record cannot be edited with invalid permissions
*
* @result student record is not edited
*/
@Test
public void testEditRecordInvalidPermission() {
this.testEditRecord(false, "jgross", true);
}
/**
* Test that a record is updated with correct values
*
* @result student record is updated with correct values
*/
@Test
public void testEditRecordInvalidValues() {
this.testEditRecordInvalid("state", "Edit Record error: Not valid address state");
testerClass.logOut();
this.testEditRecordInvalid("phone", "Edit Record error: phone does not match correct pattern");
testerClass.logOut();
this.testEditRecordInvalid("scholarship", "Edit Record error: Not valid scholarship");
}
/**
* This function is used for testing student record in various ways
* depending on the parameters Edit record
*
* @param permanent
* To write or not write to file
* @param user
* User to edit record of
* @param shouldFail
* If this test should fail
*/
public void testEditRecord(boolean permanent, String user, boolean shouldFail) {
// Login
try {
testerClass.logIn(user);
} catch (InvalidUserIdException e) {
fail("Fail: InvalidUserIdException.");
}
StudentRecord testStudent = new StudentRecord();
StudentDemographics testStudentStudent = new StudentDemographics();
Term testStudentTermBegan = new Term();
Course[] testStudentCourses = new Course[1];
try {
testStudentStudent.setId("ggay");
testStudentStudent.setFirstname("Apples");
testStudentStudent.setLastname("Limes");
testStudentStudent.setEmailAddress("jamie@jamie.com");
testStudentStudent.setAddressStreet("111 Main St");
testStudentStudent.setAddressCity("Columbia");
testStudentStudent.setAddressState("NC");
testStudentStudent.setAddressPostalCode("29201");
testStudentStudent.setPhone("222-222-2222");
testStudent.setCollege("ENGINEERING_AND_COMPUTING");
testStudentTermBegan.setSemester("FALL");
testStudentTermBegan.setYear(2016);
testStudent.setClassStatus("JUNIOR");
testStudent.setStudyAbroad("NONE");
testStudent.setInternationalStatus("NONE");
testStudent.setScholarship("SIMS");
testStudent.setInternationalStatus("SPONSORED");
Course course1 = new Course();
course1.setName("Apples Apples Apples");
course1.setId("CSCE");
course1.setNumCredits(3);
testStudentCourses[0] = course1;
testStudent.setStudent(testStudentStudent);
testStudent.setTermBegan(testStudentTermBegan);
testStudent.setCourses(testStudentCourses);
testerClass.editRecord("ggay", testStudent, permanent);
if (shouldFail) {
fail("Fail: We should have not got to this point.");
}
} catch (AdminRightsException e) {
if (!shouldFail) {
fail("Fail: AdminRightsException.");
}
} catch (Exception e) {
if (!shouldFail) {
fail("Fail: Exception.");
}
}
try {
StudentRecord retrievedStudent = testerClass.getRecord("ggay");
assertEquals("Apples", retrievedStudent.getStudent().getFirstname());
assertEquals("222-222-2222", retrievedStudent.getStudent().getPhone());
assertEquals("NC", retrievedStudent.getStudent().getAddressState());
assertEquals(2016, retrievedStudent.getTermBegan().getYear());
assertEquals("SIMS", retrievedStudent.getScholarship());
assertEquals("Apples Apples Apples", retrievedStudent.getCourses()[0].getName());
assertEquals("SPONSORED", retrievedStudent.getInternationalStatus());
} catch (GetRecordException e) {
if (!shouldFail) {
fail("Fail: GetRecordException.");
}
} catch (Exception e) {
if (!shouldFail) {
fail("Fail: Exception.");
}
}
}
/**
* Edit record with invalid value
*
* @param value
* Field to edit
* @param error
* The error it should throw
*/
public void testEditRecordInvalid(String value, String error) {
// Login
try {
testerClass.logIn("rbob");
} catch (InvalidUserIdException e) {
fail("Fail: InvalidUserIdException.");
}
StudentRecord testStudent = new StudentRecord();
StudentDemographics testStudentStudent = new StudentDemographics();
Term testStudentTermBegan = new Term();
Course[] testStudentCourses = new Course[1];
try {
testStudentStudent.setId("ggay");
testStudentStudent.setFirstname("Apples");
testStudentStudent.setLastname("Limes");
testStudentStudent.setEmailAddress("jamie@jamie.com");
testStudentStudent.setAddressStreet("111 Main St");
testStudentStudent.setAddressCity("Columbia");
if (value == "state") {
testStudentStudent.setAddressState("OP");
} else {
testStudentStudent.setAddressState("NC");
}
testStudentStudent.setAddressPostalCode("29201");
if (value == "phone") {
testStudentStudent.setPhone("222-2223-2222");
} else {
testStudentStudent.setPhone("222-222-2222");
}
testStudent.setCollege("ENGINEERING_AND_COMPUTING");
testStudentTermBegan.setSemester("FALL");
testStudentTermBegan.setYear(2016);
testStudent.setClassStatus("JUNIOR");
testStudent.setStudyAbroad("NONE");
testStudent.setInternationalStatus("NONE");
if (value == "scholarship") {
testStudent.setScholarship("SIMS!");
} else {
testStudent.setScholarship("SIMS");
}
testStudent.setInternationalStatus("SPONSORED");
Course course1 = new Course();
course1.setName("Apples Apples Apples");
course1.setId("CSCE");
course1.setNumCredits(3);
testStudentCourses[0] = course1;
testStudent.setStudent(testStudentStudent);
testStudent.setTermBegan(testStudentTermBegan);
testStudent.setCourses(testStudentCourses);
testerClass.editRecord("ggay", testStudent, false);
fail("Fail: We should have not got to this point.");
} catch (AdminRightsException e) {
fail("Fail: AdminRightsException.");
} catch (EditRecordException e) {
assertEquals(error, e.getMessage());
} catch (Exception e) {
fail("Fail: Exception.");
}
}
/**
* Apply a new payment to a user and generate the bill that logged in user
* has permission for
*/
@Test
public void testApplyPaymentGenerateBill() {
// Login
try {
testerClass.logIn("ggay");
} catch (InvalidUserIdException e) {
fail("Fail: InvalidUserIdException.");
}
try {
testerClass.applyPayment("ggay", 5000, "Limes");
} catch (AdminRightsException e) {
fail("Fail: We should have not got to this point.");
} catch (Exception e) {
fail("Fail: We should have not got to this point.");
}
try {
Gson gson = new Gson();
String json = gson.toJson(testerClass.generateBill("ggay"));
File file = new File("src/test/resources/bill.txt");
if (file.exists()) {
file.delete();
}
FileWriter writer = new FileWriter(file);
writer.write(json);
writer.close();
} catch (IOException e) {
fail("Fail: We should have not got to this point.");
} catch (AdminRightsException e) {
fail("Fail: We should have not got to this point.");
} catch (Exception e) {
fail("Fail: We should have not got to this point.");
}
}
/**
* Apply a payment to user that logged in user has no permission for
*/
@Test
public void testApplyPaymentNoPermission() {
// Login
try {
testerClass.logIn("ggay");
} catch (InvalidUserIdException e) {
fail("Fail: InvalidUserIdException.");
}
try {
testerClass.applyPayment("jgross", 5000, "Apples");
fail("Fail: We should have not got to this point.");
} catch (AdminRightsException e) {
} catch (Exception e) {
}
}
/**
* Apply invalid payment amount
*/
@Test
public void testApplyPaymentInvalid() {
// Login
try {
testerClass.logIn("ggay");
} catch (InvalidUserIdException e) {
fail("Fail: InvalidUserIdException.");
}
try {
testerClass.applyPayment("jgross", -5000, "Apples");
fail("Fail: We should have not got to this point.");
} catch (AdminRightsException e) {
} catch (InvalidPaymentException e) {
} catch (Exception e) {
}
}
/**
* Generate bill for user that logged in user does not have permission for
*/
@Test
public void testGenerateBillNoPermission() {
// Login
try {
testerClass.logIn("ggay");
} catch (InvalidUserIdException e) {
fail("Fail: InvalidUserIdException.");
}
try {
Gson gson = new Gson();
String json = gson.toJson(testerClass.generateBill("mhunt"));
File file = new File("src/test/resources/bill.txt");
if (file.exists()) {
file.delete();
}
FileWriter writer = new FileWriter(file);
writer.write(json);
writer.close();
fail("Fail: We should have not got to this point.");
} catch (IOException e) {
} catch (AdminRightsException e) {
} catch (Exception e) {
}
}
/**
* View charges for user that does have permission
*/
@Test
public void testViewCharges() {
// Login
try {
testerClass.logIn("mhunt");
} catch (InvalidUserIdException e) {
fail("Fail: InvalidUserIdException.");
}
try {
Bill charges = testerClass.viewCharges("mhunt", 10, 16, 2016, 5, 1, 2018);
assertEquals(-2000, charges.getBalance(), 0.01);
assertEquals("PHD", charges.getClassStatus());
assertEquals(2000, charges.getTransactions()[0].getAmount(), 0.01);
} catch (AdminRightsException e) {
fail("Fail: AdminRightsException.");
} catch (Exception e) {
fail("Fail: Exception.");
}
}
/**
* View charges for user that logged in student user does not have
* permission for
*/
@Test
public void testViewChargesNoPermissionForStudent() {
// Login
try {
testerClass.logIn("ggay");
} catch (InvalidUserIdException e) {
fail("Fail: InvalidUserIdException.");
}
try {
testerClass.viewCharges("mhunt", 10, 16, 2016, 5, 1, 2018);
fail("Fail: We should have not got to this point.");
} catch (AdminRightsException e) {
} catch (Exception e) {
}
}
@Test
public void testGenerateBill() {
// Login
try {
testerClass.logIn("mhunt");
} catch (InvalidUserIdException e) {
fail("Fail: InvalidUserIdException.");
}
try {
testerClass.generateBill("mhunt");
} catch(Exception e) {
fail("Fail: mhunt should be able to generate bill for mhunt.");
}
}
@After
public void after() {
}
}