Code refactoring to remove test code generation 53/20153/8
authorsubhash kumar singh <subhash.kumar.singh@huawei.com>
Mon, 23 Oct 2017 14:03:11 +0000 (14:03 +0000)
committersubhash kumar singh <subhash.kumar.singh@huawei.com>
Thu, 26 Oct 2017 09:27:02 +0000 (09:27 +0000)
Refactor code to remove test code generation code from framework
codebase.

Issue-ID: CLI-55
Change-Id: I4b45ef50143317586c39cf118a1717be150707da
Signed-off-by: subhash kumar singh <subhash.kumar.singh@huawei.com>
26 files changed:
framework/pom.xml
framework/src/main/java/org/onap/cli/fw/cmd/OnapHttpCommand.java
framework/src/main/java/org/onap/cli/fw/conf/Constants.java
framework/src/main/java/org/onap/cli/fw/conf/OnapCommandConfg.java
framework/src/main/java/org/onap/cli/fw/http/mock/MockJsonGenerator.java [deleted file]
framework/src/main/resources/onap.properties
framework/src/test/java/org/onap/cli/fw/http/mock/MockJsonGeneratorTest.java [deleted file]
main/pom.xml
main/src/main/java/org/onap/cli/main/OnapCli.java
pom.xml
validate/pom.xml [new file with mode: 0644]
validate/sample-mock-generator/pom.xml [new file with mode: 0644]
validate/sample-mock-generator/src/main/java/org/onap/cli/http/mock/MockJsonGenerator.java [new file with mode: 0644]
validate/sample-mock-generator/src/main/java/org/onap/cli/http/mock/MockObject.java [moved from framework/src/main/java/org/onap/cli/fw/http/mock/MockObject.java with 96% similarity]
validate/sample-mock-generator/src/main/java/org/onap/cli/http/mock/MockRequest.java [moved from framework/src/main/java/org/onap/cli/fw/http/mock/MockRequest.java with 64% similarity]
validate/sample-mock-generator/src/main/java/org/onap/cli/http/mock/MockResponse.java [moved from framework/src/main/java/org/onap/cli/fw/http/mock/MockResponse.java with 67% similarity]
validate/sample-mock-generator/src/test/java/org/onap/cli/http/mock/MockJsonGeneratorTest.java [new file with mode: 0644]
validate/sample-yaml-generator/pom.xml [new file with mode: 0644]
validate/sample-yaml-generator/src/main/java/org/onap/cli/sample/yaml/SampleYamlGenerator.java [new file with mode: 0644]
validate/sample-yaml-generator/src/test/java/org/onap/cli/sample/yaml/SampleYamlGeneratorTest.java [new file with mode: 0644]
validate/validation/pom.xml [moved from validation/pom.xml with 100% similarity]
validate/validation/src/test/java/org/onap/cli/moco/OnapCommandHttpMocoServer.java [moved from validation/src/test/java/org/onap/cli/moco/OnapCommandHttpMocoServer.java with 100% similarity]
validate/validation/src/test/java/org/onap/cli/moco/OnapCommandSample.java [moved from validation/src/test/java/org/onap/cli/moco/OnapCommandSample.java with 100% similarity]
validate/validation/src/test/java/org/onap/cli/validation/OnapCliMainTest.java [moved from validation/src/test/java/org/onap/cli/validation/OnapCliMainTest.java with 100% similarity]
validate/validation/src/test/resources/customer-create-sample-1.1-moco.json [moved from validation/src/test/resources/customer-create-sample-1.1-moco.json with 100% similarity]
validate/validation/src/test/resources/onap-cli-sample/customer/customer-create-sample-1.1.yaml [moved from validation/src/test/resources/onap-cli-sample/customer/customer-create-sample-1.1.yaml with 100% similarity]

index 87e6bc2..a1b81e9 100644 (file)
             <artifactId>spring-core</artifactId>
             <version>3.1.0.RELEASE</version>
         </dependency>
+        <dependency>
+            <groupId>org.onap.cli</groupId>
+            <artifactId>cli-sample-mock-generator</artifactId>
+            <version>${project.version}</version>
+        </dependency>
         <dependency>
             <groupId>junit</groupId>
             <artifactId>junit</artifactId>
