Add context in http to ignore empty nodes
[cli.git] / profiles / http / src / test / java / org / onap / cli / fw / http / utils / OnapCommandUtilsTest.java
1 /*
2  * Copyright 2017 Huawei Technologies Co., Ltd.
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.onap.cli.fw.http.utils;
18
19
20
21 import static org.junit.Assert.assertEquals;
22 import static org.junit.Assert.assertTrue;
23 import static org.junit.Assert.fail;
24
25 import java.util.ArrayList;
26 import java.util.HashMap;
27 import java.util.Map;
28
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
53 @FixMethodOrder(MethodSorters.NAME_ASCENDING)
54 public class OnapCommandUtilsTest {
55
56     @Test(expected = OnapCommandInvalidSchema.class)
57     public void oclipCommandUtilsInputStreamNullTest() throws OnapCommandException {
58         OnapCommandSchemaLoader.validateSchemaVersion("sample-test1-schema-http1.yaml", "1.0");
59     }
60
61     @Test
62     public void oclipCommandUtilsInputStreamNotNullTest() throws OnapCommandException {
63         Map<String, ?> map = OnapCommandSchemaLoader.validateSchemaVersion("sample-test1-schema-http.yaml", "1.0");
64         assertTrue(map != null);
65     }
66
67     @Test
68     public void loadHttpBasedSchemaTest() throws OnapCommandException {
69         OnapHttpCommand cmd = new OnapHttpCommandSample();
70         cmd.setName("sample-create-http");
71         try {
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());
79         }
80     }
81
82
83     @Test
84     public void loadOnapCommandSchemaAuthRequiredTest() throws OnapCommandException {
85         OnapCommand cmd = new OnapCommand() {
86
87             @Override
88             protected void run() throws OnapCommandException {
89                 // TODO Auto-generated method stub
90
91             }
92         };
93         OnapCommandSchemaLoader.loadSchema(cmd, "sample-test-schema-auth-required.yaml", true, false);
94         assertTrue("sample-test".equals(cmd.getName()));
95
96         Map<String, OnapCommandParameter> map = OnapCommandUtils.getInputMap(cmd.getParameters());
97         assertTrue(map.size() == 8);
98     }
99
100     @Test(expected = OnapCommandHttpHeaderNotFound.class)
101     public void populateOutputsTest() throws OnapCommandException {
102         HttpResult output = new HttpResult();
103         output.setBody(
104                 "{\"serviceName\":\"test\",\"version\":\"v1\",\"url\":\"/api/test/v1\",\"protocol\":\"REST\",\"visualRange\":\"1\",\"lb_policy\":\"hash\",\"nodes\":[{\"ip\":\"127.0.0.1\",\"port\":\"8012\",\"ttl\":0,\"nodeId\":\"test_127.0.0.1_8012\",\"expiration\":\"2017-02-10T05:33:25Z\",\"created_at\":\"2017-02-10T05:33:25Z\",\"updated_at\":\"2017-02-10T05:33:25Z\"}],\"status\":\"1\"}");
105         Map<String, String> mapHead = new HashMap<>();
106         mapHead.put("head1", "value1");
107         output.setRespHeaders(mapHead);
108         output.setStatus(0);
109
110         Map<String, String> params = new HashMap<>();
111         params.put("head", "$h{head1}");
112         params.put("body", "$b{$.serviceName}");
113         params.put("key", "value");
114
115         Map<String, ArrayList<String>> input1 = OnapCommandHttpUtils.populateOutputs(params, output);
116         assertEquals("{head=[value1], body=[test], key=[value]}", input1.toString());
117
118         params.put("body", "$b{{$.serviceName}");
119         try {
120             input1 = OnapCommandHttpUtils.populateOutputs(params, output);
121         } catch (OnapCommandHttpInvalidResponseBody e) {
122             assertEquals(
123                     "0x3004::Http response body does not have json entry {$.serviceName, Missing property in path $['{$']",
124                     e.getMessage());
125         }
126         output.setBody("{}");
127         input1 = OnapCommandHttpUtils.populateOutputs(params, output);
128         params.put("head", "$h{head2}");
129         output.setBody("{\"test\"}");
130         input1 = OnapCommandHttpUtils.populateOutputs(params, output);
131     }
132
133     @OnapCommandSchema(schema = "sample-test-schema-http.yaml")
134     class OnapHttpCommandSample extends OnapHttpCommand {
135
136         @Override
137         protected void run() throws OnapCommandException {
138         }
139     }
140
141     @Test
142     public void testJsonEmptyCheck() throws OnapCommandHttpInvalidRequestBody {
143         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\"]}]}}}";
144         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\"]}]}}}";
145         assertEquals(result, OnapCommandHttpUtils.normalizeJson(sample));
146     }
147 }