Search in sources :

Example 1 with JsonMockConnection

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);
}
Also used : JsonMockConnection(jmri.server.json.JsonMockConnection) DataOutputStream(java.io.DataOutputStream) Test(org.junit.Test)

Example 2 with JsonMockConnection

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);
    });
}
Also used : JsonMockConnection(jmri.server.json.JsonMockConnection) DataOutputStream(java.io.DataOutputStream) Test(org.junit.Test)

Example 3 with JsonMockConnection

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());
    }
}
Also used : JsonException(jmri.server.json.JsonException) JsonMockConnection(jmri.server.json.JsonMockConnection) SignalHead(jmri.SignalHead) JmriException(jmri.JmriException) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) Test(org.junit.Test)

Example 4 with JsonMockConnection

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);
}
Also used : JsonMockConnection(jmri.server.json.JsonMockConnection) DataOutputStream(java.io.DataOutputStream)

Example 5 with JsonMockConnection

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());
    }
}
Also used : JsonException(jmri.server.json.JsonException) JsonMockConnection(jmri.server.json.JsonMockConnection) Memory(jmri.Memory) JmriException(jmri.JmriException) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) MemoryManager(jmri.MemoryManager)

Aggregations

JsonMockConnection (jmri.server.json.JsonMockConnection)32 JsonNode (com.fasterxml.jackson.databind.JsonNode)20 IOException (java.io.IOException)18 JmriException (jmri.JmriException)18 JsonException (jmri.server.json.JsonException)18 Test (org.junit.Test)14 DataOutputStream (java.io.DataOutputStream)10 Sensor (jmri.Sensor)4 SensorManager (jmri.SensorManager)4 Turnout (jmri.Turnout)3 TurnoutManager (jmri.TurnoutManager)3 Locale (java.util.Locale)2 Light (jmri.Light)2 LightManager (jmri.LightManager)2 Memory (jmri.Memory)2 MemoryManager (jmri.MemoryManager)2 PowerManager (jmri.PowerManager)2 Reporter (jmri.Reporter)2 ReporterManager (jmri.ReporterManager)2 Route (jmri.Route)2