Implement autogeneation of moco json 83/19083/2
authorsubhash kumar singh <subhash.kumar.singh@huawei.com>
Mon, 16 Oct 2017 14:01:36 +0000 (14:01 +0000)
committersubhash kumar singh <subhash.kumar.singh@huawei.com>
Tue, 17 Oct 2017 05:53:43 +0000 (05:53 +0000)
Autogenerate mocked json files for moco-master from http request
respose.

Change-Id: I731682c41b623713a6b4de7ea74b14ab83f5d17e
Issue-ID: CLI-55
Signed-off-by: subhash kumar singh <subhash.kumar.singh@huawei.com>
12 files changed:
framework/src/main/java/org/onap/cli/fw/OnapCommand.java
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/error/OnapCommandFailedMocoGenerate.java [new file with mode: 0644]
framework/src/main/java/org/onap/cli/fw/http/mock/MockJsonGenerator.java [new file with mode: 0644]
framework/src/main/java/org/onap/cli/fw/http/mock/MockObject.java [new file with mode: 0644]
framework/src/main/java/org/onap/cli/fw/http/mock/MockRequest.java [new file with mode: 0644]
framework/src/main/java/org/onap/cli/fw/http/mock/MockResponse.java [new file with mode: 0644]
framework/src/main/resources/onap.properties
framework/src/test/java/org/onap/cli/fw/http/mock/MockJsonGeneratorTest.java [new file with mode: 0644]
plugins/sdc/src/test/resources/onap-cli-sample/license-model-show-schema-1.1-moco.json

index dc90314..61aa2cb 100644 (file)
 
 package org.onap.cli.fw;
 
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+
 import org.onap.cli.fw.ad.OnapAuthClient;
 import org.onap.cli.fw.ad.OnapCredentials;
 import org.onap.cli.fw.ad.OnapService;
