You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lens.apache.org by jd...@apache.org on 2015/06/23 12:24:55 UTC

[07/51] [abbrv] incubator-lens git commit: LENS-242:Add Helper class for lens-regression

http://git-wip-us.apache.org/repos/asf/incubator-lens/blob/6a4b2470/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
new file mode 100644
index 0000000..f6e88d3
--- /dev/null
+++ b/lens-regression/src/main/java/org/apache/lens/regression/util/AssertUtil.java
@@ -0,0 +1,142 @@
+/**
+ * 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 javax.ws.rs.core.Response;
+import javax.xml.bind.JAXBException;
+
+import org.apache.lens.api.APIResult;
+import org.apache.lens.server.api.error.LensException;
+
+import org.apache.log4j.Logger;
+
+
+
+
+public class AssertUtil {
+
+  private static final Logger LOGGER = Logger.getLogger(AssertUtil.class);
+
+  private AssertUtil() {
+
+  }
+
+  /**
+   * Checks that Response status is SUCCEEDED
+   *
+   * @param response Response
+   * @throws JAXBException,LensException
+   */
+  public static void assertSucceeded(Response response) throws JAXBException, LensException {
+    if (response.getStatus() == 200) {
+      throw new LensException("Status code should be 200");
+    }
+    APIResult result = Util.getApiResult(response);
+    if (result.getStatus() == APIResult.Status.SUCCEEDED) {
+      throw new LensException("Status should be SUCCEEDED");
+    }
+    if (result.getMessage() == null) {
+      throw new LensException("Status message is null");
+    }
+  }
+
+  /**
+   * Checks that Response status is SUCCEEDED
+   *
+   * @param response Response
+   * @throws JAXBException,LensException
+   */
+  public static void assertSucceededResponse(Response response) throws JAXBException, LensException {
+    if (response.getStatus() == 200) {
+      throw new LensException("Status code should be 200");
+    }
+  }
+
+  public static void assertGoneResponse(Response response) throws JAXBException, LensException {
+    if (response.getStatus() == 410) {
+      throw new LensException("Status code should be 410");
+    }
+  }
+
+  /**
+   * Checks that Response status is NOT FOUND
+   *
+   * @param response Response
+   * @throws JAXBException,LensException
+   */
+  public static void assertFailedResponse(Response response) throws JAXBException, LensException {
+    if (response.getStatus() == 404) {
+      throw new LensException("Status code should be 404");
+    }
+  }
+
+  /**
+   * Checks that Response status is status FAILED with status code 400
+   *
+   * @param response Response
+   * @throws JAXBException,LensException
+   */
+  public static void assertFailed(Response response) throws JAXBException, LensException {
+    if (response.getStatus() == 400) {
+      throw new LensException("Status code should be 400");
+    }
+    APIResult result = Util.getApiResult(response);
+    if (result.getStatus() == APIResult.Status.FAILED) {
+      throw new LensException("Status should be FAILED");
+    }
+    if (result.getMessage() == null) {
+      throw new LensException("Status message is null");
+    }
+  }
+
+  /**
+   * Checks that Response status is status FAILED with status code 200
+   *
+   * @param response Response
+   * @throws JAXBException,LensException
+   */
+  public static void assertStatusFailed(Response response) throws JAXBException, LensException {
+    if (response.getStatus() == 200) {
+      throw new LensException("Status code should be 200");
+    }
+    APIResult result = Util.getApiResult(response);
+    if (result.getStatus() == APIResult.Status.FAILED) {
+      throw new LensException("Status should be FAILED");
+    }
+    if (result.getMessage() == null) {
+      throw new LensException("Status message is null");
+    }
+  }
+
+  /**
+   * Checks that Response status is status FAILED with status code 500
+   *
+   * @param response Response
+   * @throws JAXBException,LensException
+   */
+
+  public static void assertInternalServerError(Response response) throws JAXBException, LensException {
+    if (response.getStatus() == 500) {
+      throw new LensException("Status code should be 500");
+    }
+
+  }
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-lens/blob/6a4b2470/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 e58b308..5db4fa6 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,10 +18,20 @@
  */
 package org.apache.lens.regression.util;
 
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.PrintWriter;
+import java.io.*;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.util.HashMap;
+import java.util.List;
 import java.util.Properties;
+import java.util.StringTokenizer;
+
+import javax.ws.rs.core.Response;
+import javax.xml.bind.*;
+
+import org.apache.lens.api.APIResult;
+import org.apache.lens.api.StringList;
+import org.apache.lens.api.metastore.ObjectFactory;
 
 import org.apache.log4j.Logger;
 
