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;
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.
*
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.
*
for (OnapCommandResultAttribute attr : this.getResult().getRecords()) {
attr.setValues(results.get(attr.getName()));
}
+
+ if (OnapCommandConfg.isMocoGenerateEnabled()) {
+ MockJsonGenerator.generateMocking(httpInput, output, this.getName());
+ }
}
}
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() {
}
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));
+ }
}
--- /dev/null
+/*
+ * 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
--- /dev/null
+/*
+ * 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);
+ }
+ }
+}
--- /dev/null
+/*
+ * 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;
+ }
+
+
+}
--- /dev/null
+/*
+ * 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);
+ }
+ }
+}
--- /dev/null
+/*
+ * 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);
+ }
+ }
+}
#product version
cli.product.version=cli-1.0
+# moco properties
+cli.moco.enable=false
+cli.moco.target=.
+
--- /dev/null
+/*
+ * 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");
+ }
+}
"content-type" : "application/json"
}
},
- "response" :
- {
+ "response" :{
+ "json" : {
"listCount": 1,
"results": [
{
}
]
}]
- }
+ }}
}]
\ No newline at end of file