1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
|
package pl.edu.mimuw.cloudatlas.agent.modules;
import java.util.AbstractMap.SimpleImmutableEntry;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Map.Entry;
import pl.edu.mimuw.cloudatlas.agent.messages.AgentMessage;
import pl.edu.mimuw.cloudatlas.agent.messages.GetStateMessage;
import pl.edu.mimuw.cloudatlas.agent.messages.RunQueriesMessage;
import pl.edu.mimuw.cloudatlas.agent.messages.StanikMessage;
import pl.edu.mimuw.cloudatlas.agent.messages.StateMessage;
import pl.edu.mimuw.cloudatlas.agent.messages.UpdateAttributesMessage;
import pl.edu.mimuw.cloudatlas.agent.MockExecutor;
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.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;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;
public class QurnikTest {
private Qurnik qurnik;
private MockExecutor executor;
@Before
public void setupLocals() {
qurnik = new Qurnik();
executor = new MockExecutor(qurnik);
}
@Test
public void runQueriesRequestsState() throws Exception {
RunQueriesMessage message = new RunQueriesMessage("", 0);
qurnik.handleTyped(message);
AgentMessage receivedMessage = (AgentMessage) executor.messagesToPass.take();
assertEquals(ModuleType.STATE, receivedMessage.getDestinationModule());
StanikMessage stanikMessage = (StanikMessage) receivedMessage;
assertEquals(StanikMessage.Type.GET_STATE, stanikMessage.getType());
GetStateMessage getStateMessage = (GetStateMessage) stanikMessage;
assertEquals(ModuleType.QUERY, getStateMessage.getRequestingModule());
}
@Test
public void simpleQuery() throws Exception {
ZMI root = new ZMI();
ZMI son = new ZMI(root);
root.addSon(son);
AttributesMap sonAttributes = new AttributesMap();
sonAttributes.add("name", new ValueString("son"));
Map<Attribute, ValueQuery> queries = new HashMap();
queries.put(
new Attribute("&query"),
new ValueQuery("SELECT 1 AS one")
);
StateMessage message = new StateMessage("", ModuleType.QUERY, 0, 0, root, queries, new HashSet());
long timeBefore = System.currentTimeMillis();
qurnik.handleTyped(message);
long timeAfter = System.currentTimeMillis();
assertEquals(1, executor.messagesToPass.size());
AgentMessage receivedMessage = (AgentMessage) executor.messagesToPass.take();
assertEquals(ModuleType.STATE, receivedMessage.getDestinationModule());
StanikMessage stanikMessage = (StanikMessage) receivedMessage;
assertEquals(StanikMessage.Type.UPDATE_ATTRIBUTES, stanikMessage.getType());
UpdateAttributesMessage updateAttributesMessage = (UpdateAttributesMessage) stanikMessage;
assertEquals("/", updateAttributesMessage.getPathName());
AttributesMap updatedAttributes = updateAttributesMessage.getAttributes();
assertEquals(3, TestUtil.iterableSize(updatedAttributes));
assertEquals(new ValueString(null), updatedAttributes.getOrNull("name"));
assertEquals(new ValueInt(1l), updatedAttributes.getOrNull("one"));
long timestamp = ((ValueTime) updatedAttributes.getOrNull("timestamp")).getValue();
assertTrue(timeBefore <= timestamp);
assertTrue(timestamp <= timeAfter);
}
private ZMI setupSampleHierarchy() {
ZMI root = new ZMI();
ZMI uw = new ZMI(root);
root.addSon(uw);
ZMI pw = new ZMI(root);
root.addSon(pw);
ZMI uw1 = new ZMI(uw);
uw.addSon(uw1);
ZMI uw2 = new ZMI(uw);
uw.addSon(uw2);
AttributesMap uwAttributes = uw.getAttributes();
uwAttributes.add("name", new ValueString("uw"));
AttributesMap pwAttributes = pw.getAttributes();
pwAttributes.add("name", new ValueString("pw"));
pwAttributes.add("x", new ValueInt(42l));
pwAttributes.add("y", new ValueInt(250l));
pwAttributes.add("z", new ValueInt(5l));
AttributesMap uw1Attributes = uw1.getAttributes();
uw1Attributes.add("name", new ValueString("uw1"));
uw1Attributes.add("x", new ValueInt(12l));
uw1Attributes.add("y", new ValueInt(100l));
uw1Attributes.add("a", new ValueInt(123l));
AttributesMap uw2Attributes = uw2.getAttributes();
uw2Attributes.add("name", new ValueString("uw2"));
uw2Attributes.add("x", new ValueInt(13l));
uw2Attributes.add("a", new ValueInt(134l));
uw2Attributes.add("b", new ValueInt(777l));
return root;
}
public Map<Attribute, ValueQuery> setupSampleQueries() throws Exception {
Map<Attribute, ValueQuery> queries = new HashMap();
queries.put(
new Attribute("&query1"),
new ValueQuery("SELECT sum(x) AS x")
);
queries.put(
new Attribute("&query2"),
new ValueQuery("SELECT min(y) AS y")
);
queries.put(
new Attribute("&query3"),
new ValueQuery("SELECT max(z) AS z")
);
queries.put(
new Attribute("&query4"),
new ValueQuery("SELECT sum(a + 1) AS a")
);
queries.put(
new Attribute("&query5"),
new ValueQuery("SELECT sum(2 * b) AS b")
);
return queries;
}
@Test
public void multipleQueries() throws Exception {
ZMI root = setupSampleHierarchy();
Map<Attribute, ValueQuery> queries = setupSampleQueries();
StateMessage message = new StateMessage("", ModuleType.QUERY, 0, 0, root, queries, new HashSet());
long timeBefore = System.currentTimeMillis();
qurnik.handleTyped(message);
long timeAfter = System.currentTimeMillis();
assertEquals(2, executor.messagesToPass.size());
UpdateAttributesMessage message1 = (UpdateAttributesMessage) executor.messagesToPass.take();
assertEquals("/uw", message1.getPathName());
AttributesMap attributes1 = message1.getAttributes();
assertEquals(6, TestUtil.iterableSize(attributes1));
assertEquals(new ValueString("uw"), attributes1.getOrNull("name"));
assertEquals(new ValueInt(25l), attributes1.getOrNull("x"));
assertEquals(new ValueInt(100l), attributes1.getOrNull("y"));
assertEquals(new ValueInt(259l), attributes1.getOrNull("a"));
assertEquals(new ValueInt(1554l), attributes1.getOrNull("b"));
long timestamp1 = ((ValueTime) attributes1.getOrNull("timestamp")).getValue();
assertTrue(timeBefore <= timestamp1);
assertTrue(timestamp1 <= timeAfter);
UpdateAttributesMessage message2 = (UpdateAttributesMessage) executor.messagesToPass.take();
assertEquals("/", message2.getPathName());
AttributesMap attributes2 = message2.getAttributes();
System.out.println("got attributes " + attributes2.toString());
assertEquals(7, TestUtil.iterableSize(attributes2));
assertEquals(new ValueString(null), attributes2.getOrNull("name"));
assertEquals(new ValueInt(67l), attributes2.getOrNull("x"));
assertEquals(new ValueInt(100l), attributes2.getOrNull("y"));
assertEquals(new ValueInt(5l), attributes2.getOrNull("z"));
assertEquals(new ValueInt(260l), attributes2.getOrNull("a"));
assertEquals(new ValueInt(3108l), attributes2.getOrNull("b"));
long timestamp2 = ((ValueTime) attributes2.getOrNull("timestamp")).getValue();
assertTrue(timeBefore <= timestamp2);
assertTrue(timestamp2 <= timeAfter);
}
@Test
public void ignoresNullQueries() throws Exception {
ZMI root = setupSampleHierarchy();
Map<Attribute, ValueQuery> queries = new HashMap();
queries.put(new Attribute("&query1"), new ValueQuery("SELECT 1 AS one"));
queries.put(new Attribute("&query2"), null);
queries.put(new Attribute("&query3"), new ValueQuery("SELECT 2 AS two"));
StateMessage message = new StateMessage("", ModuleType.QUERY, 0, 0, root, queries, new HashSet());
long timeBefore = System.currentTimeMillis();
qurnik.handleTyped(message);
long timeAfter = System.currentTimeMillis();
UpdateAttributesMessage message1 = (UpdateAttributesMessage) executor.messagesToPass.take();
assertEquals("/uw", message1.getPathName());
AttributesMap attributes1 = message1.getAttributes();
assertEquals(4, TestUtil.iterableSize(attributes1));
assertEquals(new ValueInt(1l), attributes1.getOrNull("one"));
assertEquals(new ValueInt(2l), attributes1.getOrNull("two"));
long timestamp1 = ((ValueTime) attributes1.getOrNull("timestamp")).getValue();
assertTrue(timeBefore <= timestamp1);
assertTrue(timestamp1 <= timeAfter);
UpdateAttributesMessage message2 = (UpdateAttributesMessage) executor.messagesToPass.take();
assertEquals("/", message2.getPathName());
AttributesMap attributes2 = message2.getAttributes();
System.out.println("got attributes " + attributes2.toString());
assertEquals(4, TestUtil.iterableSize(attributes2));
assertEquals(new ValueString(null), attributes2.getOrNull("name"));
assertEquals(new ValueInt(1l), attributes2.getOrNull("one"));
assertEquals(new ValueInt(2l), attributes2.getOrNull("two"));
long timestamp2 = ((ValueTime) attributes2.getOrNull("timestamp")).getValue();
assertTrue(timeBefore <= timestamp2);
assertTrue(timestamp2 <= timeAfter);
}
}
|