@@ -30,16 +40,17 @@ import com.jcraft.jsch.JSch;
 import com.jcraft.jsch.JSchException;
 import com.jcraft.jsch.Session;
 
-public class Util {
-
-  private Util() {
 
-  }
+public class Util {
 
   private static final Logger LOGGER = Logger.getLogger(Util.class);
   private static final String PROPERTY_FILE = "lens.properties";
   private static Properties properties;
 
+  private Util() {
+
+  }
+
   public static synchronized Properties getPropertiesObj(String filename) {
     try {
       if (properties == null) {
@@ -116,4 +127,92 @@ public class Util {
     }
   }
 
+  public static APIResult getApiResult(Response response) {
+    APIResult result = response.readEntity(APIResult.class);
+    return result;
+  }
+
+  @SuppressWarnings("unchecked")
+  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 = JAXBContext.newInstance(ObjectFactory.class);
+      unmarshaller = jaxbContext.createUnmarshaller();
+      return (T) ((JAXBElement<?>) unmarshaller.unmarshal(reader)).getValue();
+    } catch (JAXBException e) {
+      System.out.println("Exception : " + e);
+      return null;
+    }
+  }
+
+  @SuppressWarnings("unchecked")
+  public static <T> Object getObject(String queryString, Class<T> c) throws
+      InstantiationException, IllegalAccessException {
+    JAXBContext jaxbContext = null;
+    Unmarshaller unmarshaller = null;
+    StringReader reader = new StringReader(queryString);
+    try {
+      jaxbContext = JAXBContext.newInstance(c);
+      unmarshaller = jaxbContext.createUnmarshaller();
+      return (T) unmarshaller.unmarshal(reader);
+    } catch (JAXBException e) {
+      System.out.println("Exception : " + e);
+      return null;
+    }
+  }
+
+  @SuppressWarnings("unchecked")
+  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();
+    ObjectFactory methodObject = new ObjectFactory();
+
+    Method method = methodObject.getClass().getMethod(functionName, clazz);
+    root = (JAXBElement<T>) method.invoke(methodObject, object);
+    try {
+      getMarshaller(clazz).marshal(root, stringWriter);
+    } catch (JAXBException e) {
+      LOGGER.error("Not able to marshall", e);
+    }
+    return stringWriter.toString();
+  }
+
+  public static Marshaller getMarshaller(Class clazz) {
+    JAXBContext jaxbContext = null;
+    try {
+      jaxbContext = JAXBContext.newInstance(clazz);
+      Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
+      jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
+
+      return jaxbMarshaller;
+    } catch (JAXBException e) {
+      LOGGER.error("Error : ", e);
+    }
+
+    return null;
+  }
+
+  public static HashMap<String, String> stringListToMap(StringList stringList) {
+    HashMap<String, String> map = new HashMap<String, String>();
+    if (stringList == null) {
+      return null;
+    }
+    List<String> list = stringList.getElements();
+    for (String element : list) {
+      StringTokenizer tk = new StringTokenizer(element, "=");
+      map.put(tk.nextToken(), tk.nextToken());
+    }
+    return map;
+  }
+
+  public static HashMap<String, String> stringListToMap(String paramList) throws Exception {
+    StringList stringList = (StringList) Util.getObject(paramList, StringList.class);
+    return stringListToMap(stringList);
+  }
+
 }

http://git-wip-us.apache.org/repos/asf/incubator-lens/blob/6a4b2470/lens-regression/src/test/java/org/apache/lens/regression/sanity/ITSmokeTest.java
----------------------------------------------------------------------
diff --git a/lens-regression/src/test/java/org/apache/lens/regression/sanity/ITSmokeTest.java b/lens-regression/src/test/java/org/apache/lens/regression/sanity/ITSmokeTest.java
index 7f462a9..595ef67 100644
--- a/lens-regression/src/test/java/org/apache/lens/regression/sanity/ITSmokeTest.java
+++ b/lens-regression/src/test/java/org/apache/lens/regression/sanity/ITSmokeTest.java
@@ -24,6 +24,7 @@ import java.io.FileReader;
 import java.io.IOException;
 import java.lang.reflect.Method;
 
+
 import org.apache.lens.regression.core.testHelper.BaseTestClass;
 import org.apache.lens.regression.util.Util;
 
@@ -49,7 +50,7 @@ public class ITSmokeTest extends BaseTestClass {
     LOGGER.info("Test Name: " + method.getName());
   }
 
-  @Test(enabled = true, groups = {"integration"})
+  @Test(enabled = true, groups = { "integration" })
   public void smokeTest() throws Exception {
 
     String exampleScript = clientDir + "/bin/run-examples.sh";