a4e49643a52fa3d8b9b4285eb8b641cb61fff05d
[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  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017 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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.openecomp.appc.adapter.ansible.impl;
26
27 import static org.junit.Assert.assertEquals;
28 import static org.junit.Assert.assertNotNull;
29 import static org.junit.Assert.assertTrue;
30 import static org.junit.Assert.fail;
31
32 import java.io.File;
33 import java.io.IOException;
34 import java.lang.reflect.Field;
35 import java.util.HashMap;
36 import java.util.List;
37 import java.util.Map;
38 import java.util.Properties;
39 import java.util.Set;
40
41 import org.junit.Before;
42 import org.junit.BeforeClass;
43 import org.junit.Ignore;
44 import org.junit.Test;
45 import org.openecomp.appc.Constants;
46 import org.openecomp.appc.configuration.ConfigurationFactory;
47 import org.openecomp.appc.exceptions.APPCException;
48 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
49 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
50 import org.slf4j.MDC;
51
52 import org.openecomp.appc.adapter.ansible.AnsibleAdapter;
53 import org.openecomp.appc.adapter.ansible.impl.AnsibleAdapterImpl;
54
55 public class TestAnsibleAdapterImpl {
56
57
58     private AnsibleAdapterImpl adapter;
59     private String TestId;
60     private boolean testMode = true;
61     
62     @SuppressWarnings("nls")
63     @BeforeClass
64     public static void once() throws NoSuchFieldException, SecurityException, NoSuchMethodException {
65
66     }
67
68     @Before
69     public void setup() throws IllegalArgumentException, IllegalAccessException {
70         testMode = true;
71         adapter = new AnsibleAdapterImpl(testMode);
72     }
73     
74     @Test
75     public void testA() throws IOException, IllegalStateException, IllegalArgumentException,
76        APPCException {
77
78             Map<String, String> params = new HashMap<>();
79             params.put("AgentUrl", "https://192.168.1.1");
80             params.put("User", "test");
81             params.put("Password", "test");
82             params.put("PlaybookName", "test_playbook.yaml");
83             
84             SvcLogicContext svcContext = new SvcLogicContext();          
85             try{
86                 String Pending = "100";
87                 adapter.reqExec(params, svcContext);
88                 String status=svcContext.getAttribute("org.openecomp.appc.adapter.ansible.result.code");
89                 TestId=svcContext.getAttribute("org.openecomp.appc.adapter.ansible.result.Id");
90                 System.out.println("Comparing " + Pending + " and " + status);
91                 assertEquals(Pending,status);
92             }
93             catch(SvcLogicException e){
94                 String message  =svcContext.getAttribute("org.openecomp.appc.adapter.ansible.result.message");  
95                 String status=svcContext.getAttribute("org.openecomp.appc.adapter.ansible.result.code");
96                 fail(e.getMessage() + " Code = " + status);
97             }
98             catch(Exception e){
99                 fail(e.getMessage() + " Unknown exception encountered " );
100             }
101                 
102     }
103
104     @Test
105     public void testB() throws IOException, IllegalStateException, IllegalArgumentException,
106          APPCException {
107
108             Map<String, String> params = new HashMap<>();
109
110             params.put("AgentUrl", "https://192.168.1.1");
111             params.put("User", "test");
112             params.put("Password", "test");
113             params.put("Id", "100");
114
115             for (String ukey: params.keySet()){
116                   System.out.println(String.format("Ansible Parameter %s = %s", ukey, params.get(ukey)));
117             }
118
119             SvcLogicContext svcContext = new SvcLogicContext();          
120
121             try{
122                 adapter.reqExecResult(params, svcContext);
123                 String status=svcContext.getAttribute("org.openecomp.appc.adapter.ansible.result.code");
124                 assertEquals("400",status);
125             }
126             catch(SvcLogicException e){
127                 String message  = svcContext.getAttribute("org.openecomp.appc.adapter.ansible.result.message"); 
128                 String status=svcContext.getAttribute("org.openecomp.appc.adapter.ansible.result.code");
129                 fail(e.getMessage()  + " Code = " + status);
130             }
131             catch(Exception e){
132                 fail(e.getMessage() + " Unknown exception encountered " );
133             }
134             
135     }
136
137 }