m-chrzan.xyz
aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarcin Chrzanowski <marcin.j.chrzanowski@gmail.com>2020-01-12 21:52:38 +0100
committerMarcin Chrzanowski <marcin.j.chrzanowski@gmail.com>2020-01-12 21:52:38 +0100
commit84df6c11a868e8cd98fa14455456d313e25234db (patch)
tree73e3b3193282c3f9634fc04b91446a6f61d525ef
parent32bfe8f7efc1f4fb99ddf827a19ab466724dac06 (diff)
Set zone levels
-rw-r--r--src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/Stanik.java12
-rw-r--r--src/test/java/pl/edu/mimuw/cloudatlas/agent/modules/StanikTest.java25
2 files changed, 36 insertions, 1 deletions
diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/Stanik.java b/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/Stanik.java
index 999c193..f572efe 100644
--- a/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/Stanik.java
+++ b/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/Stanik.java
@@ -61,6 +61,7 @@ public class Stanik extends Module {
public void handleGetState(GetStateMessage message) throws InterruptedException {
pruneHierarchy();
+ addLevels();
StateMessage response = new StateMessage(
"",
message.getRequestingModule(),
@@ -78,6 +79,17 @@ public class Stanik extends Module {
pruneZMI(hierarchy, now);
}
+ private void addLevels() {
+ addLevelsRecursive(hierarchy, 0);
+ }
+
+ private void addLevelsRecursive(ZMI zmi, long level) {
+ zmi.getAttributes().addOrChange("level", new ValueInt(level));
+ for (ZMI son : zmi.getSons()) {
+ addLevelsRecursive(son, level + 1);
+ }
+ }
+
private boolean pruneZMI(ZMI zmi, ValueTime time) {
Value timestamp = zmi.getAttributes().get("timestamp");
diff --git a/src/test/java/pl/edu/mimuw/cloudatlas/agent/modules/StanikTest.java b/src/test/java/pl/edu/mimuw/cloudatlas/agent/modules/StanikTest.java
index f3ea0b0..03a89c6 100644
--- a/src/test/java/pl/edu/mimuw/cloudatlas/agent/modules/StanikTest.java
+++ b/src/test/java/pl/edu/mimuw/cloudatlas/agent/modules/StanikTest.java
@@ -57,7 +57,8 @@ public class StanikTest {
ZMI zmi = stateMessage.getZMI();
assertNull(zmi.getFather());
assertTrue(zmi.getSons().isEmpty());
- assertEquals(1, TestUtil.iterableSize(zmi.getAttributes()));
+ assertEquals(2, TestUtil.iterableSize(zmi.getAttributes()));
+ assertEquals(new ValueInt(0l), zmi.getAttributes().getOrNull("level"));
Map<Attribute, Entry<ValueQuery, ValueTime>> queries = stateMessage.getQueries();
assertEquals(0, TestUtil.iterableSize(queries.keySet()));
}
@@ -112,6 +113,28 @@ public class StanikTest {
}
@Test
+ public void newZoneHasNewLevel() throws Exception {
+ AttributesMap attributes = new AttributesMap();
+ attributes.add("foo", new ValueInt(1337l));
+ attributes.add("bar", new ValueString("baz"));
+ attributes.add("name", new ValueString("new"));
+ attributes.add("timestamp", testTime);
+ UpdateAttributesMessage message = new UpdateAttributesMessage("test_msg", 0, "/new", attributes);
+ stanik.handleTyped(message);
+ GetStateMessage newMessage = new GetStateMessage("test_msg2", 123, ModuleType.TEST, 43);
+ stanik.handleTyped(newMessage);
+
+ StateMessage newReceivedMessage = (StateMessage) executor.messagesToPass.poll();
+ AttributesMap actualAttributes = newReceivedMessage.getZMI().findDescendant("/new").getAttributes();
+ assertEquals(5, TestUtil.iterableSize(actualAttributes));
+ assertEquals(new ValueInt(1337l), actualAttributes.getOrNull("foo"));
+ assertEquals(new ValueString("baz"), actualAttributes.getOrNull("bar"));
+ assertEquals(new ValueString("new"), actualAttributes.getOrNull("name"));
+ assertEquals(testTime, actualAttributes.getOrNull("timestamp"));
+ assertEquals(new ValueInt(1l), actualAttributes.getOrNull("level"));
+ }
+
+ @Test
public void updateWithRemovedAttributes() throws Exception {
AttributesMap attributes = new AttributesMap();
attributes.add("foo", new ValueInt(1337l));