index a5c79f7..1e202a5 100644 (file)
@@ -16,6 +16,7 @@
 
 package org.onap.cli.fw.cmd;
 
+import java.io.IOException;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
@@ -26,11 +27,14 @@ import org.onap.cli.fw.conf.Constants;
 import org.onap.cli.fw.conf.OnapCommandConfg;
 import org.onap.cli.fw.error.OnapCommandException;
 import org.onap.cli.fw.error.OnapCommandExecutionFailed;
+import org.onap.cli.fw.error.OnapCommandFailedMocoGenerate;
 import org.onap.cli.fw.http.HttpInput;
 import org.onap.cli.fw.http.HttpResult;
-import org.onap.cli.fw.http.mock.MockJsonGenerator;
 import org.onap.cli.fw.output.OnapCommandResultAttribute;
 import org.onap.cli.fw.utils.OnapCommandUtils;
+import org.onap.cli.http.mock.MockJsonGenerator;
+import org.onap.cli.http.mock.MockRequest;
+import org.onap.cli.http.mock.MockResponse;
 
 /**
  * Onap Command.
@@ -95,9 +99,29 @@ public class OnapHttpCommand extends OnapCommand {
         for (OnapCommandResultAttribute attr : this.getResult().getRecords()) {
             attr.setValues(results.get(attr.getName()));
         }
+        generateJsonMock(httpInput, output, this.getName());
+    }
 
-        if (OnapCommandConfg.isMocoGenerateEnabled()) {
-            MockJsonGenerator.generateMocking(httpInput, output, this.getName());
+    private void generateJsonMock(HttpInput httpInput, HttpResult httpResult, String fileName)
+            throws OnapCommandFailedMocoGenerate {
+
+        if (OnapCommandConfg.isSampleGenerateEnabled()) {
+            try {
+                MockRequest mockRequest = new MockRequest();
+                mockRequest.setMethod(httpInput.getMethod());
+                mockRequest.setUri(httpInput.getUri());
+                mockRequest.setHeaders(httpInput.getReqHeaders());
+                mockRequest.setJson(httpInput.getBody());
+
+                MockResponse mockResponse = new MockResponse();
+                mockResponse.setStatus(httpResult.getStatus());
+                mockResponse.setJson(httpResult.getBody());
+
+                MockJsonGenerator.generateMocking(mockRequest, mockResponse, OnapCommandConfg.getSampleGenerateTargetFolder()
+                        + "/" + fileName);
+            } catch (IOException error) {
+                throw new OnapCommandFailedMocoGenerate(fileName, error);
+            }
         }
     }
 }
index f8bd928..7dfc0d4 100644 (file)
@@ -198,8 +198,8 @@ public class Constants {
 
     public static final String DISCOVER_ALWAYS = "discover_always";
 
-    public static final String MOCO_ENABLED = "cli.moco.enable";
-    public static final String MOCO_TARGET_FOLDER = "cli.moco.target";
+    public static final String SAMPLE_GEN_ENABLED = "cli.sample.gen.enable";
+    public static final String SAMPLE_GEN_TARGET_FOLDER = "cli.sample.gen.target";
 
     private Constants() {
     }
index 6c302b2..bb0e2c0 100644 (file)
@@ -174,11 +174,11 @@ public final class OnapCommandConfg {
         return Arrays.stream(prps.getProperty(key).split(",")).map(String::trim).collect(Collectors.toList());  // NOSONAR
     }
 
-    public static String getMocoTargetFolder() {
-        return prps.getProperty(Constants.MOCO_TARGET_FOLDER, ".");
+    public static String getSampleGenerateTargetFolder() {
+        return prps.getProperty(Constants.SAMPLE_GEN_TARGET_FOLDER, ".");
     }
 
-    public static boolean isMocoGenerateEnabled() {
-        return "true".equals(prps.getProperty(Constants.MOCO_ENABLED));
+    public static boolean isSampleGenerateEnabled() {
+        return "true".equals(prps.getProperty(Constants.SAMPLE_GEN_ENABLED));
     }
 }
diff --git a/framework/src/main/java/org/onap/cli/fw/http/mock/MockJsonGenerator.java b/framework/src/main/java/org/onap/cli/fw/http/mock/MockJsonGenerator.java
deleted file mode 100644 (file)
index 91586d5..0000000
+++ /dev/null
@@ -1,61 +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.fw.http.mock;
-
-import java.io.File;
-import java.io.IOException;
-import java.text.SimpleDateFormat;
-import java.util.Arrays;
-import java.util.Date;
-
-import org.onap.cli.fw.conf.OnapCommandConfg;
-import org.onap.cli.fw.error.OnapCommandFailedMocoGenerate;
-import org.onap.cli.fw.http.HttpInput;
-import org.onap.cli.fw.http.HttpResult;
-
-import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
-import com.fasterxml.jackson.databind.ObjectMapper;
-import com.fasterxml.jackson.databind.ObjectWriter;
-
-public class MockJsonGenerator {
-    public static void generateMocking(HttpInput httpInput, HttpResult httpResult,
-            String jsonFileName) throws OnapCommandFailedMocoGenerate {
-
-        MockRequest mockRequest = new MockRequest();
-        mockRequest.setMethod(httpInput.getMethod());
-        mockRequest.setUri(httpInput.getUri());
-        mockRequest.setHeaders(httpInput.getReqHeaders());
-        mockRequest.setJson(httpInput.getBody());
-
-        MockResponse mockResponse = new MockResponse();
-        mockResponse.setStatus(httpResult.getStatus());
-        mockResponse.setJson(httpResult.getBody());
-
-        MockObject mockObject = new MockObject();
-        mockObject.setRequest(mockRequest);
-        mockObject.setResponse(mockResponse);
-
-        ObjectMapper mapper = new ObjectMapper();
-        ObjectWriter writer = mapper.writer(new DefaultPrettyPrinter());
-        try {
-            String timeStamp = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss").format(new Date());
-            writer.writeValue(new File(OnapCommandConfg.getMocoTargetFolder() + "/" + jsonFileName + "-" + timeStamp + "-moco.json"),
-                    Arrays.asList(mockObject));
-        } catch (IOException error ) {
-            throw new OnapCommandFailedMocoGenerate(jsonFileName, error);
-        }
-    }
-}
index 2695567..19182ae 100644 (file)
@@ -57,6 +57,6 @@ cli.schema.mode_values=direct,catalog
 cli.product.version=cli-1.0
 
 # moco properties
-cli.moco.enable=false
-cli.moco.target=.
+cli.sample.gen.enable=false
+cli.sample.gen.target=.
 
diff --git a/framework/src/test/java/org/onap/cli/fw/http/mock/MockJsonGeneratorTest.java b/framework/src/test/java/org/onap/cli/fw/http/mock/MockJsonGeneratorTest.java
deleted file mode 100644 (file)
index 2d99640..0000000
+++ /dev/null
@@ -1,66 +0,0 @@
-/*
- * Copyright 2016-17 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.http.mock;
-
-import org.junit.Test;
-import org.onap.cli.fw.error.OnapCommandFailedMocoGenerate;
-import org.onap.cli.fw.http.HttpInput;
-import org.onap.cli.fw.http.HttpResult;
-
-public class MockJsonGeneratorTest {
-
-    @Test
-    public void mocoGenerateTest() throws OnapCommandFailedMocoGenerate {
-        HttpInput httpInput = new HttpInput();
-        httpInput.setBody("{\"value\" : \"234sdf-345\"}");
-        httpInput.setMethod("get");
-        httpInput.setUri("http://1.1.1.1:80/getResource");
-
-        HttpResult httpResult = new HttpResult();
-        httpResult.setStatus(200);
-        httpResult.setBody("{\"value\" : \"234sdf-345\"}");
-
-        MockJsonGenerator.generateMocking(httpInput, httpResult, "test");
-    }
-
-    @Test(expected=OnapCommandFailedMocoGenerate.class)
-    public void mocoGenerateFailedInvalidBodyTest() throws OnapCommandFailedMocoGenerate {
-        HttpInput httpInput = new HttpInput();
-        httpInput.setBody("{\"value\" : \"234sdf-345\"");
-        httpInput.setMethod("get");
-        httpInput.setUri("http://1.1.1.1:80/getResource");
-
-        HttpResult httpResult = new HttpResult();
-        httpResult.setStatus(200);
-        httpResult.setBody("{\"value\" : \"234sdf-345\"");
-
-        MockJsonGenerator.generateMocking(httpInput, httpResult, "test");
-    }
-
-    @Test(expected=OnapCommandFailedMocoGenerate.class)
-    public void mocoGenerateFailedInvalidUrlTest() throws OnapCommandFailedMocoGenerate {
-        HttpInput httpInput = new HttpInput();
-        httpInput.setBody("{\"value\" : \"234sdf-345\"");
-        httpInput.setMethod("get");
-        httpInput.setUri("http://1.1.1.1:80:invalid");
-
-        HttpResult httpResult = new HttpResult();
-        httpResult.setStatus(200);
-        httpResult.setBody("{\"value\" : \"234sdf-345\"");
-
-        MockJsonGenerator.generateMocking(httpInput, httpResult, "test");
-    }
-}
index 451c55d..43a7000 100644 (file)
     <name>cli/main</name>
     <packaging>jar</packaging>
     <dependencies>
+        <dependency>
+            <groupId>org.onap.cli</groupId>
+            <artifactId>cli-sample-yaml-generator</artifactId>
+            <version>${project.version}</version>
+        </dependency>
         <dependency>
             <groupId>junit</groupId>
             <artifactId>junit</artifactId>
index 35df0e3..0ed27ea 100644 (file)
@@ -25,8 +25,10 @@ import org.apache.commons.io.IOUtils;
 import org.onap.cli.fw.OnapCommand;
 import org.onap.cli.fw.OnapCommandRegistrar;
 import org.onap.cli.fw.conf.Constants;
+import org.onap.cli.fw.conf.OnapCommandConfg;
 import org.onap.cli.fw.error.OnapCommandException;
 import org.onap.cli.fw.error.OnapCommandHelpFailed;
+import org.onap.cli.fw.error.OnapCommandInvalidSample;
 import org.onap.cli.fw.error.OnapCommandWarning;
 import org.onap.cli.fw.input.OnapCommandParameter;
 import org.onap.cli.fw.output.OnapCommandResult;
@@ -37,6 +39,7 @@ import org.onap.cli.fw.output.ResultType;
 import org.onap.cli.main.conf.OnapCliConstants;
 import org.onap.cli.main.interactive.StringCompleter;
 import org.onap.cli.main.utils.OnapCliUtils;
+import org.onap.cli.sample.yaml.SampleYamlGenerator;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -355,9 +358,12 @@ public class OnapCli {
 
                 OnapCliUtils.populateParams(cmd.getParameters(), args);
                 OnapCommandResult result = cmd.execute();
+
                 this.print(result.getDebugInfo());
                 this.print(result.print());
                 this.exitSuccessfully();
+
+                generateSmapleYaml(result);
             } catch (Exception e) {
                 this.print(cmd.getResult().getDebugInfo());
                 if (e instanceof OnapCommandWarning) {
@@ -370,6 +376,19 @@ public class OnapCli {
         }
     }
 
+    private void generateSmapleYaml(OnapCommandResult result) throws OnapCommandException {
+        if (OnapCommandConfg.isSampleGenerateEnabled() && this.getExitCode() == OnapCliConstants.EXIT_SUCCESS) {
+            try {
+                SampleYamlGenerator.generateSampleYaml(args, result.print(),
+                        OnapCommandRegistrar.getRegistrar().getEnabledProductVersion(),
+                        OnapCommandConfg.getSampleGenerateTargetFolder(),
+                        result.isDebug());
+            } catch (IOException error) {
+                throw new OnapCommandInvalidSample(args.get(0), error);
+            }
+        }
+    }
+
     /**
      * Handles all client input.
      */
