Initialisation depot

This commit is contained in:
Serge NOEL committed 2026-06-17 14:00:51 +02:00
commit efdf1683af
33190 files changed
+5907629

No files matched your search

@@ -0,0 +1,49 @@
package jmri.server.json.turnout;
import java.util.Locale;
import org.junit.jupiter.api.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
/**
* Tests for the Bundle class
*
* @author Bob Jacobsen Copyright (C) 2012
*/
public class BundleTest {
@Test
public void testGoodKeyMessage() {
assertEquals("Turnout", Bundle.getMessage("BeanNameTurnout"));
}
@Test
public void testBadKeyMessage() {
assertThrows(java.util.MissingResourceException.class, () -> Bundle.getMessage("FFFFFTTTTTTT"));
}
@Test
public void testGoodKeyMessageArg() {
assertEquals("Turnout", Bundle.getMessage("BeanNameTurnout", new Object[]{}));
assertEquals("About Test", Bundle.getMessage("TitleAbout", "Test"));
}
@Test
public void testBadKeyMessageArg() {
assertThrows(java.util.MissingResourceException.class, () -> Bundle.getMessage("FFFFFTTTTTTT", new Object[]{}));
}
@Test
public void testLocaleMessage() {
assertEquals("Scambio", Bundle.getMessage(Locale.ITALY, "BeanNameTurnout"));
}
@Test
public void testLocaleMessageArg() {
assertEquals("Scambio", Bundle.getMessage(Locale.ITALY, "BeanNameTurnout", new Object[]{}));
assertEquals("Informazioni su Test", Bundle.getMessage(Locale.ITALY, "TitleAbout", "Test"));
}
}
@@ -0,0 +1,240 @@
package jmri.server.json.turnout;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.NullNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import javax.servlet.http.HttpServletResponse;
import jmri.InstanceManager;
import jmri.JmriException;
import jmri.Sensor;
import jmri.SensorManager;
import jmri.Turnout;
import jmri.TurnoutManager;
import jmri.server.json.JSON;
import jmri.server.json.JsonException;
import jmri.server.json.JsonNamedBeanHttpServiceTestBase;
import jmri.server.json.JsonRequest;
import jmri.server.json.sensor.JsonSensor;
import jmri.util.JUnitUtil;
import org.junit.jupiter.api.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assumptions.assumeTrue;
/**
*
* @author Paul Bender
* @author Randall Wood
*/
public class JsonTurnoutHttpServiceTest extends JsonNamedBeanHttpServiceTestBase<Turnout, JsonTurnoutHttpService> {
@Test
@Override
public void testDoGet() throws JmriException, JsonException {
TurnoutManager manager = InstanceManager.getDefault(TurnoutManager.class);
Turnout turnout1 = manager.provideTurnout("IT1");
JsonNode result;
turnout1.setState(Turnout.UNKNOWN);
result = service.doGet(JsonTurnout.TURNOUT, "IT1",
NullNode.getInstance(), new JsonRequest(locale, JSON.V5, JSON.GET, 42));
validate(result);
assertEquals(JsonTurnout.TURNOUT, result.path(JSON.TYPE).asText());
assertEquals("IT1", result.path(JSON.DATA).path(JSON.NAME).asText());
assertEquals(JSON.UNKNOWN, result.path(JSON.DATA).path(JSON.STATE).asInt());
assertEquals( Turnout.DIRECT, result.path(JSON.DATA).path(JsonTurnout.FEEDBACK_MODE).asInt());
turnout1.setState(Turnout.CLOSED);
result = service.doGet(JsonTurnout.TURNOUT, "IT1",
NullNode.getInstance(), new JsonRequest(locale, JSON.V5, JSON.GET, 42));
validate(result);
assertEquals(JSON.CLOSED, result.path(JSON.DATA).path(JSON.STATE).asInt());
turnout1.setState(Turnout.THROWN);
result = service.doGet(JsonTurnout.TURNOUT, "IT1",
NullNode.getInstance(), new JsonRequest(locale, JSON.V5, JSON.GET, 42));
validate(result);
assertEquals(JSON.THROWN, result.path(JSON.DATA).path(JSON.STATE).asInt());
turnout1.setState(Turnout.INCONSISTENT);
result = service.doGet(JsonTurnout.TURNOUT, "IT1",
NullNode.getInstance(), new JsonRequest(locale, JSON.V5, JSON.GET, 42));
validate(result);
assertEquals(JSON.INCONSISTENT, result.path(JSON.DATA).path(JSON.STATE).asInt());
}
@Test
public void testDoPost() throws JmriException, JsonException {
TurnoutManager manager = InstanceManager.getDefault(TurnoutManager.class);
Turnout turnout1 = manager.provideTurnout("IT1");
JsonNode result;
JsonNode message;
turnout1.setState(Turnout.UNKNOWN);
// set closed
message = mapper.createObjectNode().put(JSON.NAME, "IT1").put(JSON.STATE, JSON.CLOSED);
result = service.doPost(JsonTurnout.TURNOUT, "IT1", message, new JsonRequest(locale, JSON.V5, JSON.GET, 42));
assertEquals(Turnout.CLOSED, turnout1.getState());
validate(result);
assertEquals(JSON.CLOSED, result.path(JSON.DATA).path(JSON.STATE).asInt());
// set thrown
message = mapper.createObjectNode().put(JSON.NAME, "IT1").put(JSON.STATE, JSON.THROWN);
result = service.doPost(JsonTurnout.TURNOUT, "IT1", message, new JsonRequest(locale, JSON.V5, JSON.GET, 42));
assertEquals(Turnout.THROWN, turnout1.getState());
validate(result);
assertEquals(JSON.THROWN, result.path(JSON.DATA).path(JSON.STATE).asInt());
// set unknown - remains thrown
message = mapper.createObjectNode().put(JSON.NAME, "IT1").put(JSON.STATE, JSON.UNKNOWN);
result = service.doPost(JsonTurnout.TURNOUT, "IT1", message, new JsonRequest(locale, JSON.V5, JSON.GET, 42));
assertEquals(Turnout.THROWN, turnout1.getState());
assertEquals(JSON.THROWN, result.path(JSON.DATA).path(JSON.STATE).asInt());
// set inverted - becomes closed
assertFalse(turnout1.getInverted());
message = mapper.createObjectNode().put(JSON.NAME, "IT1").put(JSON.INVERTED, true);
result = service.doPost(JsonTurnout.TURNOUT, "IT1", message, new JsonRequest(locale, JSON.V5, JSON.GET, 42));
assertTrue( turnout1.getInverted(), "Turnout is inverted");
assertEquals(JSON.CLOSED, result.path(JSON.DATA).path(JSON.STATE).asInt());
assertEquals(true, result.path(JSON.DATA).path(JSON.INVERTED).asBoolean());
// reset inverted - becomes thrown
message = mapper.createObjectNode().put(JSON.NAME, "IT1").put(JSON.INVERTED, false);
result = service.doPost(JsonTurnout.TURNOUT, "IT1", message, new JsonRequest(locale, JSON.V5, JSON.GET, 42));
assertFalse( turnout1.getInverted(), "Turnout is not inverted");
assertEquals(JSON.THROWN, result.path(JSON.DATA).path(JSON.STATE).asInt());
// set invalid state
JsonNode messageEx1 = mapper.createObjectNode().put(JSON.NAME, "IT1").put(JSON.STATE, 42); // Invalid value
JsonException ex = assertThrows( JsonException.class, () ->
service.doPost(JsonTurnout.TURNOUT, "IT1", messageEx1,
new JsonRequest(locale, JSON.V5, JSON.GET, 42)),
"Expected exception not thrown");
assertEquals(HttpServletResponse.SC_BAD_REQUEST, ex.getCode());
assertEquals(Turnout.THROWN, turnout1.getState());
// set feedback mode (valid)
assertEquals( Turnout.DIRECT, turnout1.getFeedbackMode());
message = mapper.createObjectNode().put(JSON.NAME, "IT1").put(JsonTurnout.FEEDBACK_MODE, Turnout.DELAYED);
result = service.doPost(JsonTurnout.TURNOUT, "IT1", message, new JsonRequest(locale, JSON.V5, JSON.GET, 42));
assertEquals( Turnout.DELAYED, turnout1.getFeedbackMode());
assertEquals( Turnout.DELAYED, result.path(JSON.DATA).path(JsonTurnout.FEEDBACK_MODE).asInt());
// set feedback mode (invalid)
JsonNode messageEx2 = mapper.createObjectNode().put(JSON.NAME, "IT1").put(JsonTurnout.FEEDBACK_MODE, -1);
ex = assertThrows( JsonException.class, () ->
service.doPost(JsonTurnout.TURNOUT, "IT1", messageEx2,
new JsonRequest(locale, JSON.V5, JSON.GET, 42)),
"Expected exception not thrown");
assertEquals( HttpServletResponse.SC_BAD_REQUEST, ex.getCode());
assertEquals( "Property feedbackMode is invalid for turnout IT1.", ex.getMessage());
assertEquals( 42, ex.getId());
assertEquals( Turnout.DELAYED, turnout1.getFeedbackMode());
// set feedback mode with sensor (sensor must exist first)
Sensor sensor1 = InstanceManager.getDefault(SensorManager.class).provide("IS1");
assertNull( turnout1.getFirstNamedSensor());
assertNull( turnout1.getSecondNamedSensor());
message = mapper.createObjectNode().put(JSON.NAME, "IT1")
.put(JsonTurnout.FEEDBACK_MODE, Turnout.TWOSENSOR)
.set(JsonSensor.SENSOR, mapper.createArrayNode().add(NullNode.getInstance()).add(sensor1.getSystemName()));
result = service.doPost(JsonTurnout.TURNOUT, "IT1", message, new JsonRequest(locale, JSON.V5, JSON.GET, 42));
assertNull( turnout1.getFirstNamedSensor());
var secondNamedSensor = turnout1.getSecondNamedSensor();
assertNotNull( secondNamedSensor);
assertEquals( sensor1, secondNamedSensor.getBean());
assertEquals( Turnout.TWOSENSOR, turnout1.getFeedbackMode());
assertEquals( Turnout.TWOSENSOR, result.path(JSON.DATA).path(JsonTurnout.FEEDBACK_MODE).asInt());
assertTrue( result.path(JSON.DATA).path(JsonSensor.SENSOR).get(0).isNull());
assertEquals( JsonSensor.SENSOR, result.path(JSON.DATA).path(JsonSensor.SENSOR).get(1).path(JSON.TYPE).asText());
assertEquals( sensor1.getSystemName(), result.path(JSON.DATA).path(JsonSensor.SENSOR).get(1).path(JSON.DATA).path(JSON.NAME).asText());
}
@Test
public void testDoPut() throws JsonException {
TurnoutManager manager = InstanceManager.getDefault(TurnoutManager.class);
JsonNode message;
// add a turnout
assertNull(manager.getTurnout("IT1"));
message = mapper.createObjectNode().put(JSON.NAME, "IT1").put(JSON.STATE, Turnout.CLOSED);
service.doPut(JsonTurnout.TURNOUT, "IT1", message, new JsonRequest(locale, JSON.V5, JSON.GET, 42));
assertNotNull(manager.getTurnout("IT1"));
}
@Test
public void testDoGetList() throws JsonException {
TurnoutManager manager = InstanceManager.getDefault(TurnoutManager.class);
JsonNode result;
result = service.doGetList(JsonTurnout.TURNOUT, mapper.createObjectNode(), new JsonRequest(locale, JSON.V5, JSON.GET, 0));
validate(result);
assertEquals(0, result.size());
manager.provideTurnout("IT1");
result = service.doGetList(JsonTurnout.TURNOUT, mapper.createObjectNode(), new JsonRequest(locale, JSON.V5, JSON.GET, 0));
validate(result);
assertEquals(1, result.size());
manager.provideTurnout("IT2");
result = service.doGetList(JsonTurnout.TURNOUT, mapper.createObjectNode(), new JsonRequest(locale, JSON.V5, JSON.GET, 0));
validate(result);
assertEquals(2, result.size());
}
@Test
@Override
public void testDoDelete() throws JsonException {
TurnoutManager manager = InstanceManager.getDefault(TurnoutManager.class);
ObjectNode message = mapper.createObjectNode();
assumeTrue(service != null, "protect against JUnit tests in Eclipse that test this class directly");
// delete non-existent bean
JsonException ex = assertThrows( JsonException.class, () ->
service.doDelete(service.getType(), "non-existant", message,
new JsonRequest(locale, JSON.V5, JSON.GET, 42)),
"Expected exception not thrown.");
assertEquals( 404, ex.getCode(), "Code is HTTP NOT FOUND");
assertEquals( "Object type turnout named \"non-existant\" not found.", ex.getMessage(), "Message");
assertEquals( 42, ex.getId(), "ID is 42");
manager.newTurnout("IT1", null);
// delete existing bean (no named listener)
assertNotNull(manager.getBySystemName("IT1"));
service.doDelete(service.getType(), "IT1", message, new JsonRequest(locale, JSON.V5, JSON.GET, 42));
assertNull(manager.getBySystemName("IT1"));
Turnout turnout = manager.newTurnout("IT1", null);
assertNotNull(turnout);
turnout.addPropertyChangeListener(evt -> {
// do nothing
}, "IT1", "Test Listener");
// delete existing bean (with named listener)
ex = assertThrows( JsonException.class, () ->
service.doDelete(service.getType(), "IT1", message,
new JsonRequest(locale, JSON.V5, JSON.GET, 42)),
"Expected exception not thrown.");
assertEquals(409, ex.getCode());
assertEquals(1, ex.getAdditionalData().path(JSON.CONFLICT).size());
assertEquals("Test Listener", ex.getAdditionalData().path(JSON.CONFLICT).path(0).asText());
ObjectNode message2 = message.put(JSON.FORCE_DELETE, ex.getAdditionalData().path(JSON.FORCE_DELETE).asText());
assertNotNull(manager.getBySystemName("IT1"));
// will throw if prior catch failed
service.doDelete(service.getType(), "IT1", message2, new JsonRequest(locale, JSON.V5, JSON.GET, 0));
assertNull(manager.getBySystemName("IT1"));
ex = assertThrows( JsonException.class, () ->
service.doDelete(service.getType(), "IT1", NullNode.getInstance(),
new JsonRequest(locale, JSON.V5, JSON.GET, 0)),
"deleting again should throw an exception.");
assertEquals(404, ex.getCode());
}
@BeforeEach
@Override
public void setUp() throws Exception {
super.setUp();
service = new JsonTurnoutHttpService(mapper);
JUnitUtil.initInternalTurnoutManager();
JUnitUtil.initInternalLightManager();
JUnitUtil.initInternalSensorManager();
JUnitUtil.initDebugThrottleManager();
}
@AfterEach
@Override
public void tearDown() throws Exception {
JUnitUtil.tearDown();
}
}
@@ -0,0 +1,28 @@
package jmri.server.json.turnout;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import jmri.server.json.JSON;
import jmri.server.json.JsonServiceFactoryTestBase;
/**
* @author Randall Wood Copyright 2020
*/
public class JsonTurnoutServiceFactoryTest extends JsonServiceFactoryTestBase<JsonTurnoutHttpService, JsonTurnoutSocketService> {
@Override
@Test
public void testGetTypesV5() {
assertThat(factory.getTypes(JSON.V5)).containsExactly(JsonTurnout.TURNOUT, JsonTurnout.TURNOUTS);
}
@BeforeEach
@Override
public void setUp() throws Exception {
super.setUp();
factory = new JsonTurnoutServiceFactory();
}
}
@@ -0,0 +1,188 @@
package jmri.server.json.turnout;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Locale;
import javax.servlet.http.HttpServletResponse;
import jmri.InstanceManager;
import jmri.JmriException;
import jmri.Turnout;
import jmri.TurnoutManager;
import jmri.server.json.JSON;
import jmri.server.json.JsonException;
import jmri.server.json.JsonMockConnection;
import jmri.server.json.JsonRequest;
import jmri.util.JUnitAppender;
import jmri.util.JUnitUtil;
import org.junit.jupiter.api.*;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
*
* @author Paul Bender
* @author Randall Wood
*/
public class JsonTurnoutSocketServiceTest {
private final Locale locale = Locale.ENGLISH;
@Test
public void testTurnoutChange() throws IOException, JmriException, JsonException {
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(JsonTurnout.TURNOUT, message, new JsonRequest(locale, JSON.V5, JSON.GET, 42));
// TODO: test that service is listener in TurnoutManager
message = connection.getMessage();
assertNotNull( message, "message is not null");
assertEquals(JSON.UNKNOWN, message.path(JSON.DATA).path(JSON.STATE).asInt());
turnout1.setCommandedState(Turnout.CLOSED);
JUnitUtil.waitFor(() -> {
return turnout1.getKnownState() == Turnout.CLOSED;
}, "Turnout to close");
message = connection.getMessage();
assertNotNull( message, "message is not null");
assertEquals(Turnout.CLOSED, turnout1.getKnownState());
assertEquals(JSON.CLOSED, message.path(JSON.DATA).path(JSON.STATE).asInt());
turnout1.setCommandedState(Turnout.THROWN);
JUnitUtil.waitFor(() -> {
return turnout1.getKnownState() == Turnout.THROWN;
}, "Turnout to throw");
message = connection.getMessage();
assertNotNull( message, "message is not null");
assertEquals(JSON.THROWN, message.path(JSON.DATA).path(JSON.STATE).asInt());
service.onClose();
// TODO: test that service is no longer a listener in TurnoutManager
}
@Test
public void testOnMessageChange() throws IOException, JmriException, JsonException {
JsonMockConnection connection = new JsonMockConnection((DataOutputStream) null);
JsonNode message;
JsonTurnoutSocketService service = new JsonTurnoutSocketService(connection);
TurnoutManager manager = InstanceManager.getDefault(TurnoutManager.class);
Turnout turnout1 = manager.provideTurnout("IT1");
turnout1.setCommandedState(Turnout.UNKNOWN);
// Turnout CLOSED
message =
connection.getObjectMapper().createObjectNode().put(JSON.NAME, "IT1").put(JSON.STATE, JSON.CLOSED);
service.onMessage(JsonTurnout.TURNOUT, message, new JsonRequest(locale, JSON.V5, JSON.POST, 42));
assertEquals(Turnout.CLOSED, turnout1.getState());
// Turnout THROWN
message =
connection.getObjectMapper().createObjectNode().put(JSON.NAME, "IT1").put(JSON.STATE, JSON.THROWN);
service.onMessage(JsonTurnout.TURNOUT, message, new JsonRequest(locale, JSON.V5, JSON.POST, 42));
assertEquals(Turnout.THROWN, turnout1.getState());
// Turnout UNKNOWN - remains THROWN
message =
connection.getObjectMapper().createObjectNode().put(JSON.NAME, "IT1").put(JSON.STATE, JSON.UNKNOWN);
service.onMessage(JsonTurnout.TURNOUT, message, new JsonRequest(locale, JSON.V5, JSON.POST, 42));
assertEquals(Turnout.THROWN, turnout1.getState());
// Turnout Invalid State
JsonNode messageEx = connection.getObjectMapper().createObjectNode().put(JSON.NAME, "IT1").put(JSON.STATE, 42); // invalid state
JsonException ex = assertThrows( JsonException.class, () ->
service.onMessage(JsonTurnout.TURNOUT, messageEx,
new JsonRequest(locale, JSON.V5, JSON.POST, 42)),
"Expected exception not thrown");
assertEquals(Turnout.THROWN, turnout1.getState());
assertEquals(HttpServletResponse.SC_BAD_REQUEST, ex.getCode());
}
@Test
public void testOnList() throws IOException, JmriException, JsonException {
JsonMockConnection connection = new JsonMockConnection((DataOutputStream) null);
JsonNode message;
JsonTurnoutSocketService service = new JsonTurnoutSocketService(connection);
TurnoutManager manager = InstanceManager.getDefault(TurnoutManager.class);
// provide and deregister in tests to avoid messages from the Turnouts themselves
Turnout turnout1 = manager.provide("IT1");
Turnout turnout2 = manager.provide("IT2");
manager.deregister(turnout1);
manager.deregister(turnout2);
service.onList(JsonTurnout.TURNOUT, connection.getObjectMapper().createObjectNode(),
new JsonRequest(locale, JSON.V5, JSON.GET, 0));
message = connection.getMessage();
assertNotNull( message, "Message is not null");
assertEquals( manager.getNamedBeanSet().size(), message.size(), "Manager and message have same size");
manager.register(turnout1);
JUnitUtil.waitFor(() -> {
return manager.getBySystemName("IT1") != null;
},"IT1 not null");
message = connection.getMessage();
assertNotNull( message, "Message is not null");
assertEquals( manager.getNamedBeanSet().size(), message.size(), "Manager and message have same size");
manager.register(turnout2);
JUnitUtil.waitFor(() -> {
return manager.getBySystemName("IT2") != null;
},"match for IT2");
message = connection.getMessage();
assertNotNull( message, "Message is not null");
assertEquals( manager.getNamedBeanSet().size(), message.size(), "Manager and message have same size");
assertNotNull( turnout2, "Turnout 2 exists");
assertDoesNotThrow( () ->
manager.deleteBean(turnout2, TurnoutManager.PROPERTY_DO_DELETE),
"Exception deleting bean ");
JUnitUtil.waitFor(() -> {
return manager.getBySystemName("IT2") == null;
},"no match for IT2");
message = connection.getMessage();
assertNotNull( message, "Message is not null");
assertEquals( manager.getNamedBeanSet().size(), message.size(), "Manager and message have same size");
connection.close();
}
@Test
public void testWarningOnUserName() throws IOException, JmriException, JsonException {
String userName = "Internal Turnout 1";
JsonMockConnection connection = new JsonMockConnection((DataOutputStream) null);
ObjectNode message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, userName);
JsonTurnoutSocketService service = new JsonTurnoutSocketService(connection);
TurnoutManager manager = InstanceManager.getDefault(TurnoutManager.class);
Turnout t1 = manager.provide("IT1");
t1.setUserName(userName);
// request with user name should log a warning
service.onMessage(JsonTurnout.TURNOUT, message, new JsonRequest(locale, JSON.V5, JSON.GET, 42));
JUnitAppender.assertWarnMessage("get request for turnout made with user name \"" + userName + "\"; should use system name");
// request with system name should not log a warning
message.put(JSON.NAME, "IT1");
service.onMessage(JsonTurnout.TURNOUT, message, new JsonRequest(locale, JSON.V5, JSON.GET, 42));
assertTrue(JUnitAppender.verifyNoBacklog());
// request with name of non-existent bean should not log a warning (but should throw exception)
JsonException ex = assertThrows( JsonException.class, () -> {
message.put(JSON.NAME, "IT2");
service.onMessage(JsonTurnout.TURNOUT, message, new JsonRequest(locale, JSON.V5, JSON.GET, 42));
}, "Expected exception not thrown");
assertEquals(HttpServletResponse.SC_NOT_FOUND, ex.getCode());
assertTrue(JUnitAppender.verifyNoBacklog());
}
@BeforeEach
public void setUp() throws Exception {
JUnitUtil.setUp();
JUnitUtil.resetProfileManager();
JUnitUtil.initInternalTurnoutManager();
}
@AfterEach
public void tearDown() throws Exception {
JUnitUtil.tearDown();
}
}
@@ -0,0 +1,55 @@
package jmri.server.json.turnout;
import static org.assertj.core.api.Assertions.fail;
import static org.assertj.core.api.Assertions.assertThat;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import jmri.util.JUnitUtil;
public class JsonTurnoutTest {
@Test
public void testConstants() {
assertThat(JsonTurnout.TURNOUT).isEqualTo("turnout");
assertThat(JsonTurnout.TURNOUTS).isEqualTo("turnouts");
assertThat(JsonTurnout.FEEDBACK_MODE).isEqualTo("feedbackMode");
assertThat(JsonTurnout.FEEDBACK_MODES).isEqualTo("feedbackModes");
}
@Test
public void testConstructor() {
// because the constructor throws UnsupportedOperationException, and
// that is thrown by newInstance() into an InvocationTargetException
// we assert the exception cause is the correct class.
InvocationTargetException ex = Assertions.assertThrows( InvocationTargetException.class, () -> {
Constructor<JsonTurnout> constructor;
constructor = JsonTurnout.class.getDeclaredConstructor();
constructor.setAccessible(true);
constructor.newInstance();
fail("Instance of JsonTurnout created");
});
UnsupportedOperationException cause = Assertions.assertInstanceOf(
UnsupportedOperationException.class, ex.getCause());
Assertions.assertNotNull(cause);
}
@BeforeEach
public void setUp() {
JUnitUtil.setUp();
}
@AfterEach
public void tearDown() {
JUnitUtil.tearDown();
}
}