2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
20 * ============LICENSE_END=========================================================
23 package org.onap.ccsdk.adapter.ansible.model;
25 import java.util.HashMap;
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;
33 import static org.junit.Assert.assertEquals;
34 import static org.junit.Assert.assertTrue;
36 public class TestAnsibleMessageParser {
37 private AnsibleMessageParser msgParser;
41 msgParser = new AnsibleMessageParser();
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");
53 assertEquals("TestAgentUrl", msgParser.reqMessage(params).get("AgentUrl"));
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");
64 assertTrue(msgParser.reqUriResult(params).contains("TestId"));
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");
75 assertTrue(msgParser.reqUriLog(params).contains("TestAgent-Url"));
79 public void TestParsePostResponse() throws Exception {
80 String input = "{\"StatusCode\":\"100\",\"StatusMessage\":\"TestMessage\"}";
81 assertEquals("TestMessage", msgParser.parsePostResponse(input).getStatusMessage());
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"));
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"));
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"));
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"));
110 public void TestParseGetResponse() throws Exception {
112 + " \"StatusCode\": \"200\","
113 + " \"StatusMessage\": \"TestMessage\","
116 + " \"StatusCode\": \"200\","
117 + " \"StatusMessage\": \"SUCCESS\""
121 + " \"results-output\": {"
122 + " \"OutputResult\": \"TestOutPutResult\""
126 assertTrue(msgParser.parseGetResponse(input).getOutput().contains("TestOutPutResult"));
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"));
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"));
142 public void TestParseGetResponseResultEx() throws Exception {
144 + " \"StatusCode\": \"200\","
145 + " \"StatusMessage\": \"TestMessage\","
148 + " \"StatusCode\": \"100\","
149 + " \"StatusMessage\": \"Failure\""
153 + " \"results-output\": {"
154 + " \"OutputResult\": \"TestOutPutResult\""
158 assertTrue(msgParser.parseGetResponse(input).getOutput().contains("TestOutPutResult"));
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");
172 JSONObject jObject = msgParser.reqMessage(params);
173 assertEquals("1", jObject.get("Version"));
174 assertEquals("VNFC", jObject.get("InventoryNames"));
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}");
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"));
194 public void TestParseGetConfigResponseResult() throws Exception {
196 + " \"StatusCode\": \"200\","
197 + " \"StatusMessage\": \"TestMessage\","
200 + " \"StatusCode\": \"200\","
201 + " \"StatusMessage\": \"SUCCESS\","
204 + " \"configData\": {"
205 + " \"abc\": \"TestOutPutResult\","
206 + " \"rtr\": \"vfc\""
213 assertTrue(msgParser.parseGetResponse(input).getConfigData().contains("abc"));
217 public void testParseOptionalParamTest2() throws Exception {
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}");
231 JSONObject jObject = msgParser.reqMessage(params);
232 assertEquals("1", jObject.get("Version"));
233 assertEquals("4", jObject.get("Timeout"));
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);