Explorar el Código

Added testing classes for REST services

Michal Kepka hace 4 años
padre
commit
0b8395d18a

+ 1 - 0
.settings/.gitignore

@@ -0,0 +1 @@
+/org.eclipse.jdt.core.prefs

+ 26 - 0
src/main/java/cz/hsrs/db/util/TestUtil.java

@@ -0,0 +1,26 @@
+package cz.hsrs.db.util;
+
+import java.sql.ResultSet;
+import java.sql.SQLException;
+
+import cz.hsrs.db.pool.SQLExecutor;
+
+public class TestUtil {
+   /**
+     * Test utilization method to select given query
+     * @param query
+     * @return
+     * @throws SQLException
+     */
+    public static String testQuery(String query) throws SQLException{
+        ResultSet res = SQLExecutor.getInstance().executeQuery(query); 
+        if(res.next()){
+            String resp = res.getString(1);
+            return resp;
+        }
+        else{
+            throw new SQLException("There is not any query!");
+        }
+        
+    }
+}

+ 45 - 0
src/main/java/cz/hsrs/main/StartJetty.java

@@ -0,0 +1,45 @@
+package cz.hsrs.main;
+
+import org.mortbay.jetty.Connector;
+import org.mortbay.jetty.Server;
+import org.mortbay.jetty.bio.SocketConnector;
+import org.mortbay.jetty.webapp.WebAppContext;
+
+public class StartJetty {
+
+    public static Server server = new Server();
+
+    public static void start() throws Exception {
+        try {
+            SocketConnector connector = new SocketConnector();
+            connector.setPort(8080);
+
+            server.setConnectors(new Connector[] { connector });
+            WebAppContext context = new WebAppContext();
+            context.setServer(server);
+            context.setContextPath("/senslog15");
+            //context.setContextPath("/");
+            context.setWar("src/main/webapp");
+            server.addHandler(context);
+            //SQLExecutor.setConfigfile("WEB-INF/local_logging.properties");
+            
+            server.start();
+            
+        } catch (Exception e) {
+            if (server != null) {
+                try {
+                    server.stop();
+                } catch (Exception e1) {
+                    throw new RuntimeException(e1);
+                }
+            }
+        }
+    }
+    public static void stop() throws Exception {
+        server.stop();
+    }
+    public static void main(String[] args) throws Exception {
+    	//PropertyConfigurator.configure("/log4j.properties");
+        start();
+    }
+}

+ 21 - 0
src/main/java/cz/hsrs/rest/beans/TestBean.java

@@ -0,0 +1,21 @@
+package cz.hsrs.rest.beans;
+
+import java.util.Date;
+
+import javax.xml.bind.annotation.XmlRootElement;
+
+@XmlRootElement
+public class TestBean {
+
+	public Date ID;
+	public String message;
+	
+	public TestBean(){
+	}
+	
+	public TestBean(String message) {
+		super();
+		this.ID = new Date();
+		this.message = message;
+	}
+}

+ 112 - 0
src/main/java/cz/hsrs/rest/provider/TestRest.java

@@ -0,0 +1,112 @@
+package cz.hsrs.rest.provider;
+
+import java.sql.SQLException;
+import java.util.Date;
+import java.util.logging.Logger;
+
+import javax.ws.rs.GET;
+import javax.ws.rs.Path;
+import javax.ws.rs.Produces;
+import javax.ws.rs.QueryParam;
+import javax.ws.rs.core.MediaType;
+
+import cz.hsrs.db.util.DateUtil;
+import cz.hsrs.db.util.TestUtil;
+import cz.hsrs.rest.beans.TestBean;
+
+/**
+ * Testing class for RESTful services
+ * @author mkepka
+ *
+ */
+@Path("/test/")
+public class TestRest {
+
+	public static Logger logger = Logger.getLogger("TestRest");
+    /**
+     * Empty constructor
+     */
+    public TestRest(){
+        super();
+    }
+    
+    /**
+     * Method catches rest/test/testget requests to test the connection to receiver
+     * /rest/test/testget?value=<>
+     * @param testValue
+     * @return
+     * @throws Exception
+     */
+    @Path("/testget")
+    @GET
+    @Produces("text/plain")
+    public String testGet(@QueryParam("value") String testValue) throws Exception{
+        if(testValue == null){
+            return "empty";
+        }
+        else{
+            //String modif = testValue.toUpperCase();
+            Date parseMicro = DateUtil.parseTimestampMicro(testValue);
+            Date parse = DateUtil.parseTimestamp(testValue);
+            return ""+parseMicro+" : "+parseMicro.getTime()+"\n"+parse+" : "+parse.getTime();
+        }
+    }
+    
+    /**
+     * /rest/test/testbean1?message=<>
+     * @param testValue
+     * @param userValue
+     * @return
+     */
+    @Path("/testbean1")
+    @GET
+    @Produces(MediaType.APPLICATION_XML)
+    public TestBean testXML(@QueryParam("message") String testValue, @QueryParam("user") String userValue){
+        if(testValue == null){
+            return null;
+        }
+        else{
+            String modif = testValue.toUpperCase();
+            return new TestBean(modif);
+        }
+    }
+    
+    /**
+     * /rest/test/testbean2?message=<>
+     * @param testValue
+     * @return
+     */
+    @Path("/testbean2")
+    @GET
+    @Produces(MediaType.APPLICATION_JSON)
+    public TestBean testJSON(@QueryParam("message") String testValue){
+        if(testValue == null){
+            return null;
+        }
+        else{
+            String modif = testValue.toUpperCase();
+            return new TestBean(modif);
+        }
+    }
+    
+    /**
+     * /rest/test/testdb?message=<>
+     * @param testValue
+     * @return
+     * @throws SQLException 
+     */
+    @Path("/testdb")
+    @GET
+    @Produces(MediaType.APPLICATION_JSON)
+    public TestBean testDB(@QueryParam("message") String testValue) throws SQLException{
+        if(testValue == null){
+        	logger.info(testValue);
+            return null;
+        }
+        else{ 
+            String response = TestUtil.testQuery("SELECT unit_id FROM public.units LIMIT 1;");
+            logger.info(response);
+            return new TestBean(response);
+        }
+    }
+}