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   */
18  
19  package org.bejug.javacareers.jobs.service;
20  
21  import java.util.Iterator;
22  import java.util.List;
23  
24  import org.bejug.javacareers.common.exception.DuplicateUserNameException;
25  import org.bejug.javacareers.common.exception.LastAdminException;
26  import org.bejug.javacareers.generator.StubGenerator;
27  import org.bejug.javacareers.jobs.common.AbstractSpringContextTests;
28  import org.bejug.javacareers.jobs.model.Address;
29  import org.bejug.javacareers.jobs.model.Comment;
30  import org.bejug.javacareers.jobs.model.JobOffer;
31  import org.bejug.javacareers.jobs.model.Organisation;
32  import org.bejug.javacareers.jobs.model.OrganisationType;
33  import org.bejug.javacareers.jobs.model.User;
34  import org.springframework.dao.DataAccessException;
35  
36  /***
37   * @author : Peter Symoens (Last modified by $Author: schauwvliege $)
38   * @version $Revision: 1.16 $ - $Date: 2005/09/13 08:11:06 $:
39   */
40  public class CommentServiceImplTests extends AbstractSpringContextTests {
41  	
42  	
43      /***
44       * The comment service which gets set via an IoC setter injection.
45       */
46      protected CommentService commentService;
47  
48      /***
49       * The admint service which gets set via an IoC setter injection.
50       */
51      protected AdminService adminService;
52  
53      /***
54       * The jobservice which gets set via an IoC setter injection.
55       */
56      protected JobService jobService;
57  
58      /***
59      * @return jobService Returns the jobService
60      */
61      public JobService getJobService() {
62          return jobService;
63      }
64  
65  
66      /***
67      * @return commentService Returns the commentService
68      */
69      public CommentService getCommentService() {
70          return commentService;
71      }
72  
73      /***
74      * @return adminService Returns the adminService
75      */
76      public AdminService getAdminService() {
77          return adminService;
78      }
79  
80      /***
81       * The user for the comment.
82       */
83      private User user;
84  
85      /***
86       *  The address for the user.
87       */
88      private Address address;
89  
90      /***
91       * The Organisation for the user.
92       */
93      private  Organisation organisation;
94  
95      /***
96       * The organisationType for the Organisation.
97       */
98      private OrganisationType organisationType;
99  
100     /***
101      *  The jobOffer for the comment.
102      */
103     private JobOffer jobOffer;
104 
105 	
106    /***
107     * Test if dao gets injected
108      */
109     public void testCommentDaoInjected(){
110      //   assertNotNull("Comment dao did not get injected", commentService.getCommentDao());
111 
112     }
113 
114     /***
115      * Test the Create, Read, Update and Delete functions for a comment.
116      *
117      * @throws DataAccessException Thrown when a database exceptions occurs.
118      */
119     public void testCRUDComment() throws DataAccessException {
120 
121         createCollaboratorObjects();
122 
123         //Create the comment and assert not null.
124         Comment comment = createComment();
125         assertNotNull(comment.getId());
126 
127         //Load the comments from the offer and assert id not null.
128         List comments = getCommentService().getComments(jobOffer.getId(), true);
129         Comment foundComment = null;
130         for (Iterator iterator = comments.iterator(); iterator.hasNext();) {
131             foundComment =  (Comment) iterator.next();
132                assertNotNull(foundComment.getId());
133             User u = foundComment.getUser();
134             assertNotNull(u.getId());
135         }
136 
137         //Modify the comment and store it.
138         String content = "Modified test content 1";
139         foundComment.setContent(content);
140         getCommentService().storeComment(foundComment);
141         Comment retrievedComment = getCommentService().getComment(foundComment.getId());
142         assertEquals("Expected comment to be modified", retrievedComment.getContent(), content);
143 
144         //Delete the comment and try to load it.
145         getCommentService().deleteComment(retrievedComment.getId());
146         try{
147         getCommentService().getComment(retrievedComment.getId());
148         fail("Expected DataAccessException");
149         }
150         catch(DataAccessException e) {
151               //Exception expected
152         }
153     }
154 
155     /***
156      * Test the getCommentBySubject method.
157      * @throws DataAccessException e
158      */
159     public void testGetCommentBySubjectMethod() throws DataAccessException{
160         createCollaboratorObjects();
161 
162          //Create a comment and store it.
163         String subject = "test subject 1";
164         Comment comment = new Comment(jobOffer, user, subject, "test content 1");
165         getCommentService().storeComment(comment);
166 
167         //Retrieve and assert id not null.
168         Comment retrievedComment = getCommentService().getCommentBySubject(jobOffer.getId(), subject);
169         assertNotNull("Expected comment to have an id", retrievedComment.getId());
170 
171     }
172 
173     /***
174      * Test the getCommentBySubject method.
175      * @throws DataAccessException e
176      */
177     public void testGetUserCommentsMethod() throws DataAccessException{
178         createCollaboratorObjects();
179         createComment();
180         List comments = commentService.getUserComments(user);
181         assertFalse("Comments list should not be empty", comments.isEmpty());
182     }
183 
184    /***
185     * Test the getChildCommentCount and getChildComments methods.
186     * @throws DataAccessException e
187     */
188     public void testChildCommentsMethods() throws DataAccessException{
189         createCollaboratorObjects();
190         Comment comment = createComment();
191         Comment childComment = new Comment();
192         childComment.setParentComment(comment);
193         getCommentService().storeComment(childComment);
194         int count = getCommentService().getChildCommentCount(comment);
195         assertEquals("Count should be equal to 1", 1, count);
196         List childCommentList = commentService.getChildComments(comment);
197         assertFalse("childCommentList should not be empty", childCommentList.isEmpty());
198      }
199 
200     /***
201      *  Create collaborator objects for the comment.
202      *  @throws DataAccessException e
203      */
204     private void createCollaboratorObjects() throws DataAccessException{
205         user = StubGenerator.getStubUser();
206         address = StubGenerator.getStubAddress();
207         organisation = StubGenerator.getStubOrganisation();
208         organisationType = StubGenerator.getStubOrganisationType();
209         getAdminService().storeOrganisationType(organisationType);
210         organisation.setOrganisationType(organisationType);
211         organisation.setAddress(address);
212         user.setAddress(address);
213         user.setOrganisation(organisation);
214         try {
215             getAdminService().storeUser(user);
216         } catch (DuplicateUserNameException e) {
217             fail("No exception expected");
218             e.printStackTrace();
219         } catch (LastAdminException e) {
220             fail("last admin ");
221         }
222         jobOffer = StubGenerator.getStubJobOffer();
223         getJobService().storeJobOffer(jobOffer);
224     }
225 
226     /***
227      * @return Comment Create a comment and store it.
228      * @throws DataAccessException e
229      */
230     private Comment createComment() throws DataAccessException{
231       Comment comment = new Comment(jobOffer, user, "test subject 1", "test content 1");
232       getCommentService().storeComment(comment);
233       return comment;
234     }
235 
236 
237 }
238 
239 /***
240  * $Log: CommentServiceImplTests.java,v $
241  * Revision 1.16  2005/09/13 08:11:06  schauwvliege
242  * organize imports
243  *
244  * Revision 1.15  2005/08/30 13:07:48  psong09
245  * renamed author: psong09
246  *
247  * Revision 1.14  2005/08/17 09:09:30  schauwvliege
248  * Fixed all unit tests
249  *
250  * Revision 1.13  2005/08/11 13:31:36  bavo_jcs
251  * LastAdmin Fix
252  *
253  * Revision 1.12  2005/08/11 08:12:17  bme_jcs
254  * refactoring of adminService
255  *
256  * Revision 1.11  2005/08/03 13:16:03  bme_jcs
257  * getDao's removed and storeObject renamed to store
258  *
259  * Revision 1.10  2005/08/01 11:42:09  bme_jcs
260  * adapted for fieldinjection
261  *
262  * Revision 1.9  2005/07/06 13:32:15  psong09
263  * completed test coverage
264  *
265  * Revision 1.8  2005/07/06 08:28:54  psong09
266  * modified CRUD tests
267  *
268  * Revision 1.7  2005/07/06 08:09:51  psong09
269  * completed CRUD tests
270  *
271  * Revision 1.6  2005/07/05 15:31:14  psong09
272  * modified crud test
273  *
274  * Revision 1.5  2005/07/05 10:48:17  psong09
275  * rename of MockObs to StubGenerator
276  *
277  *
278  */