|
| 1 | +package org.opentripplanner.ext.siri.updater.mqtt; |
| 2 | + |
| 3 | +import static org.opentripplanner.framework.application.OtpFileNames.ROUTER_CONFIG_FILENAME; |
| 4 | +import static org.opentripplanner.framework.io.FileUtils.assertFileEquals; |
| 5 | +import static org.opentripplanner.framework.io.FileUtils.readFile; |
| 6 | +import static org.opentripplanner.framework.io.FileUtils.writeFile; |
| 7 | +import static org.opentripplanner.generate.doc.framework.TemplateUtil.replaceSection; |
| 8 | +import static org.opentripplanner.standalone.config.framework.json.JsonSupport.jsonNodeFromResource; |
| 9 | +import static org.opentripplanner.utils.text.MarkdownFormatter.HEADER_4; |
| 10 | + |
| 11 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 12 | +import java.io.File; |
| 13 | +import java.util.Set; |
| 14 | +import org.junit.jupiter.api.Test; |
| 15 | +import org.opentripplanner.generate.doc.framework.DocBuilder; |
| 16 | +import org.opentripplanner.generate.doc.framework.DocsTestConstants; |
| 17 | +import org.opentripplanner.generate.doc.framework.GeneratesDocumentation; |
| 18 | +import org.opentripplanner.generate.doc.framework.ParameterDetailsList; |
| 19 | +import org.opentripplanner.generate.doc.framework.ParameterSummaryTable; |
| 20 | +import org.opentripplanner.generate.doc.framework.SkipNodes; |
| 21 | +import org.opentripplanner.standalone.config.RouterConfig; |
| 22 | +import org.opentripplanner.standalone.config.framework.json.NodeAdapter; |
| 23 | + |
| 24 | +@GeneratesDocumentation |
| 25 | +public class SiriMqttConfigurationDocTest implements DocsTestConstants { |
| 26 | + |
| 27 | + private static final File TEMPLATE = new File(TEMPLATE_PATH, "sandbox/siri/SiriMqttUpdater.md"); |
| 28 | + private static final File OUT_FILE = new File(SANDBOX_USER_DOC_PATH, "siri/SiriMqttUpdater.md"); |
| 29 | + private static final String ROUTER_CONFIG_PATH = "standalone/config/" + ROUTER_CONFIG_FILENAME; |
| 30 | + private static final Set<String> INCLUDE_UPDATERS = Set.of("siri-et-mqtt"); |
| 31 | + private static final SkipNodes SKIP_NODES = SkipNodes.of().build(); |
| 32 | + public static final ObjectMapper mapper = new ObjectMapper(); |
| 33 | + |
| 34 | + /** |
| 35 | + * NOTE! This test updates the {@code doc/user/sandbox/SIRI-Config.md} document based on the latest |
| 36 | + * version of the code. |
| 37 | + */ |
| 38 | + @Test |
| 39 | + public void updateSiriDoc() { |
| 40 | + NodeAdapter node = readUpdaterConfig(); |
| 41 | + |
| 42 | + // Read and close input file (same as output file) |
| 43 | + String template = readFile(TEMPLATE); |
| 44 | + String original = readFile(OUT_FILE); |
| 45 | + |
| 46 | + for (String childName : node.listChildrenByName()) { |
| 47 | + var child = node.child(childName); |
| 48 | + var type = child.typeQualifier(); |
| 49 | + |
| 50 | + if (INCLUDE_UPDATERS.contains(type)) { |
| 51 | + template = replaceSection(template, type, updaterDoc(child)); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + writeFile(OUT_FILE, template); |
| 56 | + assertFileEquals(original, OUT_FILE); |
| 57 | + } |
| 58 | + |
| 59 | + private NodeAdapter readUpdaterConfig() { |
| 60 | + var json = jsonNodeFromResource(ROUTER_CONFIG_PATH); |
| 61 | + var conf = new RouterConfig(json, ROUTER_CONFIG_PATH, false); |
| 62 | + return conf.asNodeAdapter().child("updaters"); |
| 63 | + } |
| 64 | + |
| 65 | + private String updaterDoc(NodeAdapter node) { |
| 66 | + DocBuilder buf = new DocBuilder(); |
| 67 | + addParameterSummaryTable(buf, node); |
| 68 | + addDetailsSection(buf, node); |
| 69 | + addExample(buf, node); |
| 70 | + return buf.toString(); |
| 71 | + } |
| 72 | + |
| 73 | + private void addParameterSummaryTable(DocBuilder buf, NodeAdapter node) { |
| 74 | + buf.addSection(new ParameterSummaryTable(SKIP_NODES).createTable(node).toMarkdownTable()); |
| 75 | + } |
| 76 | + |
| 77 | + private void addDetailsSection(DocBuilder buf, NodeAdapter node) { |
| 78 | + String details = getParameterDetailsTable(node); |
| 79 | + |
| 80 | + if (!details.isBlank()) { |
| 81 | + buf.header(5, "Parameter details", null).addSection(details); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + private String getParameterDetailsTable(NodeAdapter node) { |
| 86 | + return ParameterDetailsList.listParametersWithDetails(node, SKIP_NODES, HEADER_4); |
| 87 | + } |
| 88 | + |
| 89 | + private void addExample(DocBuilder buf, NodeAdapter node) { |
| 90 | + buf.addSection("##### Example configuration"); |
| 91 | + buf.addUpdaterExample(ROUTER_CONFIG_FILENAME, node.rawNode()); |
| 92 | + } |
| 93 | +} |
0 commit comments