5fddce12baf86216fa334ec8ca12ff66a59cf67b
[appc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.appc.seqgen;
23
24 import java.io.IOException;
25 import java.net.URISyntaxException;
26 import java.nio.file.Files;
27 import java.nio.file.Paths;
28 import java.util.HashMap;
29 import java.util.Map;
30
31 import org.junit.Assert;
32 import org.junit.Test;
33 import org.onap.appc.seqgen.dgplugin.SequenceGeneratorPlugin;
34 import org.onap.appc.seqgen.dgplugin.impl.SequenceGeneratorPluginImpl;
35 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
36
37 import com.att.eelf.configuration.EELFLogger;
38 import com.att.eelf.configuration.EELFManager;
39
40 public class TestSequenceGeneratorPlugin {
41
42     private static final EELFLogger logger = EELFManager.getInstance().getLogger(TestSequenceGeneratorPlugin.class);
43     private SequenceGeneratorPlugin seqImpl;
44
45     @Test
46     public void testGenerateSequenceStart() throws URISyntaxException, IOException {
47         String inputJSON = readInput("/input/start.json");
48
49         Map<String,String> params = new HashMap<>();
50         SvcLogicContext context = new SvcLogicContext();
51         context.setAttribute("inputJSON",inputJSON);
52
53         seqImpl = new SequenceGeneratorPluginImpl();
54         seqImpl.generateSequence(params,context);
55
56         String outputJSON = context.getAttribute("output");
57         String actualOutput = readOutput("/output/Start2.json");
58         Assert.assertEquals(outputJSON.trim(),actualOutput.trim());
59     }
60
61     @Test
62     public void testGenerateSequenceWODependencyInfo()throws URISyntaxException, IOException {
63         String inputJSON = readInput("/input/start-withoutDependency.json");
64
65         Map<String,String> params = new HashMap<>();
66         SvcLogicContext context = new SvcLogicContext();
67         context.setAttribute("inputJSON",inputJSON);
68
69         SequenceGeneratorPlugin plugin = new SequenceGeneratorPluginImpl();
70         plugin.generateSequence(params,context);
71
72         String outputJSON = context.getAttribute("output");
73         String actualOutput = readOutput("/output/start-withoutDependency.json");
74         Assert.assertEquals(outputJSON.trim(),actualOutput.trim());
75     }
76
77     @Test
78     public void testGenerateSequenceSingleVM()throws URISyntaxException, IOException {
79         String inputJSON = readInput("/input/start-singleVM-.json");
80
81         Map<String,String> params = new HashMap<>();
82         SvcLogicContext context = new SvcLogicContext();
83         context.setAttribute("inputJSON",inputJSON);
84
85         SequenceGeneratorPlugin plugin = new SequenceGeneratorPluginImpl();
86         plugin.generateSequence(params,context);
87
88         String outputJSON = context.getAttribute("output");
89         String actualOutput = readOutput("/output/start-singleVM-.json");
90         Assert.assertEquals(outputJSON.trim(),actualOutput.trim());
91     }
92
93     @Test
94     public void testGenerateSequenceNoStrategy() throws URISyntaxException, IOException {
95         String inputJSON = readInput("/input/no-strategy.json");
96
97         Map<String,String> params = new HashMap<>();
98         SvcLogicContext context = new SvcLogicContext();
99         context.setAttribute("inputJSON",inputJSON);
100
101         SequenceGeneratorPlugin plugin = new SequenceGeneratorPluginImpl();
102         plugin.generateSequence(params,context);
103
104         String outputJSON = context.getAttribute("output");
105         String actualOutput = readOutput("/output/Start2.json");
106
107         Assert.assertEquals(outputJSON.trim(),actualOutput.trim());
108     }
109
110     @Test
111     public void testGenerateSequenceStop() throws URISyntaxException, IOException {
112         String inputJSON = readInput("/input/stop.json");
113
114         Map<String,String> params = new HashMap<>();
115         SvcLogicContext context = new SvcLogicContext();
116         context.setAttribute("inputJSON",inputJSON);
117
118         SequenceGeneratorPlugin plugin = new SequenceGeneratorPluginImpl();
119         plugin.generateSequence(params,context);
120
121         String outputJSON = context.getAttribute("output");
122         String actualOutput = readOutput("/output/Output-stop.json");
123
124         Assert.assertEquals(outputJSON.trim(),actualOutput.trim());
125     }
126
127     @Test
128     public void testGenerateSequenceWrongNumber() throws URISyntaxException, IOException {
129         String inputJSON = readInput("/input/wrongnumber.json");
130
131         Map<String,String> params = new HashMap<>();
132         SvcLogicContext context = new SvcLogicContext();
133         context.setAttribute("inputJSON",inputJSON);
134
135         SequenceGeneratorPlugin plugin = new SequenceGeneratorPluginImpl();
136         plugin.generateSequence(params,context);
137
138         String errorCode = context.getAttribute("error-code");
139         String errorMessage = context.getAttribute("error-message");
140         logger.debug("errorCode = " + errorCode);
141         Assert.assertEquals(errorCode,"401");
142         Assert.assertEquals(errorMessage,"Error generating sequence Invalid Number for Wait Time 6a");
143     }
144
145
146     @Test
147     public void testGenerateSequenceCyclic() throws URISyntaxException, IOException {
148         String inputJSON = readInput("/input/cyclic.json");
149
150         Map<String,String> params = new HashMap<>();
151         SvcLogicContext context = new SvcLogicContext();
152         context.setAttribute("inputJSON",inputJSON);
153
154         SequenceGeneratorPlugin plugin = new SequenceGeneratorPluginImpl();
155         plugin.generateSequence(params,context);
156
157         String errorCode = context.getAttribute("error-code");
158         String errorMessage = context.getAttribute("error-message");
159         logger.debug("errorCode = " + errorCode);
160         Assert.assertEquals(errorCode,"401");
161         Assert.assertEquals(errorMessage,"Error generating sequence There seems to be no Root/Independent node for Vnfc dependencies");
162     }
163
164     @Test
165     public void testGenerateSequenceWrongAction() throws URISyntaxException, IOException {
166         String inputJSON = readInput("/input/wrongaction.json");
167
168         Map<String,String> params = new HashMap<>();
169         SvcLogicContext context = new SvcLogicContext();
170         context.setAttribute("inputJSON",inputJSON);
171
172         SequenceGeneratorPlugin plugin = new SequenceGeneratorPluginImpl();
173         plugin.generateSequence(params,context);
174
175         String errorCode = context.getAttribute("error-code");
176         String errorMessage = context.getAttribute("error-message");
177         logger.debug("errorCode = " + errorCode);
178         Assert.assertEquals(errorCode,"401");
179         Assert.assertEquals(errorMessage,"Error generating sequence Invalid Action start");
180     }
181
182
183     @Test
184     public void testGenerateSequenceMissingRequestInfo() throws URISyntaxException, IOException {
185         String inputJSON = readInput("/input/missingrequestinfo.json");
186
187         Map<String,String> params = new HashMap<>();
188         SvcLogicContext context = new SvcLogicContext();
189         context.setAttribute("inputJSON",inputJSON);
190
191         SequenceGeneratorPlugin plugin = new SequenceGeneratorPluginImpl();
192         plugin.generateSequence(params,context);
193
194         String errorCode = context.getAttribute("error-code");
195         String errorMessage = context.getAttribute("error-message");
196         logger.debug("errorCode = " + errorCode);
197         Assert.assertEquals(errorCode,"401");
198         Assert.assertEquals(errorMessage,"Error generating sequence Request info is not provided in the input");
199     }
200
201     @Test
202     public void testGenerateSequenceStopSingleVM() throws URISyntaxException, IOException{
203         String inputJSON = readInput("/input/stop-singleVM.json");
204
205         Map<String,String> params = new HashMap<>();
206         SvcLogicContext context = new SvcLogicContext();
207         context.setAttribute("inputJSON",inputJSON);
208
209         SequenceGeneratorPlugin plugin = new SequenceGeneratorPluginImpl();
210         plugin.generateSequence(params,context);
211
212         String outputJSON = context.getAttribute("output");
213         String actualOutput = readOutput("/output/stop-singleVM.json");
214         Assert.assertEquals(outputJSON.trim(),actualOutput.trim());
215     }
216
217     @Test
218     public void testGenerateSequenceStopSingleVmPerVnfc() throws URISyntaxException, IOException{
219         String inputJSON = readInput("/input/stop-singleVmPerVnfc.json");
220
221         Map<String,String> params = new HashMap<>();
222         SvcLogicContext context = new SvcLogicContext();
223         context.setAttribute("inputJSON",inputJSON);
224
225         SequenceGeneratorPlugin plugin = new SequenceGeneratorPluginImpl();
226         plugin.generateSequence(params,context);
227
228         String outputJSON = context.getAttribute("output");
229         String actualOutput = readOutput("/output/stop-singleVmPerVnfc.json");
230         Assert.assertEquals(outputJSON.trim(),actualOutput.trim());
231     }
232
233     @Test
234     public void testGenerateSequenceRestartNoDep() throws URISyntaxException, IOException {
235         String inputJSON = readInput("/input/restartNodep.json");
236
237         Map<String,String> params = new HashMap<>();
238         SvcLogicContext context = new SvcLogicContext();
239         context.setAttribute("inputJSON",inputJSON);
240
241         SequenceGeneratorPlugin plugin = new SequenceGeneratorPluginImpl();
242         plugin.generateSequence(params,context);
243
244         String outputJSON = context.getAttribute("output");
245         String actualOutput = readOutput("/output/restart-NoDep.json");
246         outputJSON.trim();
247         Assert.assertEquals(outputJSON.trim(),actualOutput.trim());
248     }
249
250     @Test
251     public void testGenerateSequenceRestartNoDepSingleVM() throws URISyntaxException, IOException {
252         String inputJSON = readInput("/input/NoDep-SingleVM.json");
253
254         Map<String,String> params = new HashMap<>();
255         SvcLogicContext context = new SvcLogicContext();
256         context.setAttribute("inputJSON",inputJSON);
257
258         SequenceGeneratorPlugin plugin = new SequenceGeneratorPluginImpl();
259         plugin.generateSequence(params,context);
260
261         String outputJSON = context.getAttribute("output");
262         String actualOutput = readOutput("/output/restart-Nodep-SingleVM.json");
263         Assert.assertEquals(outputJSON.trim(),actualOutput.trim());
264     }
265
266     @Test
267     public void testGenerateSequenceStartSingleVmPerVnfc() throws URISyntaxException, IOException{
268         String inputJSON = readInput("/input/start-singleVmPerVnfc-.json");
269
270         Map<String,String> params = new HashMap<>();
271         SvcLogicContext context = new SvcLogicContext();
272         context.setAttribute("inputJSON",inputJSON);
273
274         SequenceGeneratorPlugin plugin = new SequenceGeneratorPluginImpl();
275         plugin.generateSequence(params,context);
276
277         String outputJSON = context.getAttribute("output");
278         String actualOutput = readOutput("/output/start-singleVmPerVnfc.json");
279         Assert.assertEquals(outputJSON.trim(),actualOutput.trim());
280     }
281
282     @Test
283     public void testGenerateSequenceVnfcNotPresentInInventory() throws URISyntaxException, IOException {
284         String inputJSON = readInput("/input/CheckVNfcInInventory.json");
285
286         Map<String,String> params = new HashMap<>();
287         SvcLogicContext context = new SvcLogicContext();
288         context.setAttribute("inputJSON",inputJSON);
289         SequenceGeneratorPlugin plugin = new SequenceGeneratorPluginImpl();
290         plugin.generateSequence(params,context);
291
292         String outputJSON = context.getAttribute("output");
293         String actualOutput = readOutput("/output/CheckVnfcInInventory.json");
294
295         Assert.assertEquals(outputJSON.trim(),actualOutput.trim());
296     }
297
298     @Test
299     public void testGenerateSequenceCheckMandatoryVnfc() throws URISyntaxException, IOException {
300         String inputJSON = readInput("/input/CheckMandatoryVnfc.json");
301
302         Map<String,String> params = new HashMap<>();
303         SvcLogicContext context = new SvcLogicContext();
304         context.setAttribute("inputJSON",inputJSON);
305
306         SequenceGeneratorPlugin plugin = new SequenceGeneratorPluginImpl();
307         plugin.generateSequence(params,context);
308
309         String errorCode = context.getAttribute("error-code");
310         String errorMessage = context.getAttribute("error-message");
311         logger.debug("errorCode = " + errorCode);
312         Assert.assertEquals(errorCode,"401");
313         Assert.assertEquals(errorMessage,"Error generating sequence VMs missing for the mandatory VNFC : [smp]");
314     }
315
316     @Test
317     public void testGenerateSequenceCheckMissingDependencyInfo() throws URISyntaxException, IOException {
318         String inputJSON = readInput("/input/MissingDependencyInfo.json");
319
320         Map<String,String> params = new HashMap<>();
321         SvcLogicContext context = new SvcLogicContext();
322         context.setAttribute("inputJSON",inputJSON);
323
324         SequenceGeneratorPlugin plugin = new SequenceGeneratorPluginImpl();
325         plugin.generateSequence(params,context);
326
327         String errorCode = context.getAttribute("error-code");
328         String errorMessage = context.getAttribute("error-message");
329         logger.debug("errorCode = " + errorCode);
330         Assert.assertEquals(errorCode,"401");
331         Assert.assertEquals(errorMessage,"Error generating sequence Dependency model is missing following vnfc type(s): [smp]");
332     }
333
334     @Test
335     public void testGenerateSequenceExtraVnfcInDependency() throws URISyntaxException, IOException {
336         String inputJSON = readInput("/input/WrongDependencyModel.json");
337
338         Map<String,String> params = new HashMap<>();
339         SvcLogicContext context = new SvcLogicContext();
340         context.setAttribute("inputJSON",inputJSON);
341
342         SequenceGeneratorPlugin plugin = new SequenceGeneratorPluginImpl();
343         plugin.generateSequence(params,context);
344
345         String errorCode = context.getAttribute("error-code");
346         String errorMessage = context.getAttribute("error-message");
347         logger.debug("errorCode = " + errorCode);
348         Assert.assertEquals(errorCode,"401");
349         Assert.assertEquals(errorMessage,"Error generating sequence Dependency model missing vnfc type SMP");
350     }
351
352     @Test
353     public void testGenerateSequenceStartWithVmStartCaps()throws URISyntaxException, IOException {
354         String inputJSON = readInput("/input/StartWithVmStartCaps.json");
355
356         Map<String,String> params = new HashMap<>();
357         SvcLogicContext context = new SvcLogicContext();
358         context.setAttribute("inputJSON",inputJSON);
359
360         SequenceGeneratorPlugin plugin = new SequenceGeneratorPluginImpl();
361         plugin.generateSequence(params,context);
362
363         String actualOutput = context.getAttribute("output");
364         String outputJSON = readOutput("/output/StartWithVmStartCaps.json");
365         Assert.assertEquals(outputJSON.trim(), actualOutput.trim());
366     }
367     
368     @Test
369     public void testGenerateSequenceRestartWithVmRestartCaps()throws URISyntaxException, IOException {
370         String inputJSON = readInput("/input/RestartWithVmRestartCaps.json");
371
372         Map<String,String> params = new HashMap<>();
373         SvcLogicContext context = new SvcLogicContext();
374         context.setAttribute("inputJSON",inputJSON);
375
376         SequenceGeneratorPlugin plugin = new SequenceGeneratorPluginImpl();
377         plugin.generateSequence(params,context);
378
379         String outputJSON = context.getAttribute("output");
380         String actualOutput = readOutput("/output/RestartWithVmRestartCaps.json");
381         Assert.assertEquals(outputJSON.trim(),actualOutput.trim());
382     }
383     
384     @Test
385     public void testGenerateSequenceStopWithVmStopCaps()throws URISyntaxException, IOException {
386         String inputJSON = readInput("/input/StopWithVmStopCaps.json");
387
388         Map<String,String> params = new HashMap<>();
389         SvcLogicContext context = new SvcLogicContext();
390         context.setAttribute("inputJSON",inputJSON);
391
392         SequenceGeneratorPlugin plugin = new SequenceGeneratorPluginImpl();
393         plugin.generateSequence(params,context);
394
395         String actualOutput = context.getAttribute("output");
396         String outputJSON = readOutput("/output/StopWithVmStopCaps.json");
397         Assert.assertEquals(outputJSON.trim(),actualOutput.trim());
398     }
399     
400     @Test
401     public void testGenerateSequenceStartWithoutAnyCaps()throws URISyntaxException, IOException {
402         String inputJSON = readInput("/input/StartWithoutAnyCaps.json");
403
404         Map<String,String> params = new HashMap<>();
405         SvcLogicContext context = new SvcLogicContext();
406         context.setAttribute("inputJSON",inputJSON);
407
408         SequenceGeneratorPlugin plugin = new SequenceGeneratorPluginImpl();
409         plugin.generateSequence(params,context);
410
411         String actualOutput = context.getAttribute("output");
412         String outputJSON = readOutput("/output/StartWithoutAnyCaps.json");
413         Assert.assertEquals(outputJSON.trim(), actualOutput.trim());
414     }
415     
416     @Test
417     public void testGenerateSequenceRestartWithoutAnyCaps()throws URISyntaxException, IOException {
418         String inputJSON = readInput("/input/RestartWithoutAnyCaps.json");
419
420         Map<String,String> params = new HashMap<>();
421         SvcLogicContext context = new SvcLogicContext();
422         context.setAttribute("inputJSON",inputJSON);
423
424         SequenceGeneratorPlugin plugin = new SequenceGeneratorPluginImpl();
425         plugin.generateSequence(params,context);
426
427         String outputJSON = context.getAttribute("output");
428         String actualOutput = readOutput("/output/RestartWithoutAnyCaps.json");
429         Assert.assertEquals(outputJSON.trim(),actualOutput.trim());
430     }
431     
432     @Test
433     public void testGenerateSequenceStopWithoutAnyCaps()throws URISyntaxException, IOException {
434         String inputJSON = readInput("/input/StopWithoutAnyCaps.json");
435
436         Map<String,String> params = new HashMap<>();
437         SvcLogicContext context = new SvcLogicContext();
438         context.setAttribute("inputJSON",inputJSON);
439
440         SequenceGeneratorPlugin plugin = new SequenceGeneratorPluginImpl();
441         plugin.generateSequence(params,context);
442
443         String outputJSON = context.getAttribute("output");
444         String actualOutput = readOutput("/output/StopWithoutAnyCaps.json");
445         Assert.assertEquals(outputJSON.trim(),actualOutput.trim());
446     }
447     @Test
448     public void testGenerateSequenceStartWithoutVmStartCaps()throws URISyntaxException, IOException {
449         String inputJSON = readInput("/input/StartWithoutVmStartCaps.json");
450
451         Map<String,String> params = new HashMap<>();
452         SvcLogicContext context = new SvcLogicContext();
453         context.setAttribute("inputJSON",inputJSON);
454
455         SequenceGeneratorPlugin plugin = new SequenceGeneratorPluginImpl();
456         plugin.generateSequence(params,context);
457
458         String errorCode = context.getAttribute("error-code");
459         String errorMessage = context.getAttribute("error-message");
460         logger.debug("errorCode = " + errorCode);
461         Assert.assertEquals("450", errorCode);
462         Assert.assertEquals("Request is not supported", errorMessage);
463     }
464     
465     @Test
466     public void testGenerateSequenceRestartWithoutVmRestartCaps()throws URISyntaxException, IOException {
467         String inputJSON = readInput("/input/RestartWithoutVmRestartCaps.json");
468
469         Map<String,String> params = new HashMap<>();
470         SvcLogicContext context = new SvcLogicContext();
471         context.setAttribute("inputJSON",inputJSON);
472
473         SequenceGeneratorPlugin plugin = new SequenceGeneratorPluginImpl();
474         plugin.generateSequence(params,context);
475
476         String errorCode = context.getAttribute("error-code");
477         String errorMessage = context.getAttribute("error-message");
478         logger.debug("errorCode = " + errorCode);
479         Assert.assertEquals("450", errorCode);
480         Assert.assertEquals("Request is not supported", errorMessage);
481     }
482     
483     @Test
484     public void testGenerateSequenceStopWithoutVmStopCaps()throws URISyntaxException, IOException {
485         String inputJSON = readInput("/input/StopWithoutVmStopCaps.json");
486
487         Map<String,String> params = new HashMap<>();
488         SvcLogicContext context = new SvcLogicContext();
489         context.setAttribute("inputJSON",inputJSON);
490
491         SequenceGeneratorPlugin plugin = new SequenceGeneratorPluginImpl();
492         plugin.generateSequence(params,context);
493
494         String errorCode = context.getAttribute("error-code");
495         String errorMessage = context.getAttribute("error-message");
496         logger.debug("errorCode = " + errorCode);
497         Assert.assertEquals("450", errorCode);
498         Assert.assertEquals("Request is not supported", errorMessage);
499     }
500
501     private String readInput(String inputFile) throws URISyntaxException, IOException {
502         return new String(Files.readAllBytes(Paths.get(this.getClass().getResource(inputFile).toURI())), "UTF-8");
503     }
504     
505     private String readOutput(String outputFile) throws IOException,URISyntaxException {
506         String output = new String(Files.readAllBytes(Paths.get(this.getClass().getResource(outputFile).toURI())), "UTF-8");
507         int start=output.indexOf("[");
508         return output.substring(start,output.length());
509     }
510 }