diff options
-rw-r--r-- | validate/validation/src/test/java/org/onap/cli/moco/OnapCommandHttpMocoServer.java | 64 | ||||
-rw-r--r-- | validate/validation/src/test/java/org/onap/cli/validation/OnapValidationTest.java | 44 |
2 files changed, 101 insertions, 7 deletions
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<String, List<OnapCommandSample>> discoverYamls(File path) throws IOException { + Map<String, List<OnapCommandSample>> cliProductSamples = new HashMap<>(); + + Stream<Path> 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<String, List<OnapCommandSample>> result) { + OnapCommandHttpMocoServer onapCommandHttpMocoServer = new OnapCommandHttpMocoServer(); + List<OnapCommandSample> 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<Resource> dicoverSampleYamls() { Resource[] resources = new Resource [] {}; try { @@ -98,20 +134,19 @@ public class OnapCommandHttpMocoServer { return ""; } - private List<OnapCommandSample> loadSamples(Resource file) throws OnapCommandInvalidSample { - + public List<OnapCommandSample> loadSamples(InputStream inputStream, String fileName) throws OnapCommandInvalidSample { List<OnapCommandSample> samples = new ArrayList<>(); Map<String, ?> values = null; try { - values = (Map<String, ?>) new Yaml().load(file.getInputStream()); + values = (Map<String, ?>) 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<OnapCommandSample> loadSamples(Resource file) throws OnapCommandInvalidSample { + try { + return loadSamples(file.getInputStream(), file.getFilename()); + } catch (IOException e) { + throw new OnapCommandInvalidSample(file.getFilename(), e); + } + } + + public List<OnapCommandSample> 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 <String> 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<String, List<OnapCommandSample>> discoveredYamls = OnapCommandHttpMocoServer.discoverYamls(root); + + writeSamples(new File(sampleFileName), discoveredYamls); + } catch (IOException e) { + fail(); + } + } + + private void writeSamples(File dest, Map<String, List<OnapCommandSample>> 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); + } + } + } + } + } |