-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTester.java
404 lines (304 loc) · 16.6 KB
/
Tester.java
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
import classes.CheckOut;
import classes.Document.AVMaterial;
import classes.Document.Book;
import classes.Document.Copy;
import classes.Document.DocAddress;
import classes.User.*;
import com.mongodb.DB;
import database.*;
import org.bson.types.ObjectId;
import org.telegram.telegrambots.api.methods.send.SendMessage;
import services.Constants;
import services.DateTime;
import updatehandler.FineSystem;
import updatehandler.NotificationSystem;
import updatehandler.RenewSystem;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.GregorianCalendar;
/**
* Class for creating tests. Was used for 1st Delivery.
*/
public class Tester {
static private final String LOGTAG = "TESTER: ";
static Book d1, d2;
static AVMaterial d3;
static Faculty p1, p2, p3;
static Student s;
static VisitingProfessor v;
static Librarian librarian;
public static void init() {
System.out.println("<-----------Initialization of objects----------->");
Constants.BLOCKED_BOT = true;
//librarian
librarian = new Librarian(01234, "Name", "Surname", "Email", "Phone", "Address");
LibrarianDB.insertLibrarian(librarian);
DocAddress address = new DocAddress("room", "level", "docCase");
//TODO add publisher and publication_year to Book
//d1
ArrayList<String> authors = new ArrayList<>(Arrays.asList(": Thomas H. Cormen", "Charles E. Leiserson", "Ronald L. Rivest and Clifford Stein"));
ArrayList<String> keywords = new ArrayList<>(Arrays.asList(""));
d1 = new Book("Introduction to Algorithms", "Third edition", authors, "https://www.pearsonhighered.com/assets/bigcovers/0/3/2/1/0321570561.jpg",
5000.0, keywords, false);
//add 3 copies
ArrayList<ObjectId> copyIds = new ArrayList<>();
for (int i = 0; i != 3; i++) {
Copy copy = new Copy(d1.getId(), address);
CopyDB.insertCopy(copy);
copyIds.add(copy.getId());
d1.setCopyIds(copyIds);
}
librarian.addDocument(d1, Constants.BOOK_COLLECTION);
//d2
authors = new ArrayList<>(Arrays.asList("Erich Gamma", "Ralph Johnson", "John Vlissides", "Richard Helm"));
keywords = new ArrayList<>(Arrays.asList(""));
d2 = new Book("Design Patterns: Elements of Reusable Object-Oriented Software", "First edition", authors, "https://ars.els-cdn.com/content/image/X0012365X.jpg",
1700.0, keywords, true);
//add 3 copies
copyIds = new ArrayList<>();
for (int i = 0; i != 3; i++) {
Copy copy = new Copy(d2.getId(), address);
CopyDB.insertCopy(copy);
copyIds.add(copy.getId());
d2.setCopyIds(copyIds);
}
librarian.addDocument(d2, Constants.BOOK_COLLECTION);
//d3
authors = new ArrayList<>(Arrays.asList(" Tony Hoare"));
keywords = new ArrayList<>(Arrays.asList(""));
d3 = new AVMaterial("Null References: The Billion Dollar Mistake", authors, "https://s00.yaplakal.com/pics/pics_original/2/0/8/10510802.jpg", 700.0, keywords);
copyIds = new ArrayList<>();
for (int i = 0; i != 2; i++) {
Copy copy = new Copy(d3.getId(), address);
CopyDB.insertCopy(copy);
copyIds.add(copy.getId());
d3.setCopyIds(copyIds);
}
librarian.addDocument(d3, Constants.BOOK_COLLECTION);
//p1
p1 = new Faculty((long) 1010, "Sergey", "Afonso", Status.Professor, "Email", "30001", "Via Margutta, 3");
PatronDB.insertPatron(p1);
//p2
p2 = new Faculty((long) 1011, "Nadia", "Teixeira", Status.Professor, "Email", "30002", "Via Sacra, 13");
PatronDB.insertPatron(p2);
//p3
p3 = new Faculty((long) 1100, "Elvira", "Espindola", Status.Professor, "Email", "30003", "Via del Corso, 22");
PatronDB.insertPatron(p3);
//s
s = new Student((long) 1101, "Andrey", "Velo", "Email", "30004", "Avenida Mazatlan 250");
PatronDB.insertPatron(s);
//v
v = new VisitingProfessor((long) 1110, "Veronika", "Rama", "Email", "30005", "Stret Atocha, 27");
PatronDB.insertPatron(v);
System.out.println("<-----------Initialization completed------------>");
}
public static void TC1() {
init();
System.out.println("<----------------Test 1 Started----------------->");
Constants.TEMPORARY_CHANGEABLE_CALENDAR = new GregorianCalendar(2018, Calendar.MARCH, 7);
p1.checkOutDocument(d1, Constants.BOOK_COLLECTION);
p1.checkOutDocument(d2, Constants.BOOK_COLLECTION);
librarian.confirmCheckout(CheckOutDB.getCheckOut(p1.getId(), d2.getId()));
ArrayList<CheckOut> checkOuts = CheckOutDB.getCheckOutsList();
ArrayList<CheckOut> overdueCheckOuts = CheckOutDB.getOverdueCheckOutsList();
assert checkOuts.size() == 1;
assert checkOuts.get(0).getDocId().equals(d1.getId());
assert checkOuts.get(0).getToDate().equals(DateTime.todayCalendar());
assert overdueCheckOuts.size() == 0;
System.out.println("<---------------------PASS---------------------->");
//drop database
DB db = DatabaseManager.getDB("Library");
db.dropDatabase();
System.out.println("<---------------Test 1 Completed---------------->");
}
public static void TC2() {
init();
System.out.println("<----------------Test 2 Started----------------->");
Constants.TEMPORARY_CHANGEABLE_CALENDAR = new GregorianCalendar(2018, Calendar.MARCH, 7);
p1.checkOutDocument(d1, Constants.BOOK_COLLECTION);
p1.checkOutDocument(d2, Constants.BOOK_COLLECTION);
s.checkOutDocument(d1, Constants.BOOK_COLLECTION);
s.checkOutDocument(d2, Constants.BOOK_COLLECTION);
v.checkOutDocument(d1, Constants.BOOK_COLLECTION);
v.checkOutDocument(d2, Constants.BOOK_COLLECTION);
ArrayList<CheckOut> checkOuts = CheckOutDB.getCheckOutsList();
ArrayList<CheckOut> overdueCheckOuts = CheckOutDB.getOverdueCheckOutsList();
assert checkOuts.size() == 2;
assert overdueCheckOuts.size() == 4;
assert DateTime.daysUntilToday(CheckOutDB.getCheckOut(p1.getId(), d1.getId()).getToDate()) == 0;
assert DateTime.daysUntilToday(CheckOutDB.getCheckOut(p1.getId(), d2.getId()).getToDate()) == 0;
assert DateTime.daysUntilToday(CheckOutDB.getCheckOut(s.getId(), d1.getId()).getToDate()) == 7;
assert DateTime.daysUntilToday(CheckOutDB.getCheckOut(s.getId(), d2.getId()).getToDate()) == 14;
assert DateTime.daysUntilToday(CheckOutDB.getCheckOut(v.getId(), d1.getId()).getToDate()) == 21;
assert DateTime.daysUntilToday(CheckOutDB.getCheckOut(v.getId(), d2.getId()).getToDate()) == 21;
assert FineSystem.fine(d1.getPrice(), 0) == 0;
assert FineSystem.fine(d2.getPrice(), 0) == 0;
assert FineSystem.fine(d1.getPrice(), 7) == 700.0;
assert FineSystem.fine(d2.getPrice(), 14) == 1400.0;
assert FineSystem.fine(d1.getPrice(), 21) == 2100.0;
assert FineSystem.fine(d2.getPrice(), 21) == 1700.0;
System.out.println("<---------------------PASS---------------------->");
//drop database
DB db = DatabaseManager.getDB("Library");
db.dropDatabase();
System.out.println("<---------------Test 2 Completed---------------->");
}
public static void TC3() {
init();
System.out.println("<----------------Test 3 Started----------------->");
Constants.TEMPORARY_CHANGEABLE_CALENDAR = new GregorianCalendar(2018, Calendar.MARCH, 31);
CheckOut checkOut1 = p1.checkOutDocument(d1, Constants.BOOK_COLLECTION);
CheckOut checkout2 = s.checkOutDocument(d2, Constants.BOOK_COLLECTION);
CheckOut checkOut3 = v.checkOutDocument(d2, Constants.BOOK_COLLECTION);
Constants.TEMPORARY_CHANGEABLE_CALENDAR = DateTime.todayCalendar();
RenewSystem.handle(checkOut1);
RenewSystem.handle(checkout2);
RenewSystem.handle(checkOut3);
ArrayList<CheckOut> checkOuts = CheckOutDB.getCheckOutsList();
ArrayList<CheckOut> overdueCheckOuts = CheckOutDB.getOverdueCheckOutsList();
assert checkOuts.size() == 3;
assert overdueCheckOuts.size() == 0;
assert CheckOutDB.getCheckOut(p1.getId(), d1.getId()).getToDate().equals(new GregorianCalendar(2018, Calendar.MAY, 2));
assert CheckOutDB.getCheckOut(s.getId(), d2.getId()).getToDate().equals(new GregorianCalendar(2018, Calendar.APRIL, 18));
assert CheckOutDB.getCheckOut(v.getId(), d2.getId()).getToDate().equals(new GregorianCalendar(2018, Calendar.APRIL, 11));
System.out.println("<---------------------PASS---------------------->");
//drop database
DB db = DatabaseManager.getDB("Library");
db.dropDatabase();
System.out.println("<---------------Test 3 Completed---------------->");
}
public static void TC4() {
init();
System.out.println("<----------------Test 4 Started----------------->");
Constants.TEMPORARY_CHANGEABLE_CALENDAR = new GregorianCalendar(2018, Calendar.MARCH, 31);
p1.checkOutDocument(d1, Constants.BOOK_COLLECTION);
s.checkOutDocument(d2, Constants.BOOK_COLLECTION);
v.checkOutDocument(d2, Constants.BOOK_COLLECTION);
librarian.sendOutstandingRequest(d2.getId());
Constants.TEMPORARY_CHANGEABLE_CALENDAR = DateTime.todayCalendar();
RenewSystem.handle(CheckOutDB.getCheckOut(p1.getId(), d1.getId()));
RenewSystem.handle(CheckOutDB.getCheckOut(s.getId(), d2.getId()));
RenewSystem.handle(CheckOutDB.getCheckOut(v.getId(), d2.getId()));
ArrayList<CheckOut> checkOuts = CheckOutDB.getCheckOutsList();
ArrayList<CheckOut> overdueCheckOuts = CheckOutDB.getOverdueCheckOutsList();
assert checkOuts.size() == 3;
assert overdueCheckOuts.size() == 0;
assert CheckOutDB.getCheckOut(p1.getId(), d1.getId()).getToDate().equals(new GregorianCalendar(2018, Calendar.MAY, 2));
assert CheckOutDB.getCheckOut(s.getId(), d2.getId()).getToDate().equals(new GregorianCalendar(2018, Calendar.APRIL, 5));
assert CheckOutDB.getCheckOut(v.getId(), d2.getId()).getToDate().equals(new GregorianCalendar(2018, Calendar.APRIL, 5));
System.out.println("<---------------------PASS---------------------->");
//drop database
DB db = DatabaseManager.getDB("Library");
db.dropDatabase();
System.out.println("<---------------Test 4 Completed---------------->");
}
public static void TC5() {
init();
System.out.println("<----------------Test 5 Started----------------->");
p1.checkOutDocument(d3, Constants.AVMATERIAL_COLLECTION);
s.checkOutDocument(d3, Constants.AVMATERIAL_COLLECTION);
v.checkOutDocument(d3, Constants.AVMATERIAL_COLLECTION);
ArrayList<Patron> queue = PriorityQueueDB.getQueue(d3.getId());
assert queue.size() == 1;
assert queue.get(0).getId() == v.getId();
System.out.println("<---------------------PASS---------------------->");
//drop database
DB db = DatabaseManager.getDB("Library");
db.dropDatabase();
System.out.println("<---------------Test 5 Completed---------------->");
}
public static void TC6(boolean dropDB) {
init();
System.out.println("<----------------Test 6 Started----------------->");
p1.checkOutDocument(d3, Constants.AVMATERIAL_COLLECTION);
p2.checkOutDocument(d3, Constants.AVMATERIAL_COLLECTION);
s.checkOutDocument(d3, Constants.AVMATERIAL_COLLECTION);
v.checkOutDocument(d3, Constants.AVMATERIAL_COLLECTION);
p3.checkOutDocument(d3, Constants.AVMATERIAL_COLLECTION);
assert PriorityQueueDB.getQueue(d3.getId()).size() == 3;
assert PriorityQueueDB.contains(s.getId(), d3.getId());
assert PriorityQueueDB.contains(v.getId(), d3.getId());
assert PriorityQueueDB.contains(p3.getId(), d3.getId());
System.out.println("<---------------------PASS---------------------->");
//drop database
if (dropDB) {
DB db = DatabaseManager.getDB("Library");
db.dropDatabase();
}
System.out.println("<---------------Test 6 Completed---------------->");
}
public static void TC7() {
TC6(false);
System.out.println("<----------------Test 7 Started----------------->");
librarian.sendOutstandingRequest(d3.getId());
ArrayList<Patron> queue = PriorityQueueDB.getQueue(d3.getId());
ArrayList<CheckOut> checkOuts = CheckOutDB.getCheckOutsList();
assert queue.size() == 0;
assert checkOuts.size() == 2;
assert checkOuts.get(0).getToDate().equals(new GregorianCalendar(2018, Calendar.APRIL, 5));
assert checkOuts.get(1).getToDate().equals(new GregorianCalendar(2018, Calendar.APRIL, 5));
System.out.println("<---------------------PASS---------------------->");
//drop database
DB db = DatabaseManager.getDB("Library");
db.dropDatabase();
System.out.println("<---------------Test 7 Completed---------------->");
}
public static void TC8() {
TC6(false);
System.out.println("<----------------Test 8 Started----------------->");
p2.returnDocument(d3);
ArrayList<CheckOut> checkOuts = CheckOutDB.getCheckOutsList();
assert CheckOutDB.getCheckOutsList().size() == 2;
librarian.confirmCheckout(CheckOutDB.getCheckOut(p2.getId(), d3.getId()));
ArrayList<SendMessage> msgs = NotificationSystem.notifyPatron(librarian.getId(), d3.getId(), Constants.AVMATERIAL_COLLECTION);
assert CheckOutDB.getCheckOutsList().size() == 1;
assert CheckOutDB.getCheckOutsListByUserId(p2.getId()).isEmpty();
assert msgs.get(0).getChatId().equals(Long.toString(s.getId()));
assert PriorityQueueDB.getQueue(d3.getId()).size() == 2;
assert PriorityQueueDB.contains(v.getId(), d3.getId());
assert PriorityQueueDB.contains(p3.getId(), d3.getId());
System.out.println("<---------------------PASS---------------------->");
//drop database
DB db = DatabaseManager.getDB("Library");
db.dropDatabase();
System.out.println("<---------------Test 8 Completed---------------->");
}
public static void TC9() {
TC6(false);
System.out.println("<----------------Test 9 Started----------------->");
Constants.TEMPORARY_CHANGEABLE_CALENDAR = new GregorianCalendar(2018, Calendar.APRIL, 18);
RenewSystem.handle(CheckOutDB.getCheckOut(p1.getId(), d3.getId()));
assert CheckOutDB.getCheckOut(p1.getId(), d3.getId()).getToDate().equals(new GregorianCalendar(2018, Calendar.MAY, 2));
assert PriorityQueueDB.getQueue(d3.getId()).size() == 3;
assert PriorityQueueDB.contains(s.getId(), d3.getId());
assert PriorityQueueDB.contains(v.getId(), d3.getId());
assert PriorityQueueDB.contains(p3.getId(), d3.getId());
System.out.println("<---------------------PASS---------------------->");
//drop database
DB db = DatabaseManager.getDB("Library");
db.dropDatabase();
System.out.println("<---------------Test 9 Completed---------------->");
}
public static void TC10() {
init();
System.out.println("<----------------Test 10 Started---------------->");
Constants.TEMPORARY_CHANGEABLE_CALENDAR = new GregorianCalendar(2018, Calendar.MARCH, 28);
p1.checkOutDocument(d1, Constants.BOOK_COLLECTION);
v.checkOutDocument(d1, Constants.BOOK_COLLECTION);
Constants.TEMPORARY_CHANGEABLE_CALENDAR = new GregorianCalendar(2018, Calendar.MARCH, 31);
RenewSystem.handle(CheckOutDB.getCheckOut(p1.getId(), d1.getId()));
RenewSystem.handle(CheckOutDB.getCheckOut(v.getId(), d1.getId()));
Constants.TEMPORARY_CHANGEABLE_CALENDAR = DateTime.todayCalendar();
RenewSystem.handle(CheckOutDB.getCheckOut(p1.getId(), d1.getId()));
RenewSystem.handle(CheckOutDB.getCheckOut(v.getId(), d1.getId()));
assert CheckOutDB.getCheckOutsList().size() == 2;
assert CheckOutDB.getCheckOut(p1.getId(), d1.getId()).getToDate().equals(new GregorianCalendar(2018, Calendar.APRIL, 28));
assert CheckOutDB.getCheckOut(v.getId(), d1.getId()).getToDate().equals(new GregorianCalendar(2018, Calendar.APRIL, 11));
System.out.println("<---------------------PASS---------------------->");
//drop database
DB db = DatabaseManager.getDB("Library");
db.dropDatabase();
System.out.println("<---------------Test 10 Completed--------------->");
}
}