/* * DavMail POP/IMAP/SMTP/CalDav/LDAP Exchange Gateway * Copyright (C) 2010 Mickael Guessant * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ package davmail.caldav; import davmail.AbstractDavMailTestCase; import davmail.DavGateway; import davmail.Settings; import davmail.exchange.ExchangeSession; import davmail.exchange.ExchangeSessionFactory; import davmail.util.StringUtil; import org.apache.commons.httpclient.*; import org.apache.commons.httpclient.auth.AuthScope; import org.apache.commons.httpclient.methods.GetMethod; import org.apache.commons.httpclient.methods.RequestEntity; import org.apache.commons.httpclient.util.URIUtil; import org.apache.jackrabbit.webdav.DavException; import org.apache.jackrabbit.webdav.MultiStatus; import org.apache.jackrabbit.webdav.MultiStatusResponse; import org.apache.jackrabbit.webdav.client.methods.DavMethodBase; import java.io.IOException; import java.io.OutputStream; import java.io.UnsupportedEncodingException; import java.text.SimpleDateFormat; import java.util.*; /** * Test Caldav listener. */ public class TestCaldav extends AbstractDavMailTestCase { class SearchReportMethod extends DavMethodBase { SearchReportMethod(String path, String stringContent) throws UnsupportedEncodingException { this(path, stringContent.getBytes("UTF-8")); } SearchReportMethod(String path, final byte[] content) { super(path); setRequestEntity(new RequestEntity() { public boolean isRepeatable() { return true; } public void writeRequest(OutputStream outputStream) throws IOException { outputStream.write(content); } public long getContentLength() { return content.length; } public String getContentType() { return "text/xml;charset=UTF-8"; } }); } @Override public String getName() { return "REPORT"; } @Override protected boolean isSuccess(int statusCode) { return statusCode == HttpStatus.SC_MULTI_STATUS; } } HttpClient httpClient; @Override public void setUp() throws IOException { super.setUp(); if (httpClient == null) { // start gateway DavGateway.start(); httpClient = new HttpClient(); HostConfiguration hostConfig = httpClient.getHostConfiguration(); URI httpURI = new URI("http://localhost:" + Settings.getProperty("davmail.caldavPort"), true); hostConfig.setHost(httpURI); AuthScope authScope = new AuthScope(null, -1); httpClient.getState().setCredentials(authScope, new NTCredentials(Settings.getProperty("davmail.username"), Settings.getProperty("davmail.password"), "", "")); } if (session == null) { session = ExchangeSessionFactory.getInstance(Settings.getProperty("davmail.username"), Settings.getProperty("davmail.password")); } } public void testGetRoot() throws IOException { GetMethod method = new GetMethod("/"); httpClient.executeMethod(method); assertEquals(HttpStatus.SC_OK, method.getStatusCode()); } public void testGetUserRoot() throws IOException { GetMethod method = new GetMethod("/users/" + session.getEmail() + '/'); httpClient.executeMethod(method); assertEquals(HttpStatus.SC_OK, method.getStatusCode()); } public void testGetCalendar() throws IOException { GetMethod method = new GetMethod("/users/" + session.getEmail() + "/calendar/"); httpClient.executeMethod(method); assertEquals(HttpStatus.SC_OK, method.getStatusCode()); } public void testReportCalendar() throws IOException, DavException { SimpleDateFormat formatter = ExchangeSession.getZuluDateFormat(); Calendar cal = Calendar.getInstance(); Date end = cal.getTime(); cal.add(Calendar.MONTH, -1); Date start = cal.getTime(); StringBuilder buffer = new StringBuilder(); buffer.append(""); buffer.append(""); buffer.append(""); buffer.append(""); buffer.append(""); buffer.append(""); buffer.append(""); buffer.append(""); buffer.append(""); buffer.append(""); buffer.append(""); buffer.append(""); buffer.append(""); SearchReportMethod method = new SearchReportMethod("/users/" + session.getEmail() + "/calendar/",buffer.toString()); httpClient.executeMethod(method); assertEquals(HttpStatus.SC_MULTI_STATUS, method.getStatusCode()); MultiStatus multiStatus = method.getResponseBodyAsMultiStatus(); MultiStatusResponse[] responses = multiStatus.getResponses(); Set ITEM_PROPERTIES = new HashSet(); ITEM_PROPERTIES.add("instancetype"); List events = session.searchEvents("/users/" + session.getEmail() + "/calendar/", ITEM_PROPERTIES, session.and( session.and( session.gt("dtstart", session.formatSearchDate(start)), session.lt("dtend", session.formatSearchDate(end)) ) , session.or(session.isEqualTo("instancetype", 1), session.isEqualTo("instancetype", 0)) ) ); assertEquals(events.size(), responses.length); } public void testCreateCalendar() throws IOException { String folderName = "test & accentué"; String encodedFolderpath = URIUtil.encodePath("/users/" + session.getEmail() + "/calendar/"+folderName+ '/'); // first delete calendar session.deleteFolder("calendar/"+folderName); String body = "\n" + " \n" + " \n" + " \n" + " "+ StringUtil.xmlEncode(folderName)+"\n" + " Calendar description\n" + " \n" + " \n" + " \n" + " \n" + " \n" + " "; SearchReportMethod method = new SearchReportMethod(encodedFolderpath, body) { @Override public String getName() { return "MKCALENDAR"; } }; httpClient.executeMethod(method); assertEquals(HttpStatus.SC_CREATED, method.getStatusCode()); GetMethod getMethod = new GetMethod(encodedFolderpath); httpClient.executeMethod(getMethod); assertEquals(HttpStatus.SC_OK, getMethod.getStatusCode()); } }