m-chrzan.xyz
aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/pl/edu/mimuw/cloudatlas/agent/modules/RemikTest.java
blob: 8dec10bf9a766bb8dee781e41cfacf6d8d94bfbb (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
package pl.edu.mimuw.cloudatlas.agent.modules;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import java.util.HashMap;
import java.util.HashSet;

import pl.edu.mimuw.cloudatlas.agent.messages.AgentMessage;
import pl.edu.mimuw.cloudatlas.agent.messages.GetStateMessage;
import pl.edu.mimuw.cloudatlas.agent.messages.RequestStateMessage;
import pl.edu.mimuw.cloudatlas.agent.messages.ResponseMessage;
import pl.edu.mimuw.cloudatlas.agent.messages.StanikMessage;
import pl.edu.mimuw.cloudatlas.agent.messages.StateMessage;
import pl.edu.mimuw.cloudatlas.agent.MockExecutor;
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 RemikTest {
    private Remik remik;
    private MockExecutor executor;

    @Before
    public void setupLocals() {
        remik = new Remik();
        executor = new MockExecutor(remik);
    }

    @Test
    public void asksForStateOnStateRequest() throws Exception {
        CompletableFuture<ResponseMessage> future = new CompletableFuture();
        RequestStateMessage message = new RequestStateMessage("", 0, future);
        remik.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.RMI, getStateMessage.getRequestingModule());
    }

    @Test
    public void completesFutureOnReceivedState() throws Exception {
        CompletableFuture<ResponseMessage> future = new CompletableFuture();
        RequestStateMessage message = new RequestStateMessage("", 0, future);
        remik.handleTyped(message);

        ZMI zmi = new ZMI();
        zmi.getAttributes().add("timestamp", new ValueTime(42l));
        StateMessage response = new StateMessage("", ModuleType.RMI, 0, 0, zmi, new HashMap(), new HashSet());
        remik.handleTyped(response);

        ResponseMessage passedResponse = future.get(100, TimeUnit.MILLISECONDS);
        assertNotNull(passedResponse);
        assertEquals(ResponseMessage.Type.STATE, passedResponse.getType());
        StateMessage stateMessage = (StateMessage) passedResponse;
        assertEquals(new ValueTime(42l), stateMessage.getZMI().getAttributes().get("timestamp"));
    }
}