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