Search in sources :

Example 11 with JsonMockConnection

use of jmri.server.json.JsonMockConnection in project JMRI by JMRI.

the class JsonUtilSocketServiceTest method testOnMessage.

/**
     * Test of onMessage method, of class JsonUtilSocketService.
     *
     * @throws java.lang.Exception if an exception unexpected in the context of
     *                             these tests occurs
     */
@Test
public void testOnMessage() throws Exception {
    Locale locale = Locale.ENGLISH;
    JsonMockConnection connection = new JsonMockConnection((DataOutputStream) null);
    JsonNode empty = connection.getObjectMapper().createObjectNode();
    JsonUtilSocketService instance = new JsonUtilSocketService(connection);
    // JSON.LOCALE
    instance.onMessage(JSON.LOCALE, empty, locale);
    assertNull(connection.getMessage());
    // JSON.PING
    instance.onMessage(JSON.PING, empty, locale);
    JsonNode result = connection.getMessage().path(JSON.TYPE);
    assertNotNull(result);
    assertTrue(JsonNode.class.isInstance(result));
    assertEquals(JSON.PONG, result.asText());
    // JSON.GOODBYE
    instance.onMessage(JSON.GOODBYE, empty, locale);
    result = connection.getMessage().path(JSON.TYPE);
    assertNotNull(result);
    assertTrue(JsonNode.class.isInstance(result));
    assertEquals(JSON.GOODBYE, result.asText());
}
Also used : Locale(java.util.Locale) JsonMockConnection(jmri.server.json.JsonMockConnection) JsonNode(com.fasterxml.jackson.databind.JsonNode) Test(org.junit.Test)

Example 12 with JsonMockConnection

use of jmri.server.json.JsonMockConnection in project JMRI by JMRI.

the class JsonUtilSocketServiceTest method testOnList.

/**
     * Test of onList method, of class JsonUtilSocketService.
     *
     * @throws java.lang.Exception if an exception unexpected in the context of
     *                             these tests occurs
     */
@Test
public void testOnList() throws Exception {
    Locale locale = Locale.ENGLISH;
    ObjectMapper mapper = new ObjectMapper();
    JsonMockConnection connection = new JsonMockConnection((DataOutputStream) null);
    JsonNode empty = connection.getObjectMapper().createObjectNode();
    JsonUtilSocketService instance = new JsonUtilSocketService(connection);
    JsonUtilHttpService helper = new JsonUtilHttpService(mapper);
    JsonServerPreferences.getDefault().setHeartbeatInterval(10);
    instance.onList(JSON.METADATA, empty, locale);
    Assert.assertEquals(helper.getMetadata(locale), connection.getMessage());
    instance.onList(JSON.NETWORK_SERVICES, empty, locale);
    Assert.assertEquals(helper.getNetworkServices(locale), connection.getMessage());
    instance.onList(JSON.SYSTEM_CONNECTIONS, empty, locale);
    Assert.assertEquals(helper.getSystemConnections(locale), connection.getMessage());
    instance.onList(JSON.CONFIG_PROFILES, empty, locale);
    Assert.assertEquals(helper.getConfigProfiles(locale), connection.getMessage());
}
Also used : Locale(java.util.Locale) JsonMockConnection(jmri.server.json.JsonMockConnection) JsonNode(com.fasterxml.jackson.databind.JsonNode) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 13 with JsonMockConnection

use of jmri.server.json.JsonMockConnection in project JMRI by JMRI.

the class JsonTurnoutSocketServiceTest method testTurnoutChange.

