]> git.somenet.org - pub/jan/dst18.git/blob - ass1-jpa/src/test/java/dst/ass1/jpa/tests/TestData.java
[1.1.1+1.3.1] model + dao(-stubs) + Material Listener.
[pub/jan/dst18.git] / ass1-jpa / src / test / java / dst / ass1 / jpa / tests / TestData.java
1 package dst.ass1.jpa.tests;
2
3 import java.math.BigDecimal;
4 import java.security.MessageDigest;
5 import java.security.NoSuchAlgorithmException;
6 import java.util.Date;
7 import java.util.GregorianCalendar;
8 import java.util.TimeZone;
9
10 import javax.persistence.EntityManager;
11
12 import dst.ass1.jpa.ITestData;
13 import dst.ass1.jpa.model.CourseStatus;
14 import dst.ass1.jpa.model.IAddress;
15 import dst.ass1.jpa.model.ICourse;
16 import dst.ass1.jpa.model.ICoursePlatform;
17 import dst.ass1.jpa.model.IEnrollment;
18 import dst.ass1.jpa.model.IEnrollmentKey;
19 import dst.ass1.jpa.model.ILecturer;
20 import dst.ass1.jpa.model.ILesson;
21 import dst.ass1.jpa.model.IMaterial;
22 import dst.ass1.jpa.model.IMaterialServer;
23 import dst.ass1.jpa.model.IMembership;
24 import dst.ass1.jpa.model.IMetadata;
25 import dst.ass1.jpa.model.IModelFactory;
26 import dst.ass1.jpa.model.IParticipant;
27
28 /**
29  * Test data used by the default set of tests.
30  */
31 public class TestData implements ITestData {
32
33     public static final String COURSE_PLATFORM_1_NAME = "coursePlatform1";
34     public static final String COURSE_PLATFORM_2_NAME = "coursePlatform2";
35     public static final String COURSE_1_NAME = "course1";
36     public static final String COURSE_2_NAME = "course2";
37     public static final String COURSE_3_NAME = "course3";
38     public static final String COURSE_4_NAME = "course4";
39     public static final String MATERIAL_SERVER_1_NAME = "materialServer1";
40     public static final String MATERIAL_SERVER_2_NAME = "materialServer2";
41     public static final String MATERIAL_SERVER_3_NAME = "materialServer3";
42     public static final String MATERIAL_1_URN = "material1";
43     public static final String MATERIAL_2_URN = "material2";
44     public static final String MATERIAL_3_URN = "material3";
45     public static final String MATERIAL_4_URN = "material4";
46     public static final String MATERIAL_5_URN = "material5";
47     public static final String METADATA_1_KEY_1 = "key11";
48     public static final String METADATA_1_VALUE_1 = "setting11";
49     public static final String METADATA_1_KEY_2 = "key12";
50     public static final String METADATA_1_VALUE_2 = "setting12";
51     public static final String METADATA_2_KEY_1 = "key21";
52     public static final String METADATA_2_VALUE_1 = "setting21";
53     public static final String LECTURER_1_NAME = "Alex";
54     public static final String LECTURER_2_NAME = "Alexandra";
55     public static final String LECTURER_3_NAME = "Alexander";
56     public static final String PARTICIPANT_1_NAME = "Participant1";
57     public static final String PARTICIPANT_1_PW = "pw4";
58     public static final String PARTICIPANT_1_ACCOUNT_NO = "account1";
59     public static final String PARTICIPANT_1_BANK_CODE = "bank1";
60     public static final String PARTICIPANT_1_EMAIL = PARTICIPANT_1_NAME + "@example.com";
61     public static final String PARTICIPANT_2_NAME = "Participant2";
62     public static final String PARTICIPANT_2_PW = "pw5";
63     public static final String PARTICIPANT_2_ACCOUNT_NO = "account2";
64     public static final String PARTICIPANT_2_BANK_CODE = "bank2";
65     public static final String PARTICIPANT_2_EMAIL = PARTICIPANT_2_NAME + "@example.com";
66     public static final String LESSON_1_DESC = "Advanced Enterprise Cloud Machine Learning";
67     public static final String LESSON_2_DESC = "Why you should invest in Bitcoin right now";
68     public static final String LESSON_3_DESC = "Blockchain for whatever domain your company works in";
69     public static final String LESSON_4_DESC = "The Dark Net and other terrible things";
70     public static final String LESSON_5_DESC = "Build nano-services to solve all your problems";
71
72     public Long lecturer1Id;
73     public Long lecturer2Id;
74     public Long lecturer3Id;
75     public Long participant1Id;
76     public Long participant2Id;
77     public Long lesson1Id;
78     public Long lesson2Id;
79     public Long lesson3Id;
80     public Long lesson4Id;
81     public Long lesson5Id;
82     public Long coursePlatform1Id;
83     public Long coursePlatform2Id;
84     public Long metadata1Id;
85     public Long metadata2Id;
86     public Long material1Id;
87     public Long material2Id;
88     public Long material3Id;
89     public Long material4Id;
90     public Long material5Id;
91     public Long materialServer1Id;
92     public Long materialServer2Id;
93     public Long materialServer3Id;
94     public Long course1Id;
95     public Long course2Id;
96     public Long course3Id;
97     public Long course4Id;
98
99     public static Date createDate(int year, int month, int day, int hours, int minutes) {
100         GregorianCalendar cal = new GregorianCalendar(year, month, day, hours, minutes);
101         cal.setTimeZone(TimeZone.getTimeZone("UTC"));
102         return cal.getTime();
103     }
104
105     @Override
106     public void insert(IModelFactory modelFactory, EntityManager em) {
107         MessageDigest md = getMessageDigest();
108
109         // Addresses
110         IAddress address1 = modelFactory.createAddress();
111         IAddress address2 = modelFactory.createAddress();
112         IAddress address3 = modelFactory.createAddress();
113         IAddress address4 = modelFactory.createAddress();
114         IAddress address5 = modelFactory.createAddress();
115
116         address1.setCity("city1");
117         address1.setStreet("street1");
118         address1.setZipCode("zip1");
119
120         address2.setCity("city2");
121         address2.setStreet("street2");
122         address2.setZipCode("zip2");
123
124         address3.setCity("city3");
125         address3.setStreet("street3");
126         address3.setZipCode("zip3");
127
128         address4.setCity("city4");
129         address4.setStreet("street4");
130         address4.setZipCode("zip4");
131
132         address5.setCity("city5");
133         address5.setStreet("street5");
134         address5.setZipCode("zip5");
135
136         // Lecturers
137         ILecturer lecturer1 = modelFactory.createLecturer();
138         ILecturer lecturer2 = modelFactory.createLecturer();
139         ILecturer lecturer3 = modelFactory.createLecturer();
140
141         lecturer1.setName(LECTURER_1_NAME);
142         lecturer1.setAddress(address1);
143
144         lecturer2.setName(LECTURER_2_NAME);
145         lecturer2.setAddress(address2);
146
147         lecturer3.setName(LECTURER_3_NAME);
148         lecturer3.setAddress(address5);
149
150         // Participants
151         IParticipant participant1 = modelFactory.createParticipant();
152         participant1.setName(PARTICIPANT_1_NAME);
153         participant1.setAddress(address3);
154         participant1.setAccountNo(PARTICIPANT_1_ACCOUNT_NO);
155         participant1.setBankCode(PARTICIPANT_1_BANK_CODE);
156         participant1.setEmail(PARTICIPANT_1_EMAIL);
157         participant1.setPassword(md.digest(PARTICIPANT_1_PW.getBytes()));
158
159         IParticipant participant2 = modelFactory.createParticipant();
160         participant2.setName(PARTICIPANT_2_NAME);
161         participant2.setAddress(address4);
162         participant2.setAccountNo(PARTICIPANT_2_ACCOUNT_NO);
163         participant2.setBankCode(PARTICIPANT_2_BANK_CODE);
164         participant2.setEmail(PARTICIPANT_2_EMAIL);
165         participant2.setPassword(md.digest(PARTICIPANT_2_PW.getBytes()));
166
167         // Materials
168         IMaterial material1 = modelFactory.createMaterial();
169         material1.setUrn(MATERIAL_1_URN);
170         material1.setType("MPEG");
171         material1.setUploaded(new Date());
172         material1.setLastUpdate(new Date());
173
174         IMaterial material2 = modelFactory.createMaterial();
175         material2.setUrn(MATERIAL_2_URN);
176         material2.setType("PDF");
177         material2.setUploaded(new Date());
178         material2.setLastUpdate(new Date());
179
180         IMaterial material3 = modelFactory.createMaterial();
181         material3.setUrn(MATERIAL_3_URN);
182         material3.setType("PNG");
183         material3.setUploaded(new Date());
184         material3.setLastUpdate(new Date());
185
186         IMaterial material4 = modelFactory.createMaterial();
187         material4.setUrn(MATERIAL_4_URN);
188         material4.setType("PDF");
189         material4.setUploaded(new Date());
190         material4.setLastUpdate(new Date());
191
192         IMaterial material5 = modelFactory.createMaterial();
193         material5.setUrn(MATERIAL_5_URN);
194         material5.setType("PNG");
195         material5.setUploaded(new Date());
196         material5.setLastUpdate(new Date());
197
198         // Material Servers
199         IMaterialServer materialServer1 = modelFactory.createMaterialServer();
200         materialServer1.setName(MATERIAL_SERVER_1_NAME);
201         materialServer1.setLastMaintenance(new Date());
202         materialServer1.setNextMaintenance(new Date());
203
204         IMaterialServer materialServer2 = modelFactory.createMaterialServer();
205         materialServer2.setName(MATERIAL_SERVER_2_NAME);
206         materialServer2.setLastMaintenance(new Date());
207         materialServer2.setNextMaintenance(new Date());
208
209         IMaterialServer materialServer3 = modelFactory.createMaterialServer();
210         materialServer3.setName(MATERIAL_SERVER_3_NAME);
211         materialServer3.setLastMaintenance(new Date());
212         materialServer3.setNextMaintenance(new Date());
213
214         materialServer1.addMaterial(material1);
215         materialServer1.addMaterial(material2);
216         material1.addMaterialServer(materialServer1);
217         material2.addMaterialServer(materialServer1);
218
219         materialServer2.addMaterial(material2);
220         materialServer2.addMaterial(material3);
221         materialServer2.addMaterial(material4);
222         material2.addMaterialServer(materialServer2);
223         material3.addMaterialServer(materialServer2);
224         material4.addMaterialServer(materialServer2);
225
226         materialServer3.addMaterial(material3);
227         materialServer3.addMaterial(material4);
228         materialServer3.addMaterial(material5);
229         material3.addMaterialServer(materialServer3);
230         material4.addMaterialServer(materialServer3);
231         material5.addMaterialServer(materialServer3);
232
233         // Metadata
234         IMetadata metadata1 = modelFactory.createMetadata();
235         metadata1.putData(METADATA_1_KEY_1, METADATA_1_VALUE_1);
236         metadata1.putData(METADATA_1_KEY_2, METADATA_1_VALUE_2);
237
238         IMetadata metadata2 = modelFactory.createMetadata();
239         metadata1.putData(METADATA_2_KEY_1, METADATA_2_VALUE_1);
240
241         IMetadata metadata3 = modelFactory.createMetadata();
242         IMetadata metadata4 = modelFactory.createMetadata();
243
244         // Course
245         ICourse course1 = modelFactory.createCourse();
246         course1.setName(COURSE_1_NAME);
247         course1.setStatus(CourseStatus.CREATED);
248         course1.setPremium(true);
249         course1.setCost(new BigDecimal(1337));
250         course1.setStart(createDate(2018, 1, 1, 8, 0));
251         course1.setEnd(createDate(2019, 1, 31, 17, 0));
252         course1.setCapacity(10);
253
254         ICourse course2 = modelFactory.createCourse();
255         course2.setName(COURSE_2_NAME);
256         course2.setStatus(CourseStatus.ONGOING);
257         course2.setPremium(false);
258         course2.setCost(new BigDecimal(0));
259         course2.setStart(createDate(2018, 2, 1, 8, 0));
260         course2.setEnd(createDate(2019, 6, 30, 17, 0));
261         course2.setCapacity(2);
262
263         ICourse course3 = modelFactory.createCourse();
264         course3.setName(COURSE_3_NAME);
265         course3.setStatus(CourseStatus.ONGOING);
266         course3.setPremium(true);
267         course3.setCost(new BigDecimal(0));
268         course3.setStart(createDate(2018, 2, 1, 8, 0));
269         course3.setEnd(createDate(2019, 6, 30, 17, 0));
270         course3.setCapacity(0);
271
272         ICourse course4 = modelFactory.createCourse();
273         course4.setName(COURSE_4_NAME);
274         course4.setStatus(CourseStatus.CREATED);
275         course4.setPremium(true);
276         course4.setCost(new BigDecimal(0));
277         course4.setStart(createDate(2018, 2, 1, 8, 0));
278         course4.setEnd(createDate(2019, 6, 30, 17, 0));
279         course4.setCapacity(10);
280
281         course1.setMetadata(metadata1);
282         course2.setMetadata(metadata2);
283         course3.setMetadata(metadata3);
284         course4.setMetadata(metadata4);
285
286         course1.setLecturer(lecturer1);
287         lecturer1.addCourse(course1);
288         course2.setLecturer(lecturer2);
289         lecturer2.addCourse(course2);
290
291         // enrollments
292         IEnrollmentKey ekey1 = modelFactory.createEnrollmentKey();
293         ekey1.setParticipant(participant1);
294         ekey1.setCourse(course1);
295
296         IEnrollment enrollment1 = modelFactory.createEnrollment();
297         enrollment1.setId(ekey1);
298         enrollment1.setEnrolledAt(new Date());
299
300         course1.addEnrollment(enrollment1);
301         participant1.addEnrollment(enrollment1);
302
303         IEnrollmentKey ekey2 = modelFactory.createEnrollmentKey();
304         ekey2.setParticipant(participant2);
305         ekey2.setCourse(course2);
306
307         IEnrollment enrollment2 = modelFactory.createEnrollment();
308         enrollment2.setId(ekey2);
309         enrollment2.setEnrolledAt(new Date());
310
311         course2.addEnrollment(enrollment2);
312         participant2.addEnrollment(enrollment2);
313
314
315         // Course Platform
316         ICoursePlatform coursePlatform1 = modelFactory.createCoursePlatform();
317         coursePlatform1.setName(COURSE_PLATFORM_1_NAME);
318         coursePlatform1.setUrl("https://www.openmoocs.com");
319
320         ICoursePlatform coursePlatform2 = modelFactory.createCoursePlatform();
321         coursePlatform2.setName(COURSE_PLATFORM_2_NAME);
322         coursePlatform2.setUrl("https://www.youdecity.com");
323
324         coursePlatform1.addMaterialServer(materialServer1);
325         coursePlatform1.addMaterialServer(materialServer2);
326         coursePlatform2.addMaterialServer(materialServer3);
327
328         course1.setCoursePlatform(coursePlatform1);
329         course2.setCoursePlatform(coursePlatform2);
330         course3.setCoursePlatform(coursePlatform2);
331         course4.setCoursePlatform(coursePlatform2);
332
333         // Memberships
334         IMembership membership1 = modelFactory.createMembership();
335         membership1.setDiscount(200.0);
336         membership1.setPremium(true);
337         membership1.setParticipant(participant1);
338         membership1.setCoursePlatform(coursePlatform1);
339         participant1.addMembership(membership1);
340
341         IMembership membership2 = modelFactory.createMembership();
342         membership2.setDiscount(0.0);
343         membership2.setPremium(false);
344         membership2.setParticipant(participant1);
345         membership2.setCoursePlatform(coursePlatform2);
346         participant1.addMembership(membership2);
347
348         IMembership membership3 = modelFactory.createMembership();
349         membership3.setDiscount(10.0);
350         membership3.setPremium(false);
351         membership3.setParticipant(participant2);
352         membership3.setCoursePlatform(coursePlatform1);
353         participant2.addMembership(membership3);
354
355         // Lesson
356         ILesson lesson1 = modelFactory.createLesson();
357         lesson1.setDescription(LESSON_1_DESC);
358         lesson1.setStart(createDate(2018, 1, 6, 11, 1)); // date in the past!
359         lesson1.setEnd(createDate(2018, 1, 6, 12, 1));
360
361         ILesson lesson2 = modelFactory.createLesson();
362         lesson2.setDescription(LESSON_2_DESC);
363         lesson2.setStart(createDate(2018, 10, 3, 11, 2));
364         lesson2.setEnd(createDate(2018, 10, 3, 12, 2));
365
366         ILesson lesson3 = modelFactory.createLesson();
367         lesson3.setDescription(LESSON_3_DESC);
368         lesson3.setStart(createDate(2018, 10, 4, 11, 3));
369         lesson3.setEnd(createDate(2018, 10, 4, 12, 3));
370
371         ILesson lesson4 = modelFactory.createLesson();
372         lesson4.setDescription(LESSON_4_DESC);
373         lesson4.setStart(createDate(2018, 10, 5, 11, 4));
374         lesson4.setEnd(createDate(2018, 10, 5, 12, 4));
375
376         ILesson lesson5 = modelFactory.createLesson();
377         lesson5.setDescription(LESSON_5_DESC);
378         lesson5.setStart(createDate(2018, 12, 1, 11, 5));
379         lesson5.setEnd(createDate(2018, 12, 0, 12, 5));
380
381         lesson1.addLecturer(lecturer1);
382         lecturer1.addLesson(lesson1);
383         lesson1.addLecturer(lecturer2);
384         lecturer2.addLesson(lesson1);
385         lesson2.addLecturer(lecturer2);
386         lecturer2.addLesson(lesson2);
387         lesson3.addLecturer(lecturer3);
388         lecturer3.addLesson(lesson3);
389         lesson4.addLecturer(lecturer1);
390         lecturer1.addLesson(lesson4);
391         lesson5.addLecturer(lecturer2);
392         lecturer2.addLesson(lesson5);
393
394         lesson1.addRequiredByLesson(lesson2);
395         lesson1.addRequiredByLesson(lesson3);
396         lesson2.addRequiredLesson(lesson1);
397         lesson3.addRequiredLesson(lesson1);
398         lesson2.addRequiredByLesson(lesson4);
399         lesson3.addRequiredByLesson(lesson4);
400         lesson4.addRequiredLesson(lesson2);
401         lesson4.addRequiredLesson(lesson3);
402
403         lesson1.setCourse(course1);
404         lesson2.setCourse(course1);
405         lesson3.setCourse(course1);
406         lesson4.setCourse(course1);
407         course1.addLesson(lesson1);
408         course1.addLesson(lesson2);
409         course1.addLesson(lesson3);
410         course1.addLesson(lesson4);
411         lesson5.setCourse(course2);
412         course2.addLesson(lesson5);
413
414         lesson1.addMaterial(material1);
415         material1.setLesson(lesson1);
416         lesson2.addMaterial(material2);
417         material2.setLesson(lesson2);
418         lesson3.addMaterial(material3);
419         material3.setLesson(lesson3);
420         lesson3.addMaterial(material4);
421         material4.setLesson(lesson3);
422         lesson3.addMaterial(material5);
423         material5.setLesson(lesson3);
424
425         // Persist all the things!
426         em.persist(lecturer1);
427         em.persist(lecturer2);
428         em.persist(lecturer3);
429         em.persist(participant1);
430         em.persist(participant2);
431         em.persist(lesson1);
432         em.persist(lesson2);
433         em.persist(lesson3);
434         em.persist(lesson4);
435         em.persist(lesson5);
436         em.persist(coursePlatform1);
437         em.persist(coursePlatform2);
438         em.persist(metadata1);
439         em.persist(metadata2);
440         em.persist(metadata3);
441         em.persist(metadata4);
442         em.persist(material1);
443         em.persist(material2);
444         em.persist(material3);
445         em.persist(material4);
446         em.persist(material5);
447         em.persist(materialServer1);
448         em.persist(materialServer2);
449         em.persist(materialServer3);
450         em.persist(course1);
451         em.persist(course2);
452         em.persist(course3);
453         em.persist(course4);
454         em.persist(enrollment1);
455         em.persist(enrollment2);
456         em.persist(membership1);
457         em.persist(membership2);
458         em.persist(membership3);
459
460         lecturer1Id = lecturer1.getId();
461         lecturer2Id = lecturer2.getId();
462         lecturer3Id = lecturer3.getId();
463         participant1Id = participant1.getId();
464         participant2Id = participant2.getId();
465         lesson1Id = lesson1.getId();
466         lesson2Id = lesson2.getId();
467         lesson3Id = lesson3.getId();
468         lesson4Id = lesson4.getId();
469         lesson5Id = lesson5.getId();
470         coursePlatform1Id = coursePlatform1.getId();
471         coursePlatform2Id = coursePlatform2.getId();
472         metadata1Id = metadata1.getId();
473         metadata2Id = metadata2.getId();
474         material1Id = material1.getId();
475         material2Id = material2.getId();
476         material3Id = material3.getId();
477         material4Id = material4.getId();
478         material5Id = material5.getId();
479         materialServer1Id = materialServer1.getId();
480         materialServer2Id = materialServer2.getId();
481         materialServer3Id = materialServer3.getId();
482         course1Id = course1.getId();
483         course2Id = course2.getId();
484         course3Id = course3.getId();
485         course4Id = course3.getId();
486
487         em.flush();
488     }
489
490     private MessageDigest getMessageDigest() {
491         try {
492             return MessageDigest.getInstance("SHA1");
493         } catch (NoSuchAlgorithmException e) {
494             throw new IllegalStateException(e);
495         }
496     }
497
498 }