use of jmri.server.json.JsonMockConnection in project JMRI by JMRI.
the class JsonSignalMastSocketServiceTest method testCtorSuccess.
@Test
public void testCtorSuccess() {
JsonSignalMastSocketService service = new JsonSignalMastSocketService(new JsonMockConnection((DataOutputStream) null));
Assert.assertNotNull(service);
}
use of jmri.server.json.JsonMockConnection in project JMRI by JMRI.
the class JsonRosterSocketServiceTest method testOnClose.
/**
* Test of onClose method, of class JsonRosterSocketService.
*/
@Test
public void testOnClose() {
JsonRosterSocketService instance = new JsonRosterSocketService(new JsonMockConnection((DataOutputStream) null));
// listen to the roster, since onClose stops listening to the roster
instance.listen();
// assert we are listening
Assert.assertEquals(2, Roster.getDefault().getPropertyChangeListeners().length);
Roster.getDefault().getEntriesInGroup(Roster.ALLENTRIES).stream().forEach((entry) -> {
Assert.assertEquals(3, entry.getPropertyChangeListeners().length);
});
// the connection is closing, stop listening
instance.onClose();
// assert we are not listening
Assert.assertEquals(0, Roster.getDefault().getPropertyChangeListeners().length);
Roster.getDefault().getEntriesInGroup(Roster.ALLENTRIES).stream().forEach((entry) -> {
Assert.assertEquals(1, entry.getPropertyChangeListeners().length);
});
}
use of jmri.server.json.JsonMockConnection in project JMRI by JMRI.
the class JsonSignalHeadSocketServiceTest method testSignalHeadChange.
@Test
public void testSignalHeadChange() {
try {
//create a signalhead for testing
String sysName = "IH1";
String userName = "SH1";
SignalHead s = new jmri.implementation.VirtualSignalHead(sysName, userName);
jmri.InstanceManager.getDefault(jmri.SignalHeadManager.class).register(s);
Assert.assertNotNull(s);
JsonMockConnection connection = new JsonMockConnection((DataOutputStream) null);
JsonNode message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, sysName);
JsonSignalHeadSocketService service = new JsonSignalHeadSocketService(connection);
service.onMessage(JsonSignalHead.SIGNAL_HEAD, message, Locale.ENGLISH);
//signalhead defaults to Dark
Assert.assertEquals(SignalHead.DARK, connection.getMessage().path(JSON.DATA).path(JSON.STATE).asInt());
//change to Green, and wait for change to show up, then verify
s.setAppearance(SignalHead.GREEN);
JUnitUtil.waitFor(() -> {
return s.getState() == SignalHead.GREEN;
}, "SignalHead is now GREEN");
Assert.assertEquals(SignalHead.GREEN, connection.getMessage().path(JSON.DATA).path(JSON.STATE).asInt());
//change to Red, and wait for change to show up, then verify
s.setAppearance(SignalHead.RED);
JUnitUtil.waitFor(() -> {
return s.getState() == SignalHead.RED;
}, "SignalHead is now RED");
Assert.assertEquals(SignalHead.RED, connection.getMessage().path(JSON.DATA).path(JSON.STATE).asInt());
service.onClose();
} catch (IOException | JmriException | JsonException ex) {
Assert.fail(ex.getMessage());
}
}
use of jmri.server.json.JsonMockConnection in project JMRI by JMRI.
the class JsonMemorySocketServiceTest method testCtorSuccess.
public void testCtorSuccess() {
JsonMemorySocketService service = new JsonMemorySocketService(new JsonMockConnection((DataOutputStream) null));
Assert.assertNotNull(service);
}
use of jmri.server.json.JsonMockConnection in project JMRI by JMRI.
the class JsonMemorySocketServiceTest method testOnMessageChange.
public void testOnMessageChange() {
try {
JsonMockConnection connection = new JsonMockConnection((DataOutputStream) null);
JsonNode message;
JsonMemorySocketService service = new JsonMemorySocketService(connection);
MemoryManager manager = InstanceManager.getDefault(MemoryManager.class);
Memory memory1 = manager.provideMemory("IM1");
// Memory "close"
message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, "IM1").put(JSON.VALUE, "close");
service.onMessage(JsonMemory.MEMORY, message, Locale.ENGLISH);
Assert.assertEquals("close", memory1.getValue());
// Memory "throw"
message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, "IM1").put(JSON.VALUE, "throw");
service.onMessage(JsonMemory.MEMORY, message, Locale.ENGLISH);
Assert.assertEquals("throw", memory1.getValue());
// Memory UNKNOWN - remains ON
message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, "IM1").putNull(JSON.VALUE);
service.onMessage(JsonMemory.MEMORY, message, Locale.ENGLISH);
Assert.assertEquals(null, memory1.getValue());
memory1.setValue("throw");
// Memory no value
message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, "IM1");
JsonException exception = null;
try {
service.onMessage(JsonMemory.MEMORY, message, Locale.ENGLISH);
} catch (JsonException ex) {
exception = ex;
}
Assert.assertEquals("throw", memory1.getValue());
Assert.assertNull(exception);
} catch (IOException | JmriException | JsonException ex) {
Assert.fail(ex.getMessage());
}
}
Aggregations