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