related to bug 38066: added test case
authorAsier Lostalé <asier.lostale@openbravo.com>
Thu, 08 Mar 2018 10:10:30 +0100
changeset 33652 92b12ce052c6
parent 33651 253508d68cbf
child 33653 0c460868d807
child 33654 bce7d98857dd
related to bug 38066: added test case
src-test/src/org/openbravo/test/AllWebserviceTests.java
src-test/src/org/openbravo/test/datasource/BaseDataSourceTestDal.java
src-test/src/org/openbravo/test/datasource/DatasourceTestUtil.java
src-test/src/org/openbravo/test/views/ETagGeneration.java
--- a/src-test/src/org/openbravo/test/AllWebserviceTests.java	Wed Mar 07 14:26:09 2018 +0100
+++ b/src-test/src/org/openbravo/test/AllWebserviceTests.java	Thu Mar 08 10:10:30 2018 +0100
@@ -43,6 +43,7 @@
 import org.openbravo.test.datasource.TestNoteDatasource;
 import org.openbravo.test.security.ExplicitCrossOrganizationReference;
 import org.openbravo.test.security.UserInfoSessionDataTest;
+import org.openbravo.test.views.ETagGeneration;
 import org.openbravo.test.webservice.JSONWebServices;
 import org.openbravo.test.webservice.JSONWebServicesWhereParameter;
 import org.openbravo.test.webservice.PerformanceTest;
@@ -92,7 +93,8 @@
     LinkToParentTreeDataSourceTest.class, //
     OtherDatasourceRequests.class, //
     NonIdForeignKeyFilters.class, //
-    ResetCookieOnLogin.class //
+    ResetCookieOnLogin.class, //
+    ETagGeneration.class //
 })
 public class AllWebserviceTests {
 }
--- a/src-test/src/org/openbravo/test/datasource/BaseDataSourceTestDal.java	Wed Mar 07 14:26:09 2018 +0100
+++ b/src-test/src/org/openbravo/test/datasource/BaseDataSourceTestDal.java	Thu Mar 08 10:10:30 2018 +0100
@@ -11,7 +11,7 @@
  * 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-2017 Openbravo SLU 
+ * All portions are Copyright (C) 2014-2018 Openbravo SLU 
  * All Rights Reserved. 
  * Contributor(s):  ______________________________________.
  ************************************************************************
@@ -68,15 +68,29 @@
    * Performs a request to Openbravo returning its response and asserting the response code matches
    * expectedResponse.
    */
+  /**
+   * 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,
       String contentType) throws Exception {
+    authenticate();
+
+    return DatasourceTestUtil.request(getOpenbravoURL(), wsPart, method, content, cookie, 200,
+        contentType);
+  }
+
+  /**
+   * Performs a request to authenticate with current settings if already not authenticated.
+   * 
+   * @return Sting with cookie with authenticated session id
+   * */
+  protected String authenticate() throws Exception {
     if (!authenticated) {
       cookie = DatasourceTestUtil.authenticate(getOpenbravoURL(), getLogin(), getPassword());
       authenticated = true;
     }
-
-    return DatasourceTestUtil.request(getOpenbravoURL(), wsPart, method, content, cookie, 200,
-        contentType);
+    return cookie;
   }
 
   /**
--- a/src-test/src/org/openbravo/test/datasource/DatasourceTestUtil.java	Wed Mar 07 14:26:09 2018 +0100
+++ b/src-test/src/org/openbravo/test/datasource/DatasourceTestUtil.java	Thu Mar 08 10:10:30 2018 +0100
@@ -11,7 +11,7 @@
  * 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-2015 Openbravo SLU 
+ * All portions are Copyright (C) 2014-2018 Openbravo SLU 
  * All Rights Reserved. 
  * Contributor(s):  ______________________________________.
  ************************************************************************
@@ -26,8 +26,6 @@
 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;
@@ -36,18 +34,21 @@
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import junit.framework.TestCase;
+
 /**
  * Utility methods to deal with datasource calls.
  * 
  * @author alostale
  * 
  */
