aboutsummaryrefslogtreecommitdiffstats
path: root/sshapi-call-node/provider/src/test/java/jtest/org/onap/ccsdk/sli/plugins/restapicall/TestSshApiCallNode.java
blob: c0bcdb82a07b0c702d065769f2605419754a5ed1 (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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
package jtest.org.onap.ccsdk.sli.plugins.restapicall;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
import org.onap.ccsdk.sli.core.sli.SvcLogicException;
import org.onap.ccsdk.sli.plugins.sshapicall.SshApiCallNode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

import static org.junit.Assert.assertEquals;

public class TestSshApiCallNode {

    private static final Logger log = LoggerFactory.getLogger(TestSshApiCallNode.class);

    private SshApiCallNode adapter;
    private String TestId;
    private boolean testMode = true;
    private Map<String, String> params;
    private SvcLogicContext svcContext;


    @Before
    public void setup() throws IllegalArgumentException {
        testMode = true;
        svcContext = new SvcLogicContext();
        adapter = new SshApiCallNode();

        params = new HashMap<>();
        params.put("AgentUrl", "https://192.168.1.1");
        params.put("User", "test");
        params.put("Password", "test");
    }

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

    @Test(expected = SvcLogicException.class)
    public void testExecCommand_noUrlFailed() throws SvcLogicException,
            IllegalStateException, IllegalArgumentException {

        params.put("HostName", "test");
        params.put("Port", "10");
        params.put("User", "test");
        params.put("Password", "test");
        params.put("Test", "fail");
        adapter.execCommand(params, svcContext);
    }

    @Test(expected = SvcLogicException.class)
    public void testExecCommandPty_noUrlFailed() throws SvcLogicException,
            IllegalStateException, IllegalArgumentException {

        params.put("HostName", "test");
        params.put("Port", "10");
        params.put("User", "test");
        params.put("Password", "test");
        params.put("Test", "fail");
        adapter.execCommandWithPty(params, svcContext);
    }

    @Test(expected = SvcLogicException.class)
    public void testExecCommandResponse_noUrlFailed() throws SvcLogicException,
            IllegalStateException, IllegalArgumentException {

        params.put("HostName", "test");
        params.put("Port", "10");
        params.put("User", "test");
        params.put("Password", "test");
        params.put("Test", "fail");
        adapter.execWithStatusCheck(params, svcContext);
    }

    @Test(expected = SvcLogicException.class)
    public void testExecCommand_noPortFailed() throws SvcLogicException,
            IllegalStateException, IllegalArgumentException {

        params.put("Url", "test");
        params.put("User", "test");
        params.put("Password", "test");
        params.put("Test", "fail");
        adapter.execCommand(params, svcContext);
    }

    @Test(expected = SvcLogicException.class)
    public void testExecCommandPty_noPortFailed() throws SvcLogicException,
            IllegalStateException, IllegalArgumentException {

        params.put("Url", "test");
        params.put("User", "test");
        params.put("Password", "test");
        params.put("Test", "fail");
        adapter.execCommandWithPty(params, svcContext);
    }

    @Test(expected = SvcLogicException.class)
    public void testExecCommandResponse_noPortFailed() throws SvcLogicException,
            IllegalStateException, IllegalArgumentException {

        params.put("Url", "test");
        params.put("User", "test");
        params.put("Password", "test");
        params.put("Test", "fail");
        adapter.execWithStatusCheck(params, svcContext);
    }

    @Test(expected = SvcLogicException.class)
    public void testExecCommand_noCmdFailed() throws SvcLogicException,
            IllegalStateException, IllegalArgumentException {

        params.put("Url", "test");
        params.put("Port", "10");
        adapter.execCommand(params, svcContext);
    }

    @Test(expected = SvcLogicException.class)
    public void testExecCommandPty_noCmdFailed() throws SvcLogicException,
            IllegalStateException, IllegalArgumentException {

        params.put("Url", "test");
        params.put("Port", "10");
        adapter.execCommandWithPty(params, svcContext);
    }

    @Test(expected = SvcLogicException.class)
    public void testExecCommandResponse_noCmdFailed() throws SvcLogicException,
            IllegalStateException, IllegalArgumentException {

        params.put("Url", "test");
        params.put("Port", "10");
        adapter.execWithStatusCheck(params, svcContext);
    }

    @Test(expected = SvcLogicException.class)
    public void testExecCommandResponse_noSSHBasicFailed() throws SvcLogicException,
            IllegalStateException, IllegalArgumentException {

        params.put("Url", "test");
        params.put("Port", "10");
        params.put("User", "test");
        params.put("Password", "test");
        params.put("AuthType", "basic");
        params.put("Cmd", "test");
        adapter.execWithStatusCheck(params, svcContext);
    }

    @Test(expected = SvcLogicException.class)
    public void testExecCommandResponse_noSSHKeyFailed() throws SvcLogicException,
            IllegalStateException, IllegalArgumentException {

        params.put("Url", "test");
        params.put("Port", "10");
        params.put("User", "test");
        params.put("Password", "test");
        params.put("AuthType", "key");
        params.put("Cmd", "test");
        adapter.execWithStatusCheck(params, svcContext);
    }

    @Test(expected = SvcLogicException.class)
    public void testExecCommandResponse_noSSHNoneFailed() throws SvcLogicException,
            IllegalStateException, IllegalArgumentException {

        params.put("Url", "test");
        params.put("Port", "10");
        params.put("User", "test");
        params.put("Password", "test");
        params.put("AuthType", "none");
        params.put("Cmd", "test");
        params.put("ResponseType", "xml");
        adapter.execWithStatusCheck(params, svcContext);
    }

    @Test(expected = SvcLogicException.class)
    public void testExecCommandResponse_noSSHFailed() throws SvcLogicException,
            IllegalStateException, IllegalArgumentException {

        params.put("Url", "test");
        params.put("Port", "10");
        params.put("User", "test");
        params.put("Password", "test");
        params.put("Cmd", "test");
        params.put("ResponseType", "json");
        adapter.execWithStatusCheck(params, svcContext);
    }

    @Test(expected = IllegalArgumentException.class)
    public void testExecCommandResponse_noSSHInvalidParam() throws SvcLogicException,
            IllegalStateException, IllegalArgumentException {

        params.put("Url", "test");
        params.put("Port", "10");
        params.put("User", "test");
        params.put("Password", "test");
        params.put("Cmd", "test");
        params.put("ResponseType", "txt");
        adapter.execWithStatusCheck(params, svcContext);
    }

    @Test(expected = IllegalArgumentException.class)
    public void testExecCommandResponse_noSSHInvalidAuthParam() throws SvcLogicException,
            IllegalStateException, IllegalArgumentException {

        params.put("Url", "test");
        params.put("Port", "10");
        params.put("User", "test");
        params.put("Password", "test");
        params.put("Cmd", "test");
        params.put("AuthType", "spring");
        params.put("ResponseType", "json");
        adapter.execWithStatusCheck(params, svcContext);
    }

    @Test
    public void testExecCommandResponse_validJSON() throws SvcLogicException,
            IllegalStateException, IllegalArgumentException {

        params.put("Url", "test");
        params.put("Port", "10");
        params.put("User", "test");
        params.put("Password", "test");
        params.put("Cmd", "test");
        params.put("AuthType", "basic");
        params.put("ResponseType", "json");
        params.put("TestOut", "{\"equipment-data\":\"boo\"}");
        params.put("TestFail", "false");
        adapter = new SshApiCallNode(true);
        adapter.execWithStatusCheck(params, svcContext);
        assertEquals("boo", svcContext.getAttribute("equipment-data"));
    }

    @Test
    public void testExecCommandResponse_validXML() throws SvcLogicException,
            IllegalStateException, IllegalArgumentException {

        params.put("Url", "test");
        params.put("Port", "10");
        params.put("User", "test");
        params.put("Password", "test");
        params.put("Cmd", "test");
        params.put("AuthType", "basic");
        params.put("ResponseType", "xml");
        params.put("TestOut", "<modelVersion>4.0.0</modelVersion>");
        params.put("TestFail", "false");
        adapter = new SshApiCallNode(true);
        adapter.execWithStatusCheck(params, svcContext);
        assertEquals("4.0.0", svcContext.getAttribute("modelVersion"));
    }

    @Test
    public void testExecCommandResponse_validJSONPrefix() throws SvcLogicException,
            IllegalStateException, IllegalArgumentException {

        params.put("Url", "test");
        params.put("Port", "10");
        params.put("User", "test");
        params.put("Password", "test");
        params.put("Cmd", "test");
        params.put("AuthType", "basic");
        params.put("ResponseType", "json");
        params.put("TestOut", "{\"equipment-data\":\"boo\"}");
        params.put("ResponsePrefix", "test");
        params.put("TestFail", "false");
        adapter = new SshApiCallNode(true);
        adapter.execWithStatusCheck(params, svcContext);
        assertEquals("boo", svcContext.getAttribute("test.equipment-data"));
    }

    @Test
    public void testExecCommandResponse_validXMLPrefix() throws SvcLogicException,
            IllegalStateException, IllegalArgumentException {

        params.put("Url", "test");
        params.put("Port", "10");
        params.put("User", "test");
        params.put("Password", "test");
        params.put("Cmd", "test");
        params.put("AuthType", "basic");
        params.put("ResponseType", "xml");
        params.put("TestOut", "<modelVersion>4.0.0</modelVersion>");
        params.put("ResponsePrefix", "test");
        params.put("TestFail", "false");
        adapter = new SshApiCallNode(true);
        adapter.execWithStatusCheck(params, svcContext);
        assertEquals("4.0.0", svcContext.getAttribute("test.modelVersion"));
    }

    @Test(expected = SvcLogicException.class)
    public void testExecCommandResponse_validXMLFail() throws SvcLogicException,
            IllegalStateException, IllegalArgumentException {

        params.put("Url", "test");
        params.put("Port", "10");
        params.put("User", "test");
        params.put("Password", "test");
        params.put("Cmd", "test");
        params.put("AuthType", "basic");
        params.put("ResponseType", "xml");
        params.put("TestOut", "<modelVersion>4.0.0</modelVersion>");
        params.put("TestFail", "true");
        params.put("ResponsePrefix", "test");
        adapter = new SshApiCallNode(true);
        adapter.execWithStatusCheck(params, svcContext);
    }

    @Test(expected = SvcLogicException.class)
    public void testExecCommandResponse_validXMLPrefixKey() throws SvcLogicException,
            IllegalStateException, IllegalArgumentException {
        params = new HashMap<>();
        params.put("Url", "test");
        params.put("Port", "10");
        params.put("SshKey", "test");
        params.put("Cmd", "test");
        params.put("ResponseType", "xml");
        params.put("TestOut", "<modelVersion>4.0.0</modelVersion>");
        params.put("ResponsePrefix", "test");
        adapter.execWithStatusCheck(params, svcContext);
        assertEquals("4.0.0", svcContext.getAttribute("test.modelVersion"));
    }
}