Search in sources :

Example 21 with JsonMockConnection

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

the class JsonSignalHeadSocketServiceTest method testOnMessageChange.

@Test
public void testOnMessageChange() {
    JsonNode message;
    JsonMockConnection connection = new JsonMockConnection((DataOutputStream) null);
    JsonSignalHeadSocketService service = new JsonSignalHeadSocketService(connection);
    //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);
    try {
        // SignalHead Yellow
        message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, userName).put(JSON.STATE, SignalHead.YELLOW);
        service.onMessage(JsonSignalHead.SIGNAL_HEAD, message, Locale.ENGLISH);
        //state should be Yellow
        Assert.assertEquals(SignalHead.YELLOW, s.getState());
    } catch (IOException | JmriException | JsonException ex) {
        Assert.fail(ex.getMessage());
    }
    // Try to set SignalHead to FLASHLUNAR, should throw error, remain at Yellow
    Exception exception = null;
    try {
        message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, userName).put(JSON.STATE, SignalHead.FLASHLUNAR);
        service.onMessage(JsonSignalHead.SIGNAL_HEAD, message, Locale.ENGLISH);
    } catch (IOException | JmriException | JsonException ex) {
        exception = ex;
    }
    Assert.assertNotNull(exception);
    //state should be Yellow
    Assert.assertEquals(SignalHead.YELLOW, s.getAppearance());
}
Also used : JsonException(jmri.server.json.JsonException) SignalHead(jmri.SignalHead) JmriException(jmri.JmriException) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) JmriException(jmri.JmriException) IOException(java.io.IOException) JsonException(jmri.server.json.JsonException) JsonMockConnection(jmri.server.json.JsonMockConnection) Test(org.junit.Test)

Example 22 with JsonMockConnection

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

the class JsonSignalMastSocketServiceTest method testOnMessageChange.

@Test
@Ignore("Needs setup completed")
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:SM2";
    String userName = "SM2";
    SignalMastManager manager = InstanceManager.getDefault(SignalMastManager.class);
    SignalMast s = manager.provideSignalMast(sysName);
    s.setUserName(userName);
    try {
        // SignalMast Stop
        message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, userName).put(JSON.STATE, "Stop");
        service.onMessage(JsonSignalMast.SIGNAL_MAST, message, Locale.ENGLISH);
        //aspect should be Stop
        Assert.assertEquals("Stop", s.getAspect());
    } 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, userName).put(JSON.STATE, JSON.ASPECT_UNKNOWN);
        service.onMessage(JsonSignalMast.SIGNAL_MAST, message, Locale.ENGLISH);
        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, Locale.ENGLISH);
    } catch (JsonException ex) {
        exception = ex;
    } catch (IOException | JmriException ex) {
        Assert.fail(ex.getMessage());
    }
    Assert.assertNull(exception);
    Assert.assertEquals("Stop", s.getAspect());
}
Also used : JsonException(jmri.server.json.JsonException) JsonMockConnection(jmri.server.json.JsonMockConnection) SignalMastManager(jmri.SignalMastManager) SignalMast(jmri.SignalMast) JmriException(jmri.JmriException) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) JmriException(jmri.JmriException) IOException(java.io.IOException) JsonException(jmri.server.json.JsonException) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 23 with JsonMockConnection

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

the class JsonThrottleServiceFactoryTest method testGetSocketService.

/**
     * Test of getSocketService method, of class JsonThrottleServiceFactory.
     */
@Test
public void testGetSocketService() {
    JsonConnection connection = new JsonMockConnection((DataOutputStream) null);
    JsonThrottleServiceFactory instance = new JsonThrottleServiceFactory();
    JsonSocketService result = instance.getSocketService(connection);
    Assert.assertNotNull(result);
    Assert.assertTrue(JsonSocketService.class.isInstance(result));
}
Also used : JsonSocketService(jmri.server.json.JsonSocketService) JsonMockConnection(jmri.server.json.JsonMockConnection) JsonConnection(jmri.server.json.JsonConnection) Test(org.junit.Test)

