2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 IBM
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 * ============LICENSE_END=========================================================
21 package org.onap.appc.adapter.ansible.model;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertTrue;
26 import java.util.HashMap;
29 import org.json.JSONObject;
30 import org.junit.Before;
31 import org.junit.Test;
33 public class TestAnsibleMessageParser {
34 private AnsibleMessageParser msgParser;
38 msgParser = new AnsibleMessageParser();
42 public void testReqMessage() throws Exception {
43 // String result = "{"\AgentUrl : TestAgentUrl}";
44 Map<String, String> params = new HashMap<String, String>();
45 params.put("AgentUrl", "TestAgentUrl");
46 params.put("PlaybookName", "TestPlaybookName");
47 params.put("User", "TestUser");
48 params.put("Password", "TestPassword");
50 JSONObject jObject = msgParser.reqMessage(params);
51 assertEquals("TestAgentUrl", jObject.get("AgentUrl"));
56 public void testReqUriResult() throws Exception {
57 Map<String, String> params = new HashMap<String, String>();
58 params.put("AgentUrl", "TestAgentUrl");
59 params.put("Id", "TestId");
60 params.put("User", "TestUser");
61 params.put("Password", "TestPassword");
63 String result = msgParser.reqUriResult(params);
64 assertTrue(result.contains("TestId"));
69 public void testReqUriLog() throws Exception {
70 Map<String, String> params = new HashMap<String, String>();
71 params.put("AgentUrl", "TestAgent-Url");
72 params.put("Id", "TestId");
73 params.put("User", "TestUser");
74 params.put("Password", "TestPassword");
76 String result = msgParser.reqUriLog(params);
77 assertTrue(result.contains("TestAgent-Url"));
82 public void TestParsePostResponse() throws Exception {
83 AnsibleResult ansibleResult;
84 String input = "{\"StatusCode\":\"100\",\"StatusMessage\":\"TestMessage\"}";
85 ansibleResult = msgParser.parsePostResponse(input);
86 assertEquals("TestMessage", ansibleResult.getStatusMessage());
90 @Test(expected = Exception.class)
91 public void TestParsePostResponseException() throws Exception {
92 AnsibleResult ansibleResult;
93 String input = "{\"StatusCode\":\"600\",\"StatusMessage\":\"TestMessage\"}";
94 ansibleResult = msgParser.parsePostResponse(input);
98 public void TestParsePostResponseException2() throws Exception {
99 AnsibleResult ansibleResult;
100 String input = "{\"StatusCode\":\"600\"}";
101 String result = "Error parsing response";
102 ansibleResult = msgParser.parsePostResponse(input);
103 assertEquals(true, ansibleResult.getStatusMessage().contains(result));
106 @Test(expected = Exception.class)
107 public void TestParseGetResponseException() throws Exception {
108 AnsibleResult ansibleResult;
109 String input = "{\"StatusCode\":\"100\",\"StatusMessage\":\"TestMessage\"}";
110 String result = "Invalid FinalResponse code";
111 ansibleResult = msgParser.parseGetResponse(input);
115 public void TestParseGetResponseExec() throws Exception {
116 AnsibleResult ansibleResult;
117 String input = "{\"StatusCode\":\"200\",\"StatusMessage\":\"TestMessage\"}";
118 String result = "Results not found in GET for response";
119 ansibleResult = msgParser.parseGetResponse(input);
120 assertEquals(true, ansibleResult.getStatusMessage().contains(result));
124 public void TestParseGetResponse() throws Exception {
125 AnsibleResult ansibleResult;
126 String input = "{\"StatusCode\":\"200\",\"StatusMessage\":\"TestMessage\",\"Results\":{\"host\":{\"StatusCode\":\"200\",\"StatusMessage\":\"SUCCESS\"}},\"Output\":{\"results-output\":{\"OutputResult\":\"TestOutPutResult\"}}}";
127 ansibleResult = msgParser.parseGetResponse(input);
128 String result = "TestOutPutResult";
129 assertEquals(true, ansibleResult.getOutput().contains(result));
133 public void TestParseGetResponseEx() throws Exception {
134 AnsibleResult ansibleResult;
135 String input = "{\"StatusCode\":\"200\",\"StatusMessage\":\"TestMessage\",\"Results\":{\"host\":\"TestHost\"}}";
136 ansibleResult = msgParser.parseGetResponse(input);
137 String result = "Error processing response message";
138 assertEquals(true, ansibleResult.getStatusMessage().contains(result));
142 public void TestParseGetResponseJsonEx() throws Exception {
143 AnsibleResult ansibleResult;
144 String input = "{\"StatusCode\":\"200\",\"StatusMessage\":\"TestMessage\",\"Results\":\"host\":\"TestHost\"}";
145 ansibleResult = msgParser.parseGetResponse(input);
146 String result = "Error parsing response";
147 assertEquals(true, ansibleResult.getStatusMessage().contains(result));
151 public void TestParseGetResponseResultEx() throws Exception {
152 AnsibleResult ansibleResult;
153 String input = "{\"StatusCode\":\"200\",\"StatusMessage\":\"TestMessage\",\"Results\":{\"host\":{\"StatusCode\":\"100\",\"StatusMessage\":\"Failure\"}},\"Output\":{\"results-output\":{\"OutputResult\":\"TestOutPutResult\"}}}";
154 ansibleResult = msgParser.parseGetResponse(input);
155 String result = "TestOutPutResult";
156 assertEquals(true, ansibleResult.getOutput().contains(result));
160 public void testParseOptionalParam() throws Exception {
161 Map<String, String> params = new HashMap<String, String>();
162 params.put("AgentUrl", "TestAgentUrl");
163 params.put("PlaybookName", "TestPlaybookName");
164 params.put("User", "TestUser");
165 params.put("Password", "TestPassword");
166 params.put("Timeout", "3");
167 params.put("Version", "1");
168 JSONObject jObject = msgParser.reqMessage(params);
169 assertEquals("1", jObject.get("Version"));
173 public void testParseOptionalParamForEnvParameters() throws Exception {
174 Map<String, String> params = new HashMap<String, String>();
175 params.put("AgentUrl", "TestAgentUrl");
176 params.put("PlaybookName", "TestPlaybookName");
177 params.put("User", "TestUser");
178 params.put("Password", "TestPassword");
179 params.put("EnvParameters", "{name:value}");
180 JSONObject result= msgParser.reqMessage(params);
181 assertEquals("TestAgentUrl",result.get("AgentUrl"));
182 assertEquals("TestPlaybookName",result.get("PlaybookName"));
183 assertEquals("TestUser",result.get("User"));
184 assertEquals("TestPassword",result.get("Password"));
188 public void testReqUriResultWithIPs() throws Exception {
189 Map<String, String> params = new HashMap<>();
190 params.put("AgentUrl", "http://xx:yy:zz");
191 params.put("Id", "TestId");
192 params.put("User", "TestUser");
193 params.put("Password", "TestPassword");
194 String serverIp="10.0.2.3";
195 String actual=msgParser.reqUriResultWithIP(params, serverIp);
196 String expected= "http://10.0.2.3:yy:zz?Id=TestId&Type=GetResult";
197 assertEquals(expected, actual);