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
|
package com.orbekk.same;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import org.junit.Before;
import org.junit.Test;
import com.orbekk.util.DelayedOperation;
public class ClientTest {
private State state = new State("ClientNetwork");
private TestConnectionManager connections = new TestConnectionManager();
private Client client = new Client(state, connections,
"http://client/ClientService.json", null);
private ClientService clientS = client.getService();
private MasterService mockMaster = mock(MasterService.class);
@Before public void setUp() {
connections.masterMap.put("master", mockMaster);
}
@Test public void disconnectedFailsUpdate() throws Exception {
ClientInterface clientI = client.getInterface();
DelayedOperation op = clientI.set(null);
assertTrue(op.isDone());
assertFalse(op.getStatus().isOk());
}
@Test public void connectedUpdateWorks() throws Exception {
clientS.masterTakeover("master", null, 0);
ClientInterface clientI = client.getInterface();
State.Component component = new State.Component(
"TestVariable", 1, "meow");
when(mockMaster.updateStateRequest("TestVariable", "meow", 1))
.thenReturn(true);
DelayedOperation op = clientI.set(component);
assertTrue(op.getStatus().isOk());
}
@Test public void testSetState() throws Exception {
clientS.setState("TestState", "Test data", 100);
assertEquals(100, state.getRevision("TestState"));
assertEquals("Test data", state.getDataOf("TestState"));
}
@Test public void testNetworkListener() throws Exception {
NetworkNotificationListener listener =
mock(NetworkNotificationListener.class);
client.setNetworkListener(listener);
clientS.notifyNetwork("MyNetwork", "MasterUrl");
verify(listener).notifyNetwork("MyNetwork", "MasterUrl");
}
@Test public void discover() throws Exception {
clientS.setState(".masterUrl", "master", 1);
ClientService mockClient = mock(ClientService.class);
connections.clientMap.put("mockClient/ClientService.json",
mockClient);
client.discover("mockClient/ClientService.json");
verify(mockClient).notifyNetwork("ClientNetwork", "master");
}
@Test public void stateListenerReceivesUpdate() throws Exception {
StateChangedListener listener = mock(StateChangedListener.class);
client.getInterface().addStateListener(listener);
clientS.setState("StateListenerVariable", "100", 1);
State.Component component = state.getComponent("StateListenerVariable");
assertEquals("100", component.getData());
verify(listener).stateChanged(eq(component));
}
}
|