From 17d87268246d32a75407590f8fef118148b87ccd Mon Sep 17 00:00:00 2001 From: Marcin Chrzanowski Date: Tue, 31 Dec 2019 15:00:10 +0100 Subject: Implement query removal in new API --- .../cloudatlas/agent/NewApiImplementation.java | 21 ++++----- .../agent/NewApiImplementationTests.java | 54 +++++++--------------- 2 files changed, 26 insertions(+), 49 deletions(-) diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/agent/NewApiImplementation.java b/src/main/java/pl/edu/mimuw/cloudatlas/agent/NewApiImplementation.java index 3bedd90..52d6321 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/agent/NewApiImplementation.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/agent/NewApiImplementation.java @@ -114,18 +114,17 @@ public class NewApiImplementation implements Api { } public void uninstallQuery(String queryName) throws RemoteException { - // uninstallQueryInHierarchy(root, new Attribute(queryName)); - } - - private void uninstallQueryInHierarchy(ZMI zmi, Attribute queryName) { - /* - if (!zmi.getSons().isEmpty()) { - zmi.getAttributes().remove(queryName); - for (ZMI son : zmi.getSons()) { - uninstallQueryInHierarchy(son, queryName); - } + try { + Attribute attributeName = new Attribute(queryName); + ValueTime timestamp = new ValueTime(System.currentTimeMillis()); + Map> queries = new HashMap(); + queries.put(attributeName, new SimpleImmutableEntry(null, timestamp)); + UpdateQueriesMessage message = new UpdateQueriesMessage("", 0, queries); + eventBus.addMessage(message); + } catch (Exception e) { + System.out.println("ERROR: failed to remove query"); + throw new RemoteException("Failed to uninstall query", e); } - */ } public void setAttributeValue(String zoneName, String attributeName, Value value) throws RemoteException { diff --git a/src/test/java/pl/edu/mimuw/cloudatlas/agent/NewApiImplementationTests.java b/src/test/java/pl/edu/mimuw/cloudatlas/agent/NewApiImplementationTests.java index e48df0b..a2c1adf 100644 --- a/src/test/java/pl/edu/mimuw/cloudatlas/agent/NewApiImplementationTests.java +++ b/src/test/java/pl/edu/mimuw/cloudatlas/agent/NewApiImplementationTests.java @@ -153,50 +153,28 @@ public class NewApiImplementationTests { assertTrue(timestamp <= timeAfter); } - /* - @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), "/"); - } - - @Test - public void testInstallQueryWithInvalidNameFails() throws Exception { - String name = "query"; - String queryCode = "SELECT 1 AS one"; - try { - api.installQuery(name, queryCode); - assertTrue("should have thrown", false); - } catch (Exception e) { - assertEquals("Invalid query identifier", e.getMessage()); - } - } - - 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); + long timeBefore = System.currentTimeMillis(); api.uninstallQuery(name); - AttributesMap attributes = api.getZoneAttributeValues("/pjwstk"); - assertNull(attributes.getOrNull(name)); + long timeAfter = System.currentTimeMillis(); + + assertEquals(1, eventBus.events.size()); + AgentMessage message = eventBus.events.take(); + assertEquals(ModuleType.STATE, message.getDestinationModule()); + StanikMessage stanikMessage = (StanikMessage) message; + assertEquals(StanikMessage.Type.UPDATE_QUERIES, stanikMessage.getType()); + UpdateQueriesMessage updateMessage = (UpdateQueriesMessage) stanikMessage; + Map> queries = updateMessage.getQueries(); + assertEquals(1, TestUtil.iterableSize(queries.keySet())); + assertNull(queries.get(new Attribute("&query")).getKey()); + long timestamp = queries.get(new Attribute("&query")).getValue().getValue(); + assertTrue(timeBefore <= timestamp); + assertTrue(timestamp <= timeAfter); } + /* @Test public void testSetAttributeValueChange() throws Exception { Value numProcesses = new ValueInt(42l); -- cgit v1.2.3