diff --git a/pom.xml b/pom.xml
index a4fd621..18241f9 100644 (file)
--- a/pom.xml
+++ b/pom.xml
@@ -39,7 +39,7 @@
         <module>plugins</module>
         <module>main</module>
         <module>deployment</module>
-        <module>validation</module>
+        <module>validate</module>
   </modules>
 
     <distributionManagement>
@@ -92,4 +92,4 @@
             </plugin>
         </plugins>
     </build>
-</project>
\ No newline at end of file
+</project>
diff --git a/validate/pom.xml b/validate/pom.xml
new file mode 100644 (file)
index 0000000..2d75b23
--- /dev/null
@@ -0,0 +1,41 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+   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.
+ -->
+
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
+         http://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <modelVersion>4.0.0</modelVersion>
+
+    <parent>
+        <groupId>org.onap.cli</groupId>
+        <artifactId>cli</artifactId>
+        <version>1.0.0-SNAPSHOT</version>
+    </parent>
+
+    <artifactId>cli-validate</artifactId>
+    <name>cli/validate</name>
+    <packaging>pom</packaging>
+
+    <modules>
+        <module>sample-mock-generator</module>
+        <module>sample-yaml-generator</module>
+        <module>validation</module>
+    </modules>
+
+
+</project>
diff --git a/validate/sample-mock-generator/pom.xml b/validate/sample-mock-generator/pom.xml
new file mode 100644 (file)
index 0000000..c9263af
--- /dev/null
@@ -0,0 +1,48 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+   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.
+ -->
+
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
+         http://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <modelVersion>4.0.0</modelVersion>
+
+    <parent>
+        <groupId>org.onap.cli</groupId>
+        <artifactId>cli</artifactId>
+        <version>1.0.0-SNAPSHOT</version>
+    </parent>
+
+    <artifactId>cli-sample-mock-generator</artifactId>
+    <name>cli/validate/sample-mock-generator</name>
+    <packaging>jar</packaging>
+
+    <dependencies>
+        <dependency>
+            <groupId>junit</groupId>
+            <artifactId>junit</artifactId>
+            <version>4.11</version>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>com.fasterxml.jackson.core</groupId>
+            <artifactId>jackson-databind</artifactId>
+            <version>2.6.3</version>
+        </dependency>
+    </dependencies>
+
+</project>
diff --git a/validate/sample-mock-generator/src/main/java/org/onap/cli/http/mock/MockJsonGenerator.java b/validate/sample-mock-generator/src/main/java/org/onap/cli/http/mock/MockJsonGenerator.java
new file mode 100644 (file)
index 0000000..c3006e3
--- /dev/null
@@ -0,0 +1,42 @@
+/*
+ * 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.http.mock;
+
+import java.io.File;
+import java.io.IOException;
+import java.text.SimpleDateFormat;
+import java.util.Arrays;
+import java.util.Date;
+
+import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.ObjectWriter;
+
+public class MockJsonGenerator {
+    public static void generateMocking(MockRequest mockRequest, MockResponse mockResponse,
+            String jsonFilePath) throws IOException {
+
+        MockObject mockObject = new MockObject();
+        mockObject.setRequest(mockRequest);
+        mockObject.setResponse(mockResponse);
+
+        ObjectMapper mapper = new ObjectMapper();
+        ObjectWriter writer = mapper.writer(new DefaultPrettyPrinter());
+        String timeStamp = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss").format(new Date());
+        writer.writeValue(new File(jsonFilePath + "-" + timeStamp + "-moco.json"),
+                Arrays.asList(mockObject));
+    }
+}
  * limitations under the License.
  */
 
