details: https://code.openbravo.com/erp/devel/pi/rev/761ac217cc3a changeset: 32022:761ac217cc3a user: Asier Lostalé <asier.lostale <at> openbravo.com> date: Thu May 04 09:55:58 2017 +0200 summary: related to bug 35729: some clean up in OrgTree
1. getDescendantTreeList had 2 boolean params which were always set to true, so they have been removed 2. Removed also some code duplication details: https://code.openbravo.com/erp/devel/pi/rev/8baa2bae6ff2 changeset: 32023:8baa2bae6ff2 user: Asier Lostalé <asier.lostale <at> openbravo.com> date: Thu May 04 10:01:48 2017 +0200 summary: related to issue 35117: improved some error messages so it makes it easier to anlyze logs when tests fail diffstat: src-test/src/org/openbravo/test/datasource/DataSourceSecurity.java | 3 +- src-test/src/org/openbravo/test/datasource/TestComboDatasource.java | 46 +++--- src/org/openbravo/base/secureApp/OrgTree.java | 68 ++------- 3 files changed, 44 insertions(+), 73 deletions(-) diffs (244 lines): diff -r 6a7acea80102 -r 8baa2bae6ff2 src-test/src/org/openbravo/test/datasource/DataSourceSecurity.java --- a/src-test/src/org/openbravo/test/datasource/DataSourceSecurity.java Thu Apr 13 17:56:06 2017 -0400 +++ b/src-test/src/org/openbravo/test/datasource/DataSourceSecurity.java Thu May 04 10:01:48 2017 +0200 @@ -484,7 +484,8 @@ } else if (dataSource.operation.equals(OPERATION_UPDATE)) { jsonResponse = updateDataSource(); } - assertThat("Request status", jsonResponse.getInt("status"), is(expectedResponseStatus)); + assertThat("Response status for: " + jsonResponse.toString(), jsonResponse.getInt("status"), + is(expectedResponseStatus)); } private JSONObject updateDataSource() throws Exception { diff -r 6a7acea80102 -r 8baa2bae6ff2 src-test/src/org/openbravo/test/datasource/TestComboDatasource.java --- a/src-test/src/org/openbravo/test/datasource/TestComboDatasource.java Thu Apr 13 17:56:06 2017 -0400 +++ b/src-test/src/org/openbravo/test/datasource/TestComboDatasource.java Thu May 04 10:01:48 2017 +0200 @@ -11,13 +11,15 @@ * 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-2016 Openbravo SLU + * All portions are Copyright (C) 2014-2017 Openbravo SLU * All Rights Reserved. * Contributor(s): ______________________________________. ************************************************************************ */ package org.openbravo.test.datasource; +import static org.hamcrest.Matchers.is; + /** * Test cases for ComboTableDatasourceService * @@ -25,6 +27,7 @@ */ import static org.hamcrest.Matchers.isEmptyOrNullString; +import static org.hamcrest.Matchers.not; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; @@ -66,8 +69,7 @@ String response = doRequest("/org.openbravo.service.datasource/ComboTableDatasourceService", params, 200, "POST"); JSONObject jsonResponse = new JSONObject(response); - assertFalse(getStatus(jsonResponse).equals( - String.valueOf(JsonConstants.RPCREQUEST_STATUS_SUCCESS))); + assertResponseStatusIsNot(jsonResponse, JsonConstants.RPCREQUEST_STATUS_SUCCESS); } /** @@ -91,8 +93,7 @@ String response = doRequest("/org.openbravo.service.datasource/ComboTableDatasourceService", params, 200, "POST"); JSONObject jsonResponse = new JSONObject(response); - assertFalse(getStatus(jsonResponse).equals( - String.valueOf(JsonConstants.RPCREQUEST_STATUS_SUCCESS))); + assertResponseStatusIsNot(jsonResponse, JsonConstants.RPCREQUEST_STATUS_SUCCESS); } /** @@ -112,8 +113,7 @@ JSONObject jsonResponse = requestCombo(params); JSONArray data = getData(jsonResponse); - assertTrue(getStatus(jsonResponse).equals( - String.valueOf(JsonConstants.RPCREQUEST_STATUS_SUCCESS))); + assertEquals("paginated combo number of records", 75, data.length()); } @@ -134,8 +134,6 @@ JSONObject jsonResponse = requestCombo(params); JSONArray data = getData(jsonResponse); - assertTrue(getStatus(jsonResponse).equals( - String.valueOf(JsonConstants.RPCREQUEST_STATUS_SUCCESS))); int totalRows = jsonResponse.getJSONObject("response").getInt("totalRows"); // 78 int endRow = jsonResponse.getJSONObject("response").getInt("endRow"); // 76 @@ -169,8 +167,7 @@ JSONObject jsonResponse = requestCombo(params); JSONArray data = getData(jsonResponse); - assertTrue(getStatus(jsonResponse).equals( - String.valueOf(JsonConstants.RPCREQUEST_STATUS_SUCCESS))); + assertEquals("number of filtered records", 3, data.length()); } @@ -197,8 +194,7 @@ JSONObject jsonResponse = requestCombo(params); JSONArray data = getData(jsonResponse); - assertTrue(getStatus(jsonResponse).equals( - String.valueOf(JsonConstants.RPCREQUEST_STATUS_SUCCESS))); + assertEquals("number of filtered records", 2, data.length()); } @@ -218,9 +214,8 @@ String response = doRequest("/org.openbravo.service.datasource/ComboTableDatasourceService", params, 200, "POST"); JSONObject jsonResponse = new JSONObject(response); - // error should be raised - assertTrue(getStatus(jsonResponse).equals( - String.valueOf(JsonConstants.RPCREQUEST_STATUS_VALIDATION_ERROR))); + + assertResponseStatusIs(jsonResponse, JsonConstants.RPCREQUEST_STATUS_VALIDATION_ERROR); } /** @@ -241,9 +236,8 @@ String response = doRequest("/org.openbravo.service.datasource/ComboTableDatasourceService", params, 200, "POST"); JSONObject jsonResponse = new JSONObject(response); - // error should be raised - assertTrue(getStatus(jsonResponse).equals( - String.valueOf(JsonConstants.RPCREQUEST_STATUS_VALIDATION_ERROR))); + + assertResponseStatusIs(jsonResponse, JsonConstants.RPCREQUEST_STATUS_VALIDATION_ERROR); } /** @@ -306,12 +300,22 @@ paramStr = "[" + paramStr + "]"; log.debug("Combo request:\n *params:{}\n *response:{}", paramStr, jsonResponse); } - assertTrue(getStatus(jsonResponse).equals( - String.valueOf(JsonConstants.RPCREQUEST_STATUS_SUCCESS))); + + assertResponseStatusIs(jsonResponse, JsonConstants.RPCREQUEST_STATUS_SUCCESS); assertNotNull("Combo response shoulnd't be null", jsonResponse.toString()); return jsonResponse; } + private void assertResponseStatusIs(JSONObject response, int status) throws JSONException { + assertThat("Response status for: " + response.toString(), getStatus(response), + is(String.valueOf(status))); + } + + private void assertResponseStatusIsNot(JSONObject response, int status) throws JSONException { + assertThat("Response status for: " + response.toString(), getStatus(response), + is(not(String.valueOf(status)))); + } + /** * * @param jsonResponse diff -r 6a7acea80102 -r 8baa2bae6ff2 src/org/openbravo/base/secureApp/OrgTree.java --- a/src/org/openbravo/base/secureApp/OrgTree.java Thu Apr 13 17:56:06 2017 -0400 +++ b/src/org/openbravo/base/secureApp/OrgTree.java Thu May 04 10:01:48 2017 +0200 @@ -267,61 +267,32 @@ return returnTree; } - /** - * List param is modified with all the child nodes, repeatNodes decides what to do in case of - * repeated nodes. - * - * @param parentNodeId - * @param list - * @param repeatNodes - */ - private void getDescendantTreeList(String parentNodeId, List<OrgTreeNode> list, - boolean repeatNodes, boolean withZero) { + /** List param is modified with all the child nodes */ + private void getDescendantTreeList(String parentNodeId, List<OrgTreeNode> list) { List<OrgTreeNode> childNodes = getNodesWithParent(parentNodeId); - if (repeatNodes) { - if (withZero || !getNodeById(parentNodeId).getId().equals("0")) { - OrgTreeNode node = getNodeById(parentNodeId); - if (node != null) { - list.add(getNodeById(parentNodeId)); - } - } - } else { - if ((list.size() == 0) && (withZero || !getNodeById(parentNodeId).getId().equals("0"))) - list.add(getNodeById(parentNodeId)); - else { - boolean exists = false; - for (int i = 0; i < list.size(); i++) - if (list.get(i).equals(parentNodeId)) - exists = true; - if ((!exists) && (withZero || !getNodeById(parentNodeId).getId().equals("0"))) - list.add(getNodeById(parentNodeId)); - } + + OrgTreeNode node = getNodeById(parentNodeId); + if (node != null) { + list.add(node); } - if (childNodes.size() != 0) - for (int i = 0; i < childNodes.size(); i++) - getDescendantTreeList(childNodes.get(i).getId(), list, repeatNodes, withZero); - } - - /** - * List param is modified with all the child nodes - * - * @param parentNodeId - * @param list - */ - private void getDescendantTreeList(String parentNodeId, List<OrgTreeNode> list) { - getDescendantTreeList(parentNodeId, list, true, true); + for (OrgTreeNode child : childNodes) { + getDescendantTreeList(child.getId(), list); + } } /** * Returns the node matching the id, in case it does not exists it returns null */ private OrgTreeNode getNodeById(String id) { - if (nodes == null) + if (nodes == null) { return null; - for (int i = 0; i < nodes.size(); i++) - if (nodes.get(i).equals(id)) - return nodes.get(i); + } + for (OrgTreeNode node : nodes) { + if (node.equals(id)) { + return node; + } + } return null; } @@ -329,12 +300,7 @@ * In case the node id is in the tree it returns true, if not false. */ private boolean isNodeInTree(String id) { - if (nodes == null) - return false; - for (int i = 0; i < nodes.size(); i++) - if (nodes.get(i).equals(id)) - return true; - return false; + return getNodeById(id) != null; } /** ------------------------------------------------------------------------------ Check out the vibrant tech community on one of the world's most engaging tech sites, Slashdot.org! http://sdm.link/slashdot _______________________________________________ Openbravo-commits mailing list Openbravo-commits@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/openbravo-commits