bcf18e3b20420a82fb0046bb1be86b2952db56b5
[ccsdk/sli.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : SLI
4  * ================================================================================
5  * Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.ccsdk.adapter.ansible.model;
24
25 import java.util.HashMap;
26 import java.util.Map;
27 import org.json.JSONObject;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.onap.ccsdk.sli.adaptors.ansible.model.AnsibleMessageParser;
31 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
32
33 import static org.junit.Assert.assertEquals;
34 import static org.junit.Assert.assertTrue;
35
36 public class TestAnsibleMessageParser {
37     private AnsibleMessageParser msgParser;
38
39     @Before
40     public void setup() {
41         msgParser = new AnsibleMessageParser();
42     }
43
44     @Test
45     public void testReqMessage() throws Exception {
46         // String result = "{"\AgentUrl : TestAgentUrl}";
47         Map<String, String> params = new HashMap<>();
48         params.put("AgentUrl", "TestAgentUrl");
49         params.put("PlaybookName", "TestPlaybookName");
50         params.put("User", "TestUser");
51         params.put("Password", "TestPass");
52
53         assertEquals("TestAgentUrl", msgParser.reqMessage(params).get("AgentUrl"));
54     }
55
56     @Test
57     public void testReqUriResult() throws Exception {
58         Map<String, String> params = new HashMap<>();
59         params.put("AgentUrl", "TestAgentUrl");
60         params.put("Id", "TestId");
61         params.put("User", "TestUser");
62         params.put("Password", "TestPass");
63
64         assertTrue(msgParser.reqUriResult(params).contains("TestId"));
65     }
66
67     @Test
68     public void testReqUriLog() throws Exception {
69         Map<String, String> params = new HashMap<>();
70         params.put("AgentUrl", "TestAgent-Url");
71         params.put("Id", "TestId");
72         params.put("User", "TestUser");
73         params.put("Password", "TestPass");
74
75         assertTrue(msgParser.reqUriLog(params).contains("TestAgent-Url"));
76     }
77
78     @Test
79     public void TestParsePostResponse() throws Exception {
80         String input = "{\"StatusCode\":\"100\",\"StatusMessage\":\"TestMessage\"}";
81         assertEquals("TestMessage", msgParser.parsePostResponse(input).getStatusMessage());
82
83     }
84
85     @Test(expected = SvcLogicException.class)
86     public void TestParsePostResponseException() throws Exception {
87         String input = "{\"StatusCode\":\"600\",\"StatusMessage\":\"TestMessage\"}";
88         assertTrue(msgParser.parsePostResponse(input).getStatusMessage().contains("Error parsing response"));
89     }
90
91     @Test(expected = SvcLogicException.class)
92     public void TestParsePostResponseException2() throws Exception {
93         String input = "{\"StatusCode\":\"600\"}";
94         assertTrue(msgParser.parsePostResponse(input).getStatusMessage().contains("Error parsing response"));
95     }
96
97     @Test(expected = SvcLogicException.class)
98     public void TestParseGetResponseException() throws Exception {
99         String input = "{\"StatusCode\":\"100\",\"StatusMessage\":\"TestMessage\"}";
100         assertTrue(msgParser.parseGetResponse(input).getStatusMessage().contains("Invalid FinalResponse code"));
101     }
102
103     @Test
104     public void TestParseGetResponseExec() throws Exception {
105         String input = "{\"StatusCode\":\"200\",\"StatusMessage\":\"TestMessage\"}";
106         assertTrue(msgParser.parseGetResponse(input).getStatusMessage().contains("Results not found in GET for response"));
107     }
108
109     @Test
110     public void TestParseGetResponse() throws Exception {
111         String input = "{"
112                        + "  \"StatusCode\": \"200\","
113                        + "  \"StatusMessage\": \"TestMessage\","
114                        + "  \"Results\": {"
115                        + "    \"host\": {"
116                        + "      \"StatusCode\": \"200\","
117                        + "      \"StatusMessage\": \"SUCCESS\""
118                        + "    }"
119                        + "  },"
120                        + "  \"Output\": {"
121                        + "    \"results-output\": {"
122                        + "      \"OutputResult\": \"TestOutPutResult\""
123                        + "    }"
124                        + "  }"
125                        + "}";
126         assertTrue(msgParser.parseGetResponse(input).getOutput().contains("TestOutPutResult"));
127     }
128
129     @Test
130     public void TestParseGetResponseEx() throws Exception {
131         String input = "{\"StatusCode\":\"200\",\"StatusMessage\":\"TestMessage\",\"Results\":{\"host\":\"TestHost\"}}";
132         assertTrue(msgParser.parseGetResponse(input).getStatusMessage().contains("Error processing response message"));
133     }
134
135     @Test
136     public void TestParseGetResponseJsonEx() throws Exception {
137         String input = "{\"StatusCode\":\"200\",\"StatusMessage\":\"TestMessage\",\"Results\":\"host\":\"TestHost\"}";
138         assertTrue(msgParser.parseGetResponse(input).getStatusMessage().contains("Error parsing response"));
139     }
140
141     @Test
142     public void TestParseGetResponseResultEx() throws Exception {
143         String input = "{"
144                        + "  \"StatusCode\": \"200\","
145                        + "  \"StatusMessage\": \"TestMessage\","
146                        + "  \"Results\": {"
147                        + "    \"host\": {"
148                        + "      \"StatusCode\": \"100\","
149                        + "      \"StatusMessage\": \"Failure\""
150                        + "    }"
151                        + "  },"
152                        + "  \"Output\": {"
153                        + "    \"results-output\": {"
154                        + "      \"OutputResult\": \"TestOutPutResult\""
155                        + "    }"
156                        + "  }"
157                        + "}";
158         assertTrue(msgParser.parseGetResponse(input).getOutput().contains("TestOutPutResult"));
159     }
160
161     @Test
162     public void testParseOptionalParam() throws Exception {
163         Map<String, String> params = new HashMap<>();
164         params.put("AgentUrl", "TestAgentUrl");
165         params.put("PlaybookName", "TestPlaybookName");
166         params.put("User", "TestUser");
167         params.put("Password", "TestPass");
168         params.put("Timeout", "3");
169         params.put("Version", "1");
170         params.put("InventoryNames", "VNFC");
171
172         JSONObject jObject = msgParser.reqMessage(params);
173         assertEquals("1", jObject.get("Version"));
174         assertEquals("VNFC", jObject.get("InventoryNames"));
175     }
176
177     @Test
178     public void testParseOptionalParamForEnvParameters() throws Exception {
179         Map<String, String> params = new HashMap<>();
180         params.put("AgentUrl", "TestAgentUrl");
181         params.put("PlaybookName", "TestPlaybookName");
182         params.put("User", "TestUser");
183         params.put("Password", "TestPass");
184         params.put("EnvParameters", "{name:value}");
185
186         JSONObject result = msgParser.reqMessage(params);
187         assertEquals("TestAgentUrl", result.get("AgentUrl"));
188         assertEquals("TestPlaybookName", result.get("PlaybookName"));
189         assertEquals("TestUser", result.get("User"));
190         assertEquals("TestPass", result.get("Password"));
191     }
192
193     @Test
194     public void TestParseGetConfigResponseResult() throws Exception {
195         String input = "{"
196                        + "  \"StatusCode\": \"200\","
197                        + "  \"StatusMessage\": \"TestMessage\","
198                        + "  \"Results\": {"
199                        + "    \"host\": {"
200                        + "      \"StatusCode\": \"200\","
201                        + "      \"StatusMessage\": \"SUCCESS\","
202                        + "      \"Output\": {"
203                        + "        \"info\": {"
204                        + "          \"configData\": {"
205                        + "            \"abc\": \"TestOutPutResult\","
206                        + "            \"rtr\": \"vfc\""
207                        + "          }"
208                        + "        }"
209                        + "      }"
210                        + "    }"
211                        + "  }"
212                        + "}";
213         assertTrue(msgParser.parseGetResponse(input).getConfigData().contains("abc"));
214     }
215
216     @Test
217     public void testParseOptionalParamTest2() throws Exception {
218
219         Map<String, String> params = new HashMap<>();
220         params.put("AgentUrl", "TestAgentUrl");
221         params.put("PlaybookName", "TestPlaybookName");
222         params.put("User", "TestUser");
223         params.put("Password", "TestPass");
224         //params.put("Timeout", "3");
225         params.put("Version", "1");
226         params.put("InventoryNames", "VNFC");
227         params.put("Timeout", "4");
228         params.put("EnvParameters", "{ \"userID\": \"$0002\", \"vnf-type\" : \"\", \"vnf\" : \"abc\" }");
229         params.put("NodeList", "${Nodelist}");
230
231         JSONObject jObject = msgParser.reqMessage(params);
232         assertEquals("1", jObject.get("Version"));
233         assertEquals("4", jObject.get("Timeout"));
234     }
235
236     @Test
237     public void testReqUriResultWithIPs() throws Exception {
238         Map<String, String> params = new HashMap<>();
239         params.put("AgentUrl", "http://xx:yy:zz");
240         params.put("Id", "TestId");
241         params.put("User", "TestUser");
242         params.put("Password", "TestPass");
243         String serverIp = "10.0.2.3";
244         String actual = msgParser.reqUriResultWithIP(params, serverIp);
245         String expected = "http://10.0.2.3:yy:zz?Id=TestId&Type=GetResult";
246         assertEquals(expected, actual);
247     }
248
249 }