Skip to content

Fix expanders for multiple modules #187

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 38 additions & 8 deletions src/main/java/io/lighty/yang/validator/formats/JsTree.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
import com.google.common.io.Resources;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import io.lighty.yang.validator.GroupArguments;
import io.lighty.yang.validator.config.Configuration;
import io.lighty.yang.validator.formats.utility.LyvNodeData;
import io.lighty.yang.validator.formats.utility.LyvStack;
import io.lighty.yang.validator.simplify.SchemaTree;
import java.io.IOException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
Expand All @@ -33,6 +35,7 @@
import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
import org.opendaylight.yangtools.yang.model.api.Module;
import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
Expand Down Expand Up @@ -72,12 +75,8 @@ public void emitFormat(final Module module) {

// Notifications
printLines(getNotificationsLines(singletonListInitializer, module));

LOG.info("</table>");
LOG.info("</div>");
LOG.info("{}", loadJS());
LOG.info("</body>");
LOG.info("</html>");
} else {
LOG.error(EMPTY_MODULE_EXCEPTION);
}
Expand Down Expand Up @@ -155,7 +154,7 @@ private List<Line> getRpcsLines(final SingletonListInitializer singletonListInit
private List<Line> getChildNodesLines(final SingletonListInitializer singletonListInitializer,
final Module module) {
final List<Line> lines = new ArrayList<>();
final String headerText = prepareHeader(module);
final String headerText = prepareModule(module);
LOG.info("{}", headerText);
for (final Module m : modelContext.getModules()) {
if (!m.getPrefix().equals(module.getPrefix())) {
Expand Down Expand Up @@ -249,18 +248,30 @@ private static String loadJS() {
return text;
}

private static String prepareHeader(final Module module) {
private static String prepareHeader() {
final URL url = Resources.getResource("header");
String text = "";
try {
text = Resources.toString(url, StandardCharsets.UTF_8);
} catch (final IOException e) {
LOG.error("Can not load text from header file");
}

return text;
}

private static String prepareModule(final Module module) {
final StringBuilder nameRevision = new StringBuilder(module.getName());
module.getRevision().ifPresent(value -> nameRevision.append("@").append(value));
final URL url = Resources.getResource("header");
final URL url = Resources.getResource("module");
String text = "";
try {
text = Resources.toString(url, StandardCharsets.UTF_8);
text = text.replace("<NAME_REVISION>", nameRevision);
text = text.replace("<NAMESPACE>", module.getNamespace().toString());
text = text.replace("<PREFIX>", module.getPrefix());
} catch (final IOException e) {
LOG.error("Can not load text from header file");
LOG.error("Can not load text from module file");
}

return text;
Expand Down Expand Up @@ -369,6 +380,16 @@ private void resolveDataNodeContainer(final Iterator<? extends DataSchemaNode> c
}
}


@Override
@SuppressFBWarnings(value = "SLF4J_SIGN_ONLY_FORMAT",
justification = "Valid output from LYV is dependent on Logback output")
void init(EffectiveModelContext context, SchemaTree tree, Configuration config) {
super.init(context, tree, config);
final String headerText = prepareHeader();
LOG.info("{}", headerText);
}

@Override
public Help getHelp() {
return new Help(HELP_NAME, HELP_DESCRIPTION);
Expand All @@ -379,6 +400,15 @@ public Optional<GroupArguments> getGroupArguments() {
return Optional.empty();
}

@Override
@SuppressFBWarnings(value = "SLF4J_SIGN_ONLY_FORMAT",
justification = "Valid output from LYV is dependent on Logback output")
public void close() {
LOG.info("{}", loadJS());
LOG.info("</body>");
LOG.info("</html>");
}

private static class SingletonListInitializer {

private int id;
Expand Down
12 changes: 1 addition & 11 deletions src/main/resources/header
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,4 @@ table.simple-tree-table tr.tree-closed .tree-icon:after {
content:"+"
}
</style>
</head><body>
<div> <b> module name <NAME_REVISION>, namespace <NAMESPACE>, prefix <PREFIX> </b></div><div>
<button type="button" id="expander" class="btn btn-danger">Expand All</button>
<button type="button" id="collapser" class="btn btn-info">Collapse All</button>
<table id="basic" class="table table-bordered table-striped simple-tree-table">
<th> element </th>
<th> schema </th>
<th> type </th>
<th> flag </th>
<th> status </th>
<th> path </th>
</head><body>
2 changes: 1 addition & 1 deletion src/main/resources/js
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ $.fn.simpleTreeTable = function(options) {

$.SimpleTreeTable = SimpleTreeTable;

$('#basic').simpleTreeTable({
$('.table.table-bordered.table-striped.simple-tree-table').simpleTreeTable({
expander: $('#expander'),
collapser: $('#collapser')
});
Expand Down
10 changes: 10 additions & 0 deletions src/main/resources/module
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<div> <b> module name <NAME_REVISION>, namespace <NAMESPACE>, prefix <PREFIX> </b></div><div>
<button type="button" id="expander" class="btn btn-danger">Expand All</button>
<button type="button" id="collapser" class="btn btn-info">Collapse All</button>
<table id="basic" class="table table-bordered table-striped simple-tree-table">
<th> element </th>
<th> schema </th>
<th> type </th>
<th> flag </th>
<th> status </th>
<th> path </th>
30 changes: 30 additions & 0 deletions src/test/java/io/lighty/yang/validator/formats/JsTreeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@

import static io.lighty.yang.validator.Main.startLyv;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;

import io.lighty.yang.validator.FormatTest;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
Expand Down Expand Up @@ -69,6 +71,34 @@ public void runDeviationTest() throws Exception {
runJsTreeTest("moduleDeviation.html");
}

@Test
public void testMultipleFiles() throws IOException {
final String module1 = Paths.get(yangPath + "/[email protected]").toString();
final String module2 = Paths.get(yangPath + "/[email protected]").toString();
builder.setYangModules(List.of(module1, module2));
final List<FormatPlugin> formats = new ArrayList<>();
formats.add(new JsTree());
formatter = new Format(formats);
builder.setFormat("jstree");
final var configuration = builder.build();
startLyv(configuration, formatter);
final Path outLog = Paths.get(outPath).resolve("out.log");
String fileCreated = Files.readString(outLog);
final String compareWith = Files.readString(outLog.resolveSibling("compare").resolve("multipleModules.html"));

//assert that file contains the first module
assertTrue(fileCreated.contains("<b> module name ietf-connection-oriented-oam@2019-04-16, "
+ "namespace urn:ietf:params:xml:ns:yang:ietf-connection-oriented-oam, prefix co-oam </b>"));
//assert that file contains the second module
assertTrue(fileCreated.contains("<b> module name ietf-routing@2018-03-13, "
+ "namespace urn:ietf:params:xml:ns:yang:ietf-routing, prefix rt </b>"));
//assert that there is only one <body> tag
final int bodyTagCount = fileCreated.split("<body>").length - 1;
assertEquals(1, bodyTagCount);
//assert that outputs match
assertEquals(fileCreated.replaceAll("\\s+", ""), compareWith.replaceAll("\\s+", ""));
}

private void runJsTreeTest(final String comapreWithFileName) throws Exception {
final Path outLog = Paths.get(outPath).resolve("out.log");
final String fileCreated = Files.readString(outLog);
Expand Down
32 changes: 32 additions & 0 deletions src/test/java/io/lighty/yang/validator/formats/JsonTreeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@
*/
package io.lighty.yang.validator.formats;

import static io.lighty.yang.validator.Main.startLyv;

import io.lighty.yang.validator.FormatTest;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import org.testng.Assert;
import org.testng.annotations.Test;

public class JsonTreeTest extends FormatTest {

Expand Down Expand Up @@ -55,6 +59,34 @@ public void runDeviationTest() throws Exception {
runJsonTreeTest("ModuleDeviation.json");
}

@Test
public void testMultipleFiles() throws IOException {
final var module1 = Paths.get(yangPath + "/[email protected]").toString();
final var module2 = Paths.get(yangPath + "/[email protected]").toString();
builder.setYangModules(List.of(module1, module2));
final List<FormatPlugin> formats = new ArrayList<>();
formats.add(new JsonTree());
formatter = new Format(formats);
builder.setFormat("json-tree");
final var configuration = builder.build();
startLyv(configuration, formatter);
final var outLog = Paths.get(outPath).resolve("out.log");
final String fileCreated = Files.readString(outLog).substring(Files.readString(outLog).indexOf("{"));
final String compareWith = Files.readString(outLog.resolveSibling("compare").resolve("multipleModules.json"));
final String compareModel1 = Files.readString(
outLog.resolveSibling("compare").resolve("connectionOrientedOam.json"))
.replaceFirst("\\{\\s+\"parsed-models\": \\[\\s+\\{\n", "")
.replaceFirst("]\n\\s*}$", "").replaceAll("\\n\\s*", "");
final String compareModel2 = Files.readString(
outLog.resolveSibling("compare").resolve("routing.json"))
.replaceFirst("\\{\\s+\"parsed-models\": \\[\\s+\\{\n", "")
.replaceFirst("]\n\\s*}$", "").replaceAll("\\n\\s*", "");

Assert.assertEquals(fileCreated.replaceAll("\\s+", ""), compareWith.replaceAll("\\s+", ""));
Assert.assertTrue(fileCreated.replaceAll("\\s+", "").contains(compareModel1.replaceAll("\\s+", "")));
Assert.assertTrue(fileCreated.replaceAll("\\s+", "").contains(compareModel2.replaceAll("\\s+", "")));
}

private void runJsonTreeTest(final String comapreWithFileName) throws Exception {
final Path outLog = Paths.get(outPath).resolve("out.log");
final String fileCreated = Files.readString(outLog);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@

$.SimpleTreeTable = SimpleTreeTable;

$('#basic').simpleTreeTable({
$('.table.table-bordered.table-striped.simple-tree-table').simpleTreeTable({
expander: $('#expander'),
collapser: $('#collapser')
});
Expand Down
2 changes: 1 addition & 1 deletion src/test/resources/out/compare/connectionOrientedOam.html
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,7 @@

$.SimpleTreeTable = SimpleTreeTable;

$('#basic').simpleTreeTable({
$('.table.table-bordered.table-striped.simple-tree-table').simpleTreeTable({
expander: $('#expander'),
collapser: $('#collapser')
});
Expand Down
2 changes: 1 addition & 1 deletion src/test/resources/out/compare/interfaces.html
Original file line number Diff line number Diff line change
Expand Up @@ -853,7 +853,7 @@

$.SimpleTreeTable = SimpleTreeTable;

$('#basic').simpleTreeTable({
$('.table.table-bordered.table-striped.simple-tree-table').simpleTreeTable({
expander: $('#expander'),
collapser: $('#collapser')
});
Expand Down
2 changes: 1 addition & 1 deletion src/test/resources/out/compare/ip.html
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,7 @@

$.SimpleTreeTable = SimpleTreeTable;

$('#basic').simpleTreeTable({
$('.table.table-bordered.table-striped.simple-tree-table').simpleTreeTable({
expander: $('#expander'),
collapser: $('#collapser')
});
Expand Down
Loading