summaryrefslogtreecommitdiffstats
path: root/framework/src/main
diff options
context:
space:
mode:
authorsubhash kumar singh <subhash.kumar.singh@huawei.com>2017-10-16 14:01:36 +0000
committersubhash kumar singh <subhash.kumar.singh@huawei.com>2017-10-17 05:53:43 +0000
commitdba77b07f034f234dba8dfe58885135b7cbbdc95 (patch)
tree57329d3f16ca991196a17b0de764dfbef1c975dc /framework/src/main
parent878e79ea47fe7500982c9703bcd253aa5a512d69 (diff)
Implement autogeneation of moco json
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>
Diffstat (limited to 'framework/src/main')
-rw-r--r--framework/src/main/java/org/onap/cli/fw/OnapCommand.java11
-rw-r--r--framework/src/main/java/org/onap/cli/fw/cmd/OnapHttpCommand.java16
-rw-r--r--framework/src/main/java/org/onap/cli/fw/conf/Constants.java3
-rw-r--r--framework/src/main/java/org/onap/cli/fw/conf/OnapCommandConfg.java7
-rw-r--r--framework/src/main/java/org/onap/cli/fw/error/OnapCommandFailedMocoGenerate.java39
-rw-r--r--framework/src/main/java/org/onap/cli/fw/http/mock/MockJsonGenerator.java61
-rw-r--r--framework/src/main/java/org/onap/cli/fw/http/mock/MockObject.java40
-rw-r--r--framework/src/main/java/org/onap/cli/fw/http/mock/MockRequest.java83
-rw-r--r--framework/src/main/java/org/onap/cli/fw/http/mock/MockResponse.java56
-rw-r--r--framework/src/main/resources/onap.properties4
10 files changed, 309 insertions, 11 deletions
diff --git a/framework/src/main/java/org/onap/cli/fw/OnapCommand.java b/framework/src/main/java/org/onap/cli/fw/OnapCommand.java
index dc90314a..61aa2cb2 100644
--- a/framework/src/main/java/org/onap/cli/fw/OnapCommand.java
+++ b/framework/src/main/java/org/onap/cli/fw/OnapCommand.java
@@ -16,6 +16,11 @@
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.
*
diff --git a/framework/src/main/java/org/onap/cli/fw/cmd/OnapHttpCommand.java b/framework/src/main/java/org/onap/cli/fw/cmd/OnapHttpCommand.java
index e3ec17de..a5c79f7c 100644
--- a/framework/src/main/java/org/onap/cli/fw/cmd/OnapHttpCommand.java
+++ b/framework/src/main/java/org/onap/cli/fw/cmd/OnapHttpCommand.java
@@ -16,20 +16,22 @@
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());
+ }
}
}
diff --git a/framework/src/main/java/org/onap/cli/fw/conf/Constants.java b/framework/src/main/java/org/onap/cli/fw/conf/Constants.java
index e9e52baa..f8bd928a 100644
--- a/framework/src/main/java/org/onap/cli/fw/conf/Constants.java
+++ b/framework/src/main/java/org/onap/cli/fw/conf/Constants.java
@@ -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() {
}
diff --git a/framework/src/main/java/org/onap/cli/fw/conf/OnapCommandConfg.java b/framework/src/main/java/org/onap/cli/fw/conf/OnapCommandConfg.java
index 249ec0e0..6c302b22 100644
--- a/framework/src/main/java/org/onap/cli/fw/conf/OnapCommandConfg.java
+++ b/framework/src/main/java/org/onap/cli/fw/conf/OnapCommandConfg.java
@@ -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
index 00000000..37e8fb35
--- /dev/null
+++ b/framework/src/main/java/org/onap/cli/fw/error/OnapCommandFailedMocoGenerate.java
@@ -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
index 00000000..91586d56
--- /dev/null
+++ b/framework/src/main/java/org/onap/cli/fw/http/mock/MockJsonGenerator.java
@@ -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
index 00000000..36d34d30
--- /dev/null
+++ b/framework/src/main/java/org/onap/cli/fw/http/mock/MockObject.java
@@ -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
index 00000000..e9188645
--- /dev/null
+++ b/framework/src/main/java/org/onap/cli/fw/http/mock/MockRequest.java
@@ -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
index 00000000..9950d87d
--- /dev/null
+++ b/framework/src/main/java/org/onap/cli/fw/http/mock/MockResponse.java
@@ -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);
+ }
+ }
+}
diff --git a/framework/src/main/resources/onap.properties b/framework/src/main/resources/onap.properties
index 7b04c6a5..26955674 100644
--- a/framework/src/main/resources/onap.properties
+++ b/framework/src/main/resources/onap.properties
@@ -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=.
+