-
-
Save fernandojunior/1f93b6e49eefb0b99736 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.saiku.web; | |
import java.net.URL; | |
import java.util.UUID; | |
import junit.framework.TestCase; | |
import org.apache.commons.httpclient.HttpClient; | |
import org.apache.commons.httpclient.UsernamePasswordCredentials; | |
import org.apache.commons.httpclient.methods.GetMethod; | |
import org.apache.commons.httpclient.methods.PostMethod; | |
import org.apache.commons.httpclient.methods.UrlGetMethod; | |
import org.codehaus.jettison.json.JSONObject; | |
public class SaikuHttpTest extends TestCase { | |
public SaikuHttpTest() { | |
super(); | |
} | |
public void testSaikuRest() { | |
try { | |
String SAIKURL = "http://demo.analytical-labs.com"; | |
String RESTPATH = "/saiku/rest/saiku"; | |
String SESSIONURL = RESTPATH + "/session"; | |
String REPO_QUERY_URL = RESTPATH + "/admin/repository/test.saiku"; | |
String NEW_QUERY_URL = RESTPATH + "/admin/query/" + UUID.randomUUID().toString(); | |
HttpClient cl = new HttpClient(); | |
// start a session, important for all following requests | |
cl.startSession(new URL(SAIKURL), new UsernamePasswordCredentials("admin", "admin")); | |
// authenticate via POST, since above credentials dont seem to work | |
PostMethod pm = new PostMethod(SESSIONURL); | |
pm.addParameter("username","admin"); | |
pm.addParameter("password","admin"); | |
cl.executeMethod(pm); | |
// check if authentication was successful | |
GetMethod gm = new GetMethod(SESSIONURL); | |
cl.executeMethod(gm); | |
String json = gm.getResponseBodyAsString(); | |
assert json.startsWith("{\"username\":\"admin\",\"roles\":[\"ROLE_USER\"]"); | |
// fetch a random query from the repository | |
GetMethod fetchQuery = new GetMethod(REPO_QUERY_URL); | |
cl.executeMethod(fetchQuery); | |
String queryJson = fetchQuery.getResponseBodyAsString(); | |
JSONObject object = new JSONObject(queryJson); | |
String query = object.getString("xml"); | |
// System.out.println("QUERY: \n" + query); | |
// use the returned xml to create a new query in the workspace | |
PostMethod nq = new PostMethod(NEW_QUERY_URL); | |
nq.addParameter("connection", "foodmart"); | |
nq.addParameter("cube", "Sales"); | |
nq.addParameter("catalog", "FoodMart"); | |
nq.addParameter("schema", "FoodMart"); | |
nq.addParameter("xml", query); | |
cl.executeMethod(nq); | |
// System.out.println("query json: \n" + nq.getResponseBodyAsString()); | |
// get the results | |
GetMethod getResult = new GetMethod(NEW_QUERY_URL + "/result/flattened"); | |
cl.executeMethod(getResult); | |
JSONObject rs = new JSONObject(getResult.getResponseBodyAsString()); | |
String error = rs.getString("error"); | |
// make sure our query didn't cause any errors | |
assert "null".equals(error); | |
//System.out.println("result json: \n" + rs.getString("cellset")); | |
// drillthrough on the first cell of the result | |
UrlGetMethod getDrillthrough = new UrlGetMethod(NEW_QUERY_URL + "/drillthrough?position=0:0&maxrows=200"); | |
cl.executeMethod(getDrillthrough); | |
rs = new JSONObject(getDrillthrough.getResponseBodyAsString()); | |
error = rs.getString("error"); | |
assert "null".equals(error); | |
// System.out.println("drillthrough result json: \n" + rs.getString("cellset")); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment