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