date-range added
[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 erwartet wird reicht es
32 //eine allfaellige mit 'throws Exception' an junit weiterzuleiten.
33 //--------------------------------------------------------------------------------------------
34 /*createUserException.expect(IllegalArgumentException.class);
35 createUserException.expectMessage("Invalid username");
36 User wrongArgumentUser1 = new User(null, "1234");
37 createUserException.expectMessage("Invalid password");
38 User wrongArgumentUser2 = new User("TestUser2", null);*/
39 }
40
41 @Test
42 public void createExistingTest() throws Exception{
43 User testUser = new User("TestUser", "pass", "grosm9@bfh.ch", "Test", "Dummy");
44 assertEquals("TestUser", testUser.getUserName());
45 assertEquals("pass", testUser.getEncryptedPassword());
46 assertEquals("grosm9@bfh.ch", testUser.getMail());
47 assertEquals("Test", testUser.getName());
48 assertEquals("Dummy", testUser.getLastname());
49 }
50
51 @Test
52 public void getEncryptedPasswordTest() throws Exception{
53 String encryptedPass = Password.getSaltedHash("testPassword");
54 User testUser = new User("TestUser", encryptedPass, "grosm9@bfh.ch", "Test", "Dummy");
55 assertEquals(encryptedPass, testUser.getEncryptedPassword());
56 }
57
58 @Test(expected=IllegalArgumentException.class)
59 public void createUserInvalidUsernameTest() throws Exception{
60 User testUser = new User(null,"pass");
61 }
62
63 @Test(expected=IllegalArgumentException.class)
64 public void createUserTooShortUsernameTest() throws Exception{
65 User testUser = new User(new String(),"pass");
66 User testUser2 = new User(new String("tes"),"pass");
67 }
68
69 @Test(expected=IllegalArgumentException.class)
70 public void createUserInvalidPasswordTest() throws Exception{
71 User testUser = new User("TestUser", null);
72 }
73
74 @Test
75 public void checkPasswordTest() throws Exception{
76 User testUser = new User("TestUser", "pass");
77 assertTrue(testUser.checkPassword("pass"));
78 assertFalse(testUser.checkPassword("notmypass"));
79 }
80
81 @Test(expected=IllegalArgumentException.class)
82 public void checkPasswordInvalidTest() throws Exception{
83 User testUser = new User("TestUser", "pass");
84 assertTrue(testUser.checkPassword(null));
85 }
86
87 @Test
88 public void changePasswordTest() throws Exception{
89 User testUser = new User("TestUser", "pass");
90 assertTrue(testUser.checkPassword("pass"));
91 testUser.changePassword("newpass");
92 assertTrue(testUser.checkPassword("newpass"));
93 assertFalse(testUser.checkPassword("pass"));
94 assertFalse(testUser.checkPassword("notmypass"));
95 }
96
97 @Test(expected=IllegalArgumentException.class)
98 public void changePasswordInvalid1Test() throws Exception{
99 User testUser = new User("TestUser", "pass");
100 testUser.changePassword(null);
101 }
102
103 @Test(expected=IllegalArgumentException.class)
104 public void changePasswordInvalid2Test() throws Exception{
105 User testUser = new User("TestUser", "pass");
106 testUser.changePassword("pass");
107 }
108
109 @Test
110 public void setMailTest() throws Exception{
111 User testUser = new User("TestUser", "pass");
112 testUser.setMail("grosm9@bfh.ch");
113 }
114
115 @Test(expected=IllegalArgumentException.class)
116 public void setMailIllegal1Test() throws Exception{
117 User testUser = new User("TestUser", "pass");
118 testUser.setMail("fda.ds");
119
120 }
121
122 @Test(expected=IllegalArgumentException.class)
123 public void setMailIllegal2Test() throws Exception{
124 User testUser = new User("TestUser", "pass");
125 testUser.setMail("adfiug@fs");
126 }
127
128 @Test(expected=IllegalArgumentException.class)
129 public void setMailIllegal3Test() throws Exception{
130 User testUser = new User("TestUser", "pass");
131 testUser.setMail(null);
132 }
133
134 @Test
135 public void getMailTest() throws Exception{
136 User testUser = new User("TestUser", "pass");
137 String mail = "grosm9@bfh.ch";
138
139 testUser.setMail(mail);
140 assertEquals(mail, testUser.getMail());
141 }
142
143 @Test
144 public void setAndGetNameTest() throws Exception{
145 User testUser = new User("TestUser", "pass");
146 testUser.setName("Tester");
147 assertEquals("Tester",testUser.getName());
148 testUser.setName(null);
149 assertEquals(null, testUser.getName());
150 }
151
152 @Test
153 public void setAndGetLastnameTest() throws Exception{
154 User testUser = new User("TestUser", "pass");
155 testUser.setLastname("Dummy");
156 assertEquals("Dummy", testUser.getLastname());
157 testUser.setLastname(null);
158 assertEquals(null, testUser.getLastname());
159 }
160 }