diff options
Diffstat (limited to 'validate/validation/src/test/java')
-rw-r--r-- | validate/validation/src/test/java/org/onap/cli/moco/OnapCommandHttpMocoServer.java | 221 | ||||
-rw-r--r-- | validate/validation/src/test/java/org/onap/cli/validation/OnapValidationTest.java | 140 |
2 files changed, 122 insertions, 239 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 deleted file mode 100644 index b1f18681..00000000 --- a/validate/validation/src/test/java/org/onap/cli/moco/OnapCommandHttpMocoServer.java +++ /dev/null @@ -1,221 +0,0 @@ -/* - * Copyright 2017 Huawei Technologies Co., Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -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 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.error.OnapCommandException; -import org.onap.cli.fw.error.OnapCommandInvalidSample; -import org.onap.cli.fw.registrar.OnapCommandRegistrar; -import org.onap.cli.fw.utils.OnapCommandDiscoveryUtils; -import org.onap.cli.main.OnapCli; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.core.io.Resource; -import org.yaml.snakeyaml.Yaml; - -import com.github.dreamhead.moco.HttpServer; -import com.github.dreamhead.moco.Runner; - -public class OnapCommandHttpMocoServer { - - public static final String SAMPLE_PATTERN = "onap-cli-sample/**/"; - - public static final String SAMPLE_VERSION = "open_cli_sample_version"; - public static final String SAMPLE_VERSION_1_0 = "1.0"; - - public static final String SAMPLE_COMMAND_NAME = "name"; - public static final String SAMPLE_PRODUCT = "version"; - public static final String SAMPLE_LIST = "samples"; - public static final String SAMPLE_DESCRIPTION = "name"; - public static final String SAMPLE_INPUT = "input"; - public static final String SAMPLE_OUTPUT = "output"; - public static final String SAMPLE_MOCO = "moco"; - - private static Logger LOG = LoggerFactory.getLogger(OnapCommandHttpMocoServer.class); - - private String samplesToTest = "*.yaml"; - - private int port = 8141; - - public int getPort() { - return port; - } - - public void setPort(int port) { - this.port = port; - } - - public OnapCommandHttpMocoServer(String samplesToTest) { - this.samplesToTest = samplesToTest; - } - - 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 { - resources = OnapCommandDiscoveryUtils.findResources(SAMPLE_PATTERN + this.samplesToTest); - } catch (IOException e) { - LOG.error("Failed to discover the samples", e); - } - - return Arrays.asList(resources); - } - - private String getValue(Map<String, ?> map, String prpName) { - Object o = map.get(prpName); - if (o != null) { - return o.toString(); - } - - return ""; - } - - 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(inputStream); - } catch (Exception e) { - throw new OnapCommandInvalidSample(fileName, e); - } - - OnapCommandSample sample = new OnapCommandSample(); - - if (!this.getValue(values, SAMPLE_VERSION).equals(SAMPLE_VERSION_1_0)) { - throw new OnapCommandInvalidSample(fileName, "Invalid sample version " + this.getValue(values, SAMPLE_VERSION)); - } - - sample.setCommandName(this.getValue(values, SAMPLE_COMMAND_NAME)); - sample.setProduct(this.getValue(values, SAMPLE_PRODUCT)); - - //Retrieve the samples - values = (Map<String, Map<String, String>>) values.get(SAMPLE_LIST); - - for (String s: values.keySet()) { - Map<String, ?> sMap = (Map<String, ?>)values.get(s); - sample.setDescription(this.getValue(sMap, SAMPLE_DESCRIPTION)); - sample.setInput(this.getValue(sMap, SAMPLE_INPUT)); - sample.setOutput(this.getValue(sMap, SAMPLE_OUTPUT)); - sample.setMoco(this.getValue(sMap, SAMPLE_MOCO)); - samples.add(sample); - } - - 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<>(); - args.add(sample.getCommandName()); - args.addAll(Arrays.asList(sample.getInput().split(" "))); - - ByteArrayOutputStream bo = new ByteArrayOutputStream(); - System.setOut(new PrintStream(bo)); - - OnapCli cli = new OnapCli(args.toArray(new String []{})); - OnapCommandRegistrar.getRegistrar().setEnabledProductVersion(sample.getProduct()); - cli.handle(); - - String output = new String(bo.toByteArray()); - - assert cli.getExitCode() == 0; - - assert sample.getOutput().equals(output); - } - - public void verifySamples() throws OnapCommandException { - for (Resource rsc : this.dicoverSampleYamls()) { - for(OnapCommandSample sample: this.loadSamples(rsc)) { - - if (!sample.getMoco().isEmpty()) { - HttpServer server = jsonHttpServer(this.getPort(), pathResource(sample.getMoco())); - Runner r = runner(server); - r.start(); - - this.verifySample(sample); - - r.stop(); - } - } - } - } -}
\ No newline at end of file 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 6f84c953..a631e3d2 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,28 +16,54 @@ 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.junit.Ignore; import org.junit.Test; +import org.onap.cli.fw.conf.OnapCommandConstants; import org.onap.cli.fw.error.OnapCommandException; +import org.onap.cli.fw.error.OnapCommandInvalidSample; import org.onap.cli.fw.registrar.OnapCommandRegistrar; import org.onap.cli.fw.schema.OnapCommandSchemaInfo; import org.onap.cli.main.OnapCli; -import org.onap.cli.moco.OnapCommandHttpMocoServer; +import org.onap.cli.main.conf.OnapCliConstants; import org.onap.cli.moco.OnapCommandSample; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.yaml.snakeyaml.Yaml; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Stream; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; public class OnapValidationTest { + public static final String SAMPLE_VERSION = "open_cli_sample_version"; + public static final String SAMPLE_VERSION_1_0 = "1.0"; + + public static final String SAMPLE_COMMAND_NAME = "name"; + public static final String SAMPLE_PRODUCT = "version"; + public static final String SAMPLE_LIST = "samples"; + public static final String SAMPLE_DESCRIPTION = "name"; + public static final String SAMPLE_INPUT = "input"; + public static final String SAMPLE_OUTPUT = "output"; + public static final String SAMPLE_MOCO = "moco"; + OnapCli cli = null; + private static Logger LOG = LoggerFactory.getLogger(OnapValidationTest.class); + private void handle(String[] args) { cli = new OnapCli(args); cli.handle(); @@ -84,13 +110,6 @@ public class OnapValidationTest { } } - @Ignore - @Test - public void verifyCommandResults() throws OnapCommandException { - OnapCommandHttpMocoServer server = new OnapCommandHttpMocoServer(); - server.verifySamples(); - } - @Test public void collectSampleYamlTest() { try { @@ -98,7 +117,7 @@ public class OnapValidationTest { String sampleFileName = "target/sample.rst"; FileUtils.deleteQuietly(new File(sampleFileName)); - Map<String, List<OnapCommandSample>> discoveredYamls = OnapCommandHttpMocoServer.discoverYamls(root); + Map<String, List<OnapCommandSample>> discoveredYamls = discoverYamls(root); writeSamples(new File(sampleFileName), discoveredYamls); } catch (IOException e) { @@ -127,4 +146,89 @@ public class OnapValidationTest { } } + 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) { + List<OnapCommandSample> loadSamples; + try { + loadSamples = 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 static 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 static 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(inputStream); + } catch (Exception e) { + throw new OnapCommandInvalidSample(fileName, e); + } + + OnapCommandSample sample = new OnapCommandSample(); + + if (!getValue(values, SAMPLE_VERSION).equals(SAMPLE_VERSION_1_0)) { + throw new OnapCommandInvalidSample(fileName, "Invalid sample version " + getValue(values, SAMPLE_VERSION)); + } + + sample.setCommandName(getValue(values, SAMPLE_COMMAND_NAME)); + sample.setProduct(getValue(values, SAMPLE_PRODUCT)); + + //Retrieve the samples + values = (Map<String, Map<String, String>>) values.get(SAMPLE_LIST); + + for (String s: values.keySet()) { + Map<String, ?> sMap = (Map<String, ?>)values.get(s); + sample.setDescription(getValue(sMap, SAMPLE_DESCRIPTION)); + sample.setInput(getValue(sMap, SAMPLE_INPUT)); + sample.setOutput(getValue(sMap, SAMPLE_OUTPUT)); + sample.setMoco(getValue(sMap, SAMPLE_MOCO)); + samples.add(sample); + } + + return samples; + } + + private static String getValue(Map<String, ?> map, String prpName) { + Object o = map.get(prpName); + if (o != null) { + return o.toString(); + } + + return ""; + } + + @Test + public void testVerify() throws OnapCommandException { + OnapCommandRegistrar.getRegistrar().setEnabledProductVersion("open-cli"); + OnapCli onapCli = new OnapCli(new String[]{"sample-test-verify", "--verify"}); + onapCli.handle(); + assertEquals(OnapCliConstants.EXIT_SUCCESS, onapCli.getExitCode()); + } } |