]> git.somenet.org - pub/jan/dst18.git/blob - ass1-jpa/src/test/java/dst/ass1/jpa/tests/Ass1_1_1_10Test.java
Add template for assignment 1
[pub/jan/dst18.git] / ass1-jpa / src / test / java / dst / ass1 / jpa / tests / Ass1_1_1_10Test.java
1 package dst.ass1.jpa.tests;
2
3 import static org.hamcrest.core.IsCollectionContaining.hasItem;
4 import static org.hamcrest.core.IsCollectionContaining.hasItems;
5 import static org.junit.Assert.assertEquals;
6 import static org.junit.Assert.assertFalse;
7 import static org.junit.Assert.assertNotNull;
8 import static org.junit.Assert.assertThat;
9 import static org.junit.Assert.assertTrue;
10
11 import java.sql.ResultSet;
12 import java.sql.Statement;
13 import java.util.List;
14
15 import javax.persistence.EntityManager;
16
17 import org.hibernate.Session;
18 import org.junit.Test;
19
20 import dst.ass1.jpa.dao.ILessonDAO;
21 import dst.ass1.jpa.dao.impl.DAOFactory;
22 import dst.ass1.jpa.model.ILesson;
23 import dst.ass1.jpa.util.Constants;
24
25 /**
26  * Tests the self reference of ILesson.
27  */
28 public class Ass1_1_1_10Test extends Ass1_TestBase {
29
30     @Test
31     public void testLessonSelfAssociation() {
32         EntityManager em = orm.createEntityManager();
33         ILessonDAO dao = new DAOFactory(em).createLessonDAO();
34
35         ILesson lesson1 = dao.findById(testData.lesson1Id);
36         ILesson lesson2 = dao.findById(testData.lesson2Id);
37         ILesson lesson3 = dao.findById(testData.lesson3Id);
38         ILesson lesson4 = dao.findById(testData.lesson4Id);
39         assertNotNull(lesson1.getRequiredByLessons());
40         assertNotNull(lesson2.getRequiredLessons());
41         assertNotNull(lesson2.getRequiredByLessons());
42         assertNotNull(lesson3.getRequiredLessons());
43         assertNotNull(lesson3.getRequiredByLessons());
44         assertNotNull(lesson4.getRequiredLessons());
45
46         List<Long> lesson1requiredBy = map(lesson1.getRequiredByLessons(), ILesson::getId);
47         List<Long> lesson2required = map(lesson2.getRequiredLessons(), ILesson::getId);
48         List<Long> lesson2requiredBy = map(lesson2.getRequiredByLessons(), ILesson::getId);
49         List<Long> lesson3required = map(lesson3.getRequiredLessons(), ILesson::getId);
50         List<Long> lesson3requiredBy = map(lesson3.getRequiredByLessons(), ILesson::getId);
51         List<Long> lesson4required = map(lesson4.getRequiredLessons(), ILesson::getId);
52
53         assertThat(lesson1requiredBy, hasItems(testData.lesson2Id, testData.lesson3Id));
54         assertThat(lesson2required, hasItem(testData.lesson1Id));
55         assertThat(lesson3required, hasItem(testData.lesson1Id));
56         assertThat(lesson2requiredBy, hasItem(testData.lesson4Id));
57         assertThat(lesson3requiredBy, hasItem(testData.lesson4Id));
58         assertThat(lesson4required, hasItems(testData.lesson2Id, testData.lesson3Id));
59     }
60
61     @Test
62     public void testLessonSelfAssociationJdbc() {
63         String sql = "SELECT " + Constants.I_LESSON_REQUIRED + ", " + Constants.I_LESSON_REQUIRED_BY +
64                 " FROM " + Constants.J_LESSONS_REQUIRED +
65                 " ORDER BY " + Constants.I_LESSON_REQUIRED + ", " + Constants.I_LESSON_REQUIRED_BY;
66
67         em.unwrap(Session.class).doWork(connection -> {
68
69             try (Statement stmt = connection.createStatement()) {
70                 ResultSet rs = stmt.executeQuery(sql);
71                 assertTrue(rs.next());
72
73                 assertEquals((long) testData.lesson1Id, rs.getLong(Constants.I_LESSON_REQUIRED));
74                 assertEquals((long) testData.lesson2Id, rs.getLong(Constants.I_LESSON_REQUIRED_BY));
75
76                 assertTrue(rs.next());
77
78                 assertEquals((long) testData.lesson1Id, rs.getLong(Constants.I_LESSON_REQUIRED));
79                 assertEquals((long) testData.lesson3Id, rs.getLong(Constants.I_LESSON_REQUIRED_BY));
80
81                 assertTrue(rs.next());
82
83                 assertEquals((long) testData.lesson2Id, rs.getLong(Constants.I_LESSON_REQUIRED));
84                 assertEquals((long) testData.lesson4Id, rs.getLong(Constants.I_LESSON_REQUIRED_BY));
85
86                 assertTrue(rs.next());
87
88                 assertEquals((long) testData.lesson3Id, rs.getLong(Constants.I_LESSON_REQUIRED));
89                 assertEquals((long) testData.lesson4Id, rs.getLong(Constants.I_LESSON_REQUIRED_BY));
90
91                 assertFalse(rs.next());
92
93                 rs.close();
94             }
95         });
96     }
97
98 }