summaryrefslogtreecommitdiff
path: root/same/src/test/java/com/orbekk/same/MasterServiceImplTest.java
blob: daa55a25edf0144e4fe4fe00a5dc21f617e798b0 (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
package com.orbekk.same;

import static org.junit.Assert.*;

import java.util.List;

import org.codehaus.jackson.type.TypeReference;
import org.junit.Test;
import org.junit.Before;

public class MasterServiceImplTest {
    private State state = new State("TestNetwork");
    private TestConnectionManager connections = new TestConnectionManager();
    private TestBroadcaster broadcaster = new TestBroadcaster();
    private MasterServiceImpl master = new MasterServiceImpl(state,
            connections, broadcaster);
    
    public static class UnreachableClient implements ClientService {
        @Override
        public void notifyNetwork(String networkName, String masterUrl)
                throws Exception {
            throw new Exception("Unreachable client");
        }

        @Override
        public void setState(String component, String data, long revision)
                throws Exception {
            throw new Exception("Unreachable client");   
        }

        @Override
        public void discoveryRequest(String remoteUrl) throws Exception {
            throw new Exception("Unreachable client");
        }
    }
    
    @Before
    public void setUp() {
        connections.masterMap.put("http://master", master);
    }
    
    @Test
    public void setsMasterUrl() {
        master.setUrl("http://10.0.0.54:10050/");
        assertEquals("http://10.0.0.54:10050/MasterService.json",
                state.getDataOf(".masterUrl"));
    }
    
    @Test
    public void testJsonState() {
        List<String> participants =
                state.getParsedData(".participants",
                        new TypeReference<List<String>>() { });
        assertEquals(participants.size(), 0);
        participants.add("http://SomeUrl/");
        state.updateFromObject(".participants", participants, 1);
    }
    
    @Test
    public void joinNetworkAddsClient() {
        master.joinNetworkRequest("http://clientUrl");
        List<String> participants = state.getList(".participants");
        assertTrue(participants.contains("http://clientUrl"));
    }
    
    @Test
    public void workLoopClearsUpdatedComponents() {
        state.update("Test", "Content", 0);
        assertTrue(master._performWork());
        assertTrue(state.getAndClearUpdatedComponents().isEmpty());
    }

    @Test
    public void clientJoin() {
        master.setUrl("http://master/");
        ClientServiceImpl client = new ClientServiceImpl(
                new State("ClientNetwork"), connections);
        ClientService clientS = client.getService();
        client.setUrl("http://client/");
        connections.clientMap.put("http://client/ClientService.json", clientS);
        client.joinNetwork("http://master");
        assertTrue(master._performWork());
        assertTrue(state.getList(".participants").contains("http://client/ClientService.json"));
        assertEquals(state, client.testGetState());
    }
    
    @Test
    public void validStateRequest() {
        master.setUrl("http://master/");
        ClientServiceImpl client1 = new ClientServiceImpl(
                new State("ClientNetwork"), connections);
        ClientService client1S = client1.getService();
        client1.setUrl("http://client/");
        connections.clientMap.put("http://client/ClientService.json", client1S);
        ClientServiceImpl client2 = new ClientServiceImpl(
                new State("ClientNetwork"), connections);
        ClientService client2S = client2.getService();
        client2.setUrl("http://client2/");
        connections.clientMap.put("http://client2/ClientService.json", client2S);
        
        client1.joinNetwork("http://master");
        client2.joinNetwork("http://master");
        
        assertTrue(master._performWork());
        assertTrue(state.getList(".participants").contains("http://client/ClientService.json"));
        assertTrue(state.getList(".participants").contains("http://client2/ClientService.json"));
        assertEquals(state, client1.testGetState());
        
        assertTrue(master.updateStateRequest("A", "1", 0));
        assertTrue(master._performWork());
        
        assertEquals(state, client1.testGetState());
        assertEquals(state, client2.testGetState());
        
        assertFalse(master.updateStateRequest("A", "2", 0));
        assertTrue(master.updateStateRequest("A", "3", 1));
        assertTrue(master._performWork());
        
        assertEquals(state, client1.testGetState());
        assertEquals(state, client2.testGetState());
    }
    
    @Test
    public void masterRemovesParticipant() {
        master.setUrl("http://master/");
        ClientServiceImpl client = new ClientServiceImpl(
                new State("ClientNetwork"), connections);
        ClientService clientS = client.getService();
        client.setUrl("http://client/");
        connections.clientMap.put("http://client/ClientService.json", clientS);
        client.joinNetwork("http://master");
        assertTrue(master._performWork());
        assertTrue(state.getList(".participants").contains("http://client/ClientService.json"));
        
        connections.clientMap.put("http://client/ClientService.json",
                new UnreachableClient());
        master.updateStateRequest("NewState", "NewStateData", 0);
        master._performWork();
        
        assertEquals("[]", state.getDataOf(".participants"));
    }
}