diff options
-rw-r--r-- | src/main/java/pl/edu/mimuw/cloudatlas/agent/NewApiImplementation.java | 24 | ||||
-rw-r--r-- | src/test/java/pl/edu/mimuw/cloudatlas/agent/NewApiImplementationTests.java | 75 |
2 files changed, 83 insertions, 16 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 4aa5148..c0b313d 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/agent/NewApiImplementation.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/agent/NewApiImplementation.java @@ -70,17 +70,23 @@ public class NewApiImplementation implements Api { } public AttributesMap getZoneAttributeValues(String zoneName) throws RemoteException { - /* + CompletableFuture<ResponseMessage> responseFuture = new CompletableFuture(); + RequestStateMessage message = new RequestStateMessage("", 0, responseFuture); try { - ZMI zmi = root.findDescendant(new PathName(zoneName)); - return zmi.getAttributes(); - } catch (ZMI.NoSuchZoneException e) { - throw new RemoteException("Zone not found", e); - } - */ + eventBus.addMessage(message); + ResponseMessage response = responseFuture.get(); - // placeholder - return new AttributesMap(); + if (response.getType() == ResponseMessage.Type.STATE) { + StateMessage stateMessage = (StateMessage) response; + return stateMessage.getZMI().findDescendant(zoneName).getAttributes(); + } else { + System.out.println("ERROR: getZoneSet didn't receive a StateMessage"); + throw new Exception("Failed to retrieve zone set"); + } + } catch (Exception e) { + System.out.println("ERROR: exception caught in getZoneSet"); + throw new RemoteException(e.getMessage()); + } } public void installQuery(String name, String queryCode) 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 a3b57a9..78e5970 100644 --- a/src/test/java/pl/edu/mimuw/cloudatlas/agent/NewApiImplementationTests.java +++ b/src/test/java/pl/edu/mimuw/cloudatlas/agent/NewApiImplementationTests.java @@ -6,15 +6,19 @@ import static org.junit.Assert.*; import static org.hamcrest.CoreMatchers.hasItems; import java.util.concurrent.TimeUnit; +import java.util.HashMap; import java.util.HashSet; import java.util.Set; import pl.edu.mimuw.cloudatlas.Container; -import pl.edu.mimuw.cloudatlas.model.ZMI; 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.StateMessage; +import pl.edu.mimuw.cloudatlas.model.AttributesMap; +import pl.edu.mimuw.cloudatlas.model.ValueString; +import pl.edu.mimuw.cloudatlas.model.ValueTime; +import pl.edu.mimuw.cloudatlas.model.ZMI; public class NewApiImplementationTests { private NewApiImplementation api; @@ -46,7 +50,6 @@ public class NewApiImplementationTests { ZMI root = new ZMI(); StateMessage responseMessage = new StateMessage("", ModuleType.RMI, 0, 0, root, null); - requestMessage.getFuture().complete(responseMessage); apiThread.join(100); @@ -56,14 +59,72 @@ public class NewApiImplementationTests { assertThat(zoneSet, hasItems("/")); } - /* @Test - public void testRootGetZoneAttributeValue() throws Exception { - AttributesMap rootAttributes = api.getZoneAttributeValues("/"); - assertEquals(new ValueInt(0l), rootAttributes.get("level")); - assertEquals(ValueNull.getInstance(), rootAttributes.get("name")); + public void testRootGetZoneAttributeValues() throws Exception { + final Container<AttributesMap> attributes = new Container(); + final Container<Exception> exceptionContainer = new Container(); + Thread apiThread = new Thread(() -> { + try { + attributes.thing = api.getZoneAttributeValues("/"); + } catch (Exception e) { + exceptionContainer.thing = e; + } + }); + apiThread.start(); + + AgentMessage message = eventBus.events.poll(100, TimeUnit.MILLISECONDS); + assertNotNull(message); + assertEquals(ModuleType.RMI, message.getDestinationModule()); + RequestStateMessage requestMessage = (RequestStateMessage) message; + + ZMI zmi = new ZMI(); + zmi.getAttributes().add("timestamp", new ValueTime(42l)); + StateMessage response = new StateMessage("", ModuleType.RMI, 0, 0, zmi, new HashMap()); + requestMessage.getFuture().complete(response); + + apiThread.join(100); + assertFalse(apiThread.isAlive()); + assertNull(exceptionContainer.thing); + + assertEquals(new ValueTime(42l), attributes.thing.getOrNull("timestamp")); + } + + @Test + public void testGetZoneAttributeValues() throws Exception { + final Container<AttributesMap> attributes = new Container(); + final Container<Exception> exceptionContainer = new Container(); + Thread apiThread = new Thread(() -> { + try { + attributes.thing = api.getZoneAttributeValues("/son"); + } catch (Exception e) { + exceptionContainer.thing = e; + } + }); + apiThread.start(); + + AgentMessage message = eventBus.events.poll(100, TimeUnit.MILLISECONDS); + assertNotNull(message); + assertEquals(ModuleType.RMI, message.getDestinationModule()); + RequestStateMessage requestMessage = (RequestStateMessage) message; + + ZMI zmi = new ZMI(); + zmi.getAttributes().add("timestamp", new ValueTime(42l)); + ZMI son = new ZMI(zmi); + zmi.addSon(son); + son.getAttributes().add("name", new ValueString("son")); + son.getAttributes().add("timestamp", new ValueTime(43l)); + StateMessage response = new StateMessage("", ModuleType.RMI, 0, 0, zmi, new HashMap()); + requestMessage.getFuture().complete(response); + + apiThread.join(100); + assertFalse(apiThread.isAlive()); + assertNull(exceptionContainer.thing); + + assertEquals(new ValueTime(43l), attributes.thing.getOrNull("timestamp")); + assertEquals(new ValueString("son"), attributes.thing.getOrNull("name")); } + /* @Test public void testIntermediateGetZoneAttributeValue() throws Exception { AttributesMap attributes = api.getZoneAttributeValues("/uw"); |