diff options
author | Kanagaraj Manickam k00365106 <kanagaraj.manickam@huawei.com> | 2017-10-11 10:51:16 +0530 |
---|---|---|
committer | Kanagaraj Manickam k00365106 <kanagaraj.manickam@huawei.com> | 2017-10-11 11:03:39 +0530 |
commit | 1ec7ee637367ed680df9cbf2dfc14bb283ec1583 (patch) | |
tree | 12e575bd9b7f1b83fa13b82133c01509285056a1 | |
parent | be4d81987526f9ce326755ffd123524fb4d457fe (diff) |
Enhance validation with moco server
CLI-2
Change-Id: Ie8a98a2a20137610634b3450d1076bec1790e9b1
Signed-off-by: Kanagaraj Manickam k00365106 <kanagaraj.manickam@huawei.com>
6 files changed, 242 insertions, 0 deletions
diff --git a/framework/src/main/java/org/onap/cli/fw/error/OnapCommandException.java b/framework/src/main/java/org/onap/cli/fw/error/OnapCommandException.java index 8e99b9a1..3fecad02 100644 --- a/framework/src/main/java/org/onap/cli/fw/error/OnapCommandException.java +++ b/framework/src/main/java/org/onap/cli/fw/error/OnapCommandException.java @@ -35,6 +35,7 @@ schema 0xb00 profile 0xc00 product/service 0xd00 catalog 0xe00 +sample 0xf00 */ public class OnapCommandException extends Exception { diff --git a/framework/src/main/java/org/onap/cli/fw/error/OnapCommandInvalidSample.java b/framework/src/main/java/org/onap/cli/fw/error/OnapCommandInvalidSample.java new file mode 100644 index 00000000..8d2d7c7a --- /dev/null +++ b/framework/src/main/java/org/onap/cli/fw/error/OnapCommandInvalidSample.java @@ -0,0 +1,43 @@ +/* + * 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.fw.error; + +/** + * Command sample is invalid. + * + */ +public class OnapCommandInvalidSample extends OnapCommandException { + + private static final long serialVersionUID = -3387652326582792835L; + + private static final String ERROR_CODE = "0xf001"; + + private static final String ERROR_MSG = "Invalid command sample "; + + public OnapCommandInvalidSample(String error) { + this(ERROR_CODE, error); + } + + public OnapCommandInvalidSample(String schema, String error) { + super(ERROR_CODE, ERROR_MSG + schema + ", " + error); + } + + public OnapCommandInvalidSample(String schema, Throwable throwable) { + super(ERROR_CODE, ERROR_MSG + schema , throwable); + } + +} diff --git a/validation/pom.xml b/validation/pom.xml index 62b0db5d..618074f6 100644 --- a/validation/pom.xml +++ b/validation/pom.xml @@ -31,6 +31,12 @@ <name>cli/validation</name> <packaging>jar</packaging> <dependencies> + <dependency> + <groupId>com.github.dreamhead</groupId> + <artifactId>moco-runner</artifactId> + <version>0.11.1</version> + <scope>test</scope> + </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> diff --git a/validation/src/test/java/org/onap/cli/moco/OnapCommandHttpMocoServer.java b/validation/src/test/java/org/onap/cli/moco/OnapCommandHttpMocoServer.java new file mode 100644 index 00000000..f92e0c00 --- /dev/null +++ b/validation/src/test/java/org/onap/cli/moco/OnapCommandHttpMocoServer.java @@ -0,0 +1,172 @@ +/* + * 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.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.IOException; +import java.io.PrintStream; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Map; + +import org.onap.cli.fw.OnapCommandRegistrar; +import org.onap.cli.fw.error.OnapCommandException; +import org.onap.cli.fw.error.OnapCommandInvalidSample; +import org.onap.cli.fw.utils.OnapCommandUtils; +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 = "onap_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() { + } + + private List<Resource> dicoverSampleYamls() { + Resource[] resources = new Resource [] {}; + try { + resources = OnapCommandUtils.getExternalResources(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 ""; + } + + private List<OnapCommandSample> loadSamples(Resource file) throws OnapCommandInvalidSample { + + List<OnapCommandSample> samples = new ArrayList<>(); + Map<String, ?> values = null; + try { + values = (Map<String, ?>) new Yaml().load(file.getInputStream()); + } catch (Exception e) { + throw new OnapCommandInvalidSample("Invalid sample", e); + } + + OnapCommandSample sample = new OnapCommandSample(); + + if (!this.getValue(values, SAMPLE_VERSION).equals(SAMPLE_VERSION_1_0)) { + throw new OnapCommandInvalidSample("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; + } + + 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()); + + //mrkanag uncomment following lines once moco server setup is done + //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/validation/src/test/resources/customer-create-sample-1.1-moco.json b/validation/src/test/resources/customer-create-sample-1.1-moco.json new file mode 100644 index 00000000..f6e4d0b2 --- /dev/null +++ b/validation/src/test/resources/customer-create-sample-1.1-moco.json @@ -0,0 +1,11 @@ +[{ + "request" : + { + "method" : "get", + "uri" : "/aai/v11/business/customers/customer/test" + }, + "response" : + { + "status" : 201 + } +}]
\ No newline at end of file diff --git a/validation/src/test/resources/onap-cli-sample/customer/customer-create-sample-1.1.yaml b/validation/src/test/resources/onap-cli-sample/customer/customer-create-sample-1.1.yaml new file mode 100644 index 00000000..30b5b303 --- /dev/null +++ b/validation/src/test/resources/onap-cli-sample/customer/customer-create-sample-1.1.yaml @@ -0,0 +1,9 @@ +onap_cli_sample_version: 1.0 + +name: customer-create +version: onap-1.1 +samples: + sample1: + name: Create a customer + input: -m http://locahost:8141 -u AAI -p AAI --customer-name test --subscriber-name test + moco: customer-create-sample-1.1-moco.json
\ No newline at end of file |