View Javadoc
1   /*
2    * Copyright (C) 2003-2008 eXo Platform SAS.
3    *
4    * This program is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Affero General Public License
6    * as published by the Free Software Foundation; either version 3
7    * of the License, or (at your option) any later version.
8    *
9    * This program is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU General Public License for more details.
13   *
14   * You should have received a copy of the GNU General Public License
15   * along with this program; if not, see<http://www.gnu.org/licenses/>.
16   */
17  package org.exoplatform.calendar.service.test;
18  
19  import java.text.SimpleDateFormat;
20  import java.util.ArrayList;
21  import java.util.Arrays;
22  import java.util.Collection;
23  import java.util.LinkedList;
24  import java.util.List;
25  import java.util.TimeZone;
26  
27  import org.exoplatform.calendar.service.Calendar;
28  import org.exoplatform.calendar.service.CalendarEvent;
29  import org.exoplatform.calendar.service.CalendarService;
30  import org.exoplatform.calendar.service.EventCategory;
31  import org.exoplatform.calendar.service.ExtendedCalendarService;
32  import org.exoplatform.calendar.service.GroupCalendarData;
33  import org.exoplatform.calendar.service.Utils;
34  import org.exoplatform.calendar.service.impl.CalendarServiceImpl;
35  import org.exoplatform.calendar.service.impl.JCRDataStorage;
36  import org.exoplatform.commons.utils.ListAccess;
37  import org.exoplatform.component.test.AbstractKernelTest;
38  import org.exoplatform.component.test.ConfigurationUnit;
39  import org.exoplatform.component.test.ConfiguredBy;
40  import org.exoplatform.component.test.ContainerScope;
41  import org.exoplatform.services.log.ExoLogger;
42  import org.exoplatform.services.log.Log;
43  import org.exoplatform.services.organization.Group;
44  import org.exoplatform.services.organization.Membership;
45  import org.exoplatform.services.organization.MembershipHandler;
46  import org.exoplatform.services.organization.OrganizationService;
47  import org.exoplatform.services.organization.User;
48  import org.exoplatform.services.organization.UserStatus;
49  import org.exoplatform.services.security.ConversationState;
50  import org.exoplatform.services.security.Identity;
51  import org.exoplatform.services.security.MembershipEntry;
52  
53  /**
54   * Created by The eXo Platform SAS
55   * 
56   * @author : Hung nguyen hung.nguyen@exoplatform.com May 7, 2008
57   */
58  
59  @ConfiguredBy({
60          @ConfigurationUnit(scope = ContainerScope.PORTAL, path = "conf/exo.portal.component.portal-configuration.xml"),
61          @ConfigurationUnit(scope = ContainerScope.PORTAL, path = "conf/exo.portal.component.test.jcr-configuration.xml"),
62          @ConfigurationUnit(scope = ContainerScope.PORTAL, path = "conf/exo.portal.component.identity-configuration.xml"),
63          @ConfigurationUnit(scope = ContainerScope.PORTAL, path = "conf/portal/test-portal-configuration.xml"),
64          @ConfigurationUnit(scope = ContainerScope.PORTAL, path = "conf/portal/exo.calendar.component.core.test.configuration.xml"),
65          @ConfigurationUnit(scope = ContainerScope.PORTAL, path = "conf/portal/exo.calendar.test.jcr-configuration.xml"),
66          @ConfigurationUnit(scope = ContainerScope.PORTAL, path = "conf/portal/exo.calendar.test.portal-configuration.xml")
67  })
68  
69  public abstract class BaseCalendarServiceTestCase extends AbstractKernelTest {
70  
71    protected static Log          log        = ExoLogger.getLogger("cs.calendar.services.test");
72  
73    protected TimeZone            tz         = java.util.Calendar.getInstance().getTimeZone();
74  
75    protected String              timeZone   = tz.getID();
76  
77    protected String              username   = "root";
78  
79    protected String[]            userGroups = new String[] { "/platform/users",
80        "/organization/management/executive-board" };
81  
82    protected SimpleDateFormat    df         = new SimpleDateFormat(Utils.DATE_TIME_FORMAT);
83  
84    protected OrganizationService organizationService_;
85  
86    protected CalendarService     calendarService_;
87    
88    protected ExtendedCalendarService xCalService;
89  
90    @Override
91    public void setUp() throws Exception {
92      begin();
93  
94      // Init services
95      organizationService_ = getService(OrganizationService.class);
96      calendarService_ = getService(CalendarService.class);
97      xCalService = getService(ExtendedCalendarService.class);
98  
99      // . Init JCR root node
100     JCRDataStorage storage = ((CalendarServiceImpl) calendarService_).getDataStorage();
101     storage.getPublicCalendarHome();
102     storage.getPublicCalendarServiceHome();
103     storage.getUserCalendarHome(username);
104     storage.getSharedCalendarHome();
105 
106     // Login user
107     login(username);
108     
109     ListAccess<User> users = organizationService_.getUserHandler().findAllUsers(UserStatus.DISABLED);
110     if (users != null) {
111       for (User user : users.load(0, users.getSize())) {
112         organizationService_.getUserHandler().setEnabled(user.getUserName(), true, false);
113       }
114     }   
115   }
116 
117   @Override
118   public void tearDown() throws Exception {
119     ListAccess<User> users = organizationService_.getUserHandler().findAllUsers();
120     int size = users.getSize();
121     for (User u : users.load(0, size)) {
122       cleanData(u);
123     }
124     end();
125   }
126 
127   protected void cleanData(User user) throws Exception {
128     // . Get all private calendar of user
129     List<Calendar> cals = calendarService_.getUserCalendars(user.getUserName(), true);
130 
131     // . Load all group calendar
132     List<Group> groups = new ArrayList<Group>();
133     groups.addAll(organizationService_.getGroupHandler().findGroupsOfUser(user.getUserName()));
134     String[] groupIds = new String[groups.size()];
135     for (int i = 0; i < groups.size(); i++) {
136       groupIds[i] = groups.get(i).getId();
137     }
138     for (GroupCalendarData g : calendarService_.getGroupCalendars(groupIds,
139                                                                   true,
140                                                                   user.getUserName())) {
141       cals.addAll(g.getCalendars());
142     }
143 
144     // . Find all shared calendar
145     GroupCalendarData gData = calendarService_.getSharedCalendars(user.getUserName(), true);
146     if (gData != null)
147       cals.addAll(gData.getCalendars());
148 
149     // . Remove all calendar
150     for (int i = 0; i < cals.size(); i++) {
151       String id = cals.get(i).getId();
152       calendarService_.removeUserCalendar(user.getUserName(), id);
153       calendarService_.removePublicCalendar(id);
154       calendarService_.removeSharedCalendar(user.getUserName(), id);
155     }
156 
157     // . Remove all EventCategory
158     List<EventCategory> categories = calendarService_.getEventCategories(user.getUserName());
159     for (EventCategory category : categories) {
160       calendarService_.removeEventCategory(user.getUserName(), category.getId());
161     }
162   }
163 
164   protected Calendar createPrivateCalendar(String username, String name, String description) throws Exception {
165     Calendar calendar = new Calendar();
166     calendar.setName(name);
167     calendar.setDescription(description);
168     calendar.setPublic(false);
169     calendarService_.saveUserCalendar(username, calendar, true);
170     return calendar;
171   }
172 
173   protected Calendar createGroupCalendar(String[] groups, String calName, String description) throws Exception {
174     Calendar calendar = new Calendar();
175     calendar.setName(calName);
176     calendar.setDescription(description);
177     calendar.setPublic(true);
178     calendar.setGroups(groups);
179     calendarService_.savePublicCalendar(calendar, true);
180     return calendar;
181   }
182   
183   protected Calendar createSharedCalendar(String name, String description, String[] shared) {
184     try {
185       Calendar sharedCalendar = new Calendar();
186       sharedCalendar.setName(name);
187       sharedCalendar.setDescription(description);
188       sharedCalendar.setPublic(true);
189       sharedCalendar.setViewPermission(shared);
190       sharedCalendar.setEditPermission(shared);
191       calendarService_.saveUserCalendar(username, sharedCalendar, true);
192 
193       calendarService_.shareCalendar(username, sharedCalendar.getId(), Arrays.asList(shared));
194 
195       return sharedCalendar;
196     } catch (Exception e) {
197       fail();
198       return null;
199     }
200   }
201 
202   protected EventCategory createUserEventCategory(String username, String name) throws Exception {
203     EventCategory eventCategory = new EventCategory();
204     eventCategory.setName(name);
205     calendarService_.saveEventCategory(username, eventCategory, true);
206     return eventCategory;
207   }
208 
209   protected CalendarEvent createCalendarEventInstance(String summary) {
210     CalendarEvent event = new CalendarEvent();
211     event.setSummary(summary);
212     java.util.Calendar from = java.util.Calendar.getInstance();
213     from.add(java.util.Calendar.MINUTE, 5);
214     event.setFromDateTime(from.getTime());
215     from.add(java.util.Calendar.HOUR, 1);
216     event.setToDateTime(from.getTime());
217     return event;
218   }
219 
220   @SuppressWarnings("unchecked")
221   protected <T> T getService(Class<T> clazz) {
222     return (T) getContainer().getComponentInstanceOfType(clazz);
223   }
224 
225   protected CalendarEvent createUserEvent(String calendarId,
226                                           EventCategory eventCategory,
227                                           String summary) {
228     java.util.Calendar fromCal = java.util.Calendar.getInstance();
229     java.util.Calendar toCal = java.util.Calendar.getInstance();
230     toCal.add(java.util.Calendar.HOUR, 1);
231     return createUserEvent(calendarId, eventCategory, summary, true, fromCal, toCal);
232   }
233 
234   protected CalendarEvent createUserEvent(String calendarId,
235                                           EventCategory eventCategory,
236                                           String summary,
237                                           boolean isPrivate,
238                                           java.util.Calendar fromCal,
239                                           java.util.Calendar toCal) {
240     try {
241       CalendarEvent calendarEvent = new CalendarEvent();
242       if (eventCategory != null) {
243         calendarEvent.setEventCategoryId(eventCategory.getId());
244         calendarEvent.setEventCategoryName(eventCategory.getName());
245       }
246       calendarEvent.setSummary(summary);
247       calendarEvent.setFromDateTime(fromCal.getTime());
248       calendarEvent.setToDateTime(toCal.getTime());
249       calendarEvent.setPrivate(isPrivate);
250       calendarService_.saveUserEvent(username, calendarId, calendarEvent, true);
251       return calendarEvent;
252     } catch (Exception e) {
253       fail("Exception while create user event", e);
254       return null;
255     }
256   }
257 
258   protected CalendarEvent createUserEvent(String username,
259                                           String calendarId,
260                                           EventCategory eventCategory,
261                                           String summary,
262                                           boolean isPrivate,
263                                           java.util.Calendar fromCal,
264                                           java.util.Calendar toCal) {
265     try {
266       CalendarEvent calendarEvent = new CalendarEvent();
267       if (eventCategory != null) {
268         calendarEvent.setEventCategoryId(eventCategory.getId());
269         calendarEvent.setEventCategoryName(eventCategory.getName());
270       }
271       calendarEvent.setSummary(summary);
272       calendarEvent.setFromDateTime(fromCal.getTime());
273       calendarEvent.setToDateTime(toCal.getTime());
274       calendarEvent.setPrivate(isPrivate);
275       calendarService_.saveUserEvent(username, calendarId, calendarEvent, true);
276       return calendarEvent;
277     } catch (Exception e) {
278       fail("Exception while create user event", e);
279       return null;
280     }
281   }
282 
283   protected CalendarEvent createUserEvent(String summary) throws Exception {
284     Calendar calendar = createPrivateCalendar(username, "CalendarTest", "CalendarTest");
285     EventCategory category = createUserEventCategory(username, "CalendarCategoryTest");
286     java.util.Calendar from = java.util.Calendar.getInstance();
287     java.util.Calendar to = java.util.Calendar.getInstance();
288     to.add(java.util.Calendar.HOUR, 1);
289     return createUserEvent(calendar.getId(), category, summary, true, from, to);
290   }
291 
292   protected CalendarEvent createGroupEvent(String publicCalendarId,
293                                            EventCategory eventCategory,
294                                            String summary,
295                                            boolean isPrivate,
296                                            java.util.Calendar fromCal,
297                                            java.util.Calendar toCal) {
298     try {
299       CalendarEvent publicEvent = new CalendarEvent();
300       if (eventCategory != null) {
301         publicEvent.setEventCategoryId(eventCategory.getId());
302         publicEvent.setEventCategoryName(eventCategory.getName());
303       }
304       publicEvent.setSummary(summary);
305       publicEvent.setFromDateTime(fromCal.getTime());
306       publicEvent.setToDateTime(toCal.getTime());
307       publicEvent.setPrivate(isPrivate);
308       calendarService_.savePublicEvent(publicCalendarId, publicEvent, true);
309       return publicEvent;
310     } catch (Exception e) {
311       fail();
312       return null;
313     }
314   }
315 
316   protected void login(String username) {
317     List<MembershipEntry> entries = new LinkedList<MembershipEntry>();
318 
319     MembershipHandler mHandler = organizationService_.getMembershipHandler();
320     try {
321       Collection<Membership> memberships = mHandler.findMembershipsByUser(username);
322       for (Membership m : memberships) {
323         entries.add(new MembershipEntry(m.getGroupId(), m.getMembershipType()));
324       }
325     } catch (Exception e) {
326       // TODO Auto-generated catch block
327       e.printStackTrace();
328     }
329     ;
330     login(username, entries);
331   }
332 
333   private void login(String username, Collection<MembershipEntry> entries) {
334     Identity identity = new Identity(username, entries);
335     ConversationState state = new ConversationState(identity);
336     ConversationState.setCurrent(state);
337   }
338 }