32dd24324f0dd322338b8d87af084c0bffd3accd
[appc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
8  * =============================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.appc.adapter.ansible.impl;
25
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.fail;
28
29 import java.util.HashMap;
30 import java.util.Map;
31
32 import org.junit.After;
33 import org.junit.Before;
34 import org.junit.Test;
35
36 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
37 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
38
39
40 public class TestAnsibleAdapterImpl {
41
42     private final String PENDING = "100";
43     private final String SUCCESS = "400";
44     private String message = "{\"Results\":{\"192.168.1.10\":{\"Id\":\"101\",\"StatusCode\":200,\"StatusMessage\":\"SUCCESS\"}},\"StatusCode\":200,\"StatusMessage\":\"FINISHED\"}";
45
46     private AnsibleAdapterImpl adapter;
47     private String TestId;
48     private boolean testMode = true;
49     private Map<String, String> params;
50     private SvcLogicContext svcContext;
51
52
53     @Before
54     public void setup() throws IllegalArgumentException {
55         testMode = true;
56         svcContext = new SvcLogicContext();
57         adapter = new AnsibleAdapterImpl(testMode);
58
59         params = new HashMap<>();
60         params.put("AgentUrl", "https://192.168.1.1");
61         params.put("User", "test");
62         params.put("Password", "test");
63     }
64
65     @After
66     public void tearDown() {
67         testMode = false;
68         adapter = null;
69         params = null;
70         svcContext = null;
71     }
72
73     @Test
74     public void reqExec_shouldSetPending() throws IllegalStateException, IllegalArgumentException {
75
76         params.put("PlaybookName", "test_playbook.yaml");
77
78         try {
79             adapter.reqExec(params, svcContext);
80             String status = svcContext.getAttribute("org.onap.appc.adapter.ansible.result.code");
81             TestId = svcContext.getAttribute("org.onap.appc.adapter.ansible.result.Id");
82             System.out.println("Comparing " + PENDING + " and " + status);
83             assertEquals(PENDING, status);
84         } catch (SvcLogicException e) {
85             String status = svcContext.getAttribute("org.onap.appc.adapter.ansible.result.code");
86             fail(e.getMessage() + " Code = " + status);
87         } catch (Exception e) {
88             fail(e.getMessage() + " Unknown exception encountered ");
89         }
90     }
91
92     @Test
93     public void reqExecResult_shouldSetSuccess() throws IllegalStateException, IllegalArgumentException {
94
95         params.put("Id", "100");
96
97         for (String ukey : params.keySet()) {
98             System.out.println(String.format("Ansible Parameter %s = %s", ukey, params.get(ukey)));
99         }
100
101         try {
102             adapter.reqExecResult(params, svcContext);
103             String status = svcContext.getAttribute("org.onap.appc.adapter.ansible.result.code");
104             assertEquals(SUCCESS, status);
105         } catch (SvcLogicException e) {
106             String status = svcContext.getAttribute("org.onap.appc.adapter.ansible.result.code");
107             fail(e.getMessage() + " Code = " + status);
108         } catch (Exception e) {
109             fail(e.getMessage() + " Unknown exception encountered ");
110         }
111     }
112
113     @Test
114     public void reqExecLog_shouldSetMessage() throws IllegalStateException, IllegalArgumentException {
115
116         params.put("Id", "101");
117
118         try {
119             adapter.reqExecLog(params, svcContext);
120             String status = svcContext.getAttribute("org.onap.appc.adapter.ansible.log");
121             assertEquals(message, status);
122         } catch (SvcLogicException e) {
123             String status = svcContext.getAttribute("org.onap.appc.adapter.ansible.log");
124             fail(e.getMessage() + " Code = " + status);
125         } catch (Exception e) {
126             fail(e.getMessage() + " Unknown exception encountered ");
127         }
128     }
129 }