-
Notifications
You must be signed in to change notification settings - Fork 0
/
Test.java
executable file
·391 lines (343 loc) · 12.2 KB
/
Test.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
import java.sql.*;
import java.util.Properties;
import java.util.ArrayList;
/**
* Class to insert values into tables created from Project.java and tests for exception handling.
*/
public class Test {
public static void main(String[] args) throws SQLException {
String protocol = "jdbc:derby:";
String dbName = "iRate";
String connStr = protocol + dbName + ";create=true";
Properties props = new Properties(); // connection properties
props.put("user", "user1");
props.put("password", "user1");
try (
// connect to the database using URL
Connection conn = DriverManager.getConnection(connStr, props);
// statement is channel for sending commands thru connection
Statement stmt = conn.createStatement();
) {
// List of customer First names to add in customer table.
String customerFirstNames[] = {
"'Siddhant'",
"'Shree Rajatha'",
"'Shraddha'",
"'Ashutosh'",
"'Kiran'",
"'Monica'",
"'Pooja'",
"'Arcey'",
"'Max'",
"'Sushrut'"
};
// List of customer Last names to add in customer table.
String customerLastNames[] = {
"'V'",
"'V'",
"'S'",
"'M'",
"'B'",
"'K'",
"'S'",
"'S'",
"'Z'",
"'B'"
};
// List of Movie names to add in Movie table.
String movieNames[] = {
"'Sivaji'",
"'La La Land'",
"'Harry Potter'",
"'Mission Impossible'",
"'Dabbang'"
};
/**
* Insert Data successfully into Customer table.
*/
System.out.println("TEST to Insert Data successfully into Customer table.");
for (int i = 0; i < 10; i++) {
try {
stmt.executeUpdate("insert into CUSTOMER values "
+ "("
+ "GENERATEUUID(),"
+ customerFirstNames[i] + ","
+ customerLastNames[i] + ","
+ "'[email protected]',"
+ "'1962-09-23 03:23:34.23'"
+ ")");
System.out.println("Inserted Custumer " + customerFirstNames[i]);
} catch (SQLException ex) {
System.out.println("Did not insert Customer " + customerFirstNames[i]);
}
}
/**
* Insert Data successfully into Movie table.
*/
System.out.println("TEST to Insert Data successfully into Movie table.");
for (int i = 0; i < 5; i++) {
try {
stmt.executeUpdate("insert into MOVIE values "
+ "("
+ "GENERATEUUID(),"
+ movieNames[i]
+ ")");
System.out.println("Inserted Movie " + movieNames[i]);
} catch (SQLException ex) {
System.out.println("Did not insert Movie " + movieNames[i]);
}
}
String customerUUID;
String movieUUID;
Statement stmt2 = conn.createStatement();
Statement stmt3 = conn.createStatement();
Statement stmt4 = conn.createStatement();
ResultSet rs = stmt.executeQuery(
"select CUSTOMERID from CUSTOMER");
ResultSet moveRS = stmt2.executeQuery(
"select MOVIEID from MOVIE");
/**
* Insert Data successfully into Attendance table.
*/
System.out.println("TEST to Insert Data successfully into Attendance table.");
for (int i = 0; i < 5; i++) {
rs.next();
moveRS.next();
customerUUID = rs.getString(1);
movieUUID = moveRS.getString(1);
//Insert valid data into Attendance
try {
stmt3.executeUpdate("insert into ATTENDANCE values "
+ "("
+ "'" + movieUUID + "'" + ","
+ "'1962-09-23 03:23:34.23',"
+ "'" + customerUUID + "'"
+ ")");
System.out.println("Inserted Attendance for " + customerUUID);
} catch (SQLException ex) {
System.out.println("Did not insert Attendance for " + customerUUID);
}
}
rs = stmt.executeQuery(
"select CUSTOMERID from CUSTOMER");
moveRS = stmt2.executeQuery(
"select MOVIEID from MOVIE");
/**
* Insert Data successfully into Review table.
*/
System.out.println("TEST to Insert Data successfully into Review table.");
for (int i = 0; i < 5; i++) {
rs.next();
moveRS.next();
customerUUID = rs.getString(1);
movieUUID = moveRS.getString(1);
try {
stmt3.executeUpdate("insert into REVIEW values "
+ "("
+ "'" + movieUUID + "'" + ","
+ "'1962-09-23 03:23:34.23',"
+ "'" + customerUUID + "',"
+ "5,"
+ "'Good Movie',"
+ "GENERATEUUID()"
+ ")");
System.out.println("Inserted review for " + customerUUID);
} catch (SQLException ex) {
System.out.println("Did not insert review for " + customerUUID);
}
}
//Insert valid data into Endorsement
rs = stmt.executeQuery(
"select CUSTOMERID from CUSTOMER");
for (int i = 0; i < 5; i++) {
rs.next();
}
ResultSet reviewRS = stmt2.executeQuery(
"select REVIEWID from REVIEW");
String reviewUUID;
/**
* Insert Data successfully into Endorsement table.
*/
System.out.println("TEST to Insert Data successfully into Endorsement table.");
for (int i = 0; i < 5; i++) {
try {
rs.next();
reviewRS.next();
} catch (Exception e) {
System.out.println("NEXT ERROR");
}
customerUUID = rs.getString(1);
reviewUUID = reviewRS.getString(1);
try {
stmt3.executeUpdate("insert into ENDORSEMENT values "
+ "("
+ "'" + customerUUID + "'" + ","
+ "'" + reviewUUID + "'" + ","
+ "'1962-09-23 03:23:34.23'"
+ ")");
System.out.println("Inserted endorsement for " + reviewUUID);
//stmt3.close();
} catch (SQLException ex) {
System.out.println("Did not endorsement for " + reviewUUID);
}
}
/**
* Test to check if insertion with invalid email ID fails.
*/
System.out.println("TEST to check if insertion with invalid email ID fails.");
try {
stmt.executeUpdate("insert into CUSTOMER values "
+ "("
+ "GENERATEUUID(),"
+ "abc" + ","
+ "efg" + ","
+ "'abc@',"
+ "'1962-09-23 03:23:34.23'"
+ ")");
System.out.println("Inserted Customer " + "abc");
} catch (SQLException ex) {
System.out.println("Did not insert Customer " + "abc");
}
/**
* Test to check for review without attendance.
*/
System.out.println("TEST to check for review without attendance.");
movieUUID = "'a7d3362f-bb56-4e80-b105-1a1c2d47f40d'";
customerUUID = "'a7d3362f-bb56-4e80-b105-1a1c2d47f401'";
try {
stmt3.executeUpdate("insert into REVIEW values "
+ "("
+ "'" + movieUUID + "'" + ","
+ "'1962-09-23 03:23:34.23',"
+ "'" + customerUUID + "',"
+ "5,"
+ "'Good Movie',"
+ "GENERATEUUID()"
+ ")");
System.out.println("Inserted review for " + customerUUID);
} catch (SQLException ex) {
System.out.println("Did not insert review for " + customerUUID);
}
rs = stmt.executeQuery(
"select CUSTOMERID from CUSTOMER");
moveRS = stmt2.executeQuery(
"select MOVIEID from MOVIE");
ResultSetMetaData rsmd = rs.getMetaData();
int columnCount = rsmd.getColumnCount();
;
ArrayList<String> customerIDList = new ArrayList<>(columnCount);
ResultSetMetaData rsmd1 = moveRS.getMetaData();
int columnCount1 = rsmd1.getColumnCount();
ArrayList<String> movieList = new ArrayList<>(columnCount);
while (rs.next()) {
int i = 1;
while (i <= columnCount) {
customerIDList.add(rs.getString(i++));
}
}
while (moveRS.next()) {
int i = 1;
while (i <= columnCount1) {
movieList.add(moveRS.getString(i++));
}
}
rs = stmt4.executeQuery("select REVIEWID from REVIEW");
ResultSetMetaData resultSetMetaData = rs.getMetaData();
int reviewColumnSize = resultSetMetaData.getColumnCount();
ArrayList<String> reviewList = new ArrayList<>(reviewColumnSize);
while (rs.next()) {
reviewList.add(rs.getString(1));
}
customerUUID = customerIDList.get(0);
movieUUID = movieList.get(0);
/**
* Test to check if insert fails when same customer writes review for the same movie again.
*/
System.out.println(
"TEST to check if insert fails when same customer writes review for the same movie again.");
try {
stmt3.executeUpdate("insert into REVIEW values "
+ "("
+ "'" + movieUUID + "'" + ","
+ "'1962-29-23 03:23:34.23',"
+ "'" + customerUUID + "',"
+ "5,"
+ "'Good Movie',"
+ "GENERATEUUID()"
+ ")");
System.out.println("Inserted review for " + customerUUID);
} catch (SQLException ex) {
System.out.println("Did not insert review for " + customerUUID);
}
/**
* Test to check if user is able to attend same movie multiple times.
*/
System.out.println("TEST to check if user is able to attend same movie multiple times.");
try {
stmt3.executeUpdate("insert into ATTENDANCE values "
+ "("
+ "'" + movieUUID + "'" + ","
+ "'1962-09-23 03:23:34.23',"
+ "'" + customerUUID + "'"
+ ")");
System.out.println("Inserted Attendance for " + customerUUID);
} catch (SQLException ex) {
System.out.println("Did not insert Attendance for " + customerUUID);
}
/**
* Test to check if insertion fails if you endorse a 2nd review before 24 hours of endorsing a first review of the same.
*/
System.out.println(
"TEST to check if insertion fails if you endorse a 2nd review before 24 hours of endorsing a first review of the same.");
String reviewer = customerIDList.get(1);
String customerID = customerIDList.get(5);
String movieID = movieList.get(0);
String reviewID = reviewList.get(0);
//Making the custumer attend the movie.
try {
stmt3.executeUpdate("insert into ATTENDANCE values "
+ "("
+ "'" + movieID + "'" + ","
+ "'1962-09-23 03:23:34.23',"
+ "'" + reviewer + "'"
+ ")");
System.out.println("Inserted Attendance for " + customerUUID);
} catch (SQLException ex) {
System.out.println("Did not insert Attendance for " + customerUUID);
}
//Making the custumer write review for the movie he attended.
try {
stmt3.executeUpdate("insert into REVIEW values "
+ "("
+ "'" + movieID + "'" + ","
+ "'1962-09-23 03:23:34.23',"
+ "'" + reviewer + "',"
+ "5,"
+ "'Good Movie',"
+ "GENERATEUUID()"
+ ")");
System.out.println("Inserted review for " + customerUUID);
} catch (SQLException ex) {
System.out.println("Did not insert review for " + customerUUID);
}
rs = stmt.executeQuery(
"select REVIEWID from REVIEW WHERE (CUSTOMERID =" + "'" + reviewer + "'" + "AND "
+ "MOVIEID = '" + movieID + "'" + ")");
rs.next();
reviewID = rs.getString(1);
//Making the endorser endorse the review of same move for before 24 hours are over.
try {
stmt3.executeUpdate("insert into ENDORSEMENT values "
+ "("
+ "'" + customerID + "'" + ","
+ "'" + reviewID + "'" + ","
+ "'1962-09-23 03:23:34.23'"
+ ")");
System.out.println("Inserted endorsement for " + reviewID);
//stmt3.close();
} catch (SQLException ex) {
System.out.println("Did not endorsement for " + reviewID);
}
}
}
}