ansible chnages for adding configdata and bugfixes
[appc.git] / appc-adapters / appc-ansible-adapter / appc-ansible-adapter-bundle / src / test / java / org / onap / appc / adapter / ansible / model / TestAnsibleMessageParser.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
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
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  * ============LICENSE_END=========================================================
20  */
21 package org.onap.appc.adapter.ansible.model;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertTrue;
25
26 import java.util.HashMap;
27 import java.util.Map;
28
29 import org.json.JSONObject;
30 import org.junit.Before;
31 import org.junit.Test;
32
33 public class TestAnsibleMessageParser {
34     private AnsibleMessageParser msgParser;
35
36     @Before
37     public void setup() {
38         msgParser = new AnsibleMessageParser();
39     }
40
41     @Test
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");
49
50         JSONObject jObject = msgParser.reqMessage(params);
51         assertEquals("TestAgentUrl", jObject.get("AgentUrl"));
52
53     }
54
55     @Test
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");
62
63         String result = msgParser.reqUriResult(params);
64         assertTrue(result.contains("TestId"));
65
66     }
67
68     @Test
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");
75
76         String result = msgParser.reqUriLog(params);
77         assertTrue(result.contains("TestAgent-Url"));
78
79     }
80
81     @Test
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());
87
88     }
89
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);
95     }
96
97     @Test
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));
104     }
105
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);
112     }
113
114     @Test
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));
121     }
122
123     @Test
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));
130     }
131
132     @Test
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));
139     }
140
141     @Test
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));
148     }
149
150     @Test
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));
157     }
158
159     @Test
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         params.put("InventoryNames", "VNFC");
169         JSONObject jObject = msgParser.reqMessage(params);
170         assertEquals("1", jObject.get("Version"));
171         assertEquals("VNFC",jObject.get("InventoryNames"));
172     }
173
174     @Test
175     public void testParseOptionalParamForEnvParameters() throws Exception {
176         Map<String, String> params = new HashMap<String, String>();
177         params.put("AgentUrl", "TestAgentUrl");
178         params.put("PlaybookName", "TestPlaybookName");
179         params.put("User", "TestUser");
180         params.put("Password", "TestPassword");
181         params.put("EnvParameters", "{name:value}");
182         JSONObject result= msgParser.reqMessage(params);
183         assertEquals("TestAgentUrl",result.get("AgentUrl"));
184         assertEquals("TestPlaybookName",result.get("PlaybookName"));
185         assertEquals("TestUser",result.get("User"));
186         assertEquals("TestPassword",result.get("Password"));
187     }
188
189     @Test
190     public void TestParseGetConfigResponseResult() throws Exception {
191         AnsibleResult ansibleResult;
192         String input = "{\"StatusCode\":\"200\",\"StatusMessage\":\"TestMessage\",\"Results\":{\"host\":{\"StatusCode\":\"200\",\"StatusMessage\":\"SUCCESS\",\"Output\":{\"info\":{\"configData\":{\"abc\":\"TestOutPutResult\",\"rtr\":\"vfc\"}}}}}}";
193         ansibleResult = msgParser.parseGetResponse(input);
194         String result = "abc";
195         assertEquals(true, ansibleResult.getconfigData().contains(result));
196     }
197
198     @Test
199     public void testParseOptionalParamTest2() throws Exception {
200         
201         Map<String, String> params = new HashMap<String, String>();
202         params.put("AgentUrl", "TestAgentUrl");
203         params.put("PlaybookName", "TestPlaybookName");
204         params.put("User", "TestUser");
205         params.put("Password", "TestPassword");
206         //params.put("Timeout", "3");
207         params.put("Version", "1");
208         params.put("InventoryNames", "VNFC");
209         params.put("Timeout", "4");
210         params.put("EnvParameters", "{ \"userID\": \"$0002\", \"vnf-type\" : \"\", \"vnf\" : \"abc\" }");
211         params.put("NodeList", "${Nodelist}");
212         JSONObject jObject = msgParser.reqMessage(params);
213         assertEquals("1", jObject.get("Version"));
214         assertEquals("4",jObject.get("Timeout"));
215     }
216     
217     @Test
218     public void testReqUriResultWithIPs() throws Exception {
219         Map<String, String> params = new HashMap<>();
220         params.put("AgentUrl", "http://xx:yy:zz");
221         params.put("Id", "TestId");
222         params.put("User", "TestUser");
223         params.put("Password", "TestPassword");
224         String serverIp="10.0.2.3";
225         String actual=msgParser.reqUriResultWithIP(params, serverIp);
226         String expected= "http://10.0.2.3:yy:zz?Id=TestId&Type=GetResult";
227         assertEquals(expected, actual);
228     }
229 }