You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lens.apache.org by ra...@apache.org on 2015/09/16 15:40:26 UTC
lens git commit: LENS-733 : Add Helper Method for lens-regression.
Repository: lens
Updated Branches:
refs/heads/master c8701a38d -> 7678a63bc
LENS-733 : Add Helper Method for lens-regression.
Project: http://git-wip-us.apache.org/repos/asf/lens/repo
Commit: http://git-wip-us.apache.org/repos/asf/lens/commit/7678a63b
Tree: http://git-wip-us.apache.org/repos/asf/lens/tree/7678a63b
Diff: http://git-wip-us.apache.org/repos/asf/lens/diff/7678a63b
Branch: refs/heads/master
Commit: 7678a63bcd41884437dabce3c47110d9876557f8
Parents: c8701a3
Author: Arshad Matin <ar...@apache.org>
Authored: Wed Sep 16 19:10:05 2015 +0530
Committer: Raghavendra Singh <ra...@apache.org>
Committed: Wed Sep 16 19:10:05 2015 +0530
----------------------------------------------------------------------
lens-regression/pom.xml | 4 +
.../core/constants/QueryInventory.java | 57 +++++
.../core/helpers/LensServerHelper.java | 5 -
.../core/helpers/MetastoreHelper.java | 218 +++++++++++++++++-
.../regression/core/helpers/QueryHelper.java | 81 +++----
.../core/helpers/ServiceManagerHelper.java | 8 +-
.../regression/core/helpers/SessionHelper.java | 4 -
.../core/testHelper/BaseTestClass.java | 41 ++++
.../apache/lens/regression/util/AssertUtil.java | 62 +++++-
.../apache/lens/regression/util/HadoopUtil.java | 61 +++++
.../org/apache/lens/regression/util/Util.java | 220 ++++++++++++++++++-
11 files changed, 675 insertions(+), 86 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/lens/blob/7678a63b/lens-regression/pom.xml
----------------------------------------------------------------------
diff --git a/lens-regression/pom.xml b/lens-regression/pom.xml
index 250d008..560b4df 100644
--- a/lens-regression/pom.xml
+++ b/lens-regression/pom.xml
@@ -63,6 +63,10 @@
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-multipart</artifactId>
</dependency>
+ <dependency>
+ <groupId>org.apache.hadoop</groupId>
+ <artifactId>hadoop-common</artifactId>
+ </dependency>
</dependencies>
http://git-wip-us.apache.org/repos/asf/lens/blob/7678a63b/lens-regression/src/main/java/org/apache/lens/regression/core/constants/QueryInventory.java
----------------------------------------------------------------------
diff --git a/lens-regression/src/main/java/org/apache/lens/regression/core/constants/QueryInventory.java b/lens-regression/src/main/java/org/apache/lens/regression/core/constants/QueryInventory.java
new file mode 100644
index 0000000..e41ca86
--- /dev/null
+++ b/lens-regression/src/main/java/org/apache/lens/regression/core/constants/QueryInventory.java
@@ -0,0 +1,57 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.lens.regression.core.constants;
+
+public class QueryInventory {
+
+ private QueryInventory() {
+
+ }
+
+ public static final String QUERY = "cube select id,name from sample_dim where name != 'first'";
+ public static final String WRONG_QUERY = "cube select NO_ID from sample_dim where name != 'first'";
+
+ public static final String DIM_QUERY = "cube select id,name from sample_dim where name != 'first'";
+ public static final String CUBE_QUERY = "cube select sample_dim.name, measure4 from sample_cube where "
+ + "time_range_in(dt, '2014-06-24-23', '2014-06-25-00')";
+ public static final String WRONG_DIM_QUERY = "cube select NO_ID from sample_dim where name != 'first'";
+
+ public static final String HIVE_DIM_QUERY = "cube select id,name from sample_dim2 where name != 'first'";
+ public static final String HIVE_CUBE_QUERY = "cube select sample_dim.name, measure4 from sample_cube where "
+ + "time_range_in(dt, '2014-06-24-23', '2014-06-25-00')";
+
+ public static final String JDBC_CUBE_QUERY = "cube select product_id from sales where "
+ + "time_range_in(delivery_time,'2015-04-12','2015-04-13')";
+ public static final String WRONG_JDBC_CUBE_QUERY = "cube select product_id from sales where "
+ + "time_range_in(delivery_time,'2015-04-12','2015-04-14')";
+
+ public static final String WRONG_HIVE_DIM_QUERY = "cube select NO_ID from sample_dim2 where name != 'first'";
+ public static final String WRONG_HIVE_CUBE_QUERY="cube select sample_dim.name, measure4 from sample_cube where "
+ + "time_range_in(dt, '2014-07-01-00', '2014-07-25-05')";
+
+ public static final String WRONG_SYNTAX_QUERY="cube select id,name from sample_dim2 name != 'first'";
+
+ public static final String JDBC_DIM_QUERY = "cube select id,name from sample_db_dim where name != 'first'";
+ public static final String WRONG_JDBC_DIM_QUERY = "cube select NO_ID from sample_db_dim where name != 'first'";
+
+ public static final String SLEEP_FUNCTION = "CREATE TEMPORARY FUNCTION sleep AS 'hive.udf.SampleUdf'";
+ public static final String SLEEP_QUERY = "cube select sleep(name) from sample_dim where name != 'first'";
+
+}
http://git-wip-us.apache.org/repos/asf/lens/blob/7678a63b/lens-regression/src/main/java/org/apache/lens/regression/core/helpers/LensServerHelper.java
----------------------------------------------------------------------
diff --git a/lens-regression/src/main/java/org/apache/lens/regression/core/helpers/LensServerHelper.java b/lens-regression/src/main/java/org/apache/lens/regression/core/helpers/LensServerHelper.java
index 8b41dba..760c088 100644
--- a/lens-regression/src/main/java/org/apache/lens/regression/core/helpers/LensServerHelper.java
+++ b/lens-regression/src/main/java/org/apache/lens/regression/core/helpers/LensServerHelper.java
@@ -21,9 +21,7 @@ package org.apache.lens.regression.core.helpers;
import java.io.IOException;
-import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Response;
-
import javax.xml.bind.JAXBException;
import org.apache.lens.regression.util.AssertUtil;
@@ -37,9 +35,6 @@ import lombok.extern.slf4j.Slf4j;
@Slf4j
public class LensServerHelper extends ServiceManagerHelper {
- private WebTarget servLens = ServiceManagerHelper.getServerLens();
- private String sessionHandleString = ServiceManagerHelper.getSessionHandle();
-
public LensServerHelper() {
}
http://git-wip-us.apache.org/repos/asf/lens/blob/7678a63b/lens-regression/src/main/java/org/apache/lens/regression/core/helpers/MetastoreHelper.java
----------------------------------------------------------------------
diff --git a/lens-regression/src/main/java/org/apache/lens/regression/core/helpers/MetastoreHelper.java b/lens-regression/src/main/java/org/apache/lens/regression/core/helpers/MetastoreHelper.java
index aff0e8e..f60ecef 100644
--- a/lens-regression/src/main/java/org/apache/lens/regression/core/helpers/MetastoreHelper.java
+++ b/lens-regression/src/main/java/org/apache/lens/regression/core/helpers/MetastoreHelper.java
@@ -19,17 +19,20 @@
package org.apache.lens.regression.core.helpers;
-
-import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
+
import javax.xml.bind.JAXBException;
+import org.apache.lens.api.DateTime;
import org.apache.lens.api.StringList;
+import org.apache.lens.api.metastore.*;
import org.apache.lens.regression.core.constants.MetastoreURL;
+import org.apache.lens.regression.core.type.FormBuilder;
import org.apache.lens.regression.core.type.MapBuilder;
import org.apache.lens.regression.util.AssertUtil;
+import org.apache.lens.regression.util.Util;
import org.apache.lens.server.api.error.LensException;
import lombok.extern.slf4j.Slf4j;
@@ -37,8 +40,6 @@ import lombok.extern.slf4j.Slf4j;
@Slf4j
public class MetastoreHelper extends ServiceManagerHelper {
- private WebTarget servLens = ServiceManagerHelper.getServerLens();
- private String sessionHandleString = ServiceManagerHelper.getSessionHandle();
public MetastoreHelper() {
}
@@ -149,4 +150,213 @@ public class MetastoreHelper extends ServiceManagerHelper {
dropDatabase(dbName, sessionHandleString);
}
+ public void createStorage(XStorage storage, String sessionHandleString) throws Exception {
+
+ String storageString = Util.convertObjectToXml(storage, XStorage.class, "createXStorage");
+ MapBuilder query = new MapBuilder("sessionid", sessionHandleString);
+
+ Response response = this
+ .exec("post", MetastoreURL.METASTORE_STORAGES_URL, servLens, null, query, MediaType.APPLICATION_XML_TYPE, null,
+ storageString);
+ AssertUtil.assertSucceeded(response);
+ }
+
+ public void createStorage(XStorage storage) throws Exception {
+ createStorage(storage, sessionHandleString);
+ }
+
+ public void dropStorage(String storageName, String sessionHandleString) throws JAXBException, LensException {
+ MapBuilder query = new MapBuilder("sessionid", sessionHandleString);
+ Response response = this
+ .exec("delete", MetastoreURL.METASTORE_STORAGES_URL + "/" + storageName, servLens, null, query,
+ MediaType.APPLICATION_XML_TYPE, null);
+ AssertUtil.assertSucceeded(response);
+ }
+
+ public void dropStorage(String storageName) throws JAXBException, LensException {
+ dropStorage(storageName, sessionHandleString);
+ }
+
+ public StringList listStorages(String sessionHandleString) throws JAXBException, LensException {
+ MapBuilder query = new MapBuilder("sessionid", sessionHandleString);
+ Response response = this.exec("get", MetastoreURL.METASTORE_STORAGES_URL, servLens, null, query);
+ AssertUtil.assertSucceededResponse(response);
+ StringList cubeList = response.readEntity(StringList.class);
+ return cubeList;
+ }
+
+ public StringList listStorages() throws JAXBException, LensException {
+ return listStorages(sessionHandleString);
+ }
+
+ public void createCube(XCube cube, String sessionHandleString) throws Exception {
+ MapBuilder query = new MapBuilder("sessionid", sessionHandleString);
+ String cubeString = Util.convertObjectToXml(cube, XCube.class, "createXCube");
+ Response response = this
+ .exec("post", MetastoreURL.METASTORE_CUBES_URL, servLens, null, query, MediaType.APPLICATION_XML_TYPE, null,
+ cubeString);
+ AssertUtil.assertSucceeded(response);
+ }
+
+ public void createCube(XCube cube) throws Exception {
+ createCube(cube, sessionHandleString);
+ }
+
+ public void createFacts(XFactTable facts, String sessionHandleString) throws Exception {
+ String factString = Util.convertObjectToXml(facts, XFactTable.class, "createXFactTable");
+
+ FormBuilder formData = new FormBuilder();
+ formData.add("sessionid", sessionHandleString);
+ formData.add("fact", factString);
+
+ Response response = this
+ .exec("post", MetastoreURL.METASTORE_FACTS_URL, servLens, null, null, MediaType.MULTIPART_FORM_DATA_TYPE,
+ MediaType.APPLICATION_XML, formData.getForm());
+ AssertUtil.assertSucceeded(response);
+ }
+
+ public void createFacts(XFactTable facts) throws Exception {
+ createFacts(facts, sessionHandleString);
+ }
+
+ public StringList listCubes(String type, String sessionHandleString) throws Exception {
+ MapBuilder query = new MapBuilder("sessionid", sessionHandleString);
+ if (type != null) {
+ query.put("type", type);
+ }
+ Response response = this.exec("get", MetastoreURL.METASTORE_CUBES_URL, servLens, null, query);
+ AssertUtil.assertSucceededResponse(response);
+ StringList cubeList = response.readEntity(StringList.class);
+ return cubeList;
+ }
+
+ public StringList listCubes(String type) throws Exception {
+ return listCubes(type, sessionHandleString);
+ }
+
+ public StringList listCubes() throws Exception {
+ return listCubes(null);
+ }
+
+ public XCube getCube(String cubeName, String sessionHandleString)
+ throws InstantiationException, IllegalAccessException, JAXBException, LensException {
+ MapBuilder query = new MapBuilder("sessionid", sessionHandleString);
+ Response response = this.exec("get", MetastoreURL.METASTORE_CUBES_URL + "/" + cubeName, servLens, null, query,
+ MediaType.APPLICATION_XML_TYPE);
+ AssertUtil.assertSucceededResponse(response);
+ String responseString = response.readEntity(String.class);
+ log.info(responseString);
+ XCube cube = (XCube) Util.extractObject(responseString, XCube.class);
+ return cube;
+ }
+
+ public XCube getCube(String cubeName)
+ throws InstantiationException, IllegalAccessException, JAXBException, LensException {
+ return getCube(cubeName, sessionHandleString);
+ }
+
+ public XFactTable getFact(String factName, String sessionHandleString)
+ throws InstantiationException, IllegalAccessException, JAXBException, LensException {
+ MapBuilder query = new MapBuilder("sessionid", sessionHandleString);
+ Response response = this.exec("get", MetastoreURL.METASTORE_FACTS_URL + "/" + factName, servLens, null, query,
+ MediaType.APPLICATION_XML_TYPE);
+ AssertUtil.assertSucceededResponse(response);
+ String responseString = response.readEntity(String.class);
+ log.info(responseString);
+ XFactTable fact = (XFactTable) Util.extractObject(responseString, XFactTable.class);
+ return fact;
+ }
+
+ public XFactTable getFact(String factName)
+ throws InstantiationException, IllegalAccessException, JAXBException, LensException {
+ return getFact(factName, sessionHandleString);
+ }
+
+ public XDimension getDimension(String dimName, String sessionHandleString)
+ throws InstantiationException, IllegalAccessException, JAXBException, LensException {
+ MapBuilder query = new MapBuilder("sessionid", sessionHandleString);
+ Response response = this.exec("get", MetastoreURL.METASTORE_DIMENSIONS_URL + "/" + dimName, servLens, null, query,
+ MediaType.APPLICATION_XML_TYPE);
+ AssertUtil.assertSucceededResponse(response);
+ String responseString = response.readEntity(String.class);
+ log.info(responseString);
+ XDimension dim = (XDimension) Util.extractObject(responseString, XDimension.class);
+ return dim;
+ }
+
+ public XDimension getDimension(String dimName)
+ throws InstantiationException, IllegalAccessException, JAXBException, LensException {
+ return getDimension(dimName, sessionHandleString);
+ }
+
+ public XDimensionTable getDimensionTable(String dimName, String sessionHandleString)
+ throws InstantiationException, IllegalAccessException, JAXBException, LensException {
+ MapBuilder query = new MapBuilder("sessionid", sessionHandleString);
+ Response response = this.exec("get", MetastoreURL.METASTORE_DIMTABLES_URL + "/" + dimName, servLens, null, query,
+ MediaType.APPLICATION_XML_TYPE);
+ AssertUtil.assertSucceededResponse(response);
+ String responseString = response.readEntity(String.class);
+ log.info(responseString);
+ XDimensionTable dim = (XDimensionTable) Util.extractObject(responseString, XDimensionTable.class);
+ return dim;
+ }
+
+ public XDimensionTable getDimensionTable(String dimName)
+ throws InstantiationException, IllegalAccessException, JAXBException, LensException {
+ return getDimensionTable(dimName, sessionHandleString);
+ }
+
+ public void updateCube(XCube cube, String cubeName, String sessionHandleString) throws Exception {
+ MapBuilder query = new MapBuilder("sessionid", sessionHandleString);
+ String cubeString = Util.convertObjectToXml(cube, XCube.class, "createXCube");
+ Response response = this.exec("put", MetastoreURL.METASTORE_CUBES_URL + "/" + cubeName, servLens, null, query,
+ MediaType.APPLICATION_XML_TYPE, null, cubeString);
+ AssertUtil.assertSucceeded(response);
+ }
+
+ public void updateCube(XCube cube, String cubeName) throws Exception {
+ updateCube(cube, cubeName, sessionHandleString);
+ }
+
+ public void dropCube(String cubeName, String sessionHandleString) throws JAXBException, LensException {
+ MapBuilder query = new MapBuilder("sessionid", sessionHandleString);
+ Response response = this.exec("delete", MetastoreURL.METASTORE_CUBES_URL + "/" + cubeName, servLens, null, query,
+ MediaType.APPLICATION_XML_TYPE, null);
+ AssertUtil.assertSucceeded(response);
+ }
+
+ public void dropCube(String cubeName) throws JAXBException, LensException {
+ dropCube(cubeName, sessionHandleString);
+ }
+
+ public void createDimTable(XDimensionTable dt, String sessionHandleString) throws Exception {
+ String dimTable = Util.convertObjectToXml(dt, XDimensionTable.class, "createDimensionTable");
+
+ FormBuilder formData = new FormBuilder();
+ formData.add("sessionid", sessionHandleString);
+ formData.add("dimensionTable", dimTable);
+
+ Response response = this
+ .exec("post", MetastoreURL.METASTORE_DIMTABLES_URL, servLens, null, null, MediaType.MULTIPART_FORM_DATA_TYPE,
+ MediaType.APPLICATION_XML, formData.getForm());
+ AssertUtil.assertSucceeded(response);
+ }
+
+ public void createDimTable(XDimensionTable dt) throws Exception {
+ createDimTable(dt, sessionHandleString);
+ }
+
+ public void getLatestDate(String sessionHandleString) throws Exception {
+ MapBuilder query = new MapBuilder("sessionid", sessionHandleString);
+ query.put("timeDimension", "event_time");
+ Response response = this
+ .exec("get", "/metastore/cubes/rrcube/latestdate", servLens, null, query, MediaType.APPLICATION_XML_TYPE, null,
+ query);
+ DateTime dt = (DateTime) Util.getObject(response.readEntity(String.class), DateTime.class);
+ }
+
+ public void getLatestDate() throws Exception {
+ getLatestDate(sessionHandleString);
+ }
+
}
http://git-wip-us.apache.org/repos/asf/lens/blob/7678a63b/lens-regression/src/main/java/org/apache/lens/regression/core/helpers/QueryHelper.java
----------------------------------------------------------------------
diff --git a/lens-regression/src/main/java/org/apache/lens/regression/core/helpers/QueryHelper.java b/lens-regression/src/main/java/org/apache/lens/regression/core/helpers/QueryHelper.java
index 70cad28..37fb703 100644
--- a/lens-regression/src/main/java/org/apache/lens/regression/core/helpers/QueryHelper.java
+++ b/lens-regression/src/main/java/org/apache/lens/regression/core/helpers/QueryHelper.java
@@ -21,16 +21,13 @@ package org.apache.lens.regression.core.helpers;
import java.util.List;
-import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
-
import javax.xml.bind.JAXBException;
import org.apache.lens.api.LensConf;
import org.apache.lens.api.query.*;
import org.apache.lens.api.result.LensAPIResult;
-import org.apache.lens.api.result.QueryCostTO;
import org.apache.lens.regression.core.constants.QueryURL;
import org.apache.lens.regression.core.type.FormBuilder;
import org.apache.lens.regression.core.type.MapBuilder;
@@ -48,8 +45,6 @@ import lombok.extern.slf4j.Slf4j;
@Slf4j
public class QueryHelper extends ServiceManagerHelper {
- private WebTarget servLens = ServiceManagerHelper.getServerLens();
- private String sessionHandleString = ServiceManagerHelper.getSessionHandle();
public QueryHelper() {
}
@@ -58,6 +53,7 @@ public class QueryHelper extends ServiceManagerHelper {
super(envFileName);
}
+
/**
* Execute with conf
*
@@ -67,8 +63,8 @@ public class QueryHelper extends ServiceManagerHelper {
* @param conf
* @return the query Handle
*/
- public QueryHandle executeQuery(String queryString, String queryName, String sessionHandleString, String conf) throws
- InstantiationException, IllegalAccessException, JAXBException, LensException {
+ public LensAPIResult executeQuery(String queryString, String queryName, String sessionHandleString,
+ String conf) throws InstantiationException, IllegalAccessException, JAXBException, LensException {
FormBuilder formData = new FormBuilder();
formData.add("sessionid", sessionHandleString);
formData.add("query", queryString);
@@ -79,30 +75,24 @@ public class QueryHelper extends ServiceManagerHelper {
}
Response response = this.exec("post", QueryURL.QUERY_URL, servLens, null, null, MediaType.MULTIPART_FORM_DATA_TYPE,
MediaType.APPLICATION_XML, formData.getForm());
- AssertUtil.assertSucceededResponse(response);
String queryHandleString = response.readEntity(String.class);
log.info("QueryHandle String:{}", queryHandleString);
- LensAPIResult successResponse = (LensAPIResult) Util.getObject(queryHandleString, LensAPIResult.class);
- QueryHandle queryHandle = (QueryHandle) successResponse.getData();
- if (queryHandle == null) {
- throw new LensException("Query Execute Failed");
- }
- log.info("Query Handle : {}", queryHandle);
- return queryHandle;
+ LensAPIResult result = (LensAPIResult) Util.getObject(queryHandleString, LensAPIResult.class);
+ return result;
}
- public QueryHandle executeQuery(String queryString, String queryName, String sessionHandleString) throws
+ public LensAPIResult executeQuery(String queryString, String queryName, String sessionHandleString) throws
InstantiationException, IllegalAccessException, JAXBException, LensException {
return executeQuery(queryString, queryName, sessionHandleString,
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><conf />");
}
- public QueryHandle executeQuery(String queryString, String queryName) throws
+ public LensAPIResult executeQuery(String queryString, String queryName) throws
InstantiationException, IllegalAccessException, JAXBException, LensException {
return executeQuery(queryString, queryName, sessionHandleString);
}
- public QueryHandle executeQuery(String queryString) throws
+ public LensAPIResult executeQuery(String queryString) throws
InstantiationException, IllegalAccessException, JAXBException, LensException {
return executeQuery(queryString, null);
}
@@ -118,7 +108,7 @@ public class QueryHelper extends ServiceManagerHelper {
* @return the queryHandleWithResultSet
*/
- public QueryHandleWithResultSet executeQueryTimeout(String queryString, String timeout, String queryName,
+ public LensAPIResult executeQueryTimeout(String queryString, String timeout, String queryName,
String sessionHandleString, String conf) throws InstantiationException, IllegalAccessException, JAXBException,
LensException {
FormBuilder formData = new FormBuilder();
@@ -134,35 +124,29 @@ public class QueryHelper extends ServiceManagerHelper {
}
Response response = this.exec("post", QueryURL.QUERY_URL, servLens, null, null, MediaType.MULTIPART_FORM_DATA_TYPE,
MediaType.APPLICATION_XML, formData.getForm());
- AssertUtil.assertSucceededResponse(response);
String queryHandleString = response.readEntity(String.class);
log.info("QueryHandle String:{}", queryHandleString);
- LensAPIResult successResponse = (LensAPIResult) Util.getObject(queryHandleString, LensAPIResult.class);
- QueryHandleWithResultSet queryHandleWithResultSet = (QueryHandleWithResultSet) successResponse.getData();
- if (queryHandleWithResultSet==null) {
- throw new LensException("Query Execute Failed");
- }
- log.info("Query Handle with ResultSet : {}", queryHandleWithResultSet);
- return queryHandleWithResultSet;
+ LensAPIResult result = (LensAPIResult) Util.getObject(queryHandleString, LensAPIResult.class);
+ return result;
}
- public QueryHandleWithResultSet executeQueryTimeout(String queryString, String timeout, String queryName,
+ public LensAPIResult executeQueryTimeout(String queryString, String timeout, String queryName,
String sessionHandleString) throws InstantiationException, IllegalAccessException, JAXBException, LensException {
return executeQueryTimeout(queryString, timeout, queryName, sessionHandleString,
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><conf />");
}
- public QueryHandleWithResultSet executeQueryTimeout(String queryString, String timeout, String queryName) throws
+ public LensAPIResult executeQueryTimeout(String queryString, String timeout, String queryName) throws
InstantiationException, IllegalAccessException, JAXBException, LensException {
return executeQueryTimeout(queryString, timeout, queryName, sessionHandleString);
}
- public QueryHandleWithResultSet executeQueryTimeout(String queryString, String timeout) throws
+ public LensAPIResult executeQueryTimeout(String queryString, String timeout) throws
InstantiationException, IllegalAccessException, JAXBException, LensException {
return executeQueryTimeout(queryString, timeout, null);
}
- public QueryHandleWithResultSet executeQueryTimeout(String queryString) throws
+ public LensAPIResult executeQueryTimeout(String queryString) throws
InstantiationException, IllegalAccessException, JAXBException, LensException {
return executeQueryTimeout(queryString, null);
}
@@ -178,7 +162,7 @@ public class QueryHelper extends ServiceManagerHelper {
* @return the query Handle
*/
- public QueryHandle executeQuery(String queryString, String queryName, String user, String sessionHandleString,
+ public LensAPIResult executeQuery(String queryString, String queryName, String user, String sessionHandleString,
LensConf conf) throws JAXBException, InstantiationException, IllegalAccessException, LensException {
FormBuilder formData = new FormBuilder();
@@ -196,12 +180,10 @@ public class QueryHelper extends ServiceManagerHelper {
MediaType.APPLICATION_XML_TYPE));
Response response = this.exec("post", "/queryapi/queries", servLens, null, null, MediaType.MULTIPART_FORM_DATA_TYPE,
MediaType.APPLICATION_XML, formData.getForm());
- AssertUtil.assertSucceededResponse(response);
String queryHandleString = response.readEntity(String.class);
log.info("QueryHandle String:{}", queryHandleString);
- LensAPIResult successResponse = (LensAPIResult) Util.getObject(queryHandleString, LensAPIResult.class);
- QueryHandle queryHandle = (QueryHandle) successResponse.getData();
- return queryHandle;
+ LensAPIResult result = (LensAPIResult) Util.getObject(queryHandleString, LensAPIResult.class);
+ return result;
}
/**
@@ -213,7 +195,7 @@ public class QueryHelper extends ServiceManagerHelper {
* @return the query Plan
*/
- public QueryPlan explainQuery(String queryString, String sessionHandleString, String conf) throws
+ public LensAPIResult explainQuery(String queryString, String sessionHandleString, String conf) throws
JAXBException, InstantiationException, IllegalAccessException, LensException {
FormBuilder formData = new FormBuilder();
formData.add("sessionid", sessionHandleString);
@@ -224,21 +206,19 @@ public class QueryHelper extends ServiceManagerHelper {
MediaType.APPLICATION_XML_TYPE));
Response response = this.exec("post", "/queryapi/queries", servLens, null, null, MediaType.MULTIPART_FORM_DATA_TYPE,
MediaType.APPLICATION_XML, formData.getForm());
- AssertUtil.assertSucceededResponse(response);
String queryPlanString = response.readEntity(String.class);
log.info("QueryPlan String:{}", queryPlanString);
- LensAPIResult successResponse = (LensAPIResult) Util.getObject(queryPlanString, LensAPIResult.class);
- QueryPlan queryPlan = (QueryPlan) successResponse.getData();
- return queryPlan;
+ LensAPIResult result = (LensAPIResult) Util.getObject(queryPlanString, LensAPIResult.class);
+ return result;
}
- public QueryPlan explainQuery(String queryString, String sessionHandleString) throws
+ public LensAPIResult explainQuery(String queryString, String sessionHandleString) throws
JAXBException, InstantiationException, IllegalAccessException, LensException {
return explainQuery(queryString, sessionHandleString,
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><conf />");
}
- public QueryPlan explainQuery(String queryString) throws
+ public LensAPIResult explainQuery(String queryString) throws
JAXBException, InstantiationException, IllegalAccessException, LensException {
return explainQuery(queryString, sessionHandleString);
}
@@ -252,7 +232,7 @@ public class QueryHelper extends ServiceManagerHelper {
* @return the Estimate result
*/
- public QueryCostTO estimateQuery(String queryString, String sessionHandleString, String conf) throws
+ public LensAPIResult estimateQuery(String queryString, String sessionHandleString, String conf) throws
InstantiationException, IllegalAccessException, JAXBException, LensException {
FormBuilder formData = new FormBuilder();
formData.add("sessionid", sessionHandleString);
@@ -261,24 +241,19 @@ public class QueryHelper extends ServiceManagerHelper {
formData.add("conf", conf);
Response response = this.exec("post", QueryURL.QUERY_URL, servLens, null, null, MediaType.MULTIPART_FORM_DATA_TYPE,
MediaType.APPLICATION_XML, formData.getForm());
- AssertUtil.assertSucceededResponse(response);
String queryCostString = response.readEntity(String.class);
log.info("QueryCost String:{}", queryCostString);
- LensAPIResult successResponse = (LensAPIResult) Util.getObject(queryCostString, LensAPIResult.class);
- QueryCostTO queryCostTO = (QueryCostTO) successResponse.getData();
- if (queryCostTO == null) {
- throw new LensException("Estimate Failed");
- }
- return queryCostTO;
+ LensAPIResult result = (LensAPIResult) Util.getObject(queryCostString, LensAPIResult.class);
+ return result;
}
- public QueryCostTO estimateQuery(String queryString, String sessionHandleString) throws
+ public LensAPIResult estimateQuery(String queryString, String sessionHandleString) throws
InstantiationException, IllegalAccessException, JAXBException, LensException {
return estimateQuery(queryString, sessionHandleString,
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><conf />");
}
- public QueryCostTO estimateQuery(String queryString) throws
+ public LensAPIResult estimateQuery(String queryString) throws
InstantiationException, IllegalAccessException, JAXBException, LensException {
return estimateQuery(queryString, sessionHandleString);
}
http://git-wip-us.apache.org/repos/asf/lens/blob/7678a63b/lens-regression/src/main/java/org/apache/lens/regression/core/helpers/ServiceManagerHelper.java
----------------------------------------------------------------------
diff --git a/lens-regression/src/main/java/org/apache/lens/regression/core/helpers/ServiceManagerHelper.java b/lens-regression/src/main/java/org/apache/lens/regression/core/helpers/ServiceManagerHelper.java
index b14b00b..feddb0f 100644
--- a/lens-regression/src/main/java/org/apache/lens/regression/core/helpers/ServiceManagerHelper.java
+++ b/lens-regression/src/main/java/org/apache/lens/regression/core/helpers/ServiceManagerHelper.java
@@ -57,8 +57,8 @@ public abstract class ServiceManagerHelper {
private static final String LENS_SERVER_HDFS_URL = "lens.server.hdfsurl";
private static final String LENS_CURRENT_DB = "lens.server.currentDB";
- private static String sessionHandleString;
- private static WebTarget servLens;
+ protected static String sessionHandleString;
+ protected static WebTarget servLens;
protected String baseUrl;
protected String adminUrl;
@@ -163,10 +163,10 @@ public abstract class ServiceManagerHelper {
MapBuilder query = new MapBuilder("sessionid", sessionHandleString);
Response response = this.exec("delete", SessionURL.SESSION_BASE_URL, ServiceManagerHelper.servLens, null, query);
APIResult result = response.readEntity(APIResult.class);
- if (result.getStatus() == APIResult.Status.SUCCEEDED) {
+ if (result.getStatus() != APIResult.Status.SUCCEEDED) {
throw new LensException("Status should be SUCCEEDED");
}
- if (response.getStatus() == 200) {
+ if (response.getStatus() != 200) {
throw new LensException("Status code should be 200");
}
if (result.getMessage() == null) {
http://git-wip-us.apache.org/repos/asf/lens/blob/7678a63b/lens-regression/src/main/java/org/apache/lens/regression/core/helpers/SessionHelper.java
----------------------------------------------------------------------
diff --git a/lens-regression/src/main/java/org/apache/lens/regression/core/helpers/SessionHelper.java b/lens-regression/src/main/java/org/apache/lens/regression/core/helpers/SessionHelper.java
index e7bc700..3c640e2 100644
--- a/lens-regression/src/main/java/org/apache/lens/regression/core/helpers/SessionHelper.java
+++ b/lens-regression/src/main/java/org/apache/lens/regression/core/helpers/SessionHelper.java
@@ -22,10 +22,8 @@ package org.apache.lens.regression.core.helpers;
import java.util.HashMap;
import java.util.Map;
-import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
-
import javax.xml.bind.JAXBException;
import org.apache.lens.api.APIResult;
@@ -40,8 +38,6 @@ import lombok.extern.slf4j.Slf4j;
@Slf4j
public class SessionHelper extends ServiceManagerHelper {
- private WebTarget servLens = ServiceManagerHelper.getServerLens();
- private String sessionHandleString = ServiceManagerHelper.getSessionHandle();
public SessionHelper() {
}
http://git-wip-us.apache.org/repos/asf/lens/blob/7678a63b/lens-regression/src/main/java/org/apache/lens/regression/core/testHelper/BaseTestClass.java
----------------------------------------------------------------------
diff --git a/lens-regression/src/main/java/org/apache/lens/regression/core/testHelper/BaseTestClass.java b/lens-regression/src/main/java/org/apache/lens/regression/core/testHelper/BaseTestClass.java
index 2985af4..f442983 100644
--- a/lens-regression/src/main/java/org/apache/lens/regression/core/testHelper/BaseTestClass.java
+++ b/lens-regression/src/main/java/org/apache/lens/regression/core/testHelper/BaseTestClass.java
@@ -16,8 +16,49 @@
* specific language governing permissions and limitations
* under the License.
*/
+
package org.apache.lens.regression.core.testHelper;
+import org.apache.lens.regression.core.helpers.LensHelper;
+import org.apache.lens.regression.core.helpers.LensServerHelper;
+import org.apache.lens.regression.core.helpers.MetastoreHelper;
+import org.apache.lens.regression.core.helpers.QueryHelper;
+import org.apache.lens.regression.core.helpers.SessionHelper;
+
public class BaseTestClass {
+ private LensHelper lensHelper;
+ private QueryHelper qHelper;
+ private MetastoreHelper mHelper;
+ private SessionHelper sHelper;
+ private LensServerHelper lens;
+
+ public static final String LENS_PROPERTIES = "lens.properties";
+
+ public BaseTestClass() {
+
+ lensHelper = new LensHelper(LENS_PROPERTIES);
+ qHelper = lensHelper.getQueryHelper();
+ mHelper = lensHelper.getMetastoreHelper();
+ sHelper = lensHelper.getSessionHelper();
+ lens = lensHelper.getServerHelper();
+ }
+
+ public QueryHelper getQueryHelper() {
+ return qHelper;
+ }
+
+ public MetastoreHelper getMetastoreHelper() {
+ return mHelper;
+ }
+
+ public SessionHelper getSessionHelper() {
+ return sHelper;
+ }
+
+ public LensServerHelper getLensServerHelper() {
+ return lens;
+ }
+
+
}
http://git-wip-us.apache.org/repos/asf/lens/blob/7678a63b/lens-regression/src/main/java/org/apache/lens/regression/util/AssertUtil.java
----------------------------------------------------------------------
diff --git a/lens-regression/src/main/java/org/apache/lens/regression/util/AssertUtil.java b/lens-regression/src/main/java/org/apache/lens/regression/util/AssertUtil.java
index 62bd160..ea36858 100644
--- a/lens-regression/src/main/java/org/apache/lens/regression/util/AssertUtil.java
+++ b/lens-regression/src/main/java/org/apache/lens/regression/util/AssertUtil.java
@@ -22,8 +22,13 @@ package org.apache.lens.regression.util;
import javax.ws.rs.core.Response;
import org.apache.lens.api.APIResult;
+import org.apache.lens.api.result.LensAPIResult;
+import org.apache.lens.api.result.LensErrorTO;
import org.apache.lens.server.api.error.LensException;
+import lombok.extern.slf4j.Slf4j;
+
+@Slf4j
public class AssertUtil {
private AssertUtil() {
@@ -37,11 +42,11 @@ public class AssertUtil {
* @throws LensException
*/
public static void assertSucceeded(Response response) throws LensException {
- if (response.getStatus() == 200) {
+ if (response.getStatus() != 200) {
throw new LensException("Status code should be 200");
}
APIResult result = Util.getApiResult(response);
- if (result.getStatus() == APIResult.Status.SUCCEEDED) {
+ if (result.getStatus() != APIResult.Status.SUCCEEDED) {
throw new LensException("Status should be SUCCEEDED");
}
if (result.getMessage() == null) {
@@ -56,13 +61,19 @@ public class AssertUtil {
* @throws LensException
*/
public static void assertSucceededResponse(Response response) throws LensException {
- if (response.getStatus() == 200) {
+ if (response.getStatus() != 200) {
throw new LensException("Status code should be 200");
}
}
+ public static void assertSucceededResponse(Response response, int expected) throws LensException {
+ if (response.getStatus() != expected) {
+ throw new LensException("Status code should be " + expected);
+ }
+ }
+
public static void assertGoneResponse(Response response) throws LensException {
- if (response.getStatus() == 410) {
+ if (response.getStatus() != 410) {
throw new LensException("Status code should be 410");
}
}
@@ -74,7 +85,7 @@ public class AssertUtil {
* @throws LensException
*/
public static void assertFailedResponse(Response response) throws LensException {
- if (response.getStatus() == 404) {
+ if (response.getStatus() != 404) {
throw new LensException("Status code should be 404");
}
}
@@ -86,11 +97,11 @@ public class AssertUtil {
* @throws LensException
*/
public static void assertFailed(Response response) throws LensException {
- if (response.getStatus() == 400) {
+ if (response.getStatus() != 400) {
throw new LensException("Status code should be 400");
}
APIResult result = Util.getApiResult(response);
- if (result.getStatus() == APIResult.Status.FAILED) {
+ if (result.getStatus() != APIResult.Status.FAILED) {
throw new LensException("Status should be FAILED");
}
if (result.getMessage() == null) {
@@ -125,10 +136,45 @@ public class AssertUtil {
*/
public static void assertInternalServerError(Response response) throws LensException {
- if (response.getStatus() == 500) {
+ if (response.getStatus() != 500) {
throw new LensException("Status code should be 500");
}
}
+ public static void validateFailedResponse(int errorCode, String errorMessage, boolean payLoad, Response response,
+ int httpResponseCode) throws InstantiationException, IllegalAccessException, LensException {
+ String queryHandleString = response.readEntity(String.class);
+ log.info(queryHandleString);
+ @SuppressWarnings("unchecked") LensAPIResult errorResponse = (LensAPIResult) Util
+ .getObject(queryHandleString, LensAPIResult.class);
+ LensErrorTO error = errorResponse.getLensErrorTO();
+ LensErrorTO expectedError = null;
+ if (payLoad) {
+ expectedError = LensErrorTO.composedOf(errorCode, errorMessage, "nothing", error.getPayload());
+ } else {
+ expectedError = LensErrorTO.composedOf(errorCode, errorMessage, "nothing");
+ }
+ log.info("expected Error-: " + expectedError);
+ log.info("actual Error-: " + error);
+ if (!error.equals(expectedError)) {
+ throw new LensException("Wrong Error Response");
+ }
+ if (!errorResponse.areValidStackTracesPresent()) {
+ throw new LensException("StackTrace should be present");
+ }
+ if (payLoad) {
+ if (error.getPayload() == null) {
+ throw new LensException("Payload should not be null");
+ }
+ } else {
+ if (error.getPayload() != null) {
+ throw new LensException("Payload should be null");
+ }
+ }
+ if (errorResponse.isSuccessResult()) {
+ throw new LensException("SuccessResponse should be false");
+ }
+ }
+
}
http://git-wip-us.apache.org/repos/asf/lens/blob/7678a63b/lens-regression/src/main/java/org/apache/lens/regression/util/HadoopUtil.java
----------------------------------------------------------------------
diff --git a/lens-regression/src/main/java/org/apache/lens/regression/util/HadoopUtil.java b/lens-regression/src/main/java/org/apache/lens/regression/util/HadoopUtil.java
new file mode 100644
index 0000000..a4ae5c1
--- /dev/null
+++ b/lens-regression/src/main/java/org/apache/lens/regression/util/HadoopUtil.java
@@ -0,0 +1,61 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.lens.regression.util;
+
+import java.io.IOException;
+
+
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.fs.FileSystem;
+import org.apache.hadoop.fs.Path;
+
+import lombok.extern.slf4j.Slf4j;
+
+@Slf4j
+public class HadoopUtil {
+
+ private HadoopUtil() {
+
+ }
+
+ static String rmIp = Util.getProperty("lens.remote.host");
+
+ public static Configuration getHadoopConfiguration() {
+ Configuration conf = new Configuration();
+ conf.set("fs.hdfs.impl", "org.apache.hadoop.hdfs.DistributedFileSystem");
+ conf.set("fs.default.name", Util.getProperty("lens.server.hdfsurl"));
+ return conf;
+ }
+
+ public static void uploadJars(String sourcePath, String hdfsDestinationPath) throws IOException {
+
+ Configuration conf = HadoopUtil.getHadoopConfiguration();
+ FileSystem fs = FileSystem.get(conf);
+
+ Path localFilePath = new Path(sourcePath);
+ Path hdfsFilePath = new Path(hdfsDestinationPath);
+
+ log.info("Copying " + sourcePath + " to " + hdfsDestinationPath);
+ fs.copyFromLocalFile(localFilePath, hdfsFilePath);
+ log.info("Copied Successfully " + sourcePath + " to " + hdfsDestinationPath);
+
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/lens/blob/7678a63b/lens-regression/src/main/java/org/apache/lens/regression/util/Util.java
----------------------------------------------------------------------
diff --git a/lens-regression/src/main/java/org/apache/lens/regression/util/Util.java b/lens-regression/src/main/java/org/apache/lens/regression/util/Util.java
index 0e531e0..7bda529 100644
--- a/lens-regression/src/main/java/org/apache/lens/regression/util/Util.java
+++ b/lens-regression/src/main/java/org/apache/lens/regression/util/Util.java
@@ -18,28 +18,55 @@
*/
package org.apache.lens.regression.util;
+import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
+
import java.io.*;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
import java.util.*;
+import java.util.Map.Entry;
import javax.ws.rs.core.Response;
import javax.xml.bind.*;
+import javax.xml.datatype.DatatypeConfigurationException;
+import javax.xml.datatype.DatatypeFactory;
+import javax.xml.datatype.XMLGregorianCalendar;
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.transform.*;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.stream.StreamResult;
+
import org.apache.lens.api.APIResult;
import org.apache.lens.api.StringList;
import org.apache.lens.api.jaxb.LensJAXBContext;
import org.apache.lens.api.metastore.ObjectFactory;
+import org.apache.lens.api.metastore.XProperties;
+import org.apache.lens.api.metastore.XProperty;
+
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
import com.jcraft.jsch.*;
-import lombok.extern.slf4j.Slf4j;
+import lombok.extern.slf4j.Slf4j;
+
@Slf4j
public class Util {
private static final String PROPERTY_FILE = "lens.properties";
private static Properties properties;
+ private static String localFilePath = "src/test/resources/";
+ private static String localFile;
+ private static String backupFile;
+ private static String remoteFile;
private Util() {
@@ -127,13 +154,14 @@ public class Util {
}
@SuppressWarnings("unchecked")
- public static <T> Object extractObject(String queryString, Class<T> c) throws
- InstantiationException, IllegalAccessException {
+ public static <T> Object extractObject(String queryString, Class<T> c)
+ throws InstantiationException, IllegalAccessException {
JAXBContext jaxbContext = null;
Unmarshaller unmarshaller = null;
StringReader reader = new StringReader(queryString);
try {
- jaxbContext = new LensJAXBContext(ObjectFactory.class) {};
+ jaxbContext = new LensJAXBContext(ObjectFactory.class) {
+ };
unmarshaller = jaxbContext.createUnmarshaller();
return (T) ((JAXBElement<?>) unmarshaller.unmarshal(reader)).getValue();
} catch (JAXBException e) {
@@ -143,8 +171,8 @@ public class Util {
}
@SuppressWarnings("unchecked")
- public static <T> Object getObject(String queryString, Class<T> c) throws
- InstantiationException, IllegalAccessException {
+ public static <T> Object getObject(String queryString, Class<T> c)
+ throws InstantiationException, IllegalAccessException {
JAXBContext jaxbContext = null;
Unmarshaller unmarshaller = null;
StringReader reader = new StringReader(queryString);
@@ -159,8 +187,8 @@ public class Util {
}
@SuppressWarnings("unchecked")
- public static <T> String convertObjectToXml(T object, Class<T> clazz, String functionName) throws
- SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException,
+ public static <T> String convertObjectToXml(T object, Class<T> clazz, String functionName)
+ throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException,
InvocationTargetException {
JAXBElement<T> root = null;
StringWriter stringWriter = new StringWriter();
@@ -209,4 +237,180 @@ public class Util {
return stringListToMap(stringList);
}
+ public static void changeConfig(HashMap<String, String> map, String remotePath) throws Exception {
+ String fileName;
+ remoteFile = remotePath;
+
+ Path p = Paths.get(remoteFile);
+
+ fileName = p.getFileName().toString();
+ backupFile = localFilePath + "backup-" + fileName;
+ localFile = localFilePath + fileName;
+ log.info("Copying " + remoteFile + " to " + localFile);
+ remoteFile("get", remoteFile, localFile);
+ File locfile = new File(localFile);
+ File remfile = new File(backupFile);
+ Files.copy(locfile.toPath(), remfile.toPath(), REPLACE_EXISTING);
+
+ DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
+ DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
+ Document doc = docBuilder.parse(new FileInputStream(localFile));
+ doc.normalize();
+
+ NodeList rootNodes = doc.getElementsByTagName("configuration");
+ Node root = rootNodes.item(0);
+ Element rootElement = (Element) root;
+ NodeList property = rootElement.getElementsByTagName("property");
+
+ for (int i = 0; i < property.getLength(); i++) //Deleting redundant properties from the document
+ {
+ Node prop = property.item(i);
+ Element propElement = (Element) prop;
+ Node propChild = propElement.getElementsByTagName("name").item(0);
+
+ Element nameElement = (Element) propChild;
+ if (map.containsKey(nameElement.getTextContent())) {
+ rootElement.removeChild(prop);
+ i--;
+ }
+
+ }
+
+ Iterator<Entry<String, String>> ab = map.entrySet().iterator();
+ while (ab.hasNext()) {
+ Entry<String, String> entry = ab.next();
+ String propertyName = entry.getKey();
+ String propertyValue = entry.getValue();
+ System.out.println(propertyName + " " + propertyValue + "\n");
+ Node newNode = doc.createElement("property");
+ rootElement.appendChild(newNode);
+ Node newName = doc.createElement("name");
+ Element newNodeElement = (Element) newNode;
+
+ newName.setTextContent(propertyName);
+ newNodeElement.appendChild(newName);
+
+ Node newValue = doc.createElement("value");
+ newValue.setTextContent(propertyValue);
+ newNodeElement.appendChild(newValue);
+ }
+ prettyPrint(doc);
+ remoteFile("put", remoteFile, localFile);
+ }
+
+ /*
+ * function to save the changes in the document
+ */
+ public static final void prettyPrint(Document xml) throws TransformerFactoryConfigurationError, TransformerException {
+ xml.normalize();
+ Transformer transformer = TransformerFactory.newInstance().newTransformer();
+ Result output = new StreamResult(new File(localFile));
+ Source input = new DOMSource(xml);
+ transformer.transform(input, output);
+ }
+
+ /*
+ * function to download or upload a file to a remote server
+ */
+ public static void remoteFile(String function, String remotePath, String localPath)
+ throws JSchException, SftpException {
+ String serverUrl = getProperty("lens.remote.host");
+ String serverUname = getProperty("lens.remote.username");
+ String serverPass = getProperty("lens.remote.password");
+
+ JSch jsch = new JSch();
+ Session session = jsch.getSession(serverUname, serverUrl);
+
+ UserInfo ui = null;
+ session.setUserInfo(ui);
+ session.setPassword(serverPass);
+
+ Properties config = new Properties();
+ config.put("StrictHostKeyChecking", "no");
+ session.setConfig(config);
+ session.connect();
+
+ Channel channel = session.openChannel("sftp");
+ channel.connect();
+
+ ChannelSftp sftpChannel = (ChannelSftp) channel;
+ if (function.equals("get")) {
+ sftpChannel.get(remotePath, localPath);
+ } else if (function.equals("put")) {
+ sftpChannel.put(localPath, remotePath);
+ }
+ sftpChannel.exit();
+ session.disconnect();
+
+ }
+
+ public static void changeConfig(String remotePath) throws JSchException, SftpException {
+ String fileName;
+ remoteFile = remotePath;
+
+ Path p = Paths.get(remoteFile);
+
+ fileName = p.getFileName().toString();
+ backupFile = localFilePath + "backup-" + fileName;
+
+ log.info("Copying " + backupFile + " to " + remoteFile);
+ remoteFile("put", remoteFile, backupFile);
+ }
+
+ public static Map<String, String> mapFromXProperties(XProperties xProperties) {
+ Map<String, String> properties = new HashMap<String, String>();
+ if (xProperties != null && xProperties.getProperty() != null
+ && !xProperties.getProperty().isEmpty()) {
+ for (XProperty xp : xProperties.getProperty()) {
+ properties.put(xp.getName(), xp.getValue());
+ }
+ }
+ return properties;
+ }
+
+ public static XProperties xPropertiesFromMap(Map<String, String> map) {
+ ObjectFactory xCF = new ObjectFactory();
+ if (map != null && !map.isEmpty()) {
+ XProperties xp = xCF.createXProperties();
+ List<XProperty> xpList = xp.getProperty();
+ for (Map.Entry<String, String> e : map.entrySet()) {
+ XProperty property = xCF.createXProperty();
+ property.setName(e.getKey());
+ property.setValue(e.getValue());
+ xpList.add(property);
+ }
+
+ return xp;
+ }
+ return null;
+ }
+
+ public static List<XProperty> xPropertyFromMap(Map<String, String> map) {
+ List<XProperty> xpList = new ArrayList<XProperty>();
+ if (map != null && !map.isEmpty()) {
+ for (Map.Entry<String, String> e : map.entrySet()) {
+ XProperty property = new XProperty();
+ property.setName(e.getKey());
+ property.setValue(e.getValue());
+ xpList.add(property);
+ }
+ }
+ return xpList;
+ }
+
+ public static XMLGregorianCalendar getXMLGregorianCalendar(Date d) {
+ if (d == null) {
+ return null;
+ }
+
+ GregorianCalendar c1 = new GregorianCalendar();
+ c1.setTime(d);
+ try {
+ return DatatypeFactory.newInstance().newXMLGregorianCalendar(c1);
+ } catch (DatatypeConfigurationException e) {
+ log.info("Error converting date " + d, e);
+ return null;
+ }
+ }
+
}