@@ -40,12 +45,6 @@ import org.onap.cli.fw.output.OnapCommandResultAttributeScope;
 import org.onap.cli.fw.output.ResultType;
 import org.onap.cli.fw.utils.OnapCommandUtils;
 
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Map;
-import java.util.Optional;
-import java.util.Set;
-
 /**
  * Onap Command.
  *
index e3ec17d..a5c79f7 100644 (file)
 
 package org.onap.cli.fw.cmd;
 
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
 import org.onap.cli.fw.OnapCommand;
 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.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 java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-
 /**
  * Onap Command.
  *
@@ -93,5 +95,9 @@ public class OnapHttpCommand extends OnapCommand {
         for (OnapCommandResultAttribute attr : this.getResult().getRecords()) {
             attr.setValues(results.get(attr.getName()));
         }
+
+        if (OnapCommandConfg.isMocoGenerateEnabled()) {
+            MockJsonGenerator.generateMocking(httpInput, output, this.getName());
+        }
     }
 }
index e9e52ba..f8bd928 100644 (file)
@@ -198,6 +198,9 @@ 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";
+
     private Constants() {
     }
 
index 249ec0e..6c302b2 100644 (file)
@@ -174,4 +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 boolean isMocoGenerateEnabled() {
+        return "true".equals(prps.getProperty(Constants.MOCO_ENABLED));
+    }
 }
diff --git a/framework/src/main/java/org/onap/cli/fw/error/OnapCommandFailedMocoGenerate.java b/framework/src/main/java/org/onap/cli/fw/error/OnapCommandFailedMocoGenerate.java
new file mode 100644 (file)
index 0000000..37e8fb3
--- /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;
+
+/**
+ * Invalid data for generating moco json .
+ *
+ */
+public class OnapCommandFailedMocoGenerate extends OnapCommandException {
+
+    private static final long serialVersionUID = -5386652726982792831L;
+
+    private static final String ERROR_CODE = "0xf002";
+
+    private static final String ERROR_MSG = "Failed to generate moco json ";
+
+    public OnapCommandFailedMocoGenerate(String cmdName, String error) {
+        super(ERROR_CODE, ERROR_MSG + cmdName + ", " + error);
+    }
+
+    public OnapCommandFailedMocoGenerate(String cmdName, Throwable throwable) {
+        super(ERROR_CODE, ERROR_MSG + cmdName , throwable);
+    }
+
+}
\ No newline at end of file
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
new file mode 100644 (file)
index 0000000..91586d5
--- /dev/null
@@ -0,0 +1,61 @@
+/*
+ * 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);
+        }
+    }
+}
diff --git a/framework/src/main/java/org/onap/cli/fw/http/mock/MockObject.java b/framework/src/main/java/org/onap/cli/fw/http/mock/MockObject.java
new file mode 100644 (file)
index 0000000..36d34d3
--- /dev/null
@@ -0,0 +1,40 @@
+/*
+ * 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;
+
+public class MockObject {
+    private MockRequest request;
+    private MockResponse response;
+
+    public MockRequest getRequest() {
+        return request;
+    }
+
+    public void setRequest(MockRequest request) {
+        this.request = request;
+    }
+
+    public MockResponse getResponse() {
+        return response;
+    }
+
+    public void setResponse(MockResponse response) {
+        this.response = response;
+    }
+
+
+}
diff --git a/framework/src/main/java/org/onap/cli/fw/http/mock/MockRequest.java b/framework/src/main/java/org/onap/cli/fw/http/mock/MockRequest.java
new file mode 100644 (file)
index 0000000..e918864
--- /dev/null
@@ -0,0 +1,83 @@
+/*
+ * 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.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;
+    private String uri;
+    private Map<String, String> headers;
+    private JsonNode json;
+
+    public String getMethod() {
+        return method;
+    }
+
+    public void setMethod(String method) {
+        this.method = method;
+    }
+
+    public String getUri() {
+        return uri;
+    }
+
+    public void setUri(String url) throws OnapCommandFailedMocoGenerate {
+        URL urlt;
+        try {
+            urlt = new URL(url);
+        } catch (MalformedURLException error) {
+            throw new OnapCommandFailedMocoGenerate(null, error);
+        }
+        this.uri = urlt.getPath();
+    }
+
+    public Map<String, String> getHeaders() {
+        return headers;
+    }
+
+    public void setHeaders(Map<String, String> headers) {
+        this.headers = headers;
+    }
+
+    public JsonNode getJson() {
+        return json;
+    }
+
+    public void setJson(String json) throws OnapCommandFailedMocoGenerate {
+        if (json.isEmpty()) {
+            this.json = JsonNodeFactory.instance.objectNode();
+            return;
+        }
+
+        ObjectMapper objectMapper = new ObjectMapper();
+        try {
+            this.json = objectMapper.readTree(json);
+        }catch (IOException error) {
+            throw new OnapCommandFailedMocoGenerate(null, error);
+        }
+    }
+}
diff --git a/framework/src/main/java/org/onap/cli/fw/http/mock/MockResponse.java b/framework/src/main/java/org/onap/cli/fw/http/mock/MockResponse.java
new file mode 100644 (file)
index 0000000..9950d87
--- /dev/null
@@ -0,0 +1,56 @@
+/*
+ * 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.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;
+    private JsonNode json;
+
+    public int getStatus() {
+        return status;
+    }
+
+    public void setStatus(int status) {
+        this.status = status;
+    }
+
+    public JsonNode getJson() {
+        return json;
+    }
+
+    public void setJson(String json) throws OnapCommandFailedMocoGenerate {
+        if (json.isEmpty()) {
+            this.json = JsonNodeFactory.instance.objectNode();
+            return;
+        }
+
+        ObjectMapper objectMapper = new ObjectMapper();
+        try {
+            this.json = objectMapper.readTree(json);
+        } catch (IOException error) {
+            throw new OnapCommandFailedMocoGenerate(null, error);
+        }
+    }
+}
index 7b04c6a..2695567 100644 (file)
@@ -56,3 +56,7 @@ cli.schema.mode_values=direct,catalog
 #product version
 cli.product.version=cli-1.0
 
+# moco properties
+cli.moco.enable=false
+cli.moco.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
new file mode 100644 (file)
index 0000000..2d99640
--- /dev/null
@@ -0,0 +1,66 @@
+/*
+ * 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 08493fa..f043988 100644 (file)
@@ -8,8 +8,8 @@
           "content-type" : "application/json"
       }
     },
-  "response" :
-    {
+  "response" :{
+    "json" : {
       "listCount": 1,
     "results": [
         {
@@ -35,5 +35,5 @@
                 }
             ]
         }]
-    }
+    }}
 }]
\ No newline at end of file