diff options
Diffstat (limited to 'profiles/http/src')
13 files changed, 329 insertions, 52 deletions
diff --git a/profiles/http/src/main/java/org/onap/cli/fw/http/cmd/OnapHttpCommand.java b/profiles/http/src/main/java/org/onap/cli/fw/http/cmd/OnapHttpCommand.java index 17d9c731..9477cfa9 100644 --- a/profiles/http/src/main/java/org/onap/cli/fw/http/cmd/OnapHttpCommand.java +++ b/profiles/http/src/main/java/org/onap/cli/fw/http/cmd/OnapHttpCommand.java @@ -16,13 +16,6 @@ package org.onap.cli.fw.http.cmd; -import java.io.IOException; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; - import org.onap.cli.fw.cmd.OnapCommand; import org.onap.cli.fw.cmd.OnapCommandType; import org.onap.cli.fw.conf.OnapCommandConfig; @@ -35,8 +28,10 @@ import org.onap.cli.fw.http.conf.OnapCommandHttpConstants; import org.onap.cli.fw.http.connect.HttpInput; import org.onap.cli.fw.http.connect.HttpResult; import org.onap.cli.fw.http.error.OnapCommandFailedMocoGenerate; +import org.onap.cli.fw.http.mock.MocoServer; import org.onap.cli.fw.http.schema.OnapCommandSchemaHttpLoader; import org.onap.cli.fw.http.utils.OnapCommandHttpUtils; +import org.onap.cli.fw.input.OnapCommandParameter; import org.onap.cli.fw.output.OnapCommandResultAttribute; import org.onap.cli.fw.schema.OnapCommandSchema; import org.onap.cli.fw.utils.OnapCommandUtils; @@ -44,6 +39,14 @@ import org.onap.cli.http.mock.MockJsonGenerator; import org.onap.cli.http.mock.MockRequest; import org.onap.cli.http.mock.MockResponse; +import java.io.IOException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Optional; + /** * Oclip http Command. * @@ -61,6 +64,10 @@ public class OnapHttpCommand extends OnapCommand { private OnapCommandHttpService oclipService = new OnapCommandHttpService(); + private MocoServer mocoServer; + + boolean shouldVerify = false; + public OnapHttpCommand() { super.addDefaultSchemas(OnapCommandHttpConstants.DEFAULT_PARAMETER_HTTP_FILE_NAME); } @@ -126,11 +133,64 @@ public class OnapHttpCommand extends OnapCommand { private boolean isAuthRequired() { return !this.getService().isNoAuth() - && "false".equals(this.getParametersMap().get(OnapCommandHttpConstants.DEFAULT_PARAMETER_NO_AUTH).getValue()) + && !(Boolean) (this.getParametersMap().get(OnapCommandHttpConstants.DEFAULT_PARAMETER_NO_AUTH).getValue()) && (this.getInfo().getCommandType().equals(OnapCommandType.CMD) || this.getInfo().getCommandType().equals(OnapCommandType.CATALOG)); } + private Optional<OnapCommandParameter> findParameterByName(String parameterName) { + return this.getParameters().stream() + .filter(e -> e.getName().equals(parameterName)) + .findFirst(); + } + + @Override + protected void preRun() throws OnapCommandException { + Optional<OnapCommandParameter> verifyOpt = this.getParameters().stream() + .filter(e -> e.getName().equals("verify")) + .findFirst(); + if(verifyOpt.isPresent()) { + shouldVerify = (boolean) verifyOpt.get().getValue(); + } + + if (shouldVerify) { + Optional<OnapCommandParameter> hostUrlParamOpt = findParameterByName(OnapCommandHttpConstants.VERIFY_HOST_PARAMETER_OPT); + Optional<OnapCommandParameter> noAuthParamOpt = findParameterByName(OnapCommandHttpConstants.VERIFY_NO_AUTH_PARAMETER_OPT); + + if (hostUrlParamOpt.isPresent()) { + OnapCommandParameter onapCommandParameter = hostUrlParamOpt.get(); + onapCommandParameter.setValue( + OnapCommandConfig.getPropertyValue(OnapCommandHttpConstants.VERIFY_MOCO_HOST) + + ":" + OnapCommandConfig.getPropertyValue(OnapCommandHttpConstants.VERIFY_MOCO_PORT)); + } + + if (noAuthParamOpt.isPresent()) { + OnapCommandParameter onapCommandParameter = noAuthParamOpt.get(); + onapCommandParameter.setValue(true); + } + + + Optional<OnapCommandParameter> contextOpt = this.getParameters().stream() + .filter(e -> e.getName().equals(OnapCommandConstants.VERIFY_CONTEXT_PARAM)) + .findFirst(); + + if (contextOpt.isPresent()) { + OnapCommandParameter context = contextOpt.get(); + String mockedFile = ((Map<String, String>)context.getValue()).get(OnapCommandConstants.VERIFY_MOCO); + + mocoServer = new MocoServer(mockedFile); + mocoServer.start(); + } + } + } + + @Override + protected void postRun() throws OnapCommandException { + if (shouldVerify) { + mocoServer.stop(); + } + } + @Override protected void run() throws OnapCommandException { try { diff --git a/profiles/http/src/main/java/org/onap/cli/fw/http/conf/OnapCommandHttpConstants.java b/profiles/http/src/main/java/org/onap/cli/fw/http/conf/OnapCommandHttpConstants.java index b18545cb..185582cb 100644 --- a/profiles/http/src/main/java/org/onap/cli/fw/http/conf/OnapCommandHttpConstants.java +++ b/profiles/http/src/main/java/org/onap/cli/fw/http/conf/OnapCommandHttpConstants.java @@ -83,12 +83,31 @@ public class OnapCommandHttpConstants { public static final String CATALOG_SERVICE_BASE_PATH = "catalog-service-base-path"; public static final String CATALOG_SERVICE_HOST_URL = "catalog-service-host-url"; - public static final String AUTH_VALUES = "cli.schema.auth_values"; - public static final String MODE_VALUES = "cli.schema.mode_values"; - public static final String SERVICE_PARAMS_LIST = "cli.schema.service_params_list"; - public static final String SERVICE_PARAMS_MANDATORY_LIST = "cli.schema.service_params_mandatory_list"; + public static final String AUTH_VALUES = "cli.schema.http.service.auth.values"; + public static final String MODE_VALUES = "cli.schema.http.service.mode.values"; + public static final String SERVICE_PARAMS_LIST = "cli.schema.http.service.sections"; + public static final String SERVICE_PARAMS_MANDATORY_LIST = "cli.schema.http.service.sections.mandatory"; public static final String DEFAULT_PARAMETER_NO_CATALOG = "no-catalog"; + + //context param + public static final String CONTEXT = "context"; + public static final String CONTEXT_REMOVE_EMPTY_JSON_NODES = "remove_empty_node"; + + // moco server const + public static final String VERIFY_MOCO_HOST = "cli.verify.host"; + public static final String VERIFY_MOCO_PORT = "cli.verify.port"; + + public static final String VERIFY_HOST_PARAMETER_OPT = DEAFULT_PARAMETER_HOST_URL; + public static final String VERIFY_NO_AUTH_PARAMETER_OPT = DEFAULT_PARAMETER_NO_AUTH; + + public static final String VERIFY_REQUEST_URI = URI; + public static final String VERIFY_RESPONSE_STATUS = "status"; + public static final String VERIFY_RESPONSE_JSON = "json"; + public static final String VERIFY_REQUEST = REQUEST; + public static final String VERIFY_RESPONSE = "response"; + public static final String VERIFY_CONTENT_TYPE = "Content-Type"; + public static final String VERIFY_CONTENT_TYPE_VALUE = APPLICATION_JSON; } diff --git a/profiles/http/src/main/java/org/onap/cli/fw/http/connect/HttpInput.java b/profiles/http/src/main/java/org/onap/cli/fw/http/connect/HttpInput.java index 722dd12e..f5922796 100644 --- a/profiles/http/src/main/java/org/onap/cli/fw/http/connect/HttpInput.java +++ b/profiles/http/src/main/java/org/onap/cli/fw/http/connect/HttpInput.java @@ -38,6 +38,8 @@ public class HttpInput { private Map<String, String> reqCookies = new HashMap<>(); + private Map<String, String> context = new HashMap<>(); + private boolean binaryData; public String getUri() { @@ -116,6 +118,10 @@ public class HttpInput { return reqCookies; } + public Map<String, String> getContext() { + return context; + } + public HttpInput setReqCookies(Map<String, String> reqCookies) { this.reqCookies = reqCookies; return this; @@ -134,6 +140,6 @@ public class HttpInput { return "\nURL: " + this.getUri() + "\nMethod: " + this.getMethod() + "\nRequest Queries: " + this.getReqQueries() + "\nRequest Body: " + this.getBody() + "\nRequest Headers: " + this.getReqHeaders().toString() + "\nRequest Cookies: " + this.getReqCookies().toString() - + "\nbinaryData=" + this.binaryData; + + "\nbinaryData=" + this.binaryData + "\nContext=" + this.context; } } diff --git a/profiles/http/src/main/java/org/onap/cli/fw/http/error/OnapCommandHttpInvalidRequestBody.java b/profiles/http/src/main/java/org/onap/cli/fw/http/error/OnapCommandHttpInvalidRequestBody.java new file mode 100644 index 00000000..73c721e3 --- /dev/null +++ b/profiles/http/src/main/java/org/onap/cli/fw/http/error/OnapCommandHttpInvalidRequestBody.java @@ -0,0 +1,43 @@ +/* + * 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.error; + +import org.onap.cli.fw.error.OnapCommandException; + +/** + * OnapCommandParameterNotFound. + * + */ +public class OnapCommandHttpInvalidRequestBody extends OnapCommandException { + + private static final long serialVersionUID = 6676137916079057963L; + + private static final String ERROR_CODE = "0x3008"; + private static final String ERR_MSG = "Http request body does not have valid json "; + + public OnapCommandHttpInvalidRequestBody(String name, String error) { + super(ERROR_CODE, ERR_MSG + name + ", " + error); + } + + public OnapCommandHttpInvalidRequestBody(String name, Throwable throwable) { + super(ERROR_CODE, ERR_MSG + name, throwable); + } + + public OnapCommandHttpInvalidRequestBody(Throwable e) { + this(ERROR_CODE, e.getMessage()); + } +} diff --git a/profiles/http/src/main/java/org/onap/cli/fw/http/mock/MocoServer.java b/profiles/http/src/main/java/org/onap/cli/fw/http/mock/MocoServer.java new file mode 100644 index 00000000..f6e58757 --- /dev/null +++ b/profiles/http/src/main/java/org/onap/cli/fw/http/mock/MocoServer.java @@ -0,0 +1,105 @@ +/* + * 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 com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.github.dreamhead.moco.HttpServer; +import com.github.dreamhead.moco.Moco; +import com.github.dreamhead.moco.ResponseHandler; +import com.github.dreamhead.moco.Runner; +import org.onap.cli.fw.conf.OnapCommandConfig; +import org.onap.cli.fw.conf.OnapCommandConstants; +import org.onap.cli.fw.error.OnapCommandDiscoveryFailed; +import org.onap.cli.fw.error.OnapCommandException; +import org.onap.cli.fw.error.OnapCommandInvalidSchema; +import org.onap.cli.fw.http.conf.OnapCommandHttpConstants; +import org.onap.cli.fw.schema.OnapCommandSchemaLoader; +import org.onap.cli.fw.utils.OnapCommandDiscoveryUtils; +import org.springframework.core.io.Resource; +import org.yaml.snakeyaml.Yaml; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import static com.github.dreamhead.moco.Runner.runner; + +public class MocoServer { + + private Runner runner; + private Map<String, Object> mocoServerConfigs = new HashMap(); + + public MocoServer(String mockFile) throws OnapCommandException { + Resource resource = null; + + try { + resource = OnapCommandDiscoveryUtils.findResource(mockFile, + OnapCommandConstants.VERIFY_SAMPLES_MOCK_PATTERN); + } catch (IOException e) { + throw new OnapCommandDiscoveryFailed(mockFile, e); + } + + List<Map<String, ?>> stringMap = null; + try { + stringMap = (List<Map<String, ?>>) new Yaml().load(resource.getInputStream()); + } catch (IOException e) { + throw new OnapCommandException("Invalid mocking file" + mockFile, e); + } + if(!stringMap.isEmpty()) { + Map<String, ?> jsonConfigs = stringMap.get(0); + Map<String, String> request = (Map<String, String>) jsonConfigs.get(OnapCommandHttpConstants.VERIFY_REQUEST); + mocoServerConfigs.put(OnapCommandHttpConstants.VERIFY_REQUEST_URI, request.get(OnapCommandHttpConstants.VERIFY_REQUEST_URI)); + + Map<String, String> response = (Map<String, String>) jsonConfigs.get(OnapCommandHttpConstants.VERIFY_RESPONSE); + mocoServerConfigs.put(OnapCommandHttpConstants.VERIFY_RESPONSE_STATUS, response.get(OnapCommandHttpConstants.VERIFY_RESPONSE_STATUS)); + + if(response.get(OnapCommandHttpConstants.VERIFY_RESPONSE_JSON) != null) { + try { + mocoServerConfigs.put(OnapCommandHttpConstants.VERIFY_RESPONSE_JSON, + new ObjectMapper().writeValueAsString(response.get(OnapCommandHttpConstants.VERIFY_RESPONSE_JSON))); + } catch (JsonProcessingException e) { + throw new OnapCommandException("Invalid mocking file" + mockFile, e); + } + } + } + } + + public void start() { + HttpServer server = Moco.httpServer(Integer.parseInt( + OnapCommandConfig.getPropertyValue(OnapCommandHttpConstants.VERIFY_MOCO_PORT))); + + List<ResponseHandler> responseHandlers = new ArrayList<>(); + + if (mocoServerConfigs.containsKey(OnapCommandHttpConstants.VERIFY_RESPONSE_JSON)) { + responseHandlers.add(Moco.with(mocoServerConfigs.get(OnapCommandHttpConstants.VERIFY_RESPONSE_JSON).toString())); + } + responseHandlers.add(Moco.status((Integer) mocoServerConfigs.get(OnapCommandHttpConstants.VERIFY_RESPONSE_STATUS))); + + server.request(Moco.by(Moco.uri((String) mocoServerConfigs.get(OnapCommandHttpConstants.VERIFY_REQUEST_URI)))) + .response(Moco.header(OnapCommandHttpConstants.VERIFY_CONTENT_TYPE, OnapCommandHttpConstants.VERIFY_CONTENT_TYPE_VALUE), + responseHandlers.toArray(new ResponseHandler[responseHandlers.size()])); + + runner = runner(server); + runner.start(); + } + + public void stop() { + runner.stop(); + } +} diff --git a/profiles/http/src/main/java/org/onap/cli/fw/http/schema/OnapCommandSchemaHttpLoader.java b/profiles/http/src/main/java/org/onap/cli/fw/http/schema/OnapCommandSchemaHttpLoader.java index 8e01b585..52b7571d 100644 --- a/profiles/http/src/main/java/org/onap/cli/fw/http/schema/OnapCommandSchemaHttpLoader.java +++ b/profiles/http/src/main/java/org/onap/cli/fw/http/schema/OnapCommandSchemaHttpLoader.java @@ -132,6 +132,20 @@ public class OnapCommandSchemaHttpLoader { cmd.getInput().setReqQueries(query); break; + case OnapCommandHttpConstants.CONTEXT: + Map<String, Object> context = (Map<String, Object>) map.get(key2); + + for (String key: context.keySet()) { + switch (key) { + case OnapCommandHttpConstants.CONTEXT_REMOVE_EMPTY_JSON_NODES: + Boolean flag = (Boolean) context.get(OnapCommandHttpConstants.CONTEXT_REMOVE_EMPTY_JSON_NODES); + cmd.getInput().getContext().put(OnapCommandHttpConstants.CONTEXT_REMOVE_EMPTY_JSON_NODES, flag.toString()); + break; + } + } + + + break; case OnapCommandHttpConstants.MULTIPART_ENTITY_NAME: Object multipartEntityName = map.get(key2); cmd.getInput().setMultipartEntityName(multipartEntityName.toString()); @@ -371,15 +385,17 @@ public class OnapCommandSchemaHttpLoader { } String body = String.valueOf(bodyString); - JSONObject obj = null; - try { - obj = new ObjectMapper().readValue(body, JSONObject.class); - } catch (IOException e1) { // NOSONAR - errorList.add(OnapCommandHttpConstants.HTTP_BODY_FAILED_PARSING); - } - if (obj == null || "".equals(obj.toString())) { + + if (body == null || "".equals(body)) { errorList.add(OnapCommandHttpConstants.HTTP_BODY_JSON_EMPTY); + } else { + try { + new ObjectMapper().readValue(body, JSONObject.class); + } catch (IOException e1) { // NOSONAR + errorList.add(OnapCommandHttpConstants.HTTP_BODY_FAILED_PARSING); + } } + OnapCommandUtils.parseParameters(body, bodyParamNames); return bodyParamNames; diff --git a/profiles/http/src/main/java/org/onap/cli/fw/http/utils/OnapCommandHttpUtils.java b/profiles/http/src/main/java/org/onap/cli/fw/http/utils/OnapCommandHttpUtils.java index 7b10dbce..b77b85da 100644 --- a/profiles/http/src/main/java/org/onap/cli/fw/http/utils/OnapCommandHttpUtils.java +++ b/profiles/http/src/main/java/org/onap/cli/fw/http/utils/OnapCommandHttpUtils.java @@ -18,6 +18,7 @@ package org.onap.cli.fw.http.utils; import java.util.ArrayList; import java.util.HashMap; +import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Map.Entry; @@ -27,9 +28,11 @@ import org.onap.cli.fw.error.OnapCommandInvalidParameterValue; import org.onap.cli.fw.error.OnapCommandParameterNotFound; import org.onap.cli.fw.error.OnapCommandResultEmpty; import org.onap.cli.fw.error.OnapCommandResultMapProcessingFailed; +import org.onap.cli.fw.http.conf.OnapCommandHttpConstants; import org.onap.cli.fw.http.connect.HttpInput; import org.onap.cli.fw.http.connect.HttpResult; import org.onap.cli.fw.http.error.OnapCommandHttpHeaderNotFound; +import org.onap.cli.fw.http.error.OnapCommandHttpInvalidRequestBody; import org.onap.cli.fw.http.error.OnapCommandHttpInvalidResponseBody; import org.onap.cli.fw.input.OnapCommandParameter; import org.onap.cli.fw.input.OnapCommandParameterType; @@ -37,6 +40,8 @@ import org.onap.cli.fw.utils.OnapCommandUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; import com.jayway.jsonpath.JsonPath; import com.jayway.jsonpath.PathNotFoundException; @@ -81,6 +86,12 @@ public class OnapCommandHttpUtils { inp.getReqQueries().put(h, OnapCommandUtils.replaceLineFromInputParameters(value, params)); } + boolean isRemoveEmptyNodes = Boolean.parseBoolean(input.getContext().getOrDefault(OnapCommandHttpConstants.CONTEXT_REMOVE_EMPTY_JSON_NODES, "false")); + + if (isRemoveEmptyNodes) { + inp.setBody(OnapCommandHttpUtils.normalizeJson(input.getBody())); + } + return inp; } @@ -235,5 +246,30 @@ public class OnapCommandHttpUtils { } } + public static void normalizeJson(JsonNode node) { + Iterator<JsonNode> it = node.iterator(); + while (it.hasNext()) { + JsonNode child = it.next(); + if (child.isTextual() && child.asText().equals("")) + it.remove(); + else if (child.isNull()) + it.remove(); + else + normalizeJson(child); + } + } + + public static String normalizeJson(String json) throws OnapCommandHttpInvalidRequestBody { + ObjectMapper mapper = new ObjectMapper(); + JsonNode node; + try { + node = mapper.readTree(json); + normalizeJson(node); + return mapper.writeValueAsString(node); + } catch (Exception e) { //NOSONAR + throw new OnapCommandHttpInvalidRequestBody(e); + } + + } } diff --git a/profiles/http/src/main/resources/open-cli-http.properties b/profiles/http/src/main/resources/open-cli-http.properties index db19d088..8cde793e 100644 --- a/profiles/http/src/main/resources/open-cli-http.properties +++ b/profiles/http/src/main/resources/open-cli-http.properties @@ -6,7 +6,7 @@ cli.http.api_key_use_cookies=true cli.schema.http.sections=request,service,success_codes,result_map,sample_response cli.schema.http.sections.mandatory=request, success_codes -cli.schema.http.request.sections=uri,method,body,headers,queries,multipart_entity_name +cli.schema.http.request.sections=uri,method,body,headers,queries,multipart_entity_name,context cli.schema.http.request.sections.mandatory=uri,method cli.schema.http.service.sections=name,version,auth,mode @@ -16,3 +16,7 @@ cli.schema.http.request.method.values=post,get,delete,put,head cli.schema.http.service.auth.values=none,basic cli.schema.http.service.mode.values=direct,catalog + +#verify properties +cli.verify.host=http://localhost +cli.verify.port=8585
\ No newline at end of file diff --git a/profiles/http/src/main/resources/open-cli-schema/http/default_input_parameters_http.yaml b/profiles/http/src/main/resources/open-cli-schema/http/default_input_parameters_http.yaml index 6721b207..6ac0ded2 100644 --- a/profiles/http/src/main/resources/open-cli-schema/http/default_input_parameters_http.yaml +++ b/profiles/http/src/main/resources/open-cli-schema/http/default_input_parameters_http.yaml @@ -40,4 +40,10 @@ parameters: long_option: no-catalog is_optional: true is_default_param: true - default_value: false
\ No newline at end of file + default_value: false + - name: verify + type: bool + description: verify the command using available command sample file and mocking file + default_value: false + is_include: true + is_optional: true
\ No newline at end of file diff --git a/profiles/http/src/test/java/org/onap/cli/fw/http/HttpInputOutputTest.java b/profiles/http/src/test/java/org/onap/cli/fw/http/HttpInputOutputTest.java index 99ee3d34..7a7a31c7 100644 --- a/profiles/http/src/test/java/org/onap/cli/fw/http/HttpInputOutputTest.java +++ b/profiles/http/src/test/java/org/onap/cli/fw/http/HttpInputOutputTest.java @@ -44,7 +44,7 @@ public class HttpInputOutputTest { inp.setReqQueries(new HashMap<String, String>()); assertTrue( - "\nURL: uri\nMethod: method\nRequest Queries: {}\nRequest Body: body\nRequest Headers: {}\nRequest Cookies: {}\nbinaryData=false" + "\nURL: uri\nMethod: method\nRequest Queries: {}\nRequest Body: body\nRequest Headers: {}\nRequest Cookies: {}\nbinaryData=false\nContext={}" .equals(inp.toString())); } diff --git a/profiles/http/src/test/java/org/onap/cli/fw/http/utils/OnapCommandUtilsTest.java b/profiles/http/src/test/java/org/onap/cli/fw/http/utils/OnapCommandUtilsTest.java index 12920515..13b2db42 100644 --- a/profiles/http/src/test/java/org/onap/cli/fw/http/utils/OnapCommandUtilsTest.java +++ b/profiles/http/src/test/java/org/onap/cli/fw/http/utils/OnapCommandUtilsTest.java @@ -42,6 +42,7 @@ import org.onap.cli.fw.error.OnapCommandSchemaNotFound; import org.onap.cli.fw.http.cmd.OnapHttpCommand; import org.onap.cli.fw.http.connect.HttpResult; import org.onap.cli.fw.http.error.OnapCommandHttpHeaderNotFound; +import org.onap.cli.fw.http.error.OnapCommandHttpInvalidRequestBody; import org.onap.cli.fw.http.error.OnapCommandHttpInvalidResponseBody; import org.onap.cli.fw.http.schema.OnapCommandSchemaHttpLoader; import org.onap.cli.fw.input.OnapCommandParameter; @@ -93,7 +94,7 @@ public class OnapCommandUtilsTest { assertTrue("sample-test".equals(cmd.getName())); Map<String, OnapCommandParameter> map = OnapCommandUtils.getInputMap(cmd.getParameters()); - assertTrue(map.size() == 7); + assertTrue(map.size() == 9); } @Test(expected = OnapCommandHttpHeaderNotFound.class) @@ -136,4 +137,11 @@ public class OnapCommandUtilsTest { protected void run() throws OnapCommandException { } } + + @Test + public void testJsonEmptyCheck() throws OnapCommandHttpInvalidRequestBody { + String sample = "{\"request\":{\"method\":\"\",\"uri\":\"/onboarding-api/v1.0/vendor-license-models/cf2d907d998e44698ce3b4cded5f66a7/versions/2.0/license-agreements\",\"headers\":{\"Authorization\":\"Basic Y3MwMDA4OmRlbW8xMjM0NTYh\",\"X-FromAppId\":\"onap-cli\",\"Accept\":\"application/json\",\"USER_ID\":\"cs0008\",\"X-TransactionId\":\"req-66a37478-d840-44f8-b436-56f4a3b6f640\",\"Content-Type\":\"application/json\"},\"json\":null},\"response\":{\"status\":200,\"json\":{\"listCount\":2,\"results\":[{\"name\":\"sf\",\"description\":\"sdfgdf\",\"licenseTerm\":{\"choice\":\"Fixed_Term\",\"other\":null},\"id\":\"1e2edfccaca847f896070d0fac26667a\",\"featureGroupsIds\":[\"3a2fb75b52a54e9c8093e7c154210f9e\"]},{\"name\":\"kanag-cli-la\",\"description\":\"kanag cli la\",\"licenseTerm\":{\"choice\":\"Fixed_Term\",\"other\":\"\"},\"id\":\"77e151d0503b45ecb7e40f5f5f1a887e\",\"featureGroupsIds\":[\"3a2fb75b52a54e9c8093e7c154210f9e\"]}]}}}"; + String result = "{\"request\":{\"uri\":\"/onboarding-api/v1.0/vendor-license-models/cf2d907d998e44698ce3b4cded5f66a7/versions/2.0/license-agreements\",\"headers\":{\"Authorization\":\"Basic Y3MwMDA4OmRlbW8xMjM0NTYh\",\"X-FromAppId\":\"onap-cli\",\"Accept\":\"application/json\",\"USER_ID\":\"cs0008\",\"X-TransactionId\":\"req-66a37478-d840-44f8-b436-56f4a3b6f640\",\"Content-Type\":\"application/json\"}},\"response\":{\"status\":200,\"json\":{\"listCount\":2,\"results\":[{\"name\":\"sf\",\"description\":\"sdfgdf\",\"licenseTerm\":{\"choice\":\"Fixed_Term\"},\"id\":\"1e2edfccaca847f896070d0fac26667a\",\"featureGroupsIds\":[\"3a2fb75b52a54e9c8093e7c154210f9e\"]},{\"name\":\"kanag-cli-la\",\"description\":\"kanag cli la\",\"licenseTerm\":{\"choice\":\"Fixed_Term\"},\"id\":\"77e151d0503b45ecb7e40f5f5f1a887e\",\"featureGroupsIds\":[\"3a2fb75b52a54e9c8093e7c154210f9e\"]}]}}}"; + assertEquals(result, OnapCommandHttpUtils.normalizeJson(sample)); + } } diff --git a/profiles/http/src/test/resources/open-cli-schema/sample-test1-schema-http.yaml b/profiles/http/src/test/resources/open-cli-schema/sample-test1-schema-http.yaml index f162231f..f7da6da2 100644 --- a/profiles/http/src/test/resources/open-cli-schema/sample-test1-schema-http.yaml +++ b/profiles/http/src/test/resources/open-cli-schema/sample-test1-schema-http.yaml @@ -75,6 +75,8 @@ http: method: POST body: '{"name":"${name}","vendor":"${vendor}","version":"${vim-version}","description":"${description}","type":"${type}","url":"${url}","userName":"${username}","password":"${password}","domain":"${domain}","tenant":"${tenant}"}' headers: + context: + remove_empty_node: true success_codes: - 201 - 200 diff --git a/profiles/http/src/test/resources/sample-test-schema-swagger.yaml b/profiles/http/src/test/resources/sample-test-schema-swagger.yaml deleted file mode 100644 index 4108d4e6..00000000 --- a/profiles/http/src/test/resources/sample-test-schema-swagger.yaml +++ /dev/null @@ -1,28 +0,0 @@ -open_cli_schema_version: 1.0 -name: sample-test-swagger -description: Sample swagger command test. -info: - product: open-cli - service: test - type: cmd - author: Kanagaraj Manickam kanagaraj.manickam@huawei.com -parameters: - - name: user - type: string - description: Oclip user - short_option: n - long_option: username - is_optional: false -results: - direction: portrait - attributes: - - name: name - description: Oclip user - scope: short - type: string -exec: - api: org.onap.common_services.auth.auth_service.client.api.DefaultApi - client: org.onap.common_services.auth.auth_service.client.invoker.ApiClient - entity: org.onap.common_services.auth.auth_service.client.model.User, username(userName), password, description - method: create - exception: org.onap.common_services.auth.auth_service.client.invoker.ApiException
\ No newline at end of file |