View Javadoc
1   /**
2    * Copyright (C) 2003-2007 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  
18  package org.exoplatform.webservice.cs.rest;
19  
20  import java.text.SimpleDateFormat;
21  import java.util.Date;
22  import java.util.TimeZone;
23  
24  import javax.ws.rs.WebApplicationException;
25  import javax.ws.rs.core.MultivaluedMap;
26  import javax.ws.rs.ext.RuntimeDelegate;
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.Utils;
31  import org.exoplatform.common.http.HTTPStatus;
32  import org.exoplatform.services.rest.impl.ContainerResponse;
33  import org.exoplatform.services.rest.impl.MultivaluedMapImpl;
34  import org.exoplatform.services.rest.impl.RuntimeDelegateImpl;
35  import org.exoplatform.services.rest.tools.ByteArrayContainerResponseWriter;
36  import org.exoplatform.webservice.cs.calendar.CalendarWebservice;
37  
38  /**
39   * Created by The eXo Platform SARL Author : Volodymyr Krasnikov
40   * volodymyr.krasnikov@exoplatform.com.ua
41   */
42  
43  public class TestWebservice extends AbstractResourceTest {
44  
45  	protected CalendarWebservice calendarWebservice;
46    protected CalendarService calendarService;
47  
48    protected static final String             baseURI = "";
49    protected String username = "root";
50    
51    protected MultivaluedMap<String, String> h = new MultivaluedMapImpl();
52    
53  
54    public void setUp() throws Exception {
55      RuntimeDelegate.setInstance(new RuntimeDelegateImpl());
56      super.setUp();
57      calendarWebservice = (CalendarWebservice) container.getComponentInstanceOfType(CalendarWebservice.class);
58      calendarService = (CalendarService) container.getComponentInstanceOfType(CalendarService.class);
59      binder.addResource(calendarWebservice, null);
60      login();
61      h.putSingle("username", username);
62    }
63  
64    protected Calendar createCalendar(String name) {
65      Calendar cal = new Calendar() ;
66      cal.setName(name) ;
67      cal.setDescription("Desscription") ;
68      cal.setPublic(true) ;
69      cal.setCalType(Calendar.TYPE_PRIVATE);
70      return cal;
71    }
72    protected Calendar createPublicCalendar(String name, String description) {
73        Calendar publicCalendar = new Calendar();
74        publicCalendar.setName(name);
75        publicCalendar.setDescription(description);
76        publicCalendar.setPublic(true);
77        publicCalendar.setGroups(new String[]{"/platform/users","/organization/management/executive-board"});
78        publicCalendar.setCalType(Calendar.TYPE_PUBLIC);
79        return publicCalendar;
80    }
81    
82    private CalendarEvent createEvent(String summary, String calendarId, Date from, Date to, String calType) {
83      CalendarEvent c = new CalendarEvent() ;
84      c.setSummary(summary) ;
85      c.setCalendarId(calendarId);
86      c.setEventType(calType) ;
87      c.setFromDateTime(from) ;
88      c.setToDateTime(to);
89      return c ;
90    }
91  
92    public void testCheckPublicRss() throws Exception {
93     
94  
95      //create/get calendar in private folder
96      Calendar cal = createCalendar("myCalendar");
97       
98  
99      String extURI = "/cs/calendar/subscribe/" + username + "/" + cal.getId() + "/0";
100 
101     cal.setPublicUrl(extURI);
102     
103     calendarService.saveUserCalendar(username, cal, true);
104     CalendarEvent event = new CalendarEvent();
105     event.setSummary("test");
106     event.setDescription("event description");
107     event.setCalendarId(cal.getId());
108     event.setEventCategoryId("eventCategoryId");
109     event.setEventCategoryName("EventCategirtName");
110     event.setFromDateTime(new Date());
111     event.setToDateTime(new Date());
112     calendarService.saveUserEvent(username, cal.getId(), event, true);
113     event.setLocation(extURI.replaceFirst("rss", "subscribe") +"/"+ event.getId());
114     ByteArrayContainerResponseWriter writer = new ByteArrayContainerResponseWriter();
115     try {
116     ContainerResponse response = service("GET", extURI, baseURI, h, null, writer);
117     assertNotNull(response);
118     assertEquals(HTTPStatus.OK, response.getStatus());
119 
120     response = service("GET", cal.getPublicUrl(), baseURI, h, null, writer);
121     assertNotNull(response);
122     assertEquals(HTTPStatus.OK, response.getStatus());
123 
124     } catch (WebApplicationException wae){
125         //TODO check null pointer exception
126     }
127    
128     //deleteData(username, calCate.getId());
129     
130   }
131 
132   public void testUpcomingEvent() throws Exception {
133     
134   //create/get calendar in private folder
135     Calendar cal = createCalendar("myCalendar");
136      
137 
138     String extURI = "/cs/calendar/subscribe/" + username + "/" + cal.getId() + "/0";
139 
140     cal.setPublicUrl(extURI);
141     
142     calendarService.saveUserCalendar(username, cal, true);
143     
144     CalendarEvent calE = createEvent("newEvent", cal.getId(), new Date(), new Date(), CalendarEvent.TYPE_EVENT);
145     calendarService.saveUserEvent(username, cal.getId(), calE, true);
146     
147     
148     //ArrayList<String> calIds = new ArrayList<String>() ;
149     //calIds.add(cal.getId());
150     //assertEquals(calendarService.getUserEventByCalendar(username, calIds).size(), 1);
151     // data correct
152     String eventURI = "/cs/calendar/getissues/"+ CalendarEvent.TYPE_EVENT + "/10";
153     ByteArrayContainerResponseWriter writer = new ByteArrayContainerResponseWriter();
154     ContainerResponse response = service("GET", eventURI, baseURI, h, null, writer);
155     assertNotNull(response);
156     assertEquals(HTTPStatus.OK ,response.getStatus());
157     
158     //deleteData(username, calCate.getId());
159   }
160 
161   public void testUpdateStatus() throws Exception {
162 
163     //create/get calendar in private folder
164     Calendar cal = createCalendar("myCalendar");
165      
166 
167     String extURI = "/cs/calendar/subscribe/" + username + "/" + cal.getId() + "/0";
168 
169     cal.setPublicUrl(extURI);
170     
171     calendarService.saveUserCalendar(username, cal, true);
172     
173     CalendarEvent calE = createEvent("newTask", cal.getId(), new Date(), new Date(), CalendarEvent.TYPE_TASK);
174     calendarService.saveUserEvent(username, cal.getId(), calE, true);
175     
176      
177     
178     String eventURI = "/cs/calendar/updatestatus/"+calE.getId()+"/" + calE.getEventState();
179     
180     ByteArrayContainerResponseWriter writer = new ByteArrayContainerResponseWriter();
181     ContainerResponse response = service("GET", eventURI, baseURI, h, null, writer);
182     assertNotNull(response);
183     assertEquals(HTTPStatus.OK, response.getStatus());
184     
185     //deleteData(username, calCate.getId());
186   }
187   
188   //mvn test -Dtest=TestWebservice#testGetEvent
189   public void testGetEvent() throws Exception {
190 
191     //create/get calendar in private folder
192     Calendar cal = createCalendar("myCalendar");
193      
194 
195     String extURI = "/cs/calendar/subscribe/" + username + "/" + cal.getId() + "/0";
196 
197     cal.setPublicUrl(extURI);
198     
199     calendarService.saveUserCalendar(username, cal, true);
200     
201     CalendarEvent calE = createEvent("newEvent", cal.getId(), new Date(), new Date(), CalendarEvent.TYPE_EVENT);
202     calendarService.saveUserEvent(username, cal.getId(), calE, true);
203     
204     String eventURI = "/cs/calendar/getevent/" + calE.getId();
205     ByteArrayContainerResponseWriter writer = new ByteArrayContainerResponseWriter();
206     ContainerResponse response = service("GET", eventURI, baseURI, h, null, writer);
207     
208     assertNotNull(response);
209     assertEquals(HTTPStatus.OK, response.getStatus());
210     
211     //deleteData(username, calCate.getId());
212     
213   }
214   
215   public void testGetEventById() throws Exception {
216 
217     //create/get calendar in private folder
218     Calendar cal = createCalendar("myCalendar");
219      
220     String extURI = "/cs/calendar/subscribe/" + username + "/" + cal.getId() + "/0";
221 
222     cal.setPublicUrl(extURI);
223     
224     calendarService.saveUserCalendar(username, cal, true);
225     
226     CalendarEvent calE = createEvent("newEvent", cal.getId(), new Date(), new Date(), CalendarEvent.TYPE_EVENT);
227     calendarService.saveUserEvent(username, cal.getId(), calE, true);
228     
229     String eventURI = "/cs/calendar/geteventbyid/" + calE.getId();
230     ByteArrayContainerResponseWriter writer = new ByteArrayContainerResponseWriter();
231     ContainerResponse response = service("GET", eventURI, baseURI, h, null, writer);
232     
233     assertNotNull(response);
234     assertEquals(HTTPStatus.OK, response.getStatus());
235     
236     //deleteData(username, calCate.getId());
237   }
238   
239   public void testGetOccurrence() throws Exception {
240     //create/get calendar in private folder
241     Calendar cal = createCalendar("myCalendar");
242 
243     String extURI = "/cs/calendar/subscribe/" + username + "/" + cal.getId() + "/0";
244 
245     cal.setPublicUrl(extURI);
246 
247     calendarService.saveUserCalendar(username, cal, true);
248 
249     java.util.Calendar fromTime = java.util.Calendar.getInstance();
250     java.util.Calendar toTime = java.util.Calendar.getInstance();
251     toTime.add(java.util.Calendar.HOUR, 1);
252     
253     CalendarEvent event = createEvent("newEvent", cal.getId(), fromTime.getTime(), toTime.getTime(), CalendarEvent.TYPE_EVENT);
254     event.setRepeatType(CalendarEvent.RP_DAILY);
255     calendarService.saveUserEvent(username, cal.getId(), event, true);
256 
257     SimpleDateFormat sdf = new SimpleDateFormat(Utils.DATE_FORMAT_RECUR_ID);
258     sdf.setTimeZone(TimeZone.getDefault());
259     String recurId = sdf.format(fromTime.getTime());
260 
261     String eventURI = "/cs/calendar/getoccurrence/" + event.getId() + "/" + recurId;
262 
263     ByteArrayContainerResponseWriter writer = new ByteArrayContainerResponseWriter();
264     ContainerResponse response = service("GET", eventURI, baseURI, h, null, writer);
265 
266     assertNotNull(response);
267     assertEquals(HTTPStatus.OK, response.getStatus());
268   }
269   
270   public void testGetCalendars() throws Exception {
271 
272     //create/get calendar in private folder
273     Calendar cal = createCalendar("myCalendar");
274      
275     String extURI = "/cs/calendar/subscribe/" + username + "/" + cal.getId() + "/0";
276 
277     cal.setPublicUrl(extURI);
278     
279     calendarService.saveUserCalendar(username, cal, true);
280     
281     String eventURI = "/cs/calendar/getcalendars/";
282     ByteArrayContainerResponseWriter writer = new ByteArrayContainerResponseWriter();
283     ContainerResponse response = service("GET", eventURI, baseURI, h, null, writer);
284 
285     assertNotNull(response);
286     assertEquals(HTTPStatus.OK, response.getStatus());
287     
288     //deleteData(username, calCate.getId());
289   }
290   
291   private void login() {    
292     login(username, "member:/platform/users");
293   }
294 }