Example 24 with JsonMockConnection

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

the class JsonRouteSocketServiceTest method testOnMessageChange.

public void testOnMessageChange() {
    // the route state on a sensorless route
    try {
        JsonMockConnection connection = new JsonMockConnection((DataOutputStream) null);
        JsonNode message;
        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");
        Turnout turnout1 = InstanceManager.getDefault(TurnoutManager.class).provideTurnout("IT1");
        turnout1.setCommandedState(Turnout.CLOSED);
        route1.setTurnoutsAlignedSensor(sensor1.getSystemName());
        Assert.assertTrue(route1.addOutputTurnout(turnout1.getSystemName(), Turnout.THROWN));
        route1.activateRoute();
        Assert.assertNotNull(route1.getTurnoutsAlgdSensor());
        // Route ACTIVE - becomes ACTIVE
        message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, "IR1").put(JSON.STATE, JSON.ACTIVE);
        service.onMessage(JsonRouteServiceFactory.ROUTE, message, Locale.ENGLISH);
        JUnitUtil.waitFor(() -> {
            return route1.getState() == Sensor.ACTIVE;
        }, "Route to activate");
        Assert.assertEquals(Sensor.ACTIVE, route1.getState());
        // Route INACTIVE - remains ACTIVE
        message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, "IR1").put(JSON.STATE, JSON.INACTIVE);
        service.onMessage(JsonRouteServiceFactory.ROUTE, message, Locale.ENGLISH);
        Assert.assertEquals(Sensor.ACTIVE, route1.getState());
        // Route UNKNOWN - remains ACTIVE
        message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, "IR1").put(JSON.STATE, JSON.UNKNOWN);
        service.onMessage(JsonRouteServiceFactory.ROUTE, message, Locale.ENGLISH);
        JUnitUtil.waitFor(() -> {
            return route1.getState() == Sensor.ACTIVE;
        }, "Route to activate");
        Assert.assertEquals(Sensor.ACTIVE, route1.getState());
        // Route TOGGLE - remains ACTIVE
        message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, "IR1").put(JSON.STATE, JSON.TOGGLE);
        service.onMessage(JsonRouteServiceFactory.ROUTE, message, Locale.ENGLISH);
        JUnitUtil.waitFor(() -> {
            return route1.getState() == Sensor.ACTIVE;
        }, "Route to activate");
        Assert.assertEquals(Sensor.ACTIVE, route1.getState());
        // Route TOGGLE - becomes ACTIVE
        sensor1.setKnownState(Sensor.INACTIVE);
        message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, "IR1").put(JSON.STATE, JSON.TOGGLE);
        service.onMessage(JsonRouteServiceFactory.ROUTE, message, Locale.ENGLISH);
        JUnitUtil.waitFor(() -> {
            return route1.getState() == Sensor.ACTIVE;
        }, "Route to activate");
        Assert.assertEquals(Sensor.ACTIVE, route1.getState());
        // Route Invalid State
        // invalid state
        message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, "IR1").put(JSON.STATE, 42);
        JsonException exception = null;
        try {
            service.onMessage(JsonRouteServiceFactory.ROUTE, message, Locale.ENGLISH);
        } catch (JsonException ex) {
            exception = ex;
        }
        Assert.assertEquals(Sensor.ACTIVE, route1.getState());
        Assert.assertNotNull(exception);
        Assert.assertEquals(HttpServletResponse.SC_BAD_REQUEST, exception.getCode());
    } catch (IOException | JmriException | JsonException ex) {
        Assert.fail(ex.getMessage());
    }
}
Also used : JsonException(jmri.server.json.JsonException) JmriException(jmri.JmriException) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) TurnoutManager(jmri.TurnoutManager) SensorManager(jmri.SensorManager) JsonMockConnection(jmri.server.json.JsonMockConnection) Turnout(jmri.Turnout) Route(jmri.Route) RouteManager(jmri.RouteManager) Sensor(jmri.Sensor)

Example 25 with JsonMockConnection

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

the class JsonTurnoutSocketServiceTest method testCtorSuccess.

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

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