From 75e7e152a728c2c462e53ad7c8a1946b49100b87 Mon Sep 17 00:00:00 2001 From: Marcin Chrzanowski Date: Tue, 31 Dec 2019 17:42:24 +0100 Subject: Set attributes from new API --- .../cloudatlas/agent/NewApiImplementation.java | 46 +++------------------- .../agent/NewApiImplementationTests.java | 22 +++++++++-- 2 files changed, 24 insertions(+), 44 deletions(-) (limited to 'src') 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 52d6321..a146eb6 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/agent/NewApiImplementation.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/agent/NewApiImplementation.java @@ -18,6 +18,7 @@ import java.util.regex.Matcher; import pl.edu.mimuw.cloudatlas.agent.messages.RequestStateMessage; import pl.edu.mimuw.cloudatlas.agent.messages.ResponseMessage; +import pl.edu.mimuw.cloudatlas.agent.messages.SetAttributeMessage; import pl.edu.mimuw.cloudatlas.agent.messages.StateMessage; import pl.edu.mimuw.cloudatlas.agent.messages.UpdateQueriesMessage; import pl.edu.mimuw.cloudatlas.interpreter.Interpreter; @@ -128,48 +129,13 @@ public class NewApiImplementation implements Api { } public void setAttributeValue(String zoneName, String attributeName, Value value) throws RemoteException { - /* try { - ZMI zmi = root.findDescendant(new PathName(zoneName)); - zmi.getAttributes().addOrChange(new Attribute(attributeName), value); - executeAllQueries(root); - } catch (ZMI.NoSuchZoneException e) { - throw new RemoteException("Zone not found", e); - } - */ - } - - private void executeAllQueries(ZMI zmi) { - /* - if(!zmi.getSons().isEmpty()) { - for(ZMI son : zmi.getSons()) { - executeAllQueries(son); - } - - Interpreter interpreter = new Interpreter(zmi); - for (ValueQuery query : getQueries(zmi)) { - try { - List result = interpreter.interpretProgram(query.getQuery()); - for(QueryResult r : result) { - zmi.getAttributes().addOrChange(r.getName(), r.getValue()); - } - } catch(InterpreterException exception) {} - } - } - */ - } - - private Set getQueries(ZMI zmi) { - Set querySet = new HashSet(); - /* - for (Map.Entry attribute : zmi.getAttributes()) { - if (attribute.getValue().getType().getPrimaryType() == Type.PrimaryType.QUERY) { - querySet.add((ValueQuery) attribute.getValue()); - } + SetAttributeMessage message = new SetAttributeMessage("", 0, zoneName, new Attribute(attributeName), value, new ValueTime(System.currentTimeMillis())); + eventBus.addMessage(message); + } catch (Exception e) { + System.out.println("ERROR: failed to set attribute"); + throw new RemoteException("Failed to set attribute", e); } - */ - - return querySet; } public void setFallbackContacts(Set contacts) 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 a2c1adf..6f6bd0f 100644 --- a/src/test/java/pl/edu/mimuw/cloudatlas/agent/NewApiImplementationTests.java +++ b/src/test/java/pl/edu/mimuw/cloudatlas/agent/NewApiImplementationTests.java @@ -16,12 +16,14 @@ import pl.edu.mimuw.cloudatlas.Container; import pl.edu.mimuw.cloudatlas.agent.modules.ModuleType; import pl.edu.mimuw.cloudatlas.agent.messages.AgentMessage; import pl.edu.mimuw.cloudatlas.agent.messages.RequestStateMessage; +import pl.edu.mimuw.cloudatlas.agent.messages.SetAttributeMessage; import pl.edu.mimuw.cloudatlas.agent.messages.StanikMessage; import pl.edu.mimuw.cloudatlas.agent.messages.StateMessage; import pl.edu.mimuw.cloudatlas.agent.messages.UpdateQueriesMessage; import pl.edu.mimuw.cloudatlas.model.Attribute; import pl.edu.mimuw.cloudatlas.model.AttributesMap; import pl.edu.mimuw.cloudatlas.model.TestUtil; +import pl.edu.mimuw.cloudatlas.model.ValueInt; import pl.edu.mimuw.cloudatlas.model.ValueString; import pl.edu.mimuw.cloudatlas.model.ValueQuery; import pl.edu.mimuw.cloudatlas.model.ValueTime; @@ -174,15 +176,27 @@ public class NewApiImplementationTests { assertTrue(timestamp <= timeAfter); } - /* @Test public void testSetAttributeValueChange() throws Exception { - Value numProcesses = new ValueInt(42l); + ValueInt numProcesses = new ValueInt(42l); + long timeBefore = System.currentTimeMillis(); api.setAttributeValue("/uw/khaki13", "num_processes", numProcesses); - AttributesMap attributes = api.getZoneAttributeValues("/uw/khaki13"); - assertEquals(numProcesses, attributes.get("num_processes")); + 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.SET_ATTRIBUTE, stanikMessage.getType()); + SetAttributeMessage setMessage = (SetAttributeMessage) stanikMessage; + assertEquals(new Attribute("num_processes"), setMessage.getAttribute()); + assertEquals(new ValueInt(42l), setMessage.getValue()); + long timestamp = setMessage.getUpdateTimestamp().getValue(); + assertTrue(timeBefore <= timestamp); + assertTrue(timestamp <= timeAfter); } + /* @Test public void testSetAttributeValueAdd() throws Exception { Value numProcesses = new ValueInt(42l); -- cgit v1.2.3