From 2e2d4a6efc22e1803a9ee02503fbc15b381360f1 Mon Sep 17 00:00:00 2001 From: subhash kumar singh Date: Thu, 2 Nov 2017 13:45:27 +0000 Subject: Enable documentation generation for sample yaml Collect all sample yaml files to create documentation. Change-Id: I5374c51a7ba8e4744956b7fdef20e4449313d705 Issue-ID: CLI-55 Signed-off-by: subhash kumar singh --- .../onap/cli/moco/OnapCommandHttpMocoServer.java | 64 +++++++++++++++++++--- .../onap/cli/validation/OnapValidationTest.java | 44 +++++++++++++++ 2 files changed, 101 insertions(+), 7 deletions(-) (limited to 'validate/validation/src/test/java') diff --git a/validate/validation/src/test/java/org/onap/cli/moco/OnapCommandHttpMocoServer.java b/validate/validation/src/test/java/org/onap/cli/moco/OnapCommandHttpMocoServer.java index a3e23a64..488c6193 100644 --- a/validate/validation/src/test/java/org/onap/cli/moco/OnapCommandHttpMocoServer.java +++ b/validate/validation/src/test/java/org/onap/cli/moco/OnapCommandHttpMocoServer.java @@ -16,18 +16,25 @@ package org.onap.cli.moco; +import static com.github.dreamhead.moco.Moco.pathResource; import static com.github.dreamhead.moco.MocoJsonRunner.jsonHttpServer; import static com.github.dreamhead.moco.Runner.runner; -import static com.github.dreamhead.moco.Moco.pathResource; -import static com.github.dreamhead.moco.Moco.file; import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; import java.io.IOException; +import java.io.InputStream; import java.io.PrintStream; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.ArrayList; import java.util.Arrays; +import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.stream.Stream; import org.onap.cli.fw.OnapCommandRegistrar; import org.onap.cli.fw.error.OnapCommandException; @@ -78,6 +85,35 @@ public class OnapCommandHttpMocoServer { public OnapCommandHttpMocoServer() { } + public static Map> discoverYamls(File path) throws IOException { + Map> cliProductSamples = new HashMap<>(); + + Stream walk = Files.walk(path.toPath()); + walk.filter(p -> (p.toString().contains("src/test/resources/onap-cli-sample"))) + .filter(p -> p.toString().endsWith("sample.yaml")) + .forEach(p -> { + collectSamples(new File(p.toUri()), cliProductSamples); + }); + + return cliProductSamples; + } + + private static void collectSamples(File file, Map> result) { + OnapCommandHttpMocoServer onapCommandHttpMocoServer = new OnapCommandHttpMocoServer(); + List loadSamples; + try { + loadSamples = onapCommandHttpMocoServer.loadSamples(file); + loadSamples.stream().forEach(sample -> { + if (!result.containsKey(sample.getProduct())) { + result.put(sample.getProduct(), new ArrayList<>()); + } + result.get(sample.getProduct()).add(sample); + }); + } catch (OnapCommandInvalidSample e) { + LOG.error("Failed to read sample file", e); + } + } + private List dicoverSampleYamls() { Resource[] resources = new Resource [] {}; try { @@ -98,20 +134,19 @@ public class OnapCommandHttpMocoServer { return ""; } - private List loadSamples(Resource file) throws OnapCommandInvalidSample { - + public List loadSamples(InputStream inputStream, String fileName) throws OnapCommandInvalidSample { List samples = new ArrayList<>(); Map values = null; try { - values = (Map) new Yaml().load(file.getInputStream()); + values = (Map) new Yaml().load(inputStream); } catch (Exception e) { - throw new OnapCommandInvalidSample(file.getFilename(), e); + throw new OnapCommandInvalidSample(fileName, e); } OnapCommandSample sample = new OnapCommandSample(); if (!this.getValue(values, SAMPLE_VERSION).equals(SAMPLE_VERSION_1_0)) { - throw new OnapCommandInvalidSample(file.getFilename(), "Invalid sample version " + this.getValue(values, SAMPLE_VERSION)); + throw new OnapCommandInvalidSample(fileName, "Invalid sample version " + this.getValue(values, SAMPLE_VERSION)); } sample.setCommandName(this.getValue(values, SAMPLE_COMMAND_NAME)); @@ -132,6 +167,21 @@ public class OnapCommandHttpMocoServer { return samples; } + public List loadSamples(Resource file) throws OnapCommandInvalidSample { + try { + return loadSamples(file.getInputStream(), file.getFilename()); + } catch (IOException e) { + throw new OnapCommandInvalidSample(file.getFilename(), e); + } + } + + public List loadSamples(File file) throws OnapCommandInvalidSample { + try { + return loadSamples(new FileInputStream(file), file.getName()); + } catch (FileNotFoundException e) { + throw new OnapCommandInvalidSample(file.getName(), e); + } + } private void verifySample(OnapCommandSample sample) throws OnapCommandException { List args = new ArrayList<>(); diff --git a/validate/validation/src/test/java/org/onap/cli/validation/OnapValidationTest.java b/validate/validation/src/test/java/org/onap/cli/validation/OnapValidationTest.java index f2c27fc7..71d015b5 100644 --- a/validate/validation/src/test/java/org/onap/cli/validation/OnapValidationTest.java +++ b/validate/validation/src/test/java/org/onap/cli/validation/OnapValidationTest.java @@ -16,8 +16,15 @@ package org.onap.cli.validation; +import static org.junit.Assert.fail; + +import java.io.File; import java.io.IOException; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import org.apache.commons.io.FileUtils; import org.aspectj.lang.annotation.After; import org.junit.Ignore; import org.junit.Test; @@ -26,6 +33,7 @@ import org.onap.cli.fw.error.OnapCommandException; import org.onap.cli.fw.utils.ExternalSchema; import org.onap.cli.main.OnapCli; import org.onap.cli.moco.OnapCommandHttpMocoServer; +import org.onap.cli.moco.OnapCommandSample; public class OnapValidationTest { @@ -83,4 +91,40 @@ public class OnapValidationTest { server.verifySamples(); } + @Test + public void collectSampleYamlTest() { + try { + File root = new File("../../"); + String sampleFileName = "target/sample.rst"; + + FileUtils.deleteQuietly(new File(sampleFileName)); + Map> discoveredYamls = OnapCommandHttpMocoServer.discoverYamls(root); + + writeSamples(new File(sampleFileName), discoveredYamls); + } catch (IOException e) { + fail(); + } + } + + private void writeSamples(File dest, Map> cliProductSamples) throws IOException { + + for (String product: cliProductSamples.keySet()) { + FileUtils.write(dest, "\n" + product, "UTF-8", true); + FileUtils.write(dest, "\n========\n\n", "UTF-8", true); + + for(OnapCommandSample sample: cliProductSamples.get(product)) { + FileUtils.write(dest, "\n\n" + sample.getCommandName(), "UTF-8", true); + FileUtils.write(dest, "\n" + String.join("", Collections.nCopies(sample.getCommandName().length(), "-"))+ "\n", "UTF-8", true); + + if (!sample.getInput().isEmpty()) { + FileUtils.write(dest, "\ninput::\n\n " + sample.getInput() + "\n", "UTF-8", true); + } + + if (!sample.getOutput().isEmpty()) { + FileUtils.write(dest, "\noutput::\n\n " + sample.getOutput().replaceAll("\n", "\n ").trim(), "UTF-8", true); + } + } + } + } + } -- cgit 1.2.3-korg