package eu.uqasar.util.reporting;

import java.util.ArrayList;
import java.util.List;

import junit.framework.Assert;
import junit.framework.TestCase;

import org.jboss.solder.logging.Logger;
import org.json.JSONObject;
import org.junit.Test;


public class UtilTest extends TestCase {

	private static Logger logger = Logger.getLogger(UtilTest.class);


	@Test
	public void testCreateExpressionEditor() {
		logger.info("Testing createExpressionEditor");
		String res = Util.createExpressionEditor(null,"http://uqasar.pythonanywhere.com");
		Assert.assertNotNull(res);
		//TODO VERIFY CORRECT JSON FORMAT WHEN IT IS DEFINITIVE

	}


	@Test
	public void testRetrieveDimensionsKO() {
		logger.info("Testing retrieveDimensionsKO");
		List res = Util.retrieveDimensions("http://uqasar.pythonanywhere.com/model");
		Assert.assertNull(res);
	}


	@Test
	public void testRetrieveDimensionsJsonExc() {
		logger.info("Testing RetrieveDimensionsJsonExc");
		List res = Util.retrieveDimensions("http://uqasar.pythonanywhere.com/cubes"); //json exception
		Assert.assertNotNull(res);
		Assert.assertEquals(res, new ArrayList());

	}


	@Test
	public void testRetrieveDimensionsError() {
		logger.info("Testing retrieveDimensionsError");
		List res = Util.retrieveDimensions("http://uqasar.pythonanywhere.com/cube/sonar/model"); 
		Assert.assertNull(res);

	}

	@Test
	public void testRetrieveDimensionsOK() {
		logger.info("Testing retrieveDimensionsOK");
		List res = Util.retrieveDimensions("http://uqasar.pythonanywhere.com/cube/jira/model"); 
		Assert.assertNotNull(res);
		Assert.assertEquals("Project", res.get(0));
	}

	@Test
	public void testCreateSummaryTable() {
		logger.info("Testing createSummaryTable");
		String urlToLoad = "http://uqasar.pythonanywhere.com/cube/jira/aggregate?&cut=Project:UQASAR";
		JSONObject cuberesponse = Util.readJsonFromUrl(urlToLoad);
		String res =Util.createSummaryTable(cuberesponse);
		Assert.assertNotNull(res);
		Assert.assertNotSame("<table cellpadding='10'>", res);
	}	

	@Test
	public void testGetAvailableCubes() {
		logger.info("Testing getAvailableCubes");

		String uqasarOLAPDNS = "http://uqasar.pythonanywhere.com/";
		String  cubes = Util.getAvailableCubes(uqasarOLAPDNS);
		Assert.assertNotNull(cubes);

	}	


	@Test
	public void testGetAvailableCubesKO() {
		logger.info("Testing getAvailableCubes KO");

		String uqasarOLAPDNS = "http://uqasar.pythonanywhere/";
		try {
			Util.getAvailableCubes(uqasarOLAPDNS);
			Assert.assertTrue(false);
		} catch (Exception ex) {
			Assert.assertTrue(true);
		}

	}


	@Test
	public void testConstructCubeRetrieverURL() {
		logger.info("Testing constructCubeRetrieverURL");

		String cubeurl = "http://uqasar.pythonanywhere.com/cube/jira";
		String cuberules = "{\"condition\": \"AND\",\"rules\": [ {  \"id\": \"Project\", \"field\": \"Project\", \"type\": \"string\", \"input\": \"select\", \"operator\": \"equal\", \"value\": \"UQASAR\"}]}";
		String expected = "http://uqasar.pythonanywhere.com/cube/jira/aggregate?&cut=Project:UQASAR";

		String urlToLoad = Util.constructCubeRetrieverURL(cuberules, cubeurl);
		Assert.assertEquals(urlToLoad, expected);

	}

	@Test
	public void testConstructCubeRetrieverURLDrilldown() {
		logger.info("Testing constructCubeRetrieverURLDrilldown");

		String cubeurl = "http://uqasar.pythonanywhere.com/cube/jira";
		String cuberules = "{\"condition\": \"AND\",\"rules\": [ { \"id\": \"drilldown\", \"field\": \"drilldown\", \"type\": \"string\", \"input\": \"select\", \"operator\": \"equal\", \"value\": \"Project\" }]}";
		String expected = "http://uqasar.pythonanywhere.com/cube/jira/aggregate?&drilldown=Project";

		String urlToLoad = Util.constructCubeRetrieverURL(cuberules, cubeurl);
		Assert.assertEquals(urlToLoad, expected);

	}

}

@@ -1665,32 +1665,34 @@
 0));%0D%0A%09%7D%0D%0A%0D%0A
+//
 %09@Test%0D%0A
 %09public void
@@ -1671,32 +1671,34 @@
 %09%7D%0D%0A%0D%0A//%09@Test%0D%0A
+//
 %09public void tes
@@ -1718,24 +1718,26 @@
 yTable() %7B%0D%0A
+//
 %09%09logger.inf
@@ -1766,24 +1766,26 @@
 ryTable%22);%0D%0A
+//
 %09%09String url
@@ -1870,16 +1870,18 @@
 ASAR%22;%0D%0A
+//
 %09%09JSONOb
@@ -1930,24 +1930,26 @@
 rlToLoad);%0D%0A
+//
 %09%09String res
@@ -1986,24 +1986,26 @@
 response);%0D%0A
+//
 %09%09Assert.ass
@@ -2014,32 +2014,34 @@
 tNotNull(res);%0D%0A
+//
 %09%09Assert.assertN
@@ -2082,16 +2082,18 @@
  res);%0D%0A
+//
 %09%7D%09%0D%0A%0D%0A%09

disable one test case until problem with http://uqasar.pythonanywhere.com is solved