1   /***
2    Copyright (C) 2005 The Java Community
3   
4    This program is free software; you can redistribute it and/or modify  it under
5    the terms of the GNU General Public License as published by  the Free Software
6    Foundation; either version 2 of the License, or  (at your option) any later
7    version.
8   
9    This program is distributed in the hope that it will be useful,  but WITHOUT
10   ANY WARRANTY; without even the implied warranty of  MERCHANTABILITY or FITNESS
11   FOR A PARTICULAR PURPOSE. See the  GNU General Public License for more details.
12  
13   You should have received a copy of the GNU General Public License  along with
14   this program; if not, write to the Free Software  Foundation, Inc., 59 Temple
15   Place, Suite 330, Boston, MA 02111-1307 USA.
16   */
17  package org.bejug.javacareers.jobs.service;
18  
19  import java.util.List;
20  
21  import org.bejug.javacareers.common.exception.DuplicateUserNameException;
22  import org.bejug.javacareers.common.exception.LastAdminException;
23  import org.bejug.javacareers.jobs.common.AbstractSpringContextTests;
24  import org.bejug.javacareers.jobs.dao.ParameterDao;
25  import org.bejug.javacareers.jobs.model.OrganisationType;
26  import org.bejug.javacareers.jobs.model.Profile;
27  import org.bejug.javacareers.jobs.model.User;
28  import org.springframework.dao.DataAccessException;
29  
30  /***
31   * Tests the user service.
32   *
33   * @author Sven Schauwvliege (Last modified by $Author: shally $)
34   * @version $Revision: 1.10 $ - $Date: 2005/12/20 15:36:47 $
35   * Todo: Introduce dbUnit.
36   */
37  public class AdminServiceImplTests extends AbstractSpringContextTests {
38  
39      /***
40       * The user service reference which gets set via an IoC setter injection.
41       */
42      protected AdminService adminService;
43      
44      /***
45       * The parameter dao reference which gets set via an IoC setter injection.
46       */
47      protected ParameterDao profileDao;
48  
49      /***
50       * @return the adminService
51       */
52      public AdminService getAdminService() {
53          return adminService;
54      }
55  
56      /***
57       * Test if the userService is configured and injected.
58       */
59      public void testAdminService() {
60          assertNotNull(getAdminService());
61      }
62  
63      /***
64       * Test the Create, Read, Update and Delete functions for a User.
65       *
66       * @throws DataAccessException Thrown when a database exceptions occurs.
67       */
68      public void testCRUDUser() throws DataAccessException {
69  
70          User user = new User("test userName", "test email",
71                  "password", "role", "test mobile", "Dr", "test firstName",
72                  "test lastName", "test phone");
73          assertTrue(user.isNew());
74          assertNotNull(user.getCreationDate());
75          assertNull(user.getModificationDate());
76  
77          try {
78              //Save test user
79              getAdminService().storeUser(user);
80          } catch (DuplicateUserNameException e1) {
81              assertTrue("duplicate username", false);
82              e1.printStackTrace();
83          } catch (LastAdminException e) {
84  
85          }
86          Integer id = user.getId();
87          
88          // If saved the user id, creation and modification date should not be null!
89          assertNotNull(id);
90          assertNotNull(user.getCreationDate());
91          assertNotNull(user.getModificationDate());
92  
93          //
94          // (2) Get a user.
95          //
96          User newUser = getAdminService().getUser(id);
97          assertEquals(newUser.getUserName(), user.getUserName());
98          assertEquals(newUser.getId(), user.getId());
99          assertEquals(newUser.getContact().getEmail(), user.getContact().getEmail());
100         assertEquals(newUser.getRole(), user.getRole());
101         assertEquals(newUser.getPassword(), user.getPassword());
102         assertEquals(newUser.getCreationDate(), user.getCreationDate());
103 
104         assertTrue(newUser.equals(user));
105 
106         //
107         // (3) Update an user.
108         //
109         newUser.setUserName("Updated Name");
110         try {
111             getAdminService().storeUser(newUser);
112         } catch (DuplicateUserNameException e2) {
113             assertTrue("duplicate username ", false);
114             e2.printStackTrace();
115         } catch (LastAdminException e) {
116             assertTrue("last admin ", false);
117         }
118         //
119         // (4) Delete an user.
120         //
121         try {
122             getAdminService().deleteUser(id);
123         } catch (LastAdminException e3) {
124             e3.printStackTrace();
125         }
126         boolean found = true;
127         try {
128             // Check if user has been removed
129             getAdminService().getUser(id);
130         } catch (DataAccessException e) {
131             // The exception MUST BE thrown if the user was successfully deleted.
132             found = false;
133         }
134         assertFalse(found);
135     }
136 
137     /***
138      * Test the findUserByUsername method.
139      *
140      * @throws DataAccessException
141      * @throws DataAccessException in case of DataAccessException 
142      */
143     public void testGetUserByUserName() throws DataAccessException {
144         User user = new User("test userName", "test email", "password",
145                 "role", "test mobile", "dr", "test firstName", "test lastName", "test phone");
146         try {
147             getAdminService().storeUser(user);
148         } catch (DuplicateUserNameException e2) {
149             assertTrue("Duplicate username ", false);
150             e2.printStackTrace();
151         } catch (LastAdminException e) {
152             assertTrue("last admin ", false);
153         }
154         assertNotNull(user.getId());
155 
156         User foundUser = getAdminService().getUserByUserName(user.getUserName());
157         assertNotNull(user);
158         assertEquals(foundUser.getUserName(), user.getUserName());
159         assertEquals(foundUser.getId(), user.getId());
160         assertEquals(foundUser.getContact().getEmail(), user.getContact().getEmail());
161         assertEquals(foundUser.getRole(), user.getRole());
162         assertEquals(foundUser.getPassword(), user.getPassword());
163         assertEquals(foundUser.getCreationDate(), user.getCreationDate());
164 
165         assertTrue(foundUser.equals(user));
166     }
167 
168     /***
169      * Test the getUsers method.
170      *
171      * @throws DataAccessException
172      * @throws DataAccessException in case of DataAccessException
173      */
174     public void testGetUsers() throws DataAccessException {
175         User user1 = new User("test userName1", "test email", "password", "role",
176                 "test mobile", "mr", "test firstName", "test lastName", "test phone");
177         try {
178             getAdminService().storeUser(user1);
179         } catch (DuplicateUserNameException e2) {
180             assertTrue("Duplicate username ", false);
181             e2.printStackTrace();
182         } catch (LastAdminException e) {
183             assertTrue("last admin ", false);
184         }
185         assertNotNull(user1.getId());
186 
187         User user2 = new User("test userName2", "test email", "password", "role",
188                 "test mobile", "mss", "test firstName", "test lastName", "test phone");
189         try {
190             getAdminService().storeUser(user2);
191         } catch (DuplicateUserNameException e2) {
192             assertTrue("Duplicate username ", false);
193             e2.printStackTrace();
194         } catch (LastAdminException e) {
195             assertTrue("last admin ", false);
196         }
197         assertNotNull(user2.getId());
198 
199         List list = getAdminService().getUsers();
200         assertNotNull(list);
201         assertTrue(list.size() == 2);
202     }
203 
204     /***
205      * Test the Create, Read, Update and Delete functions for a OrganisationType.
206      *
207      * @throws DataAccessException Thrown when a database exceptions occurs.
208      */
209     public void testCRUDOrganisationType() throws DataAccessException {
210 
211         //
212         // (1) Create test organisationType.
213         //
214         OrganisationType organisationType = new OrganisationType("Test name",
215                 "Test organisation discription", "be_NL");
216         assertTrue(organisationType.isNew());
217         assertNotNull(organisationType.getCreationDate());
218         assertNull(organisationType.getModificationDate());
219 
220         getAdminService().storeOrganisationType(organisationType);
221         Integer id = organisationType.getId();
222 
223         // If saved the organisation type id, creation and modification date
224         // should not be null!
225         assertNotNull(id);
226         assertNotNull(organisationType.getCreationDate());
227         assertNotNull(organisationType.getModificationDate());
228 
229         //
230         // (2) Get an organisationType.
231         //
232         OrganisationType newOrganisationType = getAdminService()
233                 .getOrganisationType(id);
234         assertEquals(newOrganisationType.getName(), organisationType.getName());
235         assertEquals(newOrganisationType.getId(), organisationType.getId());
236         assertEquals(newOrganisationType.getDescription(), organisationType
237                 .getDescription());
238         assertEquals(newOrganisationType.getCreationDate(), organisationType
239                 .getCreationDate());
240 
241         assertTrue(newOrganisationType.equals(organisationType));
242 
243         //
244         // (3) Update an organisationType.
245         //
246         newOrganisationType.setName("Updated Name");
247         getAdminService().storeOrganisationType(newOrganisationType);
248 
249         //
250         // (4) Delete an organisationType.
251         //
252         getAdminService().deleteOrganisationType(id);
253         boolean found = true;
254         try {
255             // Check if organisationType has been removed
256             getAdminService().getOrganisationType(id);
257         } catch (DataAccessException e) {
258             // The exception MUST BE thrown if the organisation type was
259             // successfully deleted.
260             found = false;
261         }
262         assertFalse(found);
263     }
264 
265     /***
266      * Test the getOrganisationTypes method.
267      *
268      * @throws DataAccessException
269      * @throws DataAccessException in case of DataAccessException     * 
270      */
271     public void testGetOrganisationTypes() throws DataAccessException {
272         OrganisationType organisationType1 = new OrganisationType("Test name",
273                 "Test organisation discription",
274                 "be_NL");
275         getAdminService().storeOrganisationType(organisationType1);
276         assertNotNull(organisationType1.getId());
277 
278         OrganisationType organisationType2 = new OrganisationType("Test name",
279                 "Test organisation discription",
280                 "be_NL");
281 
282         getAdminService().storeOrganisationType(organisationType2);
283         assertNotNull(organisationType2.getId());
284 
285         List list = getAdminService().getOrganisationTypes();
286         assertNotNull(list);
287         assertTrue(list.size() == 2);
288     }
289     
290     /***
291      * tests the offertypedao.
292      */
293     public void testCRUDProfile() {
294 
295         Profile myProfile = new Profile();
296         myProfile.setName("Senior Developer");
297         myProfile.setDescription("Senior Developer met enkele jaren ervaring");
298 
299         //add the profile to the database
300         getAdminService().storeProfile(myProfile);
301         //id should not be null
302         Integer id = myProfile.getId();
303         assertNotNull(id);
304 
305         //get all the profiles from the database
306         List profiles = getAdminService().getProfiles();
307         assertNotNull(profiles);
308 
309         //get a profile by id
310         Profile profileFromDB = (Profile) profileDao.getParameter(id);
311         assertEquals(profileFromDB.getDescription(), myProfile.getDescription());
312         assertEquals(profileFromDB.getName(), myProfile.getName());
313 
314         //update a profile
315         myProfile.setDescription("No description");
316         getAdminService().storeProfile(myProfile);
317 
318         profileFromDB = (Profile) profileDao.getParameter(id);
319         assertEquals(profileFromDB.getDescription(), "No description");
320 
321         //delete a profile
322         getAdminService().deleteProfile(id);
323         boolean found = true;
324         try {
325             // Check if the profile has been removed
326             profileDao.getParameter(id);
327         } catch (DataAccessException e) {
328             // The exception should be thrown if the profile was deleted  :)
329             found = false;
330         }
331         assertFalse(found);
332 
333     } //testOfferType
334 }
335 
336 /***
337  * $Log: AdminServiceImplTests.java,v $
338  * Revision 1.10  2005/12/20 15:36:47  shally
339  * CheckStyle and PMD changes.
340  *
341  * Revision 1.9  2005/12/08 14:53:46  shally
342  * Opkuis voor checkstyle.
343  *
344  * Revision 1.8  2005/08/12 08:33:09  bme_jcs
345  * moved profile-methods from jobservice to adminservice
346  *
347  * Revision 1.7  2005/08/11 13:31:36  bavo_jcs
348  * LastAdmin Fix
349  *
350  * Revision 1.6  2005/08/11 08:12:17  bme_jcs
351  * refactoring of adminService
352  *
353  * Revision 1.5  2005/07/05 15:18:12  schauwvliege
354  * added person/contact and location to model
355  *
356  * Revision 1.4  2005/06/29 09:00:26  psong09
357  * comment component updated
358  *
359  * Revision 1.3  2005/06/14 13:34:59  schauwvliege
360  * fixed tests
361  *
362  * Revision 1.2  2005/06/09 08:19:04  bejug_cc
363  * Fix initial import
364  *
365  * Revision 1.11  2005/06/07 12:52:24  ssc
366  * Delete last admin exception
367  *
368  * Revision 1.10  2005/05/17 11:59:56  ssc
369  * Refactored User and Publisher class to User Class added cvAdded Boolean, added Address to user, Fixed test to work with this setup
370  *
371  * Revision 1.9  2005/05/12 08:23:55  ssc
372  * Checkstyle errors
373  *
374  * Revision 1.8  2005/05/11 17:57:26  sja
375  * Corrected import.
376  *
377  * Revision 1.7  2005/05/11 12:49:30  ssc
378  * Checstyle errors
379  *
380  * Revision 1.6  2005/05/10 15:00:03  ssc
381  * I18N
382  *
383  * Revision 1.5  2005/05/10 14:38:06  ssc
384  * Deleted Salutation
385  *
386  * Revision 1.4  2005/05/10 11:59:58  ssc
387  * Duplicate user/user exception
388  *
389  * Revision 1.3  2005/05/04 13:33:43  ssc
390  * update
391  *
392  * Revision 1.2  2005/05/03 13:18:05  ssc
393  * Added OrganisationType in AdminService
394  *
395  * Revision 1.1  2005/05/03 11:09:25  ssc
396  * Renamed User service to AdminService
397  *
398  * Revision 1.1  2005/05/02 13:38:14  ssc
399  * First release
400  *
401  *
402  */