diff options
Diffstat (limited to 'framework/src')
9 files changed, 35 insertions, 317 deletions
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 a5c79f7c..1e202a54 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,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); + } } } } 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 f8bd928a..7dfc0d46 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,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() { } 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 6c302b22..bb0e2c06 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,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 index 91586d56..00000000 --- a/framework/src/main/java/org/onap/cli/fw/http/mock/MockJsonGenerator.java +++ /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); - } - } -} 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 deleted file mode 100644 index 36d34d30..00000000 --- a/framework/src/main/java/org/onap/cli/fw/http/mock/MockObject.java +++ /dev/null @@ -1,40 +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; - -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 deleted file mode 100644 index e9188645..00000000 --- a/framework/src/main/java/org/onap/cli/fw/http/mock/MockRequest.java +++ /dev/null @@ -1,83 +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.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 deleted file mode 100644 index 9950d87d..00000000 --- a/framework/src/main/java/org/onap/cli/fw/http/mock/MockResponse.java +++ /dev/null @@ -1,56 +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.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 26955674..19182ae3 100644 --- a/framework/src/main/resources/onap.properties +++ b/framework/src/main/resources/onap.properties @@ -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 index 2d996407..00000000 --- a/framework/src/test/java/org/onap/cli/fw/http/mock/MockJsonGeneratorTest.java +++ /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"); - } -} |