Java tutorial
/** * This file is part of Jahia, next-generation open source CMS: * Jahia's next-generation, open source CMS stems from a widely acknowledged vision * of enterprise application convergence - web, search, document, social and portal - * unified by the simplicity of web content management. * * For more information, please visit http://www.jahia.com. * * Copyright (C) 2002-2012 Jahia Solutions Group SA. All rights reserved. * * 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. * * As a special exception to the terms and conditions of version 2.0 of * the GPL (or any later version), you may redistribute this Program in connection * with Free/Libre and Open Source Software ("FLOSS") applications as described * in Jahia's FLOSS exception. You should have received a copy of the text * describing the FLOSS exception, and it is also available here: * http://www.jahia.com/license * * Commercial and Supported Versions of the program (dual licensing): * alternatively, commercial and supported versions of the program may be used * in accordance with the terms and conditions contained in a separate * written agreement between you and Jahia Solutions Group SA. * * If you are unsure which license is appropriate for your use, * please contact the sales department at sales@jahia.com. */ package org.jahia.bin; import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.httpclient.methods.PostMethod; import org.apache.commons.httpclient.params.HttpMethodParams; import org.slf4j.Logger; import org.jahia.exceptions.JahiaException; import org.jahia.params.valves.LoginEngineAuthValveImpl; import org.jahia.services.content.JCRCallback; import org.jahia.services.content.JCRSessionFactory; import org.jahia.services.content.JCRSessionWrapper; import org.jahia.services.content.JCRTemplate; import org.jahia.test.TestHelper; import org.json.JSONArray; import org.json.JSONException; import org.junit.*; import javax.jcr.RepositoryException; import java.io.IOException; import static org.junit.Assert.assertNotNull; /** * Test of the find principal servlet. * * @author loom * Date: Jun 16, 2010 * Time: 12:03:19 PM */ public class FindPrincipalTest { private static Logger logger = org.slf4j.LoggerFactory.getLogger(FindPrincipalTest.class); private HttpClient client; private final static String TESTSITE_NAME = "findPrincipalTestSite"; private final static String SITECONTENT_ROOT_NODE = "/sites/" + TESTSITE_NAME; @BeforeClass public static void oneTimeSetUp() throws Exception { try { JCRTemplate.getInstance().doExecuteWithSystemSession(new JCRCallback<Object>() { public Object doInJCR(JCRSessionWrapper session) throws RepositoryException { try { TestHelper.createSite(TESTSITE_NAME, "localhost", TestHelper.WEB_TEMPLATES); } catch (Exception e) { logger.error("Cannot create or publish site", e); } session.save(); return null; } }); } catch (Exception ex) { logger.warn("Exception during test setUp", ex); } } @AfterClass public static void oneTimeTearDown() throws Exception { try { JCRSessionWrapper session = JCRSessionFactory.getInstance().getCurrentUserSession(); if (session.nodeExists(SITECONTENT_ROOT_NODE)) { TestHelper.deleteSite(TESTSITE_NAME); } session.save(); session.logout(); } catch (Exception ex) { logger.warn("Exception during test tearDown", ex); } } @Before public void setUp() throws Exception { // Create an instance of HttpClient. client = new HttpClient(); // todo we should really insert content to test the find. PostMethod loginMethod = new PostMethod(getLoginServletURL()); loginMethod.addParameter("username", "root"); loginMethod.addParameter("password", "root1234"); loginMethod.addParameter("redirectActive", "false"); // the next parameter is required to properly activate the valve check. loginMethod.addParameter(LoginEngineAuthValveImpl.LOGIN_TAG_PARAMETER, "1"); int statusCode = client.executeMethod(loginMethod); if (statusCode != HttpStatus.SC_OK) { logger.error("Method failed: " + loginMethod.getStatusLine()); } } @After public void tearDown() throws Exception { PostMethod logoutMethod = new PostMethod(getLogoutServletURL()); logoutMethod.addParameter("redirectActive", "false"); int statusCode = client.executeMethod(logoutMethod); if (statusCode != HttpStatus.SC_OK) { logger.error("Method failed: " + logoutMethod.getStatusLine()); } logoutMethod.releaseConnection(); } @Test public void testFindUsers() throws IOException, JSONException, JahiaException { PostMethod method = new PostMethod(getFindPrincipalServletURL()); method.addParameter("principalType", "users"); method.addParameter("wildcardTerm", "*root*"); // Provide custom retry handler is necessary method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false)); // Execute the method. int statusCode = client.executeMethod(method); if (statusCode != HttpStatus.SC_OK) { logger.error("Method failed: " + method.getStatusLine()); } // Read the response body. String responseBody = method.getResponseBodyAsString(); JSONArray jsonResults = new JSONArray(responseBody); assertNotNull("A proper JSONObject instance was expected, got null instead", jsonResults); // @todo we need to add more tests to validate results. } @Test public void testFindGroups() throws IOException, JSONException { PostMethod method = new PostMethod(getFindPrincipalServletURL()); method.addParameter("principalType", "groups"); method.addParameter("siteKey", TESTSITE_NAME); method.addParameter("wildcardTerm", "*administrators*"); // Provide custom retry handler is necessary method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false)); // Execute the method. int statusCode = client.executeMethod(method); if (statusCode != HttpStatus.SC_OK) { logger.error("Method failed: " + method.getStatusLine()); } // Read the response body. String responseBody = method.getResponseBodyAsString(); JSONArray jsonResults = new JSONArray(responseBody); assertNotNull("A proper JSONObject instance was expected, got null instead", jsonResults); // @todo we need to add more tests to validate results. } private String getBaseServerURL() { return "http://localhost:8080"; } private String getLoginServletURL() { return getBaseServerURL() + Jahia.getContextPath() + "/cms/login"; } private String getLogoutServletURL() { return getBaseServerURL() + Jahia.getContextPath() + "/cms/logout"; } private String getFindPrincipalServletURL() { return getBaseServerURL() + Jahia.getContextPath() + FindPrincipal.getFindPrincipalServletPath(); } }