-package org.onap.cli.fw.http.mock;
+package org.onap.cli.http.mock;
 
 import java.io.IOException;
-import java.net.MalformedURLException;
 import java.net.URL;
 import java.util.Map;
 
-import org.onap.cli.fw.error.OnapCommandFailedMocoGenerate;
-
 import com.fasterxml.jackson.databind.JsonNode;
 import com.fasterxml.jackson.databind.ObjectMapper;
-import com.fasterxml.jackson.databind.node.JsonNodeFactory;
 
 public class MockRequest {
     private String method;
@@ -45,13 +41,9 @@ public class MockRequest {
         return uri;
     }
 
-    public void setUri(String url) throws OnapCommandFailedMocoGenerate {
+    public void setUri(String url) throws IOException {
         URL urlt;
-        try {
-            urlt = new URL(url);
-        } catch (MalformedURLException error) {
-            throw new OnapCommandFailedMocoGenerate(null, error);
-        }
+        urlt = new URL(url);
         this.uri = urlt.getPath();
     }
 
@@ -67,17 +59,11 @@ public class MockRequest {
         return json;
     }
 
-    public void setJson(String json) throws OnapCommandFailedMocoGenerate {
-        if (json.isEmpty()) {
-            this.json = JsonNodeFactory.instance.objectNode();
-            return;
-        }
-
-        ObjectMapper objectMapper = new ObjectMapper();
-        try {
+    public void setJson(String json) throws IOException {
+        if (!json.isEmpty()) {
+            ObjectMapper objectMapper = new ObjectMapper();
             this.json = objectMapper.readTree(json);
-        }catch (IOException error) {
-            throw new OnapCommandFailedMocoGenerate(null, error);
         }
+
     }
 }
  * limitations under the License.
  */
 
-package org.onap.cli.fw.http.mock;
+package org.onap.cli.http.mock;
 
 import java.io.IOException;
 
-import org.onap.cli.fw.error.OnapCommandFailedMocoGenerate;
-
 import com.fasterxml.jackson.databind.JsonNode;
 import com.fasterxml.jackson.databind.ObjectMapper;
-import com.fasterxml.jackson.databind.node.JsonNodeFactory;
 
 public class MockResponse {
     private int status;
@@ -40,17 +37,10 @@ public class MockResponse {
         return json;
     }
 
-    public void setJson(String json) throws OnapCommandFailedMocoGenerate {
-        if (json.isEmpty()) {
-            this.json = JsonNodeFactory.instance.objectNode();
-            return;
-        }
-
-        ObjectMapper objectMapper = new ObjectMapper();
-        try {
+    public void setJson(String json) throws IOException {
+        if (!json.isEmpty()) {
+            ObjectMapper objectMapper = new ObjectMapper();
             this.json = objectMapper.readTree(json);
-        } catch (IOException error) {
-            throw new OnapCommandFailedMocoGenerate(null, error);
         }
     }
 }
diff --git a/validate/sample-mock-generator/src/test/java/org/onap/cli/http/mock/MockJsonGeneratorTest.java b/validate/sample-mock-generator/src/test/java/org/onap/cli/http/mock/MockJsonGeneratorTest.java
new file mode 100644 (file)
index 0000000..3b79057
--- /dev/null
@@ -0,0 +1,65 @@
+/*
+ * 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.http.mock;
+
+import java.io.IOException;
+
+import org.junit.Test;
+
+public class MockJsonGeneratorTest {
+
+    @Test
+    public void mocoGenerateTest() throws IOException {
+        MockRequest mockRequest = new MockRequest();
+        mockRequest.setJson("{\"value\" : \"234sdf-345\"}");
+        mockRequest.setMethod("get");
+        mockRequest.setUri("http://1.1.1.1:80/getResource");
+
+        MockResponse mockResponse = new MockResponse();
+        mockResponse.setStatus(200);
+        mockResponse.setJson("{\"value\" : \"234sdf-345\"}");
+
+        MockJsonGenerator.generateMocking(mockRequest, mockResponse, "target/test");
+    }
+
+    @Test(expected=IOException.class)
+    public void mocoGenerateFailedInvalidBodyTest() throws IOException {
+        MockRequest mockRequest = new MockRequest();
+        mockRequest.setJson("{\"value\" : \"234sdf-345\"");
+        mockRequest.setMethod("get");
+        mockRequest.setUri("http://1.1.1.1:80/getResource");
+
+        MockResponse mockResponse = new MockResponse();
+        mockResponse.setStatus(200);
+        mockResponse.setJson("{\"value\" : \"234sdf-345\"");
+
+        MockJsonGenerator.generateMocking(mockRequest, mockResponse, "target/test");
+    }
+
+    @Test(expected=IOException.class)
+    public void mocoGenerateFailedInvalidUrlTest() throws IOException {
+        MockRequest mockRequest = new MockRequest();
+        mockRequest.setJson("{\"value\" : \"234sdf-345\"");
+        mockRequest.setMethod("get");
+        mockRequest.setUri("http://1.1.1.1:80:invalid");
+
+        MockResponse mockResponse = new MockResponse();
+        mockResponse.setStatus(200);
+        mockResponse.setJson("{\"value\" : \"234sdf-345\"");
+
+        MockJsonGenerator.generateMocking(mockRequest, mockResponse, "target/test");
+    }
+}
diff --git a/validate/sample-yaml-generator/pom.xml b/validate/sample-yaml-generator/pom.xml
new file mode 100644 (file)
index 0000000..76ff484
--- /dev/null
@@ -0,0 +1,43 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+   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.
+ -->
+
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
+         http://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <modelVersion>4.0.0</modelVersion>
+
+    <parent>
+        <groupId>org.onap.cli</groupId>
+        <artifactId>cli</artifactId>
+        <version>1.0.0-SNAPSHOT</version>
+    </parent>
+
+    <artifactId>cli-sample-yaml-generator</artifactId>
+    <name>cli/validate/sample-yaml-generator</name>
+    <packaging>jar</packaging>
+
+    <dependencies>
+        <dependency>
+            <groupId>junit</groupId>
+            <artifactId>junit</artifactId>
+            <version>4.11</version>
+            <scope>test</scope>
+        </dependency>
+    </dependencies>
+
+</project>
diff --git a/validate/sample-yaml-generator/src/main/java/org/onap/cli/sample/yaml/SampleYamlGenerator.java b/validate/sample-yaml-generator/src/main/java/org/onap/cli/sample/yaml/SampleYamlGenerator.java
new file mode 100644 (file)
index 0000000..5c30815
--- /dev/null
@@ -0,0 +1,82 @@
+/*
+ * 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.sample.yaml;
+
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.util.Arrays;
+import java.util.List;
+import java.util.stream.Collectors;
+
+public class SampleYamlGenerator {
+
+    static int nTab;
+
+    public static void generateSampleYaml(List<String> input, String ouput, String version,
+            String targetFolder, boolean debug) throws IOException {
+
+        String cmdName = input.get(0);
+
+        PrintWriter writer = new PrintWriter(targetFolder + "/" + cmdName + "-sample.yaml", "UTF-8");
+        writeKeyValuePair(writer, "onap_cli_sample_version", "1.0");
+        writeKeyValuePair(writer, "name", cmdName);
+        writeKeyValuePair(writer, "version", version);
+
+        writeKey(writer, "samples");
+        writeKey(writer, "sample1");
+
+        writeKeyValuePair(writer, "name", cmdName);
+        writeKeyValuePair(writer, "input", input.stream().skip(1).collect(Collectors.joining(" ")));
+        writeKeyValuePair(writer, "moco", cmdName + "-sample-yaml.yaml");
+        writeMultilineKeyValue(writer, "ouput", ouput, debug);
+
+        writeEndKey();
+        writeEndKey();
+
+        writer.flush();
+        writer.close();
+    }
+
+    private static void writeMultilineKeyValue(PrintWriter writer, String key, String value, boolean debug) {
+        writer.write(printTabs() + key + ": |\n");
+        nTab++;
+        String[] lines = value.split("\n");
+        long skipLines = debug ? 12 : 0;
+        Arrays.stream(lines).skip(skipLines ).forEach(line -> writer.write(printTabs() + line + "\n"));
+    }
+
+    private static String printTabs() {
+        StringBuffer spaces = new StringBuffer();
+        for (int i=0; i < nTab; i++) {
+            spaces.append("  ");
+        }
+        return spaces.toString();
+    }
+
+    private static void writeKeyValuePair(PrintWriter writer, String key, String value) {
+        writer.write(printTabs() +key + ": " + value + "\n");
+    }
+
+    private static void writeKey(PrintWriter writer, String key) {
+        writer.write(printTabs() + key + ":\n");
+        nTab++;
+    }
+
+    private static void writeEndKey() {
+        nTab--;
+    }
+}
diff --git a/validate/sample-yaml-generator/src/test/java/org/onap/cli/sample/yaml/SampleYamlGeneratorTest.java b/validate/sample-yaml-generator/src/test/java/org/onap/cli/sample/yaml/SampleYamlGeneratorTest.java
new file mode 100644 (file)
index 0000000..c98cb71
--- /dev/null
@@ -0,0 +1,32 @@
+/*
+ * 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.sample.yaml;
+
+import java.io.IOException;
+import java.util.Arrays;
+
+import org.junit.Test;
+
+public class SampleYamlGeneratorTest {
+
+    @Test
+    public void testGenerateSampleYaml() throws IOException {
+        SampleYamlGenerator.generateSampleYaml(Arrays.asList("testcmd", "-a", "argument"),
+                "+--------+\n+val     +\n+argument+", "test-version-1.0", "target", false);
+    }
+
+}