-class DatasourceTestUtil {
+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 {
+  /** Creates a connection to a given URL without processing it. */
+  public 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);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src-test/src/org/openbravo/test/views/ETagGeneration.java	Thu Mar 08 10:10:30 2018 +0100
@@ -0,0 +1,113 @@
+/*
+ *************************************************************************
+ * 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) 2018 Openbravo SLU 
+ * All Rights Reserved. 
+ * Contributor(s):  ______________________________________.
+ ************************************************************************
+ */
+
+package org.openbravo.test.views;
+
+import static org.hamcrest.CoreMatchers.not;
+import static org.hamcrest.Matchers.is;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assume.assumeThat;
+
+import java.net.HttpURLConnection;
+
+import javax.servlet.http.HttpServletResponse;
+
+import org.hibernate.Query;
+import org.junit.Test;
+import org.openbravo.dal.service.OBDal;
+import org.openbravo.erpCommon.businessUtility.Preferences;
+import org.openbravo.model.ad.domain.Preference;
+import org.openbravo.model.ad.system.Client;
+import org.openbravo.model.common.enterprise.Organization;
+import org.openbravo.test.datasource.BaseDataSourceTestDal;
+import org.openbravo.test.datasource.DatasourceTestUtil;
+
+/** Test cases covering ETag management for generated views */
+public class ETagGeneration extends BaseDataSourceTestDal {
+  private static final String SALES_ORDER_WINDOW = "143";
+
+  @Test
+  public void eTagShouldBeStable() throws Exception {
+    assumeThat("Has modules in development", hasModulesInDevelopment(), is(false));
+
+    String eTag = getEtag(SALES_ORDER_WINDOW);
+
+    assertResponseCode("Response without changes", SALES_ORDER_WINDOW, eTag,
+        HttpServletResponse.SC_NOT_MODIFIED);
+  }
+
+  @Test
+  public void serverDisplayLogicConfigShouldChangeETag() throws Exception {
+    assumeThat("Has modules in development", hasModulesInDevelopment(), is(false));
+
+    Preference newPref = null;
+    setSystemAdministratorContext();
+    try {
+      String oldEtag = getEtag(SALES_ORDER_WINDOW);
+
+      if (!Preferences.existsPreference("UomManagement", true, "0", "0", null, null, null)) {
+        newPref = Preferences.setPreferenceValue("UomManagement", "Y", true, OBDal.getInstance()
+            .getProxy(Client.class, "0"), OBDal.getInstance().getProxy(Organization.class, "0"),
+            null, null, null, null);
+      }
+      OBDal.getInstance().commitAndClose();
+
+      String newEtag = getEtag(SALES_ORDER_WINDOW);
+      assertThat("ETag should change", newEtag, is(not(oldEtag)));
+
+      assertResponseCode("Response after adding server dl config", SALES_ORDER_WINDOW, oldEtag,
+          HttpServletResponse.SC_OK);
+      assertResponseCode("Response on 2nd request after change in dl server config",
+          SALES_ORDER_WINDOW, newEtag, HttpServletResponse.SC_NOT_MODIFIED);
+    } finally {
+      if (newPref != null) {
+        OBDal.getInstance().remove(newPref);
+      }
+    }
+  }
+
+  private Boolean hasModulesInDevelopment() {
+    final Query indevelMods = OBDal.getInstance().getSession()
+        .createQuery("select 1 from ADModule m where m.inDevelopment=true");
+    indevelMods.setMaxResults(1);
+    return indevelMods.list().size() > 0;
+  }
+
+  private void assertResponseCode(String msg, String windowId, String eTag, int expectedCode)
+      throws Exception {
+    HttpURLConnection conn = getConnection(windowId);
+    conn.setRequestProperty("If-None-Match", eTag);
+    conn.connect();
+
+    assertThat(msg, conn.getResponseCode(), is(expectedCode));
+  }
+
+  private String getEtag(String windowId) throws Exception {
+    HttpURLConnection conn = getConnection(windowId);
+    conn.connect();
+    return conn.getHeaderField("Etag");
+  }
+
+  private HttpURLConnection getConnection(String windowId) throws Exception {
+    String cookie = authenticate();
+    HttpURLConnection conn = DatasourceTestUtil.createConnection(getOpenbravoURL(),
+        "/org.openbravo.client.kernel/OBUIAPP_MainLayout/View?viewId=_" + windowId, "GET", cookie);
+    return conn;
+  }
+}