-
Notifications
You must be signed in to change notification settings - Fork 325
/
JsonSignalMastSocketServiceTest.java
140 lines (121 loc) · 5.64 KB
/
JsonSignalMastSocketServiceTest.java
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
package jmri.server.json.signalmast;
import com.fasterxml.jackson.databind.JsonNode;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Locale;
import jmri.InstanceManager;
import jmri.JmriException;
import jmri.SignalHeadManager;
import jmri.SignalMast;
import jmri.SignalMastManager;
import jmri.implementation.VirtualSignalHead;
import jmri.server.json.JSON;
import jmri.server.json.JsonException;
import jmri.server.json.JsonMockConnection;
import jmri.server.json.JsonRequest;
import jmri.util.JUnitUtil;
import org.junit.Assert;
import org.junit.jupiter.api.*;
/**
*
* @author Paul Bender
* @author Randall Wood
* @author Steve Todd
*/
public class JsonSignalMastSocketServiceTest {
private Locale locale = Locale.ENGLISH;
@Test
public void testSignalMastChange() {
try {
//create a signalmast for testing
String sysName = "IF$shsm:basic:one-searchlight(IH2)";
String userName = "SM2";
InstanceManager.getDefault(SignalHeadManager.class).register(new VirtualSignalHead("IH2"));
SignalMast s = InstanceManager.getDefault(SignalMastManager.class).provideSignalMast(sysName);
s.setUserName(userName);
JsonMockConnection connection = new JsonMockConnection((DataOutputStream) null);
JsonNode message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, sysName);
JsonSignalMastSocketService service = new JsonSignalMastSocketService(connection);
service.onMessage(JsonSignalMast.SIGNAL_MAST, message, new JsonRequest(locale, JSON.V5, JSON.POST, 42));
// TODO: test that service is listener in SignalMastManager
message = connection.getMessage();
Assert.assertNotNull("Message is not null", message);
Assert.assertEquals(JSON.ASPECT_UNKNOWN, message.path(JSON.DATA).path(JSON.STATE).asText());
//change to Approach, and wait for change to show up
s.setAspect("Approach");
JUnitUtil.waitFor(() -> {
return s.getAspect().equals("Approach");
}, "SignalMast is now Approach");
message = connection.getMessage();
Assert.assertNotNull("Message is not null", message);
Assert.assertEquals("Approach", message.path(JSON.DATA).path(JSON.STATE).asText());
//change to Stop, and wait for change to show up
s.setAspect("Stop");
JUnitUtil.waitFor(() -> {
return s.getAspect().equals("Stop");
}, "SignalMast is now Stop");
message = connection.getMessage();
Assert.assertNotNull("Message is not null", message);
Assert.assertEquals("Stop", message.path(JSON.DATA).path(JSON.STATE).asText());
service.onClose();
// // TODO: test that service is no longer a listener in SignalMastManager
} catch (IOException | JmriException | JsonException ex) {
Assert.fail(ex.getMessage());
}
}
@Test
public void testOnMessageChange() {
JsonNode message;
JsonMockConnection connection = new JsonMockConnection((DataOutputStream) null);
JsonSignalMastSocketService service = new JsonSignalMastSocketService(connection);
//create a signalmast for testing
String sysName = "IF$shsm:basic:one-searchlight(IH2)";
String userName = "SM2";
InstanceManager.getDefault(SignalHeadManager.class).register(new VirtualSignalHead("IH2"));
SignalMast s = InstanceManager.getDefault(SignalMastManager.class).provideSignalMast(sysName);
s.setUserName(userName);
try {
// SignalMast Stop
message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, sysName).put(JSON.STATE, "Stop");
service.onMessage(JsonSignalMast.SIGNAL_MAST, message, new JsonRequest(locale, JSON.V5, JSON.POST, 42));
Assert.assertEquals("Stop", s.getAspect()); //aspect should be Stop
} catch (IOException | JmriException | JsonException ex) {
Assert.fail(ex.getMessage());
}
// Try to set SignalMast to Unknown, should throw error, remain at Stop
Exception exception = null;
try {
message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, sysName).put(JSON.STATE, JSON.ASPECT_UNKNOWN);
service.onMessage(JsonSignalMast.SIGNAL_MAST, message, new JsonRequest(locale, JSON.V5, JSON.POST, 42));
Assert.assertEquals("Stop", s.getAspect());
} catch (IOException | JmriException | JsonException ex) {
exception = ex;
}
Assert.assertNotNull(exception);
// set SignalMast no value, should throw error, remain at Stop
message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, sysName);
exception = null;
try {
service.onMessage(JsonSignalMast.SIGNAL_MAST, message, new JsonRequest(locale, JSON.V5, JSON.POST, 42));
} catch (JsonException ex) {
exception = ex;
} catch (IOException | JmriException ex) {
Assert.fail(ex.getMessage());
}
Assert.assertNull(exception);
Assert.assertEquals("Stop", s.getAspect());
}
// from here down is testing infrastructure
@BeforeEach
public void setUp() throws Exception {
JUnitUtil.setUp();
JUnitUtil.resetProfileManager();
JUnitUtil.initDefaultSignalMastManager();
JUnitUtil.initInternalSignalHeadManager();
JUnitUtil.initSignalMastLogicManager();
}
@AfterEach
public void tearDown() throws Exception {
JUnitUtil.tearDown();
}
}