001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. The ASF licenses this file to You
004: * under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License. For additional information regarding
015: * copyright in this work, please see the NOTICE file in the top level
016: * directory of this distribution.
017: */
018: /*
019: * Created on Jun 15, 2004
020: */
021: package org.apache.roller.webservices.xmlrpc;
022:
023: import com.mockrunner.mock.web.MockHttpServletRequest;
024: import com.mockrunner.mock.web.MockHttpServletResponse;
025: import com.mockrunner.mock.web.MockServletContext;
026: import com.mockrunner.mock.web.WebMockObjectFactory;
027: import com.mockrunner.servlet.ServletTestModule;
028:
029: import java.sql.Timestamp;
030:
031: import java.util.ArrayList;
032: import java.util.Date;
033: import java.util.HashMap;
034: import java.util.Iterator;
035: import java.util.regex.Pattern;
036:
037: import junit.framework.Test;
038: import junit.framework.TestCase;
039: import junit.framework.TestSuite;
040:
041: import org.apache.commons.logging.Log;
042: import org.apache.commons.logging.LogFactory;
043: import org.apache.roller.TestUtils;
044: import org.apache.roller.business.RollerFactory;
045: import org.apache.roller.pojos.UserData;
046: import org.apache.roller.pojos.WeblogEntryData;
047: import org.apache.roller.pojos.WebsiteData;
048: import org.apache.roller.ui.MockRollerContext;
049: import org.apache.roller.ui.core.RollerRequest;
050: import org.apache.roller.util.RegexUtil;
051:
052: /**
053: * Makes calls to the RollerXmlRpcServer, which should handle a
054: * post just as it would with a real XML-RPC call.
055: *
056: * @author lance.lavandowska
057: */
058: public class RollerXmlRpcServerTest extends TestCase {
059: public static Log log = LogFactory
060: .getLog(RollerXmlRpcServerTest.class);
061: UserData testUser = null;
062: WebsiteData testWeblog = null;
063: private static HashMap typeMap = new HashMap();
064: static {
065: typeMap.put(Boolean.class, "boolean");
066: typeMap.put(Double.class, "double");
067: typeMap.put(Date.class, "dateTime.iso8601");
068: typeMap.put(Integer.class, "int");
069: }
070:
071: protected WebMockObjectFactory mockFactory;
072: protected MockRollerContext rollerContext;
073: protected MockHttpServletRequest mockRequest;
074: protected ServletTestModule servletTestModule;
075:
076: public void testBloggerGetRecentPosts() {
077: ArrayList params = new ArrayList();
078: params.add("roller"); // appkey
079: params.add(testWeblog.getHandle()); // blogid
080: params.add(testUser.getUserName()); // userid
081: params.add(testUser.getPassword()); // password
082: params.add(new Integer(5)); // numposts
083: String message = buildXmlRpcString("blogger.getRecentPosts",
084: params);
085:
086: mockRequest.setBodyContent(message);
087: servletTestModule.doPost();
088: MockHttpServletResponse response = mockFactory
089: .getMockResponse();
090: String responseBody = response.getOutputStreamContent();
091:
092: // assert no fault code
093: assertTrue(responseBody, responseBody
094: .indexOf("<name>faultCode</name>") == -1);
095:
096: // make sure all/any userids returned belong to our test user
097: Pattern userPattern = Pattern
098: .compile("<name>userid</name><value>(.*?)</value>");
099: ArrayList users = RegexUtil.getMatches(userPattern,
100: responseBody, 1);
101: Iterator it = users.iterator();
102: while (it.hasNext()) {
103: String user = (String) it.next();
104: //System.out.println(user);
105: if (user.equals(testUser.getUserName())) {
106: continue;
107: } else {
108: fail("getRecentPosts() returned entry for a user ["
109: + user + "] other than "
110: + testUser.getUserName());
111: }
112: }
113: }
114:
115: /**
116: * Build an XML-RPC message from methodName and params.
117: *
118: * @param methodName
119: * @param params
120: * @return
121: */
122: private String buildXmlRpcString(String methodName, ArrayList params) {
123: StringBuffer buf = new StringBuffer("<?xml version=\"1.0\"?>");
124: buf.append("<methodCall>");
125: buf.append("<methodName>").append(methodName).append(
126: "</methodName>");
127: buf.append("<params>");
128: Iterator it = params.iterator();
129: while (it.hasNext()) {
130: buf.append("<param><value>");
131: Object param = it.next();
132: String paramType = (String) typeMap.get(param.getClass());
133: if (paramType != null) {
134: buf.append("<").append(paramType).append(">").append(
135: param).append("</").append(paramType).append(
136: ">");
137: } else {
138: buf.append("<string>").append(param)
139: .append("</string>");
140: }
141: buf.append("</value></param>");
142: }
143: buf.append("</params> ");
144: buf.append("</methodCall>");
145: return buf.toString();
146: }
147:
148: //-----------------------------------------------------------------------
149:
150: /**
151: * All tests in this suite require a user and a weblog.
152: */
153: public void setUp() throws Exception {
154:
155: try {
156: mockFactory = new WebMockObjectFactory();
157:
158: // create mock RollerContext
159: MockServletContext ctx = mockFactory
160: .getMockServletContext();
161: ctx.setRealPath("/", ".");
162: rollerContext = new MockRollerContext();
163: rollerContext.init(ctx);
164:
165: mockRequest = mockFactory.getMockRequest();
166: mockRequest.setContextPath("/roller");
167: RollerRequest.getRollerRequest(mockRequest, mockFactory
168: .getMockServletContext());
169:
170: servletTestModule = new ServletTestModule(mockFactory);
171: servletTestModule.createServlet(RollerXMLRPCServlet.class);
172:
173: testUser = TestUtils.setupUser("entryTestUser");
174: testWeblog = TestUtils.setupWeblog("entryTestWeblog",
175: testUser);
176:
177: WeblogEntryData testEntry1 = new WeblogEntryData();
178: testEntry1.setTitle("entryTestEntry1");
179: testEntry1.setLink("testEntryLink1");
180: testEntry1.setText("blah blah entry1");
181: testEntry1.setAnchor("testEntryAnchor1");
182: testEntry1.setPubTime(new Timestamp(new Date().getTime()));
183: testEntry1
184: .setUpdateTime(new Timestamp(new Date().getTime()));
185: testEntry1.setWebsite(testWeblog);
186: testEntry1.setCreator(testUser);
187: testEntry1.setCategory(testWeblog.getDefaultCategory());
188: RollerFactory.getRoller().getWeblogManager()
189: .saveWeblogEntry(testEntry1);
190:
191: WeblogEntryData testEntry2 = new WeblogEntryData();
192: testEntry2.setTitle("entryTestEntry2");
193: testEntry2.setLink("testEntryLink2");
194: testEntry2.setText("blah blah entry2");
195: testEntry2.setAnchor("testEntryAnchor2");
196: testEntry2.setPubTime(new Timestamp(new Date().getTime()));
197: testEntry2
198: .setUpdateTime(new Timestamp(new Date().getTime()));
199: testEntry2.setWebsite(testWeblog);
200: testEntry2.setCreator(testUser);
201: testEntry2.setCategory(testWeblog.getDefaultCategory());
202: RollerFactory.getRoller().getWeblogManager()
203: .saveWeblogEntry(testEntry1);
204:
205: WeblogEntryData testEntry3 = new WeblogEntryData();
206: testEntry3.setTitle("entryTestEntry3");
207: testEntry3.setLink("testEntryLink3");
208: testEntry3.setText("blah blah entry3");
209: testEntry3.setAnchor("testEntryAnchor3");
210: testEntry3.setPubTime(new Timestamp(new Date().getTime()));
211: testEntry3
212: .setUpdateTime(new Timestamp(new Date().getTime()));
213: testEntry3.setWebsite(testWeblog);
214: testEntry3.setCreator(testUser);
215: testEntry3.setCategory(testWeblog.getDefaultCategory());
216: RollerFactory.getRoller().getWeblogManager()
217: .saveWeblogEntry(testEntry1);
218:
219: TestUtils.endSession(true);
220:
221: } catch (Exception ex) {
222: log.error(ex);
223: throw new Exception("Test setup failed", ex);
224: }
225: }
226:
227: public void tearDown() throws Exception {
228:
229: try {
230: TestUtils.teardownWeblog(testWeblog.getId());
231: TestUtils.teardownUser(testUser.getId());
232: TestUtils.endSession(true);
233:
234: mockRequest = null;
235: servletTestModule.clearOutput();
236: servletTestModule.releaseFilters();
237: servletTestModule = null;
238: rollerContext = null;
239: mockFactory = null;
240:
241: } catch (Exception ex) {
242: log.error(ex);
243: throw new Exception("Test teardown failed", ex);
244: }
245: }
246:
247: public static Test suite() {
248: return new TestSuite(RollerXmlRpcServerTest.class);
249: }
250: }
|