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;
53 @FixMethodOrder(MethodSorters.NAME_ASCENDING)
54 public class OnapCommandUtilsTest {
56 @Test(expected = OnapCommandInvalidSchema.class)
57 public void oclipCommandUtilsInputStreamNullTest() throws OnapCommandException {
58 OnapCommandSchemaLoader.validateSchemaVersion("sample-test1-schema-http1.yaml", "1.0");
62 public void oclipCommandUtilsInputStreamNotNullTest() throws OnapCommandException {
63 Map<String, ?> map = OnapCommandSchemaLoader.validateSchemaVersion("sample-test1-schema-http.yaml", "1.0");
64 assertTrue(map != null);
68 public void loadHttpBasedSchemaTest() throws OnapCommandException {
69 OnapHttpCommand cmd = new OnapHttpCommandSample();
70 cmd.setName("sample-create-http");
72 OnapCommandSchemaHttpLoader.loadHttpSchema(cmd, "sample-test-schema-http.yaml", true, true);
73 assertTrue(cmd.getSuccessStatusCodes().size() == 2);
74 } catch (OnapCommandParameterNameConflict | OnapCommandParameterOptionConflict
75 | OnapCommandInvalidParameterType | OnapCommandInvalidPrintDirection
76 | OnapCommandInvalidResultAttributeScope | OnapCommandSchemaNotFound | OnapCommandInvalidSchema
77 | OnapCommandInvalidSchemaVersion e) {
78 fail("Test should not have thrown this exception : " + e.getMessage());
84 public void loadOnapCommandSchemaAuthRequiredTest() throws OnapCommandException {
85 OnapCommand cmd = new OnapCommand() {
88 protected void run() throws OnapCommandException {
89 // TODO Auto-generated method stub
93 OnapCommandSchemaLoader.loadSchema(cmd, "sample-test-schema-auth-required.yaml", true, false);
94 assertTrue("sample-test".equals(cmd.getName()));
96 Map<String, OnapCommandParameter> map = OnapCommandUtils.getInputMap(cmd.getParameters());
97 assertTrue(map.size() == 9);
100 @Test(expected = OnapCommandHttpHeaderNotFound.class)
101 public void populateOutputsTest() throws OnapCommandException {
102 HttpResult output = new HttpResult();
104 "{\"serviceName\":\"test\",\"version\":\"v1\",\"url\":\"/api/test/v1\",\"protocol\":\"REST\","
105 + "\"visualRange\":\"1\",\"lb_policy\":\"hash\",\"nodes\":[{\"ip\":\"127.0.0.1\",\"port\":\"8012\","
106 + "\"ttl\":0,\"nodeId\":\"test_127.0.0.1_8012\",\"expiration\":\"2017-02-10T05:33:25Z\","
107 + "\"created_at\":\"2017-02-10T05:33:25Z\",\"updated_at\":\"2017-02-10T05:33:25Z\"}],"
108 + "\"status\":\"1\"}");
109 Map<String, String> mapHead = new HashMap<>();
110 mapHead.put("head1", "value1");
111 output.setRespHeaders(mapHead);
114 Map<String, String> params = new HashMap<>();
115 params.put("head", "$h{head1}");
116 params.put("body", "$b{$.serviceName}");
117 params.put("key", "value");
119 Map<String, ArrayList<String>> input1 = OnapCommandHttpUtils.populateOutputs(params, output);
120 assertEquals("{head=[value1], body=[test], key=[value]}", input1.toString());
122 params.put("body", "$b{{$.serviceName}");
124 input1 = OnapCommandHttpUtils.populateOutputs(params, output);
125 } catch (OnapCommandHttpInvalidResponseBody e) {
127 "0x3004::Http response body does not have json entry {$.serviceName, "
128 + "Missing property in path $['{$']",
131 output.setBody("{}");
132 input1 = OnapCommandHttpUtils.populateOutputs(params, output);
133 params.put("head", "$h{head2}");
134 output.setBody("{\"test\"}");
135 input1 = OnapCommandHttpUtils.populateOutputs(params, output);
138 @OnapCommandSchema(schema = "sample-test-schema-http.yaml")
139 class OnapHttpCommandSample extends OnapHttpCommand {
142 protected void run() throws OnapCommandException {
147 public void testJsonEmptyCheck() throws OnapCommandHttpInvalidRequestBody {
148 String sample = "{\"request\":{\"method\":\"\",\"uri\":\"/onboarding-api/v1.0/"
149 + "vendor-license-models/cf2d907d998e44698ce3b4cded5f66a7/versions/2.0/license-agreements\","
150 + "\"headers\":{\"Authorization\":\"Basic Y3MwMDA4OmRlbW8xMjM0NTYh\",\"X-FromAppId\":\"onap-cli\","
151 + "\"Accept\":\"application/json\",\"USER_ID\":\"cs0008\","
152 + "\"X-TransactionId\":\"req-66a37478-d840-44f8-b436-56f4a3b6f640\","
153 + "\"Content-Type\":\"application/json\"},\"json\":null},"
154 + "\"response\":{\"status\":200,\"json\":{\"listCount\":2,"
155 + "\"results\":[{\"name\":\"sf\",\"description\":\"sdfgdf\","
156 + "\"licenseTerm\":{\"choice\":\"Fixed_Term\",\"other\":null},"
157 + "\"id\":\"1e2edfccaca847f896070d0fac26667a\","
158 + "\"featureGroupsIds\":[\"3a2fb75b52a54e9c8093e7c154210f9e\"]},"
159 + "{\"name\":\"kanag-cli-la\",\"description\":\"kanag cli la\","
160 + "\"licenseTerm\":{\"choice\":\"Fixed_Term\",\"other\":\"\"},"
161 + "\"id\":\"77e151d0503b45ecb7e40f5f5f1a887e\","
162 + "\"featureGroupsIds\":[\"3a2fb75b52a54e9c8093e7c154210f9e\"]}]}}}";
163 String result = "{\"request\":{\"uri\":"
164 + "\"/onboarding-api/v1.0/vendor-license-models/cf2d907d998e44698ce3b4cded5f66a7/"
165 + "versions/2.0/license-agreements\",\"headers\":"
166 + "{\"Authorization\":\"Basic Y3MwMDA4OmRlbW8xMjM0NTYh\","
167 + "\"X-FromAppId\":\"onap-cli\",\"Accept\":\"application/json\","
168 + "\"USER_ID\":\"cs0008\",\"X-TransactionId\":\"req-66a37478-d840-44f8-b436-56f4a3b6f640\","
169 + "\"Content-Type\":\"application/json\"}},\"response\":{\"status\":200,"
170 + "\"json\":{\"listCount\":2,\"results\":[{\"name\":\"sf\","
171 + "\"description\":\"sdfgdf\",\"licenseTerm\":{\"choice\":\"Fixed_Term\"},"
172 + "\"id\":\"1e2edfccaca847f896070d0fac26667a\",\"featureGroupsIds\":"
173 + "[\"3a2fb75b52a54e9c8093e7c154210f9e\"]},{\"name\":\"kanag-cli-la\","
174 + "\"description\":\"kanag cli la\",\"licenseTerm\":{\"choice\":\"Fixed_Term\"},"
175 + "\"id\":\"77e151d0503b45ecb7e40f5f5f1a887e\",\"featureGroupsIds\":"
176 + "[\"3a2fb75b52a54e9c8093e7c154210f9e\"]}]}}}";
177 assertEquals(result, OnapCommandHttpUtils.normalizeJson(sample));