Junit Test cases for Ansible adapter
[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.Test;
31 import org.powermock.reflect.Whitebox;
32
33 public class TestAnsibleMessageParser {
34
35     @Test
36     public void testReqMessage() throws Exception {
37         // String result = "{"\AgentUrl : TestAgentUrl}";
38         AnsibleMessageParser msgParser = new AnsibleMessageParser();
39         Map<String, String> params = new HashMap<String, String>();
40         params.put("AgentUrl", "TestAgentUrl");
41         params.put("PlaybookName", "TestPlaybookName");
42         params.put("User", "TestUser");
43         params.put("Password", "TestPassword");
44
45         JSONObject jObject = msgParser.reqMessage(params);
46         assertEquals("TestAgentUrl", jObject.get("AgentUrl"));
47
48     }
49
50     @Test
51     public void testReqUriResult() throws Exception {
52         AnsibleMessageParser msgParser = new AnsibleMessageParser();
53         Map<String, String> params = new HashMap<String, String>();
54         params.put("AgentUrl", "TestAgentUrl");
55         params.put("Id", "TestId");
56         params.put("User", "TestUser");
57         params.put("Password", "TestPassword");
58
59         String result = msgParser.reqUriResult(params);
60         assertTrue(result.contains("TestId"));
61
62     }
63
64     @Test
65     public void testReqUriLog() throws Exception {
66         AnsibleMessageParser msgParser = new AnsibleMessageParser();
67         Map<String, String> params = new HashMap<String, String>();
68         params.put("AgentUrl", "TestAgent-Url");
69         params.put("Id", "TestId");
70         params.put("User", "TestUser");
71         params.put("Password", "TestPassword");
72
73         String result = msgParser.reqUriLog(params);
74         assertTrue(result.contains("TestAgent-Url"));
75
76     }
77
78     @Test
79     public void TestParsePostResponse() throws Exception {
80         AnsibleResult ansibleResult;
81         AnsibleMessageParser msgParser = new AnsibleMessageParser();
82         String input = "{\"StatusCode\":\"100\",\"StatusMessage\":\"TestMessage\"}";
83         ansibleResult = msgParser.parsePostResponse(input);
84         assertEquals("TestMessage", ansibleResult.getStatusMessage());
85
86     }
87
88     @Test(expected = Exception.class)
89     public void TestParsePostResponseException() throws Exception {
90         AnsibleResult ansibleResult;
91         AnsibleMessageParser msgParser = new AnsibleMessageParser();
92         String input = "{\"StatusCode\":\"600\",\"StatusMessage\":\"TestMessage\"}";
93         ansibleResult = msgParser.parsePostResponse(input);
94     }
95
96     @Test
97     public void TestParsePostResponseException2() throws Exception {
98         AnsibleResult ansibleResult;
99         AnsibleMessageParser msgParser = new AnsibleMessageParser();
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         AnsibleMessageParser msgParser = new AnsibleMessageParser();
110         String input = "{\"StatusCode\":\"100\",\"StatusMessage\":\"TestMessage\"}";
111         String result = "Invalid FinalResponse code";
112         ansibleResult = msgParser.parseGetResponse(input);
113     }
114
115     @Test
116     public void TestParseGetResponseExec() throws Exception {
117         AnsibleResult ansibleResult;
118         AnsibleMessageParser msgParser = new AnsibleMessageParser();
119         String input = "{\"StatusCode\":\"200\",\"StatusMessage\":\"TestMessage\"}";
120         String result = "Results not found in GET for response";
121         ansibleResult = msgParser.parseGetResponse(input);
122         assertEquals(true, ansibleResult.getStatusMessage().contains(result));
123     }
124
125     @Test
126     public void TestParseGetResponse() throws Exception {
127         AnsibleResult ansibleResult;
128         AnsibleMessageParser msgParser = new AnsibleMessageParser();
129         String input = "{\"StatusCode\":\"200\",\"StatusMessage\":\"TestMessage\",\"Results\":{\"host\":{\"StatusCode\":\"200\",\"StatusMessage\":\"SUCCESS\"}},\"Output\":{\"results-output\":{\"OutputResult\":\"TestOutPutResult\"}}}";
130         ansibleResult = msgParser.parseGetResponse(input);
131         String result = "TestOutPutResult";
132         assertEquals(true, ansibleResult.getOutput().contains(result));
133     }
134
135     @Test
136     public void TestParseGetResponseEx() throws Exception {
137         AnsibleResult ansibleResult;
138         AnsibleMessageParser msgParser = new AnsibleMessageParser();
139         String input = "{\"StatusCode\":\"200\",\"StatusMessage\":\"TestMessage\",\"Results\":{\"host\":\"TestHost\"}}";
140         ansibleResult = msgParser.parseGetResponse(input);
141         String result = "Error processing response message";
142         assertEquals(true, ansibleResult.getStatusMessage().contains(result));
143     }
144
145     @Test
146     public void TestParseGetResponseJsonEx() throws Exception {
147         AnsibleResult ansibleResult;
148         AnsibleMessageParser msgParser = new AnsibleMessageParser();
149         String input = "{\"StatusCode\":\"200\",\"StatusMessage\":\"TestMessage\",\"Results\":\"host\":\"TestHost\"}";
150         ansibleResult = msgParser.parseGetResponse(input);
151         String result = "Error parsing response";
152         assertEquals(true, ansibleResult.getStatusMessage().contains(result));
153     }
154
155     @Test
156     public void TestParseGetResponseResultEx() throws Exception {
157         AnsibleResult ansibleResult;
158         AnsibleMessageParser msgParser = new AnsibleMessageParser();
159         String input = "{\"StatusCode\":\"200\",\"StatusMessage\":\"TestMessage\",\"Results\":{\"host\":{\"StatusCode\":\"100\",\"StatusMessage\":\"Failure\"}},\"Output\":{\"results-output\":{\"OutputResult\":\"TestOutPutResult\"}}}";
160         ansibleResult = msgParser.parseGetResponse(input);
161         String result = "TestOutPutResult";
162         assertEquals(true, ansibleResult.getOutput().contains(result));
163     }
164
165     @Test
166     public void testParseOptionalParam() throws Exception {
167         AnsibleMessageParser msgParser = new AnsibleMessageParser();
168         Map<String, String> params = new HashMap<String, String>();
169         params.put("AgentUrl", "TestAgentUrl");
170         params.put("PlaybookName", "TestPlaybookName");
171         params.put("User", "TestUser");
172         params.put("Password", "TestPassword");
173         params.put("Timeout", "3");
174         params.put("Version", "1");
175         JSONObject jObject = msgParser.reqMessage(params);
176         assertEquals("1", jObject.get("Version"));
177     }
178 }