2 * Copyright 2017 Huawei Technologies Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.onap.cli.fw.http.utils;
21 import static org.junit.Assert.assertEquals;
22 import static org.junit.Assert.assertTrue;
23 import static org.junit.Assert.fail;
25 import java.util.ArrayList;
26 import java.util.HashMap;
29 import org.junit.FixMethodOrder;
30 import org.junit.Test;
31 import org.junit.runners.MethodSorters;
32 import org.onap.cli.fw.cmd.OnapCommand;
33 import org.onap.cli.fw.error.OnapCommandException;
34 import org.onap.cli.fw.error.OnapCommandInvalidParameterType;
35 import org.onap.cli.fw.error.OnapCommandInvalidPrintDirection;
36 import org.onap.cli.fw.error.OnapCommandInvalidResultAttributeScope;
37 import org.onap.cli.fw.error.OnapCommandInvalidSchema;
38 import org.onap.cli.fw.error.OnapCommandInvalidSchemaVersion;
39 import org.onap.cli.fw.error.OnapCommandParameterNameConflict;
40 import org.onap.cli.fw.error.OnapCommandParameterOptionConflict;
41 import org.onap.cli.fw.error.OnapCommandSchemaNotFound;
42 import org.onap.cli.fw.http.cmd.OnapHttpCommand;
43 import org.onap.cli.fw.http.connect.HttpResult;
44 import org.onap.cli.fw.http.error.OnapCommandHttpHeaderNotFound;
45 import org.onap.cli.fw.http.error.OnapCommandHttpInvalidRequestBody;
46 import org.onap.cli.fw.http.error.OnapCommandHttpInvalidResponseBody;
47 import org.onap.cli.fw.http.schema.OnapCommandSchemaHttpLoader;
48 import org.onap.cli.fw.input.OnapCommandParameter;
49 import org.onap.cli.fw.schema.OnapCommandSchema;
50 import org.onap.cli.fw.schema.OnapCommandSchemaLoader;
51 import org.onap.cli.fw.utils.OnapCommandUtils;
52 import java.util.List;
53 import org.onap.cli.fw.error.OnapCommandResultMapProcessingFailed;
54 import org.onap.cli.fw.error.OnapCommandResultEmpty;
55 import static org.junit.Assert.assertFalse;
56 import org.onap.cli.fw.http.connect.HttpInput;
57 import org.onap.cli.fw.http.connect.HttpInput.Part;
58 import static org.junit.Assert.assertNotNull;
60 @FixMethodOrder(MethodSorters.NAME_ASCENDING)
61 public class OnapCommandUtilsTest {
63 @Test(expected = OnapCommandInvalidSchema.class)
64 public void oclipCommandUtilsInputStreamNullTest() throws OnapCommandException {
65 OnapCommandSchemaLoader.validateSchemaVersion("sample-test1-schema-http1.yaml", "1.0");
69 public void oclipCommandUtilsInputStreamNotNullTest() throws OnapCommandException {
70 Map<String, ?> map = OnapCommandSchemaLoader.validateSchemaVersion("sample-test1-schema-http.yaml", "1.0");
75 public void loadHttpBasedSchemaTest() throws OnapCommandException {
76 OnapHttpCommand cmd = new OnapHttpCommandSample();
77 cmd.setName("sample-create-http");
79 OnapCommandSchemaHttpLoader.loadHttpSchema(cmd, "sample-test-schema-http.yaml", true, true);
80 assertEquals(2, cmd.getSuccessStatusCodes().size());
81 } catch (OnapCommandParameterNameConflict | OnapCommandParameterOptionConflict
82 | OnapCommandInvalidParameterType | OnapCommandInvalidPrintDirection
83 | OnapCommandInvalidResultAttributeScope | OnapCommandSchemaNotFound | OnapCommandInvalidSchema
84 | OnapCommandInvalidSchemaVersion e) {
85 fail("Test should not have thrown this exception : " + e.getMessage());
91 public void loadOnapCommandSchemaAuthRequiredTest() throws OnapCommandException {
92 OnapCommand cmd = new OnapCommand() {
95 protected void run() throws OnapCommandException {
96 // TODO Auto-generated method stub
100 OnapCommandSchemaLoader.loadSchema(cmd, "sample-test-schema-auth-required.yaml", true, false);
101 assertEquals("sample-test", cmd.getName());
103 Map<String, OnapCommandParameter> map = OnapCommandUtils.getInputMap(cmd.getParameters());
104 assertEquals(9, map.size());
107 @Test(expected = OnapCommandHttpHeaderNotFound.class)
108 public void populateOutputsTest() throws OnapCommandException {
109 HttpResult output = new HttpResult();
111 "{\"serviceName\":\"test\",\"version\":\"v1\",\"url\":\"/api/test/v1\",\"protocol\":\"REST\","
112 + "\"visualRange\":\"1\",\"lb_policy\":\"hash\",\"nodes\":[{\"ip\":\"127.0.0.1\",\"port\":\"8012\","
113 + "\"ttl\":0,\"nodeId\":\"test_127.0.0.1_8012\",\"expiration\":\"2017-02-10T05:33:25Z\","
114 + "\"created_at\":\"2017-02-10T05:33:25Z\",\"updated_at\":\"2017-02-10T05:33:25Z\"}],"
115 + "\"status\":\"1\"}");
116 Map<String, String> mapHead = new HashMap<>();
117 mapHead.put("head1", "value1");
118 output.setRespHeaders(mapHead);
121 Map<String, String> params = new HashMap<>();
122 params.put("head", "$h{head1}");
123 params.put("body", "$b{$.serviceName}");
124 params.put("key", "value");
126 Map<String, List<String>> input1 = OnapCommandHttpUtils.populateOutputs(params, output);
127 assertEquals("{head=[value1], body=[test], key=[value]}", input1.toString());
129 params.put("body", "$b{{$.serviceName}");
131 input1 = OnapCommandHttpUtils.populateOutputs(params, output);
132 } catch (OnapCommandHttpInvalidResponseBody e) {
134 "0x3004::Http response body does not have json entry {$.serviceName, "
135 + "Missing property in path $['{$']",
138 output.setBody("{}");
139 input1 = OnapCommandHttpUtils.populateOutputs(params, output);
140 params.put("head", "$h{head2}");
141 output.setBody("{\"test\"}");
142 input1 = OnapCommandHttpUtils.populateOutputs(params, output);
146 public void replaceLineFromOutputResultsTest() throws OnapCommandHttpHeaderNotFound, OnapCommandHttpInvalidResponseBody, OnapCommandResultMapProcessingFailed, OnapCommandResultEmpty {
147 HttpResult output = new HttpResult();
149 Map<String, String> mapHead = new HashMap<>();
150 mapHead.put("head1", "value1");
151 output.setRespHeaders(mapHead);
153 List<String> actualResult = OnapCommandHttpUtils.replaceLineFromOutputResults("", output);
154 assertTrue(actualResult.get(0).isEmpty());
156 actualResult = OnapCommandHttpUtils.replaceLineFromOutputResults("$h{head1}$b{$.serviceName}", output);
157 assertTrue(actualResult.isEmpty());
158 // assertTrue(actualResult.size() > 0);
160 "{\"serviceName\":\"test\",\"version\":\"v1\",\"url\":\"/api/test/v1\",\"protocol\":\"REST\","
161 + "\"visualRange\":\"1\",\"lb_policy\":\"hash\",\"nodes\":[{\"ip\":\"127.0.0.1\",\"port\":\"8012\","
162 + "\"ttl\":0,\"nodeId\":\"test_127.0.0.1_8012\",\"expiration\":\"2017-02-10T05:33:25Z\","
163 + "\"created_at\":\"2017-02-10T05:33:25Z\",\"updated_at\":\"2017-02-10T05:33:25Z\"}],"
164 + "\"status\":\"1\"}");
165 actualResult = OnapCommandHttpUtils.replaceLineFromOutputResults("$h{head1}${$.serviceName}", output);
166 assertTrue(actualResult.size()>0);
167 actualResult = OnapCommandHttpUtils.replaceLineFromOutputResults("$h{head1}$b{$.serviceName}", output);
168 assertFalse(actualResult.get(0).isEmpty());
170 @OnapCommandSchema(schema = "sample-test-schema-http.yaml")
171 class OnapHttpCommandSample extends OnapHttpCommand {
174 protected void run() throws OnapCommandException {
179 public void testJsonEmptyCheck() throws OnapCommandHttpInvalidRequestBody {
180 String sample = "{\"request\":{\"method\":\"\",\"uri\":\"/onboarding-api/v1.0/"
181 + "vendor-license-models/cf2d907d998e44698ce3b4cded5f66a7/versions/2.0/license-agreements\","
182 + "\"headers\":{\"Authorization\":\"Basic Y3MwMDA4OmRlbW8xMjM0NTYh\",\"X-FromAppId\":\"onap-cli\","
183 + "\"Accept\":\"application/json\",\"USER_ID\":\"cs0008\","
184 + "\"X-TransactionId\":\"req-66a37478-d840-44f8-b436-56f4a3b6f640\","
185 + "\"Content-Type\":\"application/json\"},\"json\":null},"
186 + "\"response\":{\"status\":200,\"json\":{\"listCount\":2,"
187 + "\"results\":[{\"name\":\"sf\",\"description\":\"sdfgdf\","
188 + "\"licenseTerm\":{\"choice\":\"Fixed_Term\",\"other\":null},"
189 + "\"id\":\"1e2edfccaca847f896070d0fac26667a\","
190 + "\"featureGroupsIds\":[\"3a2fb75b52a54e9c8093e7c154210f9e\"]},"
191 + "{\"name\":\"kanag-cli-la\",\"description\":\"kanag cli la\","
192 + "\"licenseTerm\":{\"choice\":\"Fixed_Term\",\"other\":\"\"},"
193 + "\"id\":\"77e151d0503b45ecb7e40f5f5f1a887e\","
194 + "\"featureGroupsIds\":[\"3a2fb75b52a54e9c8093e7c154210f9e\"]}]}}}";
195 String result = "{\"request\":{\"uri\":"
196 + "\"/onboarding-api/v1.0/vendor-license-models/cf2d907d998e44698ce3b4cded5f66a7/"
197 + "versions/2.0/license-agreements\",\"headers\":"
198 + "{\"Authorization\":\"Basic Y3MwMDA4OmRlbW8xMjM0NTYh\","
199 + "\"X-FromAppId\":\"onap-cli\",\"Accept\":\"application/json\","
200 + "\"USER_ID\":\"cs0008\",\"X-TransactionId\":\"req-66a37478-d840-44f8-b436-56f4a3b6f640\","
201 + "\"Content-Type\":\"application/json\"}},\"response\":{\"status\":200,"
202 + "\"json\":{\"listCount\":2,\"results\":[{\"name\":\"sf\","
203 + "\"description\":\"sdfgdf\",\"licenseTerm\":{\"choice\":\"Fixed_Term\"},"
204 + "\"id\":\"1e2edfccaca847f896070d0fac26667a\",\"featureGroupsIds\":"
205 + "[\"3a2fb75b52a54e9c8093e7c154210f9e\"]},{\"name\":\"kanag-cli-la\","
206 + "\"description\":\"kanag cli la\",\"licenseTerm\":{\"choice\":\"Fixed_Term\"},"
207 + "\"id\":\"77e151d0503b45ecb7e40f5f5f1a887e\",\"featureGroupsIds\":"
208 + "[\"3a2fb75b52a54e9c8093e7c154210f9e\"]}]}}}";
209 assertEquals(result, OnapCommandHttpUtils.normalizeJson(sample));
213 public void testPopulateParameters() throws OnapCommandException {
214 List <Part> multiparts = new ArrayList <HttpInput.Part> ();
215 Part part = new Part();
216 part.setContent("content");
217 multiparts.add(part);
218 Map <String, OnapCommandParameter> params = new HashMap <String, OnapCommandParameter> ();
219 OnapCommandParameter onapCommandParameter = new OnapCommandParameter();
220 onapCommandParameter.setName("CMD");
221 onapCommandParameter.setRawDefaultValue("value");
222 params.put("key", onapCommandParameter);
223 HttpInput httpInput = new HttpInput();
224 httpInput.setMultiparts(multiparts);
225 HttpInput input = OnapCommandHttpUtils.populateParameters(params, httpInput);
226 assertNotNull(input);