m-chrzan.xyz
aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/pl/edu/mimuw/cloudatlas/agent/NewApiImplementationTests.java
blob: a5ecdddb0ce8b8c95140882e2504d46f792ec1d8 (plain)
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
package pl.edu.mimuw.cloudatlas.agent;

import org.junit.Before;
import org.junit.Test;
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.Map;
import java.util.Map.Entry;
import java.util.Set;

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;
import pl.edu.mimuw.cloudatlas.model.ZMI;

public class NewApiImplementationTests {
    private NewApiImplementation api;
    private MockEventBus eventBus;

    @Before
    public void initializeApi() throws Exception {
        eventBus = new MockEventBus();
        api = new NewApiImplementation(eventBus);
    }

    @Test
    public void testGetZoneSet() throws Exception {
        final Set<String> zoneSet = new HashSet();
        final Container<Exception> exceptionContainer = new Container();
        Thread apiThread = new Thread(() -> {
            try {
                zoneSet.addAll(api.getZoneSet());
            } 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 root = new ZMI();
        StateMessage responseMessage = new StateMessage("", ModuleType.RMI, 0, 0, root, null, null);
        requestMessage.getFuture().complete(responseMessage);

        apiThread.join(100);
        assertFalse(apiThread.isAlive());
        assertNull(exceptionContainer.thing);

        assertThat(zoneSet, hasItems("/"));
    }

    @Test
    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(), null);
        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(), null);
        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 testInstallQuery() throws Exception {
        String name = "&query";
        String queryCode = "SELECT 1 AS one";
        long timeBefore = System.currentTimeMillis();
        api.installQuery(name, queryCode);
        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<Attribute, Entry<ValueQuery, ValueTime>> queries = updateMessage.getQueries();
        assertEquals(1, TestUtil.iterableSize(queries.keySet()));
        assertEquals(new ValueQuery("SELECT 1 AS one"), queries.get(new Attribute("&query")).getKey());
        long timestamp = queries.get(new Attribute("&query")).getValue().getValue();
        assertTrue(timeBefore <= timestamp);
        assertTrue(timestamp <= timeAfter);
    }

    @Test
    public void testUninstallQuery() throws Exception {
        String name = "&query";
        long timeBefore = System.currentTimeMillis();
        api.uninstallQuery(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<Attribute, Entry<ValueQuery, ValueTime>> 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 {
        ValueInt numProcesses = new ValueInt(42l);
        long timeBefore = System.currentTimeMillis();
        api.setAttributeValue("/uw/khaki13", "num_processes", numProcesses);
        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);
    }
}