]> git.somenet.org - pub/jan/dst18.git/blob - ass1-jpa/src/test/java/dst/ass1/jpa/tests/Ass1_1_1_01Test.java
Add template for assignment 1
[pub/jan/dst18.git] / ass1-jpa / src / test / java / dst / ass1 / jpa / tests / Ass1_1_1_01Test.java
1 package dst.ass1.jpa.tests;
2
3 import static dst.ass1.jpa.CaseInsensitiveStringCollectionMatcher.hasItems;
4 import static dst.ass1.jpa.util.Constants.*;
5 import static org.hamcrest.CoreMatchers.is;
6 import static org.hamcrest.CoreMatchers.not;
7
8 import org.junit.Before;
9 import org.junit.ClassRule;
10 import org.junit.FixMethodOrder;
11 import org.junit.Rule;
12 import org.junit.Test;
13 import org.junit.rules.ErrorCollector;
14 import org.junit.runners.MethodSorters;
15
16 import dst.ass1.jpa.DatabaseGateway;
17 import dst.ass1.jpa.ORMService;
18 import dst.ass1.jpa.util.Constants;
19
20 /**
21  * Tests the basic object-relational mapping by examining the created database tables and constraints.
22  */
23 @FixMethodOrder(MethodSorters.NAME_ASCENDING)
24 public class Ass1_1_1_01Test {
25
26     @ClassRule
27     public static ORMService orm = new ORMService();
28
29     @Rule
30     public ErrorCollector err = new ErrorCollector();
31
32     private DatabaseGateway db;
33
34     @Before
35     public void setUp() throws Exception {
36         db = orm.getDatabaseGateway();
37     }
38
39     @Test
40     public void printTables() throws Exception {
41         // not a test, just some systout output that may help you to gain insight into the created database schema
42         for (String table : db.getTables()) {
43             System.out.printf("%-30s %s%n", table, db.getColumns(table));
44         }
45     }
46
47     @Test
48     public void testBasicTablesJdbc() throws Exception {
49         // checks that all basic tables exist
50         err.checkThat(db.getTables(), hasItems(
51                 T_LECTURER,
52                 T_PARTICIPANT,
53                 T_MEMBERSHIP,
54                 T_LESSON,
55                 T_COURSE,
56                 T_ENROLLMENT,
57                 T_COURSEPLATFORM,
58                 T_MATERIAL,
59                 T_METADATA,
60                 T_MATERIALSERVER
61         ));
62     }
63
64     @Test
65     public void testRelation01Jdbc() throws Exception {
66         // lecturer <-> lesson
67         err.checkThat(db.getTables(), hasItems(J_LECTURER_LESSON));
68         err.checkThat(db.getColumns(J_LECTURER_LESSON), hasItems(
69                 I_LESSON,
70                 I_LECTURER
71         ));
72     }
73
74     @Test
75     public void testRelation02Jdbc() throws Exception {
76         // lecturer <-> course
77         err.checkThat(db.getColumns(T_COURSE), hasItems(I_LECTURER));
78     }
79
80     @Test
81     public void testRelation03Jdbc() throws Exception {
82         // participant <-enrollment-> course
83
84         err.checkThat(db.getColumns(T_ENROLLMENT), hasItems(
85                 I_PARTICIPANT,
86                 I_COURSE
87         ));
88         err.checkThat(db.getTables(), hasItems(
89                 J_COURSE_ENROLLMENT,
90                 J_PARTICIPANT_ENROLLMENT
91         ));
92     }
93
94     @Test
95     public void testRelation04Jdbc() throws Exception {
96         // participant <-> membership -> courseplatform
97
98         err.checkThat(db.getColumns(T_MEMBERSHIP), hasItems(
99                 I_COURSEPLATFORM,
100                 I_PARTICIPANT
101         ));
102         err.checkThat(db.getColumns(T_PARTICIPANT), not(hasItems(I_MEMBERSHIP)));
103         err.checkThat(db.getColumns(T_COURSEPLATFORM), not(hasItems(I_MEMBERSHIP)));
104
105         err.checkThat(db.getTables(), hasItems(J_PARTICIPANT_MEMBERSHIP));
106         err.checkThat(db.getColumns(J_PARTICIPANT_MEMBERSHIP), hasItems(I_PARTICIPANT));
107     }
108
109     @Test
110     public void testRelation05Jdbc() throws Exception {
111         // lesson <-> lesson
112
113         err.checkThat("join table should be explicitly renamed!", db.getTables(), not(hasItems(
114                 T_LESSON + "_" + T_LESSON
115         )));
116         err.checkThat(db.getTables(), hasItems(
117                 J_LESSONS_REQUIRED
118         ));
119         err.checkThat(db.getColumns(J_LESSONS_REQUIRED), hasItems(
120                 I_LESSON_REQUIRED,
121                 I_LESSON_REQUIRED_BY
122         ));
123     }
124
125     @Test
126     public void testRelation06Jdbc() throws Exception {
127         // lesson <-> course
128         err.checkThat(db.getColumns(T_LESSON), hasItems(I_COURSE));
129         err.checkThat(db.getColumns(T_COURSE), not(hasItems(I_LESSON)));
130         err.checkThat(db.isIndex(T_LESSON, I_COURSE, true), is(true));
131     }
132
133     @Test
134     public void testRelation07Jdbc() throws Exception {
135         // lesson <-> material
136
137         err.checkThat(db.getColumns(T_MATERIAL), hasItems(I_LESSON));
138         err.checkThat(db.getColumns(T_LESSON), not(hasItems(I_MATERIAL)));
139         err.checkThat(db.isIndex(T_MATERIAL, I_LESSON, true), is(true));
140     }
141
142     @Test
143     public void testRelation08Jdbc() throws Exception {
144         // course -> courseplatform
145         err.checkThat(db.getColumns(T_COURSE), hasItems(I_COURSEPLATFORM));
146         err.checkThat(db.isIndex(T_COURSE, I_COURSEPLATFORM, true), is(true));
147     }
148
149     @Test
150     public void testRelation09Jdbc() throws Exception {
151         // course -> metadata
152         err.checkThat(db.getTables(), hasItems(T_METADATA));
153         err.checkThat(db.getColumns(T_COURSE), hasItems(I_METADATA));
154         err.checkThat(db.getColumns(T_METADATA), not(hasItems(I_COURSE)));
155         err.checkThat(db.isIndex(Constants.T_COURSE, Constants.I_METADATA, false), is(true));
156
157     }
158
159     @Test
160     public void testRelation10Jdbc() throws Exception {
161         // courseplatform -> materialserver
162         err.checkThat(db.getTables(), hasItems(J_COURSEPLATFORM_MATERIALSERVER));
163         err.checkThat(db.getColumns(T_MATERIALSERVER), not(hasItems(I_COURSEPLATFORM)));
164     }
165
166     @Test
167     public void testRelation11Jdbc() throws Exception {
168         // material <-> materialserver
169
170         err.checkThat(db.getTables(), hasItems(J_MATERIALSERVER_MATERIAL));
171     }
172
173     @Test
174     public void testColumnType01Jdbc() {
175         err.checkThat(db.isColumnInTableWithType(T_PARTICIPANT, M_PERSON_PASSWORD, "VARBINARY", "20"), is(true));
176     }
177
178 }