Initialisation depot

This commit is contained in:
Serge NOEL
2026-06-17 14:00:51 +02:00
commit efdf1683af
33190 changed files with 5907629 additions and 0 deletions
@@ -0,0 +1,49 @@
package jmri.server.json.reporter;
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,221 @@
package jmri.server.json.reporter;
import static jmri.server.json.reporter.JsonReporter.REPORTER;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.annotation.Nonnull;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.NullNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import jmri.InstanceManager;
import jmri.JmriException;
import jmri.Reporter;
import jmri.ReporterManager;
import jmri.jmrit.operations.locations.Location;
import jmri.jmrit.operations.locations.LocationManager;
import jmri.jmrix.internal.InternalReporterManager;
import jmri.jmrix.internal.InternalSystemConnectionMemo;
import jmri.server.json.JSON;
import jmri.server.json.JsonException;
import jmri.server.json.JsonNamedBeanHttpServiceTestBase;
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.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
/**
*
* @author Paul Bender
* @author Randall Wood
*/
public class JsonReporterHttpServiceTest extends JsonNamedBeanHttpServiceTestBase<Reporter, JsonReporterHttpService> {
@Test
@Override
public void testDoGet() throws JmriException, JsonException {
ReporterManager manager = InstanceManager.getDefault(ReporterManager.class);
Reporter reporter1 = manager.provideReporter("IR1"); // no value
JsonNode result;
result = service.doGet(REPORTER, "IR1", NullNode.getInstance(), new JsonRequest(locale, JSON.V5, JSON.GET, 42));
validate(result);
assertEquals(REPORTER, result.path(JSON.TYPE).asText());
assertEquals("IR1", result.path(JSON.DATA).path(JSON.NAME).asText());
// JSON node has the text "null" if reporter is null
assertEquals("null", result.path(JSON.DATA).path(JsonReporter.REPORT).asText());
reporter1.setReport("throw");
result = service.doGet(REPORTER, "IR1", service.getObjectMapper().createObjectNode(), new JsonRequest(locale, JSON.V5, JSON.GET, 42));
validate(result);
assertEquals("throw", result.path(JSON.DATA).path(JsonReporter.REPORT).asText());
reporter1.setReport("close");
result = service.doGet(REPORTER, "IR1", service.getObjectMapper().createObjectNode(), new JsonRequest(locale, JSON.V5, JSON.GET, 42));
validate(result);
assertEquals("close", result.path(JSON.DATA).path(JsonReporter.REPORT).asText());
// Request a non-existent reporter
JsonException ex = assertThrows( JsonException.class, () ->
service.doGet(REPORTER, "IR2", service.getObjectMapper().createObjectNode(),
new JsonRequest(locale, JSON.V5, JSON.GET, 42)),
"Expected exception not thrown.");
assertEquals(404, ex.getCode());
}
@Test
public void testDoPost() throws JmriException, JsonException {
ReporterManager manager = InstanceManager.getDefault(ReporterManager.class);
Reporter reporter1 = manager.provideReporter("IR1");
reporter1.setUserName("test reporter");
JsonNode result;
JsonNode message;
// set non-null report
message = mapper.createObjectNode().put(JSON.NAME, "IR1").put(JsonReporter.REPORT, "close");
result = service.doPost(REPORTER, "IR1", message, new JsonRequest(locale, JSON.V5, JSON.GET, 42));
assertEquals("close", reporter1.getCurrentReport());
validate(result);
assertEquals("close", result.path(JSON.DATA).path(JsonReporter.REPORT).asText());
// set different non-null report
message = mapper.createObjectNode().put(JSON.NAME, "IR1").put(JsonReporter.REPORT, "throw");
result = service.doPost(REPORTER, "IR1", message, new JsonRequest(locale, JSON.V5, JSON.GET, 42));
assertEquals("throw", reporter1.getCurrentReport());
validate(result);
assertEquals("throw", result.path(JSON.DATA).path(JsonReporter.REPORT).asText());
// set null report
message = mapper.createObjectNode().put(JSON.NAME, "IR1").putNull(JsonReporter.REPORT);
result = service.doPost(REPORTER, "IR1", message, new JsonRequest(locale, JSON.V5, JSON.GET, 42));
assertNull(reporter1.getCurrentReport());
assertEquals("null", result.path(JSON.DATA).path(JsonReporter.REPORT).asText());
// set new user name
message = mapper.createObjectNode().put(JSON.NAME, "IR1").put(JSON.USERNAME, "TEST REPORTER");
assertEquals( "test reporter", reporter1.getUserName(), "expected name");
result = service.doPost(REPORTER, "IR1", message, new JsonRequest(locale, JSON.V5, JSON.GET, 42));
assertEquals( "TEST REPORTER", reporter1.getUserName(), "new name");
validate(result);
assertEquals( "TEST REPORTER", result.path(JSON.DATA).path(JSON.USERNAME).asText(),
"new name in JSON");
// set comment
message = mapper.createObjectNode().put(JSON.NAME, "IR1").put(JSON.COMMENT, "a comment");
assertNull( reporter1.getComment(), "null comment");
result = service.doPost(REPORTER, "IR1", message, new JsonRequest(locale, JSON.V5, JSON.GET, 42));
assertEquals( "a comment", reporter1.getComment(), "new comment");
validate(result);
assertEquals( "a comment", result.path(JSON.DATA).path(JSON.COMMENT).asText(),
"new comment in JSON");
// post non-existent reporter
JsonException ex = assertThrows( JsonException.class, () -> {
// set off
JsonNode messageEx = mapper.createObjectNode().put(JSON.NAME, "JR1").put(JsonReporter.REPORT, "close");
service.doPost(REPORTER, "JR1", messageEx, new JsonRequest(locale, JSON.V5, JSON.GET, 42));
}, "Expected exception not thrown ");
assertEquals( 404, ex.getCode(), "Not found thrown");
}
@Test
public void testDoPut() throws JsonException {
ReporterManager manager = InstanceManager.getDefault(ReporterManager.class);
JsonNode message;
// add a reporter
assertNull(manager.getReporter("IR1"));
message = mapper.createObjectNode().put(JSON.NAME, "IR1").put(JsonReporter.REPORT, "close");
service.doPut(REPORTER, "IR1", message, new JsonRequest(locale, JSON.V5, JSON.GET, 42));
assertNotNull(manager.getReporter("IR1"));
// add a reporter with invalid name
assertNull(manager.getReporter("JR1"));
// create a default reporter manager that overrides provide() to require valid system name
// this allows testing that invalid names are reported to clients correctly
InstanceManager.setDefault(ReporterManager.class, new InternalReporterManager(InstanceManager.getDefault(InternalSystemConnectionMemo.class)) {
@Nonnull
@Override
public Reporter provide(@Nonnull String name) {
return this.newReporter(name, null);
}
});
JsonNode messageEx = mapper.createObjectNode().put(JSON.NAME, "JR1").put(JsonReporter.REPORT, "close");
JsonException ex = assertThrows( JsonException.class, () ->
service.doPut(REPORTER, "JR1", messageEx,
new JsonRequest(locale, JSON.V5, JSON.GET, 42)),
"JR1 should not have been created");
JUnitAppender.assertErrorMessageStartsWith("Invalid system name for reporter");
assertEquals( 400, ex.getCode(), "400 name was invalid");
}
@Test
@Override
public void testDoDelete() throws JsonException {
ReporterManager manager = InstanceManager.getDefault(ReporterManager.class);
Reporter reporter1 = manager.provide("IR1");
Location location1 = new Location("1", "Location 1");
location1.setReporter(reporter1);
InstanceManager.getDefault(LocationManager.class).register(location1);
ObjectNode message = mapper.createObjectNode();
assertNotNull(message);
// add a reporter
assertNotNull(manager.getReporter("IR1"));
service.doDelete(REPORTER, "IR1", NullNode.getInstance(), new JsonRequest(locale, JSON.V5, JSON.GET, 0));
assertNull(manager.getReporter("IR1"));
manager.provide("IR1").addPropertyChangeListener(new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent evt) {
// do nothing
}
}, "IR1", "Test Listener");
// delete an idTag with a named listener ref
assertNotNull(manager.getReporter("IR1"));
message = mapper.createObjectNode().put(JSON.NAME, "IR1");
JsonException ex = assertThrows( JsonException.class, () ->
service.doDelete(REPORTER, "IR1", NullNode.getInstance(),
new JsonRequest(locale, JSON.V5, JSON.GET, 0)),
"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());
message = message.put(JSON.FORCE_DELETE, ex.getAdditionalData().path(JSON.FORCE_DELETE).asText());
assertNotNull(manager.getReporter("IR1"));
// will throw if prior catch failed
service.doDelete(REPORTER, "IR1", message, new JsonRequest(locale, JSON.V5, JSON.GET, 0));
assertNull(manager.getBySystemName("IR1"));
ex = assertThrows( JsonException.class, () ->
service.doDelete(REPORTER, "IR1", NullNode.getInstance(),
new JsonRequest(locale, JSON.V5, JSON.GET, 0)),
"deleting again should throw an exception.");
assertEquals(404, ex.getCode());
}
@Test
public void testDoGetList() throws JsonException {
ReporterManager manager = InstanceManager.getDefault(ReporterManager.class);
JsonNode result;
result = service.doGetList(REPORTER, mapper.createObjectNode(), new JsonRequest(locale, JSON.V5, JSON.GET, 0));
validate(result);
assertEquals(0, result.size());
manager.provideReporter("IR1");
manager.provideReporter("IR2");
result = service.doGetList(REPORTER, mapper.createObjectNode(), new JsonRequest(locale, JSON.V5, JSON.GET, 0));
validate(result);
assertEquals(2, result.size());
}
@BeforeEach
@Override
public void setUp() throws Exception {
super.setUp();
service = new JsonReporterHttpService(mapper);
JUnitUtil.initReporterManager();
JUnitUtil.initInternalSensorManager();
JUnitUtil.initDebugThrottleManager();
}
@AfterEach
@Override
public void tearDown() throws Exception {
super.tearDown();
}
}
@@ -0,0 +1,111 @@
package jmri.server.json.reporter;
import com.fasterxml.jackson.databind.JsonNode;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Locale;
import jmri.InstanceManager;
import jmri.JmriException;
import jmri.Reporter;
import jmri.ReporterManager;
import jmri.server.json.JSON;
import jmri.server.json.JsonException;
import jmri.server.json.JsonMockConnection;
import jmri.server.json.JsonRequest;
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.assertNull;
/**
*
* @author Paul Bender
* @author Randall Wood
*/
public class JsonReporterSocketServiceTest {
private final Locale locale = Locale.ENGLISH;
@Test
public void testReporterChange() throws IOException, JmriException, JsonException {
JsonMockConnection connection = new JsonMockConnection((DataOutputStream) null);
JsonNode message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, "IR1");
JsonReporterSocketService service = new JsonReporterSocketService(connection);
ReporterManager manager = InstanceManager.getDefault(ReporterManager.class);
Reporter memory1 = manager.provideReporter("IR1");
service.onMessage(JsonReporter.REPORTER, message, new JsonRequest(locale, JSON.V5, JSON.POST, 42));
// TODO: test that service is listener in ReporterManager
// default null value of memory1 has text representation "null" in JSON
message = connection.getMessage();
assertNotNull( message, "Message is not null");
assertEquals("null", message.path(JSON.DATA).path(JsonReporter.REPORT).asText());
memory1.setReport("throw");
JUnitUtil.waitFor(() -> {
return memory1.getCurrentReport().equals("throw");
}, "Reporter to throw");
message = connection.getMessage();
assertNotNull( message, "Message is not null");
assertEquals("throw", message.path(JSON.DATA).path(JsonReporter.REPORT).asText());
memory1.setReport("close");
JUnitUtil.waitFor(() -> {
return memory1.getCurrentReport().equals("close");
}, "Reporter to close");
message = connection.getMessage();
assertNotNull( message, "Message is not null");
assertEquals("close", memory1.getCurrentReport());
assertEquals("close", message.path(JSON.DATA).path(JsonReporter.REPORT).asText());
service.onClose();
// TODO: test that service is no longer a listener in ReporterManager
}
@Test
public void testOnMessageChange() throws IOException, JmriException, JsonException {
JsonMockConnection connection = new JsonMockConnection((DataOutputStream) null);
JsonNode message;
JsonReporterSocketService service = new JsonReporterSocketService(connection);
ReporterManager manager = InstanceManager.getDefault(ReporterManager.class);
Reporter memory1 = manager.provideReporter("IR1");
// Reporter "close"
message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, "IR1").put(JsonReporter.REPORT, "close");
service.onMessage(JsonReporter.REPORTER, message, new JsonRequest(locale, JSON.V5, JSON.POST, 42));
assertEquals("close", memory1.getCurrentReport());
// Reporter "throw"
message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, "IR1").put(JsonReporter.REPORT, "throw");
service.onMessage(JsonReporter.REPORTER, message, new JsonRequest(locale, JSON.V5, JSON.POST, 42));
assertEquals("throw", memory1.getCurrentReport());
// Reporter UNKNOWN - remains ON
message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, "IR1").putNull(JsonReporter.REPORT);
service.onMessage(JsonReporter.REPORTER, message, new JsonRequest(locale, JSON.V5, JSON.POST, 42));
assertNull( memory1.getCurrentReport());
memory1.setReport("throw");
// Reporter no value
JsonNode messageNoEx = connection.getObjectMapper().createObjectNode().put(JSON.NAME, "IR1");
assertDoesNotThrow( () ->
service.onMessage(JsonReporter.REPORTER, messageNoEx,
new JsonRequest(locale, JSON.V5, JSON.POST, 42)));
assertEquals("throw", memory1.getCurrentReport());
}
@BeforeEach
public void setUp() throws Exception {
JUnitUtil.setUp();
JUnitUtil.resetProfileManager();
JUnitUtil.initReporterManager();
}
@AfterEach
public void tearDown() throws Exception {
JUnitUtil.tearDown();
}
}
@@ -0,0 +1,46 @@
package jmri.server.json.reporter;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import jmri.util.JUnitUtil;
import org.junit.jupiter.api.*;
/**
*
* @author Randall Wood Copyright 2018
*/
public class JsonReporterTest {
@BeforeEach
public void setUp() {
JUnitUtil.setUp();
}
@AfterEach
public void tearDown() {
JUnitUtil.tearDown();
}
@Test
public void testConstructor() {
// because the constructor throws UnsupportedOperationException, and
// that is thrown by newInstance() into an InvocationTargetException
// we pass an InvocationTargetException that is caused by an
// UnsupportedOperationException and fail everything else.
InvocationTargetException ex = Assertions.assertThrows( InvocationTargetException.class, () -> {
Constructor<JsonReporter> constructor;
constructor = JsonReporter.class.getDeclaredConstructor();
constructor.setAccessible(true);
constructor.newInstance();
Assertions.fail("Instance of JsonReporter created");
});
UnsupportedOperationException cause = Assertions.assertInstanceOf(
UnsupportedOperationException.class, ex.getCause());
Assertions.assertNotNull(cause);
}
}