diff options
Diffstat (limited to 'src/test/java/pl/edu')
4 files changed, 325 insertions, 58 deletions
diff --git a/src/test/java/pl/edu/mimuw/cloudatlas/agent/AgentIntegrationTest.java b/src/test/java/pl/edu/mimuw/cloudatlas/agent/AgentIntegrationTest.java new file mode 100644 index 0000000..1d7a2c5 --- /dev/null +++ b/src/test/java/pl/edu/mimuw/cloudatlas/agent/AgentIntegrationTest.java @@ -0,0 +1,151 @@ +package pl.edu.mimuw.cloudatlas.agent; + +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; +import static org.junit.Assert.*; +import static org.junit.Assert.assertThat; +import static org.hamcrest.CoreMatchers.hasItems; + + +import java.lang.Runtime; +import java.lang.Process; +import java.lang.Thread; + +import java.io.InputStream; + +import java.util.ArrayList; +import java.util.List; +import java.util.Set; + +import java.rmi.registry.LocateRegistry; +import java.rmi.RemoteException; +import java.rmi.registry.Registry; + +import pl.edu.mimuw.cloudatlas.api.Api; +import pl.edu.mimuw.cloudatlas.model.AttributesMap; +import pl.edu.mimuw.cloudatlas.model.TypePrimitive; +import pl.edu.mimuw.cloudatlas.model.Value; +import pl.edu.mimuw.cloudatlas.model.ValueDouble; +import pl.edu.mimuw.cloudatlas.model.ValueInt; +import pl.edu.mimuw.cloudatlas.model.ValueList; +import pl.edu.mimuw.cloudatlas.model.ValueNull; +import pl.edu.mimuw.cloudatlas.model.ValueQuery; +import pl.edu.mimuw.cloudatlas.model.ValueString; +import pl.edu.mimuw.cloudatlas.model.ValueTime; + +public class AgentIntegrationTest { + private static Process registryProcess; + private static Process agentProcess; + + private static Registry registry; + private static Api api; + + @BeforeClass + public static void bindApi() throws Exception { + registryProcess = Runtime.getRuntime().exec("./scripts/registry"); + Thread.sleep(10000); + agentProcess = Runtime.getRuntime().exec("./gradlew runAgent"); + Thread.sleep(10000); + + registry = LocateRegistry.getRegistry("localhost"); + api = (Api) registry.lookup("Api"); + } + + @AfterClass + public static void killProcesses() throws Exception { + try { + registryProcess.destroy(); + agentProcess.destroy(); + } catch (Exception e) { + System.out.println("Caught exception: " + e); + } + } + + @Test + public void testGetZoneSet() throws Exception { + Set<String> set = api.getZoneSet(); + assertEquals(8, set.size()); + assertThat(set, hasItems("/")); + assertThat(set, hasItems("/uw")); + assertThat(set, hasItems("/uw/violet07", "/uw/khaki31", "/uw/khaki13")); + assertThat(set, hasItems("/pjwstk")); + assertThat(set, hasItems("/pjwstk/whatever01", "/pjwstk/whatever02")); + } + + @Test + public void testRootGetZoneAttributeValue() throws Exception { + AttributesMap rootAttributes = api.getZoneAttributeValues("/"); + assertEquals(new ValueInt(0l), rootAttributes.get("level")); + assertEquals(ValueNull.getInstance(), rootAttributes.get("name")); + } + + @Test + public void testIntermediateGetZoneAttributeValue() throws Exception { + AttributesMap attributes = api.getZoneAttributeValues("/uw"); + assertEquals(new ValueInt(1l), attributes.get("level")); + assertEquals(new ValueString("uw"), attributes.get("name")); + } + + @Test + public void testLeafGetZoneAttributeValue() throws Exception { + AttributesMap attributes = api.getZoneAttributeValues("/pjwstk/whatever01"); + assertEquals(new ValueInt(2l), attributes.get("level")); + assertEquals(new ValueString("whatever01"), attributes.get("name")); + assertEquals(new ValueString("/pjwstk/whatever01"), attributes.get("owner")); + assertEquals(new ValueTime("2012/11/09 21:12:00.000"), attributes.get("timestamp")); + assertEquals(new ValueInt(1l), attributes.get("cardinality")); + assertEquals(new ValueTime("2012/10/18 07:03:00.000"), attributes.get("creation")); + assertEquals(new ValueDouble(0.1), attributes.get("cpu_usage")); + assertEquals(new ValueInt(7l), attributes.get("num_cores")); + assertEquals(new ValueInt(215l), attributes.get("num_processes")); + + List<Value> phpModules = new ArrayList<Value>(); + phpModules.add(new ValueString("rewrite")); + assertEquals(new ValueList(phpModules, TypePrimitive.STRING), attributes.get("php_modules")); + } + + @Test + public void testInstallQuery() throws Exception { + String name = "&query"; + String queryCode = "SELECT 1 AS one"; + api.installQuery(name, queryCode); + AttributesMap attributes = api.getZoneAttributeValues("/pjwstk"); + assertEquals(new ValueQuery(queryCode), attributes.get(name)); + } + + @Test + public void testInstallQueryRuns() throws Exception { + String name = "&query"; + String queryCode = "SELECT 1 AS one"; + api.installQuery(name, queryCode); + AttributesMap attributes = api.getZoneAttributeValues("/pjwstk"); + assertEquals(new ValueInt(1l), attributes.get("one")); + } + + @Test + public void testUninstallQuery() throws Exception { + String name = "&query"; + String queryCode = "SELECT 1 AS one"; + api.installQuery(name, queryCode); + api.uninstallQuery(name); + AttributesMap attributes = api.getZoneAttributeValues("/pjwstk"); + assertNull(attributes.getOrNull(name)); + } + + @Test + public void testSetAttributeValueChange() throws Exception { + Value numProcesses = new ValueInt(42l); + api.setAttributeValue("/uw/khaki13", "num_processes", numProcesses); + AttributesMap attributes = api.getZoneAttributeValues("/uw/khaki13"); + assertEquals(numProcesses, attributes.get("num_processes")); + } + + @Test + public void testSetAttributeValueAdd() throws Exception { + Value numProcesses = new ValueInt(42l); + api.setAttributeValue("/uw/khaki13", "an_attribute", numProcesses); + AttributesMap attributes = api.getZoneAttributeValues("/uw/khaki13"); + assertEquals(numProcesses, attributes.get("an_attribute")); + } +} diff --git a/src/test/java/pl/edu/mimuw/cloudatlas/agent/AgentTest.java b/src/test/java/pl/edu/mimuw/cloudatlas/agent/AgentTest.java deleted file mode 100644 index 6ab10d6..0000000 --- a/src/test/java/pl/edu/mimuw/cloudatlas/agent/AgentTest.java +++ /dev/null @@ -1,57 +0,0 @@ -package pl.edu.mimuw.cloudatlas.agent; - -import org.junit.AfterClass; -import org.junit.BeforeClass; -import org.junit.Test; -import static org.junit.Assert.*; - -import java.lang.Runtime; -import java.lang.Process; -import java.lang.Thread; - -import java.io.InputStream; - -import java.util.Set; - -import java.rmi.registry.LocateRegistry; -import java.rmi.RemoteException; -import java.rmi.registry.Registry; -import java.math.BigDecimal; - -import pl.edu.mimuw.cloudatlas.api.Api; - -public class AgentTest { - private static Process registryProcess; - private static Process agentProcess; - - @BeforeClass - public static void bindApi() throws Exception { - registryProcess = Runtime.getRuntime().exec("./scripts/registry"); - Thread.sleep(2000); - agentProcess = Runtime.getRuntime().exec("./gradlew runAgent"); - Thread.sleep(5000); - } - - @AfterClass - public static void killProcesses() throws Exception { - try { - registryProcess.destroy(); - agentProcess.destroy(); - } catch (Exception e) { - System.out.println("Caught exception: " + e); - } - } - - @Test - public void testGetZoneSet() { - try { - Registry registry = LocateRegistry.getRegistry("localhost"); - Api api = (Api) registry.lookup("Api"); - Set<String> set = api.getZoneSet(); - assertEquals(null, set); - } catch (Exception e) { - e.printStackTrace(); - assertTrue(false); - } - } -} diff --git a/src/test/java/pl/edu/mimuw/cloudatlas/agent/ApiImplementationTests.java b/src/test/java/pl/edu/mimuw/cloudatlas/agent/ApiImplementationTests.java new file mode 100644 index 0000000..d98377c --- /dev/null +++ b/src/test/java/pl/edu/mimuw/cloudatlas/agent/ApiImplementationTests.java @@ -0,0 +1,145 @@ +package pl.edu.mimuw.cloudatlas.agent; + +import org.junit.Before; +import org.junit.Test; +import static org.junit.Assert.*; +import static org.junit.Assert.assertThat; +import static org.hamcrest.CoreMatchers.hasItems; + +import java.util.ArrayList; +import java.util.List; +import java.util.Set; + +import pl.edu.mimuw.cloudatlas.api.Api; +import pl.edu.mimuw.cloudatlas.interpreter.Main; +import pl.edu.mimuw.cloudatlas.model.AttributesMap; +import pl.edu.mimuw.cloudatlas.model.TypePrimitive; +import pl.edu.mimuw.cloudatlas.model.Value; +import pl.edu.mimuw.cloudatlas.model.ValueDouble; +import pl.edu.mimuw.cloudatlas.model.ValueInt; +import pl.edu.mimuw.cloudatlas.model.ValueList; +import pl.edu.mimuw.cloudatlas.model.ValueNull; +import pl.edu.mimuw.cloudatlas.model.ValueQuery; +import pl.edu.mimuw.cloudatlas.model.ValueString; +import pl.edu.mimuw.cloudatlas.model.ValueTime; +import pl.edu.mimuw.cloudatlas.model.ZMI; + +public class ApiImplementationTests { + private ApiImplementation api; + + @Before + public void initializeApi() throws Exception { + ZMI root = Main.createTestHierarchy2(); + api = new ApiImplementation(root); + } + + @Test + public void testGetZoneSet() throws Exception { + Set<String> set = api.getZoneSet(); + assertEquals(8, set.size()); + assertThat(set, hasItems("/")); + assertThat(set, hasItems("/uw")); + assertThat(set, hasItems("/uw/violet07", "/uw/khaki31", "/uw/khaki13")); + assertThat(set, hasItems("/pjwstk")); + assertThat(set, hasItems("/pjwstk/whatever01", "/pjwstk/whatever02")); + } + + @Test + public void testRootGetZoneAttributeValue() throws Exception { + AttributesMap rootAttributes = api.getZoneAttributeValues("/"); + assertEquals(new ValueInt(0l), rootAttributes.get("level")); + assertEquals(ValueNull.getInstance(), rootAttributes.get("name")); + } + + @Test + public void testIntermediateGetZoneAttributeValue() throws Exception { + AttributesMap attributes = api.getZoneAttributeValues("/uw"); + assertEquals(new ValueInt(1l), attributes.get("level")); + assertEquals(new ValueString("uw"), attributes.get("name")); + } + + @Test + public void testLeafGetZoneAttributeValue() throws Exception { + AttributesMap attributes = api.getZoneAttributeValues("/pjwstk/whatever01"); + assertEquals(new ValueInt(2l), attributes.get("level")); + assertEquals(new ValueString("whatever01"), attributes.get("name")); + assertEquals(new ValueString("/pjwstk/whatever01"), attributes.get("owner")); + assertEquals(new ValueTime("2012/11/09 21:12:00.000"), attributes.get("timestamp")); + assertEquals(new ValueInt(1l), attributes.get("cardinality")); + assertEquals(new ValueTime("2012/10/18 07:03:00.000"), attributes.get("creation")); + assertEquals(new ValueDouble(0.1), attributes.get("cpu_usage")); + assertEquals(new ValueInt(7l), attributes.get("num_cores")); + assertEquals(new ValueInt(215l), attributes.get("num_processes")); + + List<Value> phpModules = new ArrayList<Value>(); + phpModules.add(new ValueString("rewrite")); + assertEquals(new ValueList(phpModules, TypePrimitive.STRING), attributes.get("php_modules")); + } + + @Test + public void testInstallQuery() throws Exception { + String name = "&query"; + String queryCode = "SELECT 1 AS one"; + api.installQuery(name, queryCode); + assertAttributeInZmiEquals(name, new ValueQuery(queryCode), "/"); + assertAttributeInZmiEquals(name, new ValueQuery(queryCode), "/uw"); + assertAttributeInZmiEquals(name, new ValueQuery(queryCode), "/pjwstk"); + } + + @Test + public void testInstallQueryRuns() throws Exception { + api.installQuery("&query", "SELECT 1 AS one"); + assertAttributeInZmiEquals("one", new ValueInt(1l), "/"); + assertAttributeInZmiEquals("one", new ValueInt(1l), "/uw"); + assertAttributeInZmiEquals("one", new ValueInt(1l), "/pjwstk"); + } + + @Test + public void testInstallQueryRuns2() throws Exception { + api.installQuery("&query", "SELECT sum(num_processes) AS num_processes"); + assertAttributeInZmiEquals("num_processes", new ValueInt(362l), "/uw"); + assertAttributeInZmiEquals("num_processes", new ValueInt(437l), "/pjwstk"); + assertAttributeInZmiEquals("num_processes", new ValueInt(799l), "/"); + } + + public void assertAttributeInZmiEquals(String attribute, Value expected, String zmiPath) throws Exception { + AttributesMap attributes = api.getZoneAttributeValues(zmiPath); + assertEquals(expected, attributes.get(attribute)); + } + + @Test + public void testUninstallQuery() throws Exception { + String name = "&query"; + String queryCode = "SELECT 1 AS one"; + api.installQuery(name, queryCode); + api.uninstallQuery(name); + AttributesMap attributes = api.getZoneAttributeValues("/pjwstk"); + assertNull(attributes.getOrNull(name)); + } + + @Test + public void testSetAttributeValueChange() throws Exception { + Value numProcesses = new ValueInt(42l); + api.setAttributeValue("/uw/khaki13", "num_processes", numProcesses); + AttributesMap attributes = api.getZoneAttributeValues("/uw/khaki13"); + assertEquals(numProcesses, attributes.get("num_processes")); + } + + @Test + public void testSetAttributeValueAdd() throws Exception { + Value numProcesses = new ValueInt(42l); + api.setAttributeValue("/uw/khaki13", "an_attribute", numProcesses); + AttributesMap attributes = api.getZoneAttributeValues("/uw/khaki13"); + assertEquals(numProcesses, attributes.get("an_attribute")); + } + + @Test + public void testSetAttributeValueRunsQueries() throws Exception { + api.installQuery("&query", "SELECT sum(num_processes) AS num_processes"); + Value numProcesses = new ValueInt(42l); + api.setAttributeValue("/uw/khaki13", "num_processes", numProcesses); + assertAttributeInZmiEquals("num_processes", new ValueInt(297l), "/uw"); + assertAttributeInZmiEquals("num_processes", new ValueInt(437l), "/pjwstk"); + assertAttributeInZmiEquals("num_processes", new ValueInt(734l), "/"); + } +} diff --git a/src/test/java/pl/edu/mimuw/cloudatlas/interpreter/InterpreterTests.java b/src/test/java/pl/edu/mimuw/cloudatlas/interpreter/InterpreterTests.java index 20f7c2e..3d8584d 100644 --- a/src/test/java/pl/edu/mimuw/cloudatlas/interpreter/InterpreterTests.java +++ b/src/test/java/pl/edu/mimuw/cloudatlas/interpreter/InterpreterTests.java @@ -2,15 +2,39 @@ package pl.edu.mimuw.cloudatlas.interpreter; import java.io.PrintStream; import java.io.FileInputStream; +import java.io.InputStream; import java.io.File; +import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.net.URL; import static org.junit.Assert.*; import org.junit.Test; +import org.junit.Ignore; + +import pl.edu.mimuw.cloudatlas.model.AttributesMap; +import pl.edu.mimuw.cloudatlas.model.ValueTime; +import pl.edu.mimuw.cloudatlas.model.ZMI; public class InterpreterTests { @Test + public void modifiesZmi() throws Exception { + ZMI root = Main.createTestHierarchy(); + InputStream in = new ByteArrayInputStream("SELECT epoch() AS timestamp".getBytes("UTF-8")); + ByteArrayOutputStream outByteArray = new ByteArrayOutputStream(); + PrintStream outPrint = new PrintStream(outByteArray); + Main.runTest(in, outPrint, root); + + AttributesMap rootAttributes = root.getAttributes(); + assertEquals(new ValueTime("2000/01/01 00:00:00.000"), rootAttributes.get("timestamp")); + + for (ZMI son : root.getSons()) { + AttributesMap sonAttributes = son.getAttributes(); + assertEquals(new ValueTime("2000/01/01 00:00:00.000"), sonAttributes.get("timestamp")); + } + } + + @Test public void fileTest01() throws Exception { runFileTest(1); } @@ -65,6 +89,7 @@ public class InterpreterTests { runFileTest(11); } + @Ignore @Test public void fileTest12() throws Exception { runFileTest(12); @@ -112,7 +137,10 @@ public class InterpreterTests { FileInputStream in = new FileInputStream(test.getFile()); ByteArrayOutputStream outByteArray = new ByteArrayOutputStream(); PrintStream outPrint = new PrintStream(outByteArray); - Main.runTest(in, outPrint); + + ZMI root = Main.createTestHierarchy(); + Main.runTest(in, outPrint, root); + String actual = outByteArray.toString(); File expectedFile = new File(testOut.getFile()); |