]> git.somenet.org - pub/jan/dst18.git/blob - ass1-jpa/src/test/java/dst/ass1/jpa/tests/Ass1_2_3b_02Test.java
Add template for assignment 1
[pub/jan/dst18.git] / ass1-jpa / src / test / java / dst / ass1 / jpa / tests / Ass1_2_3b_02Test.java
1 package dst.ass1.jpa.tests;
2
3 import static org.hamcrest.core.IsCollectionContaining.hasItems;
4 import static org.junit.Assert.assertNotNull;
5 import static org.junit.Assert.assertThat;
6 import static org.junit.Assert.assertTrue;
7
8 import java.util.Collection;
9 import java.util.Date;
10 import java.util.GregorianCalendar;
11
12 import javax.persistence.EntityTransaction;
13
14 import org.junit.Before;
15 import org.junit.Test;
16
17 import dst.ass1.jpa.dao.ICourseDAO;
18 import dst.ass1.jpa.model.CourseStatus;
19 import dst.ass1.jpa.model.ICourse;
20
21 public class Ass1_2_3b_02Test extends Ass1_TestBase {
22
23     private ICourseDAO dao;
24
25     @Before
26     public void setUp() throws Exception {
27         dao = daoFactory.createCourseDAO();
28
29         EntityTransaction tx = em.getTransaction();
30         tx.begin();
31         ICourse course1 = dao.findById(testData.course1Id);
32         course1.setStatus(CourseStatus.CANCELLED);
33         em.persist(course1);
34         em.flush();
35         tx.commit();
36     }
37
38     @Test
39     public void testFindNonCancelledCoursesBetweenStartAndFinish() {
40         Collection<ICourse> courses = dao.findNonCancelledCoursesBetweenStartAndEnd(null, null);
41
42         assertThat(map(courses, e -> e.getId()), hasItems(
43                 testData.course2Id
44         ));
45     }
46
47     @Test
48     public void testFindEventsForStatusFinishedBetweenStartAndFinish2() {
49         Date start = createDate(2017, 1, 1, 1, 1);
50         Collection<ICourse> courses = dao.findNonCancelledCoursesBetweenStartAndEnd(start, null);
51
52         assertThat(map(courses, e -> e.getId()), hasItems(
53                 testData.course2Id
54         ));
55     }
56
57     @Test
58     public void testFindEventsForStatusFinishedBetweenStartAndFinish3() {
59         Date end = dao.findById(testData.course2Id).getEnd();
60         Collection<ICourse> courses = dao.findNonCancelledCoursesBetweenStartAndEnd(null, end);
61         assertNotNull(courses);
62         assertTrue(courses.isEmpty());
63     }
64
65     private Date createDate(int year, int month, int day, int hours, int minutes) {
66         return new GregorianCalendar(year, month, day, hours, minutes).getTime();
67     }
68
69 }