public void testTurnoutChange() {
    try {
        JsonMockConnection connection = new JsonMockConnection((DataOutputStream) null);
        JsonNode message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, "IT1");
        JsonTurnoutSocketService service = new JsonTurnoutSocketService(connection);
        TurnoutManager manager = InstanceManager.getDefault(TurnoutManager.class);
        Turnout turnout1 = manager.provideTurnout("IT1");
        turnout1.setCommandedState(Turnout.UNKNOWN);
        service.onMessage(JsonTurnoutServiceFactory.TURNOUT, message, Locale.ENGLISH);
        // TODO: test that service is listener in TurnoutManager
        Assert.assertEquals(JSON.UNKNOWN, connection.getMessage().path(JSON.DATA).path(JSON.STATE).asInt());
        turnout1.setCommandedState(Turnout.CLOSED);
        JUnitUtil.waitFor(() -> {
            return turnout1.getKnownState() == Turnout.CLOSED;
        }, "Turnout to close");
        Assert.assertEquals(Turnout.CLOSED, turnout1.getKnownState());
        Assert.assertEquals(JSON.CLOSED, connection.getMessage().path(JSON.DATA).path(JSON.STATE).asInt());
        turnout1.setCommandedState(Turnout.THROWN);
        JUnitUtil.waitFor(() -> {
            return turnout1.getKnownState() == Turnout.THROWN;
        }, "Turnout to throw");
        Assert.assertEquals(JSON.THROWN, connection.getMessage().path(JSON.DATA).path(JSON.STATE).asInt());
        service.onClose();
    // TODO: test that service is no longer a listener in TurnoutManager
    } catch (IOException | JmriException | JsonException ex) {
        Assert.fail(ex.getMessage());
    }
}
Also used : JsonException(jmri.server.json.JsonException) JsonMockConnection(jmri.server.json.JsonMockConnection) JmriException(jmri.JmriException) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) TurnoutManager(jmri.TurnoutManager) Turnout(jmri.Turnout)

Example 14 with JsonMockConnection

use of jmri.server.json.JsonMockConnection in project JMRI by JMRI.

the class JsonRouteSocketServiceTest method testCtorSuccess.

public void testCtorSuccess() {
    JsonRouteSocketService service = new JsonRouteSocketService(new JsonMockConnection((DataOutputStream) null));
    Assert.assertNotNull(service);
}
Also used : JsonMockConnection(jmri.server.json.JsonMockConnection) DataOutputStream(java.io.DataOutputStream)

Example 15 with JsonMockConnection

use of jmri.server.json.JsonMockConnection in project JMRI by JMRI.

the class JsonRouteSocketServiceTest method testRouteChange.

public void testRouteChange() {
    // the route state on a sensorless route
    try {
        JsonMockConnection connection = new JsonMockConnection((DataOutputStream) null);
        JsonNode message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, "IR1");
        JsonRouteSocketService service = new JsonRouteSocketService(connection);
        RouteManager manager = InstanceManager.getDefault(RouteManager.class);
        Route route1 = manager.provideRoute("IR1", "Route1");
        Sensor sensor1 = InstanceManager.getDefault(SensorManager.class).provideSensor("IS1");
        sensor1.setKnownState(Sensor.UNKNOWN);
        route1.setTurnoutsAlignedSensor(sensor1.getSystemName());
        service.onMessage(JsonRouteServiceFactory.ROUTE, message, Locale.ENGLISH);
        // TODO: test that service is listener in RouteManager
        Assert.assertEquals(JSON.UNKNOWN, connection.getMessage().path(JSON.DATA).path(JSON.STATE).asInt());
        sensor1.setKnownState(Sensor.ACTIVE);
        JUnitUtil.waitFor(() -> {
            return route1.getState() == Sensor.ACTIVE;
        }, "Route to activate");
        Assert.assertEquals(JSON.ACTIVE, connection.getMessage().path(JSON.DATA).path(JSON.STATE).asInt());
        sensor1.setKnownState(Sensor.INACTIVE);
        JUnitUtil.waitFor(() -> {
            return route1.getState() == Sensor.INACTIVE;
        }, "Route to deactivate");
        Assert.assertEquals(Sensor.INACTIVE, route1.getState());
        Assert.assertEquals(JSON.INACTIVE, connection.getMessage().path(JSON.DATA).path(JSON.STATE).asInt());
        service.onClose();
    // TODO: test that service is no longer a listener in RouteManager
    } catch (IOException | JmriException | JsonException ex) {
        Assert.fail(ex.getMessage());
    }
}
Also used : JsonException(jmri.server.json.JsonException) SensorManager(jmri.SensorManager) JsonMockConnection(jmri.server.json.JsonMockConnection) JmriException(jmri.JmriException) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) Route(jmri.Route) RouteManager(jmri.RouteManager) Sensor(jmri.Sensor)

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