Merge "Add CLI_PRODUCT_VERION in install.sh"
authorKanagaraj Manickam <kanagaraj.manickam@huawei.com>
Wed, 11 Oct 2017 09:52:28 +0000 (09:52 +0000)
committerGerrit Code Review <gerrit@onap.org>
Wed, 11 Oct 2017 09:52:28 +0000 (09:52 +0000)
13 files changed:
framework/src/main/java/org/onap/cli/fw/error/OnapCommandException.java
framework/src/main/java/org/onap/cli/fw/error/OnapCommandInvalidSample.java [new file with mode: 0644]
framework/src/test/java/org/onap/cli/fw/OnapCommandRegistrarTest.java
framework/src/test/java/org/onap/cli/fw/error/OnapCommandErrorTest.java
plugins/msb/src/main/resources/onap-cli-schema/microservice-create-schema.yaml
plugins/msb/src/main/resources/onap-cli-schema/microservice-list-schema.yaml
plugins/msb/src/main/resources/onap-cli-schema/microservice-show-schema.yaml
validation/pom.xml
validation/src/test/java/org/onap/cli/moco/OnapCommandHttpMocoServer.java [new file with mode: 0644]
validation/src/test/java/org/onap/cli/moco/OnapCommandSample.java [new file with mode: 0644]
validation/src/test/java/org/onap/cli/validation/OnapCliMainTest.java
validation/src/test/resources/customer-create-sample-1.1-moco.json [new file with mode: 0644]
validation/src/test/resources/onap-cli-sample/customer/customer-create-sample-1.1.yaml [new file with mode: 0644]

