DataHandlerForTestsTest added,
[staff/due1/sed-hs15-srs-purple.git] / src / test / java / ch / bfh / ti / srs / core / UserTest.java
CommitLineData
ed6d035d
M
1/*\r
2 * Copyright (c) 2015 Berner Fachhochschule, Switzerland.\r
3 *\r
4 * Project Smart Reservation System.\r
5 *\r
6 * Distributable under GPL license. See terms of license at gnu.org.\r
7 */\r
8package ch.bfh.ti.srs.core;\r
9\r
10import static org.junit.Assert.assertEquals;\r
11import static org.junit.Assert.assertFalse;\r
12import static org.junit.Assert.assertTrue;\r
13\r
14import org.junit.Rule;\r
15import org.junit.Test;\r
16import org.junit.rules.ExpectedException;\r
17\r
1f7b5004
M
18import ch.bfh.ti.srs.extern.Password;\r
19\r
1ec0e01f
M
20/**\r
21 * @author Michael Grossen\r
22 */\r
ed6d035d
M
23public class UserTest {\r
24 //@Rule\r
25 //public ExpectedException createUserException = ExpectedException.none();\r
26 \r
27 @Test\r
28 public void createUserTest() throws Exception{\r
29 User testUser = new User("TestUser", "pass");\r
30 assertEquals("TestUser", testUser.getUserName());\r
31 \r
32 //Zweite Moeglichkeit um Exceptions zu testen, so lange keine Exception erwartet wird reicht es\r
33 //eine allfaellige mit 'throws Exception' an junit weiterzuleiten.\r
34 //--------------------------------------------------------------------------------------------\r
35 /*createUserException.expect(IllegalArgumentException.class);\r
36 createUserException.expectMessage("Invalid username");\r
37 User wrongArgumentUser1 = new User(null, "1234");\r
38 createUserException.expectMessage("Invalid password");\r
39 User wrongArgumentUser2 = new User("TestUser2", null);*/\r
40 }\r
41 \r
1f7b5004
M
42 @Test\r
43 public void createExistingTest() throws Exception{\r
44 User testUser = new User("TestUser", "pass", "grosm9@bfh.ch", "Test", "Dummy");\r
45 assertEquals("TestUser", testUser.getUserName());\r
46 assertEquals("pass", testUser.getEncryptedPassword());\r
47 assertEquals("grosm9@bfh.ch", testUser.getMail());\r
48 assertEquals("Test", testUser.getName());\r
49 assertEquals("Dummy", testUser.getLastname());\r
50 }\r
51 \r
52 @Test\r
53 public void getEncryptedPasswordTest() throws Exception{\r
54 String encryptedPass = Password.getSaltedHash("testPassword");\r
55 User testUser = new User("TestUser", encryptedPass, "grosm9@bfh.ch", "Test", "Dummy");\r
56 assertEquals(encryptedPass, testUser.getEncryptedPassword());\r
57 }\r
58 \r
ed6d035d
M
59 @Test(expected=IllegalArgumentException.class)\r
60 public void createUserInvalidUsernameTest() throws Exception{\r
61 User testUser = new User(null,"pass");\r
62 }\r
63 \r
64 @Test(expected=IllegalArgumentException.class)\r
65 public void createUserTooShortUsernameTest() throws Exception{\r
66 User testUser = new User(new String(),"pass");\r
67 User testUser2 = new User(new String("tes"),"pass");\r
68 }\r
69 \r
70 @Test(expected=IllegalArgumentException.class)\r
71 public void createUserInvalidPasswordTest() throws Exception{\r
72 User testUser = new User("TestUser", null);\r
73 }\r
74\r
75 @Test\r
76 public void checkPasswordTest() throws Exception{\r
77 User testUser = new User("TestUser", "pass");\r
78 assertTrue(testUser.checkPassword("pass"));\r
79 assertFalse(testUser.checkPassword("notmypass"));\r
80 }\r
81 \r
82 @Test(expected=IllegalArgumentException.class)\r
83 public void checkPasswordInvalidTest() throws Exception{\r
84 User testUser = new User("TestUser", "pass");\r
85 assertTrue(testUser.checkPassword(null));\r
86 }\r
87 \r
88 @Test\r
89 public void changePasswordTest() throws Exception{\r
90 User testUser = new User("TestUser", "pass");\r
91 assertTrue(testUser.checkPassword("pass"));\r
92 testUser.changePassword("newpass");\r
93 assertTrue(testUser.checkPassword("newpass"));\r
94 assertFalse(testUser.checkPassword("pass"));\r
95 assertFalse(testUser.checkPassword("notmypass"));\r
96 }\r
97 \r
98 @Test(expected=IllegalArgumentException.class)\r
99 public void changePasswordInvalid1Test() throws Exception{\r
100 User testUser = new User("TestUser", "pass");\r
101 testUser.changePassword(null);\r
102 }\r
103 \r
104 @Test(expected=IllegalArgumentException.class)\r
105 public void changePasswordInvalid2Test() throws Exception{\r
106 User testUser = new User("TestUser", "pass");\r
107 testUser.changePassword("pass");\r
108 }\r
109\r
110 @Test\r
111 public void setMailTest() throws Exception{\r
112 User testUser = new User("TestUser", "pass");\r
113 testUser.setMail("grosm9@bfh.ch");\r
114 }\r
115 \r
116 @Test(expected=IllegalArgumentException.class)\r
117 public void setMailIllegal1Test() throws Exception{\r
118 User testUser = new User("TestUser", "pass");\r
119 testUser.setMail("fda.ds");\r
120\r
121 }\r
122 \r
123 @Test(expected=IllegalArgumentException.class)\r
124 public void setMailIllegal2Test() throws Exception{\r
125 User testUser = new User("TestUser", "pass");\r
126 testUser.setMail("adfiug@fs");\r
127 }\r
128 \r
129 @Test(expected=IllegalArgumentException.class)\r
130 public void setMailIllegal3Test() throws Exception{\r
131 User testUser = new User("TestUser", "pass");\r
132 testUser.setMail(null);\r
133 }\r
134\r
135 @Test\r
136 public void getMailTest() throws Exception{\r
137 User testUser = new User("TestUser", "pass");\r
138 String mail = "grosm9@bfh.ch";\r
139\r
140 testUser.setMail(mail);\r
141 assertEquals(mail, testUser.getMail());\r
142 }\r
143\r
144 @Test\r
145 public void setAndGetNameTest() throws Exception{\r
146 User testUser = new User("TestUser", "pass");\r
147 testUser.setName("Tester");\r
148 assertEquals("Tester",testUser.getName());\r
149 testUser.setName(null);\r
150 assertEquals(null, testUser.getName());\r
151 }\r
152\r
153 @Test\r
154 public void setAndGetLastnameTest() throws Exception{\r
155 User testUser = new User("TestUser", "pass");\r
156 testUser.setLastname("Dummy");\r
157 assertEquals("Dummy", testUser.getLastname());\r
158 testUser.setLastname(null);\r
159 assertEquals(null, testUser.getLastname());\r
160 }\r
161}\r