summaryrefslogtreecommitdiffstats
path: root/adaptors/ansible-adapter/ansible-adapter-bundle/src/test/java/org/onap/ccsdk/adapter/ansible/impl/TestAnsibleAdapterImpl.java
blob: be4bfd8f284485c9855f162352835cb2fb0952dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/*-
 * ============LICENSE_START=======================================================
 * ONAP : SLI
 * ================================================================================
 * Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
 * ============LICENSE_END=========================================================
 */

package org.onap.ccsdk.adapter.ansible.impl;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.runners.MockitoJUnitRunner;
import org.onap.ccsdk.sli.adaptors.ansible.impl.AnsibleAdapterImpl;
import org.onap.ccsdk.sli.adaptors.ansible.impl.AnsibleAdapterPropertiesProviderImpl;
import org.onap.ccsdk.sli.adaptors.ansible.model.AnsibleMessageParser;
import org.onap.ccsdk.sli.adaptors.ansible.model.AnsibleResult;
import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
import org.onap.ccsdk.sli.core.sli.SvcLogicException;
import org.powermock.reflect.Whitebox;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.when;
import static org.onap.ccsdk.sli.adaptors.ansible.AnsibleAdapterConstants.*;
@RunWith(MockitoJUnitRunner.class)
public class TestAnsibleAdapterImpl {

    private static final String PENDING = "100";
    private static final String AGENT_URL = "https://192.168.1.1";

    private static String KEYSTORE_PSWD;
    private static Properties properties;
    private boolean testMode = true;

    private AnsibleAdapterImpl adapter;
    private AnsibleResult result;
    private AnsibleAdapterImpl spyAdapter;
    private Map<String, String> params;
    private SvcLogicContext svcContext;
    private JSONObject jsonPayload;

    @Mock
    private AnsibleMessageParser messageProcessor;

    @BeforeClass
    public static void once() {
        properties = new AnsibleAdapterPropertiesProviderImpl().getProperties();
        KEYSTORE_PSWD = properties.getProperty("org.onap.appc.adapter.ansible.trustStore.trustPasswd");
    }

    /**
     * Use reflection to locate fields and methods so that they can be manipulated
     * during the test to change the internal state accordingly.
     */
    @Before
    public void setup() {
        testMode = true;
        svcContext = new SvcLogicContext();
        adapter = new AnsibleAdapterImpl(testMode);
        params = new HashMap<>();
        params.put("AgentUrl", AGENT_URL);
        jsonPayload = new JSONObject();
        jsonPayload.put("Id", "100");
        jsonPayload.put("User", "test");
        jsonPayload.put("Password", "test");
        jsonPayload.put("PlaybookName", "test_playbook.yaml");
        jsonPayload.put("Timeout", "60000");
        jsonPayload.put("AgentUrl", AGENT_URL);
        result = new AnsibleResult();
        result.setStatusMessage("Success");
        result.setResults("Success");
        result.setOutput("{}");
        Whitebox.setInternalState(adapter, "messageProcessor", messageProcessor);
        spyAdapter = Mockito.spy(adapter);
    }

    @After
    public void tearDown() {
        testMode = false;
        adapter = null;
        params = null;
        svcContext = null;
    }

    @Test
    public void reqExec_shouldSetPending() throws SvcLogicException {
        result.setStatusCode(Integer.parseInt(PENDING));
        when(messageProcessor.reqMessage(params)).thenReturn(jsonPayload);
        when(messageProcessor.parsePostResponse(anyString())).thenReturn(result);
        spyAdapter.reqExec(params, svcContext);
        assertEquals(PENDING, svcContext.getAttribute(RESULT_CODE_ATTRIBUTE_NAME));
    }

    @Test(expected = SvcLogicException.class)
    public void reqExecResult_shouldSetSuccess() throws SvcLogicException {
        params.put("Id", "100");
        result.setStatusMessage(SUCCESS);
        when(messageProcessor.reqUriResult(params)).thenReturn(AGENT_URL);
        when(messageProcessor.parseGetResponse(anyString())).thenReturn(result);
        spyAdapter.reqExecResult(params, svcContext);
        assertEquals(SUCCESS, svcContext.getAttribute(SUCCESS));
    }
    @Test(expected = SvcLogicException.class)
    public void reqExecResult_Failure() throws SvcLogicException {
        params.put("Id", "100");
        result.setStatusCode(100);
        result.setStatusMessage("Failed");
        JSONObject cData = new JSONObject();
        cData.put("GatewayInfo", "Radius");
        result.setConfigData(cData.toString());
        result.setOutput(cData.toString());
        when(messageProcessor.reqUriResult(params)).thenReturn(AGENT_URL);
        when(messageProcessor.parseGetResponse(anyString())).thenReturn(result);
        adapter.reqExecResult(params, svcContext);
    }

