· 4 years ago · Aug 16, 2021, 09:56 AM
1package com.epam.rd.java.basic.practice8;
2
3import com.epam.rd.java.basic.practice8.db.DBManager;
4import com.epam.rd.java.basic.practice8.db.entity.User;
5import org.junit.*;
6
7import java.io.FileOutputStream;
8import java.io.IOException;
9import java.io.OutputStream;
10import java.sql.*;
11import java.util.Properties;
12import java.util.logging.Level;
13import java.util.logging.Logger;
14
15import static org.junit.Assert.*;
16
17public class Part1StudentTest {
18 private static final String DB_URL = "jdbc:h2:mem:part8";
19 private static final String URL_CONNECTION = "jdbc:h2:~/test;user=youruser;password=yourpassword;";
20 private static final String USER = "youruser";
21 private static final String PASS = "yourpassword";
22 private static final Logger logger = Logger.getLogger(Package.class.getName());
23
24 private static DBManager dbManager;
25 private User user;
26
27 @Before
28 public void init() throws SQLException {
29 try (OutputStream output = new FileOutputStream("app.properties")) {
30 Properties prop = new Properties();
31 prop.setProperty("connection.url", URL_CONNECTION);
32 prop.store(output, null);
33 } catch (IOException e) {
34 logger.log(Level.INFO, e.getLocalizedMessage());
35 }
36
37 dbManager = DBManager.getInstance();
38
39 try (Connection con = DriverManager.getConnection(DB_URL, USER, PASS);
40 Statement statement = con.createStatement()) {
41 String sql = "CREATE SCHEMA IF NOT EXISTS part8;" +
42 "drop table if exists users_teams;\n" +
43 "drop table if exists users;\n" +
44 "drop table if exists teams;" +
45 " CREATE TABLE IF NOT EXISTS users (\n" +
46 " id INTEGER(11) NOT NULL AUTO_INCREMENT,\n" +
47 " login VARCHAR(20) NOT NULL,\n" +
48 " PRIMARY KEY (id));" +
49 " CREATE TABLE IF NOT EXISTS teams (\n" +
50 " id INTEGER(11) NOT NULL AUTO_INCREMENT,\n" +
51 " name VARCHAR(20) NOT NULL,\n" +
52 " PRIMARY KEY (id));" +
53 " CREATE TABLE IF NOT EXISTS users_teams (\n" +
54 " user_id int not null,\n" +
55 " team_id int not null,\n" +
56 " FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE,\n" +
57 " FOREIGN KEY (team_id) REFERENCES teams (id) ON DELETE CASCADE,\n" +
58 " PRIMARY KEY (user_id, team_id))";
59
60 statement.execute(sql);
61 }
62 }
63
64 @Before
65 public void setUp() {
66 user = new User();
67 user.setLogin("TestUser");
68 }
69
70 @Test
71 public void shouldAddUserToDatabase() {
72 try {
73 dbManager.insertUser(user);
74
75 assertEquals(3, dbManager.findAllUsers().size());
76 } catch (Exception e) {
77 fail();
78 logger.log(Level.INFO, e.getLocalizedMessage());
79 }
80
81 }
82
83 @Test
84 public void shouldDeleteUserFormDatabase() {
85 try {
86 assertTrue(dbManager.deleteUser(user));
87 } catch (Exception e) {
88 fail();
89 logger.log(Level.INFO, e.getLocalizedMessage());
90 }
91 }
92
93 @Test
94 public void shouldReturnListOfUsers() {
95 try {
96 dbManager.insertUser(user);
97
98 assertEquals(3, dbManager.findAllUsers().size());
99 } catch (Exception e) {
100 fail();
101 logger.log(Level.INFO, e.getLocalizedMessage());
102 }
103 }
104
105 @After
106 public void delete() {
107 dbManager.deleteUser(user);
108 }
109}