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