    @Test(expected = SvcLogicException.class)
    public void reqExecResult_SvcLogicException() throws SvcLogicException {
        when(messageProcessor.reqUriResult(params)).thenThrow(new SvcLogicException());
        adapter.reqExecResult(params, svcContext);
    }

    @Test(expected = SvcLogicException.class)
    public void reqExecResult_numberFormatException()
            throws IllegalStateException, IllegalArgumentException, SvcLogicException {
        when(messageProcessor.reqUriResult(params)).thenThrow(new NumberFormatException());
        adapter.reqExecResult(params, svcContext);
    }

    @Test
    public void reqExecLog_shouldSetMessage() throws SvcLogicException {
        params.put("Id", "101");
        when(messageProcessor.reqUriLog(params)).thenReturn(AGENT_URL);
        adapter.reqExecLog(params, svcContext);
        String message = getResponseMessage();
        assertEquals(message, svcContext.getAttribute(LOG_ATTRIBUTE_NAME));
    }

    private String getResponseMessage() {
        JSONObject response = new JSONObject();
        response.put(STATUS_CODE, 200);
        response.put(STATUS_MESSAGE, "FINISHED");
        JSONObject results = new JSONObject();

        JSONObject vmResults = new JSONObject();
        vmResults.put(STATUS_CODE, 200);
        vmResults.put(STATUS_MESSAGE, "SUCCESS");
        vmResults.put("Id", "");
        results.put("192.168.1.10", vmResults);

        response.put("Results", results);
        return response.toString();
    }

    @Test(expected = SvcLogicException.class)
    public void reqExecException()
            throws IllegalStateException, IllegalArgumentException, SvcLogicException {
        when(messageProcessor.reqUriLog(params)).thenThrow(new SvcLogicException("Appc Exception"));
        adapter.reqExecLog(params, svcContext);
    }

    @Test(expected = SvcLogicException.class)
    public void reqExec_SvcLogicException()
            throws IllegalStateException, IllegalArgumentException, SvcLogicException {
        when(messageProcessor.reqMessage(params)).thenThrow(new SvcLogicException());
        adapter.reqExec(params, svcContext);
    }

    @Test(expected = SvcLogicException.class)
    public void reqExec_JsonException()
            throws IllegalStateException, IllegalArgumentException, SvcLogicException {
        when(messageProcessor.reqMessage(params)).thenThrow(new JSONException("Json Exception"));
        adapter.reqExec(params, svcContext);
    }

    @Test(expected = SvcLogicException.class)
    public void reqExec_NumberFormatException()
            throws IllegalStateException, IllegalArgumentException, SvcLogicException {
        when(messageProcessor.reqMessage(params)).thenThrow(new NumberFormatException("Numbre Format Exception"));
        adapter.reqExec(params, svcContext);
    }

    @Test
    public void testInitializeWithDefault() {
        properties.setProperty("org.onap.appc.adapter.ansible.clientType", "");
        adapter = new AnsibleAdapterImpl();
        assertNotNull(adapter);
    }

    @Test
    public void testInitializeWithTrustAll() {
        properties.setProperty("org.onap.appc.adapter.ansible.clientType", "TRUST_ALL");
        adapter = new AnsibleAdapterImpl();
        assertNotNull(adapter);
    }

    @Test
    public void testInitializeWithTrustCert() {
        properties.setProperty("org.onap.appc.adapter.ansible.clientType", "TRUST_CERT");
        properties.setProperty("org.onap.appc.adapter.ansible.trustStore.trustPasswd", KEYSTORE_PSWD);
        adapter = new AnsibleAdapterImpl();
        assertNotNull(adapter);
    }

    @Test
    public void testInitializeWithException() {
        properties.setProperty("org.onap.appc.adapter.ansible.clientType", "TRUST_CERT");
        properties.setProperty("org.onap.appc.adapter.ansible.trustStore.trustPasswd", "appc");
        adapter = new AnsibleAdapterImpl();
        assertNotNull(adapter);
    }

}