index 8e99b9a..3fecad0 100644 (file)
@@ -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 (file)
index 0000000..32e5cf6
--- /dev/null
@@ -0,0 +1,39 @@
+/*
+ * 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 schema, String error) {
+        super(ERROR_CODE, ERROR_MSG + schema + ", " + error);
+    }
+
+    public OnapCommandInvalidSample(String schema, Throwable throwable) {
+        super(ERROR_CODE, ERROR_MSG + schema , throwable);
+    }
+
+}
index 16c3f2f..4f5c95e 100644 (file)
@@ -60,19 +60,18 @@ public class OnapCommandRegistrarTest {
     public void registerTest() throws OnapCommandException {
         OnapCommand test = new OnapCommandTest();
         Class<OnapCommand> cmd = (Class<OnapCommand>) test.getClass();
-        registerar.register("Test", "cli-1.0", cmd);
-        OnapCommand cc = registerar.get("Test");
+        registerar.register("test", "cli-1.0", cmd);
+        OnapCommand cc = registerar.get("test");
         assertTrue(cmd == cc.getClass());
 
     }
 
     @Test
-    // For Coverage
     public void cmdTestSchema() throws OnapCommandException {
         OnapCommand test = new OnapCommandTest();
         Class<OnapCommand> cmd = (Class<OnapCommand>) test.getClass();
-        registerar.register("Test", "cli-1.0", cmd);
-        OnapCommand cc = registerar.get("Test");
+        registerar.register("test", "cli-1.0", cmd);
+        OnapCommand cc = registerar.get("test");
     }
 
     @Test
index e56eac0..cc12cfc 100644 (file)
@@ -255,6 +255,17 @@ public class OnapCommandErrorTest {
         assertEquals("0xb002::Command schema is missing for command Test", failed.getMessage());
     }
 
+    @Test
+    public void onapCommandSampleInvalidTest() {
+        OnapCommandInvalidSample failed = new OnapCommandInvalidSample("Test", "error");
+
+        assertEquals("0xf001::Invalid command sample Test, error", failed.getMessage());
+
+        failed = new OnapCommandInvalidSample("Test", new Exception("error"));
+
+        assertEquals("0xf001::Invalid command sample Test, error", failed.getMessage());
+    }
+
     @Test
     public void onapCommandServiceNotFoundTest() {
         OnapCommandServiceNotFound failed = new OnapCommandServiceNotFound("Service");
index c8b7e9b..79afd2c 100644 (file)
@@ -26,6 +26,19 @@ parameters:
     short_option: r
     long_option: service-url
     is_optional: false
+  - name: enable-ssl
+    description: Onap service is enabled with https or not
+    type: string
+    short_option: b
+    long_option: enable-ssl
+    is_optional: true
+    default: false
+  - name: path
+    description: Onap service path
+    type: string
+    short_option: c
+    long_option: path
+    is_optional: true
   - name: node-ip
     description: Onap service running node IP
     type: string
@@ -59,11 +72,19 @@ results:
       description: Onap service running nodes
       scope: long
       type: string
+    - name: enable-ssl
+      description: Onap service is enabled with https or not
+      scope: long
+      type: string
+    - name: path
+      description: Onap service path
+      scope: long
+      type: string
 http:
   request:
     uri: /services
     method: POST
-    body: '{"serviceName":"${service-name}","version":"${service-version}","url":"${service-url}","protocol":"REST","visualRange":"1","lb_policy":"ip_hash","nodes":[{"ip":"${node-ip}","port":"${node-port}","ttl":0}]}'
+    body: '{"serviceName":"${service-name}","version":"${service-version}","path":"${path}","url":"${service-url}","protocol":"REST", "enable_ssl":"${enable-ssl}", "visualRange":"1","lb_policy":"ip_hash","nodes":[{"ip":"${node-ip}","port":"${node-port}","ttl":0}]}'
     headers:
     queries:
       createOrUpdate: ${create-or-update}
@@ -75,6 +96,8 @@ http:
     version: $b{$.version}
     url: $b{$.url}
     status: $b{$.status}
+    enable-ssl: $b{$.enable_ssl}
+    path: $b{$.path}
     nodes: $b{$.nodes[*].ip}:$b{$.nodes[*].port}
 
   sample_response:
index b9bc306..52014c9 100644 (file)
@@ -29,6 +29,14 @@ results:
       description: Onap service running nodes
       scope: long
       type: json
+    - name: enable-ssl
+      description: Onap service is enabled with https or not
+      scope: long
+      type: string
+    - name: path
+      description: Onap service path
+      scope: long
+      type: string
 http:
   request:
     uri: /services
@@ -41,6 +49,8 @@ http:
     version: $b{$.[*].version}
     url: $b{$.[*].url}
     status: $b{$.[*].status}
+    enable-ssl: $b{$.[*].enable_ssl}
+    path: $b{$.[*].path}
     nodes: $b{$.[*].nodes[*].ip}:$b{$.[*].nodes[*].port}
   sample_response:
     body: '[{"serviceName":"test","version":"v1","url":"/api/test/v1","protocol":"REST","visualRange":"1","lb_policy":"hash","nodes":[{"ip":"127.0.0.1","port":"8012","ttl":0,"nodeId":"test_127.0.0.1_8012","expiration":"2017-02-10T05:33:25Z","created_at":"2017-02-10T05:33:25Z","updated_at":"2017-02-10T05:33:25Z"}],"status":"1"}]'
index 437e567..a9e1cce 100644 (file)
@@ -20,7 +20,7 @@ parameters:
     long_option: service-version
     is_optional: false
 results:
-  direction: landscape
+  direction: portrait
   attributes:
     - name: name
       description: Onap service name
@@ -42,6 +42,14 @@ results:
       description: Onap service running nodes
       scope: long
       type: json
+    - name: enable-ssl
+      description: Onap service is enabled with https or not
+      scope: long
+      type: string
+    - name: path
+      description: Onap service path
+      scope: long
+      type: string
 http:
   request:
     uri: /services/${service-name}/version/${service-version}
@@ -54,6 +62,8 @@ http:
     version: $b{$.version}
     url: $b{$.url}
     status: $b{$.status}
+    enable-ssl: $b{$.enable_ssl}
+    path: $b{$.path}
     nodes: $b{$.nodes[*].ip}:$b{$.nodes[*].port}
   sample_response:
     body: '{"serviceName":"test","version":"v1","url":"/api/test/v1","protocol":"REST","visualRange":"1","lb_policy":"hash","nodes":[{"ip":"127.0.0.1","port":"8012","ttl":0,"nodeId":"test_127.0.0.1_8012","expiration":"2017-02-10T05:33:25Z","created_at":"2017-02-10T05:33:25Z","updated_at":"2017-02-10T05:33:25Z"}],"status":"1"}'
index 62b0db5..618074f 100644 (file)
     <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 (file)
index 0000000..7f84704
--- /dev/null
@@ -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(file.getFilename(), 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));
+        }
+
+        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/java/org/onap/cli/moco/OnapCommandSample.java b/validation/src/test/java/org/onap/cli/moco/OnapCommandSample.java
new file mode 100644 (file)
index 0000000..31448e4
--- /dev/null
@@ -0,0 +1,80 @@
+/*
+ * 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;
+
+public class OnapCommandSample {
+
+    private String commandName;
+
+    private String product;
+
+    private String input;
+
+    private String output;
+
+    private String moco;
+
+    private String description;
+
+    public String getCommandName() {
+        return commandName;
+    }
+
+    public void setCommandName(String commandName) {
+        this.commandName = commandName;
+    }
+
+    public String getProduct() {
+        return product;
+    }
+
+    public void setProduct(String product) {
+        this.product = product;
+    }
+
+    public String getInput() {
+        return input;
+    }
+
+    public void setInput(String input) {
+        this.input = input;
+    }
+
+    public String getOutput() {
+        return output;
+    }
+
+    public void setOutput(String output) {
+        this.output = output;
+    }
+
+    public String getDescription() {
+        return description;
+    }
+
+    public void setDescription(String description) {
+        this.description = description;
+    }
+
+    public String getMoco() {
+        return moco;
+    }
+
+    public void setMoco(String moco) {
+        this.moco = moco;
+    }
+}
index 836505d..779978f 100644 (file)
 package org.onap.cli.validation;
 
 import java.io.IOException;
-import java.util.Map;
-import java.util.Set;
 
 import org.aspectj.lang.annotation.After;
 import org.junit.Test;
 import org.onap.cli.fw.OnapCommandRegistrar;
 import org.onap.cli.fw.error.OnapCommandException;
 import org.onap.cli.fw.utils.ExternalSchema;
-import org.onap.cli.fw.utils.OnapCommandUtils;
 import org.onap.cli.main.OnapCli;
+import org.onap.cli.moco.OnapCommandHttpMocoServer;
 
 public class OnapCliMainTest {
 
@@ -50,7 +48,7 @@ public class OnapCliMainTest {
     }
 
     @Test
-    public void validateCommands() throws IOException, OnapCommandException {
+    public void validateCommandSchemas() throws IOException, OnapCommandException {
         OnapCommandRegistrar.getRegistrar().setEnabledProductVersion("cli-1.0");
         for (ExternalSchema sch : OnapCommandRegistrar.getRegistrar().listCommandInfo()) {
             System.out.println(
@@ -76,4 +74,10 @@ public class OnapCliMainTest {
         }
     }
 
+    @Test
+    public void validateCommands() throws OnapCommandException {
+        OnapCommandHttpMocoServer server = new OnapCommandHttpMocoServer();
+        server.verifySamples();
+    }
+
  }
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 (file)
index 0000000..f6e4d0b
--- /dev/null
@@ -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 (file)
index 0000000..30b5b30
--- /dev/null
@@ -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