details:   https://code.openbravo.com/erp/devel/pi/rev/f11238e33374
changeset: 22817:f11238e33374
user:      Asier Lostalé <asier.lostale <at> openbravo.com>
date:      Wed Apr 02 13:48:43 2014 +0200
summary:   fixed issue 26147: Implement infrastructure to test requests

diffstat:

 src-test/org/openbravo/test/datasource/BaseDataSourceTestDal.java   |  110 
+++++++
 src-test/org/openbravo/test/datasource/BaseDataSourceTestNoDal.java |  110 
+++++++
 src-test/org/openbravo/test/datasource/DatasourceTestUtil.java      |  143 
++++++++++
 3 files changed, 363 insertions(+), 0 deletions(-)

diffs (truncated from 375 to 300 lines):

diff -r 9fa79145ed3c -r f11238e33374 
src-test/org/openbravo/test/datasource/BaseDataSourceTestDal.java
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/src-test/org/openbravo/test/datasource/BaseDataSourceTestDal.java Wed Apr 
02 13:48:43 2014 +0200
@@ -0,0 +1,110 @@
+/*
+ *************************************************************************
+ * The contents of this file are subject to the Openbravo  Public  License
+ * Version  1.1  (the  "License"),  being   the  Mozilla   Public  License
+ * Version 1.1  with a permitted attribution clause; you may not  use this
+ * file except in compliance with the License. You  may  obtain  a copy of
+ * the License at http://www.openbravo.com/legal/license.html 
+ * Software distributed under the License  is  distributed  on  an "AS IS"
+ * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
+ * License for the specific  language  governing  rights  and  limitations
+ * under the License. 
+ * The Original Code is Openbravo ERP. 
+ * The Initial Developer of the Original Code is Openbravo SLU 
+ * All portions are Copyright (C) 2014 Openbravo SLU 
+ * All Rights Reserved. 
+ * Contributor(s):  ______________________________________.
+ ************************************************************************
+ */
+
+package org.openbravo.test.datasource;
+
+import java.util.Map;
+
+import org.openbravo.test.base.BaseTest;
+
+/**
+ * Base for tests performing requests to a live Openbravo instance. Allows to 
work with DAL, in case
+ * it is not needed {@link BaseDataSourceTestNoDal} can be used instead.
+ * 
+ * NOTE FOR DEVELOPERS: {@link BaseDataSourceTestNoDal} class should be 
maintained in parallel to
+ * this one
+ * 
+ * @author alostale
+ * 
+ */
+public class BaseDataSourceTestDal extends BaseTest {
+  private static String OB_URL = null;
+  protected static final String LOGIN = "Openbravo";
+  protected static final String PWD = "openbravo";
+  private static boolean authenticated = false;
+  private static String cookie;
+
+  /**
+   * Performs a request to Openbravo returning its response and asserting the 
response code matches
+   * expectedResponse.
+   */
+  protected String doRequest(String wsPart, Map<String, String> params, int 
expectedResponse,
+      String method) throws Exception {
+    return doRequest(wsPart, DatasourceTestUtil.getParamsContent(params), 
expectedResponse, method);
+  }
+
+  /**
+   * Performs a request to Openbravo returning its response and asserting the 
response code matches
+   * expectedResponse.
+   */
+  protected String doRequest(String wsPart, String content, int 
expectedResponse, String method)
+      throws Exception {
+    if (!authenticated) {
+      cookie = DatasourceTestUtil.authenticate(getOpenbravoURL(), getLogin(), 
getPassword());
+      authenticated = true;
+    }
+
+    return DatasourceTestUtil.request(getOpenbravoURL(), wsPart, method, 
content, cookie, 200);
+  }
+
+  /**
+   * Obtains URL of Openbravo instance, by default taken from 
Openbravo.poperties context.url
+   * property
+   * 
+   * @return
+   */
+  protected String getOpenbravoURL() {
+    if (OB_URL != null) {
+      return OB_URL;
+    }
+    OB_URL = DatasourceTestUtil.getOpenbravoURL();
+    return OB_URL;
+  }
+
+  /**
+   * Returns the login used to login for the requests. The default value is 
{@link #LOGIN}
+   * 
+   * @return the login name used to login for the requests
+   */
+  protected String getLogin() {
+    return LOGIN;
+  }
+
+  /**
+   * Returns the password used to login for the requests. The default value is 
{@link #PWD}
+   * 
+   * @return the password used to login for the requests
+   */
+  protected String getPassword() {
+    return PWD;
+  }
+
+  /**
+   * Changes current session's profile
+   */
+  protected void changeProfile(String roleId, String langId, String orgId, 
String warehouseId)
+      throws Exception {
+    if (!authenticated) {
+      cookie = DatasourceTestUtil.authenticate(getOpenbravoURL(), getLogin(), 
getPassword());
+      authenticated = true;
+    }
+
+    DatasourceTestUtil.changeProfile(getOpenbravoURL(), cookie, roleId, 
langId, orgId, warehouseId);
+  }
+}
diff -r 9fa79145ed3c -r f11238e33374 
src-test/org/openbravo/test/datasource/BaseDataSourceTestNoDal.java
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/src-test/org/openbravo/test/datasource/BaseDataSourceTestNoDal.java       
Wed Apr 02 13:48:43 2014 +0200
@@ -0,0 +1,110 @@
+/*
+ *************************************************************************
+ * The contents of this file are subject to the Openbravo  Public  License
+ * Version  1.1  (the  "License"),  being   the  Mozilla   Public  License
+ * Version 1.1  with a permitted attribution clause; you may not  use this
+ * file except in compliance with the License. You  may  obtain  a copy of
+ * the License at http://www.openbravo.com/legal/license.html 
+ * Software distributed under the License  is  distributed  on  an "AS IS"
+ * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
+ * License for the specific  language  governing  rights  and  limitations
+ * under the License. 
+ * The Original Code is Openbravo ERP. 
+ * The Initial Developer of the Original Code is Openbravo SLU 
+ * All portions are Copyright (C) 2014 Openbravo SLU 
+ * All Rights Reserved. 
+ * Contributor(s):  ______________________________________.
+ ************************************************************************
+ */
+
+package org.openbravo.test.datasource;
+
+import java.util.Map;
+
+import junit.framework.TestCase;
+
+/**
+ * Base for tests performing requests to a live Openbravo instance. It doesn't 
allow to work with
+ * DAL, in case it is needed {@link BaseDataSourceTestDal} can be used instead.
+ * 
+ * NOTE FOR DEVELOPERS: {@link BaseDataSourceTestDal} class should be 
maintained in parallel to this
+ * one
+ * 
+ * @author alostale
+ * 
+ */
+public class BaseDataSourceTestNoDal extends TestCase {
+  private static String OB_URL = null;
+  protected static final String LOGIN = "Openbravo";
+  protected static final String PWD = "openbravo";
+  private static boolean authenticated = false;
+  private static String cookie;
+
+  /**
+   * Performs a request to Openbravo returning its response and asserting the 
response code matches
+   * expectedResponse.
+   */
+  protected String doRequest(String wsPart, Map<String, String> params, int 
expectedResponse,
+      String method) throws Exception {
+    return doRequest(wsPart, DatasourceTestUtil.getParamsContent(params), 
expectedResponse, method);
+  }
+
+  /**
+   * Performs a request to Openbravo returning its response and asserting the 
response code matches
+   * expectedResponse.
+   */
+  protected String doRequest(String wsPart, String content, int 
expectedResponse, String method)
+      throws Exception {
+    if (!authenticated) {
+      cookie = DatasourceTestUtil.authenticate(getOpenbravoURL(), getLogin(), 
getPassword());
+      authenticated = true;
+    }
+
+    return DatasourceTestUtil.request(getOpenbravoURL(), wsPart, method, 
content, cookie, 200);
+  }
+
+  /**
+   * Obtains URL of Openbravo instance, by default taken from 
Openbravo.poperties context.url
+   * property
+   * 
+   * @return
+   */
+  protected String getOpenbravoURL() {
+    if (OB_URL != null) {
+      return OB_URL;
+    }
+    OB_URL = DatasourceTestUtil.getOpenbravoURL();
+    return OB_URL;
+  }
+
+  /**
+   * Returns the login used to login for the requests. The default value is 
{@link #LOGIN}
+   * 
+   * @return the login name used to login for the requests
+   */
+  protected String getLogin() {
+    return LOGIN;
+  }
+
+  /**
+   * Returns the password used to login for the requests. The default value is 
{@link #PWD}
+   * 
+   * @return the password used to login for the requests
+   */
+  protected String getPassword() {
+    return PWD;
+  }
+
+  /**
+   * Changes current session's profile
+   */
+  protected void changeProfile(String roleId, String langId, String orgId, 
String warehouseId)
+      throws Exception {
+    if (!authenticated) {
+      cookie = DatasourceTestUtil.authenticate(getOpenbravoURL(), getLogin(), 
getPassword());
+      authenticated = true;
+    }
+
+    DatasourceTestUtil.changeProfile(getOpenbravoURL(), cookie, roleId, 
langId, orgId, warehouseId);
+  }
+}
diff -r 9fa79145ed3c -r f11238e33374 
src-test/org/openbravo/test/datasource/DatasourceTestUtil.java
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/src-test/org/openbravo/test/datasource/DatasourceTestUtil.java    Wed Apr 
02 13:48:43 2014 +0200
@@ -0,0 +1,143 @@
+/*
+ *************************************************************************
+ * The contents of this file are subject to the Openbravo  Public  License
+ * Version  1.1  (the  "License"),  being   the  Mozilla   Public  License
+ * Version 1.1  with a permitted attribution clause; you may not  use this
+ * file except in compliance with the License. You  may  obtain  a copy of
+ * the License at http://www.openbravo.com/legal/license.html 
+ * Software distributed under the License  is  distributed  on  an "AS IS"
+ * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
+ * License for the specific  language  governing  rights  and  limitations
+ * under the License. 
+ * The Original Code is Openbravo ERP. 
+ * The Initial Developer of the Original Code is Openbravo SLU 
+ * All portions are Copyright (C) 2014 Openbravo SLU 
+ * All Rights Reserved. 
+ * Contributor(s):  ______________________________________.
+ ************************************************************************
+ */
+
+package org.openbravo.test.datasource;
+
+import java.io.OutputStream;
+import java.io.StringWriter;
+import java.net.HttpURLConnection;
+import java.net.URL;
+import java.util.Map;
+import java.util.Properties;
+
+import junit.framework.TestCase;
+
+import org.apache.commons.io.IOUtils;
+import org.apache.commons.lang.StringUtils;
+import org.codehaus.jettison.json.JSONObject;
+import org.openbravo.base.exception.OBException;
+import org.openbravo.base.session.OBPropertiesProvider;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Utility methods to deal with datasource calls.
+ * 
+ * @author alostale
+ * 
+ */
+public class DatasourceTestUtil {
+  private static final Logger log = 
LoggerFactory.getLogger(DatasourceTestUtil.class);
+  private static final String CONTEXT_PROPERTY = "context.url";
+
+  static HttpURLConnection createConnection(String url, String wsPart, String 
method, String cookie)
+      throws Exception {
+
+    String completeUrl = url + wsPart;
+    log.debug("Create conntection URL: {}, method {}", completeUrl, method);
+    final URL connUrl = new URL(completeUrl);
+    final HttpURLConnection hc = (HttpURLConnection) connUrl.openConnection();
+    hc.setRequestMethod(method);
+    hc.setAllowUserInteraction(false);
+    hc.setDefaultUseCaches(false);
+    hc.setDoOutput(true);
+    hc.setDoInput(true);
+    hc.setInstanceFollowRedirects(true);
+    hc.setUseCaches(false);
+    if (cookie != null) {
+      hc.setRequestProperty("Cookie", cookie);
+    }
+    return hc;
+  }
+

------------------------------------------------------------------------------
_______________________________________________
Openbravo-commits mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/openbravo-commits

Reply via email to