937001f6a38a81157822383d0e60d5f79e32048a
[appc.git] / appc-sequence-generator / appc-sequence-generator-bundle / src / test / java / org / onap / appc / seqgen / TestSequenceGeneratorPlugin.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APP-C
4  * ================================================================================
5  * Copyright (C) 2017 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  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.appc.seqgen;
22
23 import com.att.eelf.configuration.EELFLogger;
24 import com.att.eelf.configuration.EELFManager;
25 import org.junit.Assert;
26 import org.junit.Test;
27 import org.onap.appc.seqgen.dgplugin.SequenceGeneratorPlugin;
28 import org.onap.appc.seqgen.dgplugin.impl.SequenceGeneratorPluginImpl;
29 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
30
31 import java.io.File;
32 import java.io.FileInputStream;
33 import java.io.IOException;
34 import java.net.URISyntaxException;
35 import java.util.HashMap;
36 import java.util.Map;
37
38 public class TestSequenceGeneratorPlugin {
39
40     private static final EELFLogger logger = EELFManager.getInstance().getLogger(TestSequenceGeneratorPlugin.class);
41
42     @Test
43     public void testGenerateSequenceStart() throws URISyntaxException, IOException {
44         String inputJSON = readInput("/input/start.json");
45
46         Map<String,String> params = new HashMap<>();
47         SvcLogicContext context = new SvcLogicContext();
48         context.setAttribute("inputJSON",inputJSON);
49
50         SequenceGeneratorPlugin plugin = new SequenceGeneratorPluginImpl();
51         plugin.generateSequence(params,context);
52
53         String outputJSON = context.getAttribute("output");
54         String actualOutput = readOutput("/output/Start2.json");
55         Assert.assertEquals(outputJSON.trim(),actualOutput.trim());
56     }
57
58     @Test
59     public void testGenerateSequenceWODependencyInfo()throws URISyntaxException, IOException {
60         String inputJSON = readInput("/input/start-withoutDependency.json");
61
62         Map<String,String> params = new HashMap<>();
63         SvcLogicContext context = new SvcLogicContext();
64         context.setAttribute("inputJSON",inputJSON);
65
66         SequenceGeneratorPlugin plugin = new SequenceGeneratorPluginImpl();
67         plugin.generateSequence(params,context);
68
69         String outputJSON = context.getAttribute("output");
70         String actualOutput = readOutput("/output/start-withoutDependency.json");
71         Assert.assertEquals(outputJSON.trim(),actualOutput.trim());
72     }
73
74     @Test
75     public void testGenerateSequenceSingleVM()throws URISyntaxException, IOException {
76         String inputJSON = readInput("/input/start-singleVM-.json");
77
78         Map<String,String> params = new HashMap<>();
79         SvcLogicContext context = new SvcLogicContext();
80         context.setAttribute("inputJSON",inputJSON);
81
82         SequenceGeneratorPlugin plugin = new SequenceGeneratorPluginImpl();
83         plugin.generateSequence(params,context);
84
85         String outputJSON = context.getAttribute("output");
86         String actualOutput = readOutput("/output/start-singleVM-.json");
87         Assert.assertEquals(outputJSON.trim(),actualOutput.trim());
88     }
89
90     @Test
91     public void testGenerateSequenceNoStrategy() throws URISyntaxException, IOException {
92         String inputJSON = readInput("/input/no-strategy.json");
93
94         Map<String,String> params = new HashMap<>();
95         SvcLogicContext context = new SvcLogicContext();
96         context.setAttribute("inputJSON",inputJSON);
97
98         SequenceGeneratorPlugin plugin = new SequenceGeneratorPluginImpl();
99         plugin.generateSequence(params,context);
100
101         String outputJSON = context.getAttribute("output");
102         String actualOutput = readOutput("/output/Start2.json");
103
104         Assert.assertEquals(outputJSON.trim(),actualOutput.trim());
105     }
106
107     @Test
108     public void testGenerateSequenceStop() throws URISyntaxException, IOException {
109         String inputJSON = readInput("/input/stop.json");
110
111         Map<String,String> params = new HashMap<>();
112         SvcLogicContext context = new SvcLogicContext();
113         context.setAttribute("inputJSON",inputJSON);
114
115         SequenceGeneratorPlugin plugin = new SequenceGeneratorPluginImpl();
116         plugin.generateSequence(params,context);
117
118         String outputJSON = context.getAttribute("output");
119         String actualOutput = readOutput("/output/Output-stop.json");
120
121         Assert.assertEquals(outputJSON.trim(),actualOutput.trim());
122     }
123
124     @Test
125     public void testGenerateSequenceWrongNumber() throws URISyntaxException, IOException {
126         String inputJSON = readInput("/input/wrongnumber.json");
127
128         Map<String,String> params = new HashMap<>();
129         SvcLogicContext context = new SvcLogicContext();
130         context.setAttribute("inputJSON",inputJSON);
131
132         SequenceGeneratorPlugin plugin = new SequenceGeneratorPluginImpl();
133         plugin.generateSequence(params,context);
134
135         String errorCode = context.getAttribute("error-code");
136         String errorMessage = context.getAttribute("error-message");
137         logger.debug("errorCode = " + errorCode);
138         Assert.assertEquals(errorCode,"401");
139         Assert.assertEquals(errorMessage,"Error generating sequence Invalid Number for Wait Time 6a");
140     }
141
142
143     @Test
144     public void testGenerateSequenceCyclic() throws URISyntaxException, IOException {
145         String inputJSON = readInput("/input/cyclic.json");
146
147         Map<String,String> params = new HashMap<>();
148         SvcLogicContext context = new SvcLogicContext();
149         context.setAttribute("inputJSON",inputJSON);
150
151         SequenceGeneratorPlugin plugin = new SequenceGeneratorPluginImpl();
152         plugin.generateSequence(params,context);
153
154         String errorCode = context.getAttribute("error-code");
155         String errorMessage = context.getAttribute("error-message");
156         logger.debug("errorCode = " + errorCode);
157         Assert.assertEquals(errorCode,"401");
158         Assert.assertEquals(errorMessage,"Error generating sequence There seems to be no Root/Independent node for Vnfc dependencies");
159     }
160
161     @Test
162     public void testGenerateSequenceWrongAction() throws URISyntaxException, IOException {
163         String inputJSON = readInput("/input/wrongaction.json");
164
165         Map<String,String> params = new HashMap<>();
166         SvcLogicContext context = new SvcLogicContext();
167         context.setAttribute("inputJSON",inputJSON);
168
169         SequenceGeneratorPlugin plugin = new SequenceGeneratorPluginImpl();
170         plugin.generateSequence(params,context);
171
172         String errorCode = context.getAttribute("error-code");
173         String errorMessage = context.getAttribute("error-message");
174         logger.debug("errorCode = " + errorCode);
175         Assert.assertEquals(errorCode,"401");
176         Assert.assertEquals(errorMessage,"Error generating sequence Invalid Action start");
177     }
178
179
180     @Test
181     public void testGenerateSequenceMissingRequestInfo() throws URISyntaxException, IOException {
182         String inputJSON = readInput("/input/missingrequestinfo.json");
183
184         Map<String,String> params = new HashMap<>();
185         SvcLogicContext context = new SvcLogicContext();
186         context.setAttribute("inputJSON",inputJSON);
187
188         SequenceGeneratorPlugin plugin = new SequenceGeneratorPluginImpl();
189         plugin.generateSequence(params,context);
190
191         String errorCode = context.getAttribute("error-code");
192         String errorMessage = context.getAttribute("error-message");
193         logger.debug("errorCode = " + errorCode);
194         Assert.assertEquals(errorCode,"401");
195         Assert.assertEquals(errorMessage,"Error generating sequence Request info is not provided in the input");
196     }
197
198     @Test
199     public void testGenerateSequenceStopSingleVM() throws URISyntaxException, IOException{
200         String inputJSON = readInput("/input/stop-singleVM.json");
201
202         Map<String,String> params = new HashMap<>();
203         SvcLogicContext context = new SvcLogicContext();
204         context.setAttribute("inputJSON",inputJSON);
205
206         SequenceGeneratorPlugin plugin = new SequenceGeneratorPluginImpl();
207         plugin.generateSequence(params,context);
208
209         String outputJSON = context.getAttribute("output");
210         String actualOutput = readOutput("/output/stop-singleVM.json");
211         Assert.assertEquals(outputJSON.trim(),actualOutput.trim());
212     }
213
214     @Test
215     public void testGenerateSequenceStopSingleVmPerVnfc() throws URISyntaxException, IOException{
216         String inputJSON = readInput("/input/stop-singleVmPerVnfc.json");
217
218         Map<String,String> params = new HashMap<>();
219         SvcLogicContext context = new SvcLogicContext();
220         context.setAttribute("inputJSON",inputJSON);
221
222         SequenceGeneratorPlugin plugin = new SequenceGeneratorPluginImpl();
223         plugin.generateSequence(params,context);
224
225         String outputJSON = context.getAttribute("output");
226         String actualOutput = readOutput("/output/stop-singleVmPerVnfc.json");
227         Assert.assertEquals(outputJSON.trim(),actualOutput.trim());
228     }
229
230     @Test
231     public void testGenerateSequenceRestartNoDep() throws URISyntaxException, IOException {
232         String inputJSON = readInput("/input/restartNodep.json");
233
234         Map<String,String> params = new HashMap<>();
235         SvcLogicContext context = new SvcLogicContext();
236         context.setAttribute("inputJSON",inputJSON);
237
238         SequenceGeneratorPlugin plugin = new SequenceGeneratorPluginImpl();
239         plugin.generateSequence(params,context);
240
241         String outputJSON = context.getAttribute("output");
242         String actualOutput = readInput("/output/restart-NoDep.json");
243         outputJSON.trim();
244         Assert.assertEquals(outputJSON.trim(),actualOutput.trim());
245     }
246
247     @Test
248     public void testGenerateSequenceRestartNoDepSingleVM() throws URISyntaxException, IOException {
249         String inputJSON = readInput("/input/NoDep-SingleVM.json");
250
251         Map<String,String> params = new HashMap<>();
252         SvcLogicContext context = new SvcLogicContext();
253         context.setAttribute("inputJSON",inputJSON);
254
255         SequenceGeneratorPlugin plugin = new SequenceGeneratorPluginImpl();
256         plugin.generateSequence(params,context);
257
258         String outputJSON = context.getAttribute("output");
259         String actualOutput = readInput("/output/restart-Nodep-SingleVM.json");
260         Assert.assertEquals(outputJSON.trim(),actualOutput.trim());
261     }
262
263     @Test
264     public void testGenerateSequenceStartSingleVmPerVnfc() throws URISyntaxException, IOException{
265         String inputJSON = readInput("/input/start-singleVmPerVnfc-.json");
266
267         Map<String,String> params = new HashMap<>();
268         SvcLogicContext context = new SvcLogicContext();
269         context.setAttribute("inputJSON",inputJSON);
270
271         SequenceGeneratorPlugin plugin = new SequenceGeneratorPluginImpl();
272         plugin.generateSequence(params,context);
273
274         String outputJSON = context.getAttribute("output");
275         String actualOutput = readOutput("/output/start-singleVmPerVnfc.json");
276         Assert.assertEquals(outputJSON.trim(),actualOutput.trim());
277     }
278
279     @Test
280     public  void testGenerateSequenceStopWithoutDep() throws  URISyntaxException,IOException{
281         String inputJSON = readInput("/input/stop-WithoutDep.json");
282
283         Map<String,String> params = new HashMap<>();
284         SvcLogicContext context = new SvcLogicContext();
285         context.setAttribute("inputJSON",inputJSON);
286
287         SequenceGeneratorPlugin plugin = new SequenceGeneratorPluginImpl();
288         plugin.generateSequence(params,context);
289
290         String outputJSON = context.getAttribute("output");
291         String actualOutput = readOutput("/output/stop-WithoutDep.json");
292
293         Assert.assertEquals(outputJSON.trim(),actualOutput.trim());
294     }
295
296     @Test
297     public void testGenerateSequenceVnfcNotPresentInInventory() throws URISyntaxException, IOException {
298         String inputJSON = readInput("/input/CheckVNfcInInventory.json");
299
300         Map<String,String> params = new HashMap<>();
301         SvcLogicContext context = new SvcLogicContext();
302         context.setAttribute("inputJSON",inputJSON);
303         SequenceGeneratorPlugin plugin = new SequenceGeneratorPluginImpl();
304         plugin.generateSequence(params,context);
305
306         String outputJSON = context.getAttribute("output");
307         String actualOutput = readOutput("/output/CheckVnfcInInventory.json");
308
309         Assert.assertEquals(outputJSON.trim(),actualOutput.trim());
310     }
311
312     @Test
313     public void testGenerateSequenceCheckMandatoryVnfc() throws URISyntaxException, IOException {
314         String inputJSON = readInput("/input/CheckMandatoryVnfc.json");
315
316         Map<String,String> params = new HashMap<>();
317         SvcLogicContext context = new SvcLogicContext();
318         context.setAttribute("inputJSON",inputJSON);
319
320         SequenceGeneratorPlugin plugin = new SequenceGeneratorPluginImpl();
321         plugin.generateSequence(params,context);
322
323         String errorCode = context.getAttribute("error-code");
324         String errorMessage = context.getAttribute("error-message");
325         logger.debug("errorCode = " + errorCode);
326         Assert.assertEquals(errorCode,"401");
327         Assert.assertEquals(errorMessage,"Error generating sequence VMs missing for the mandatory VNFC : [smp]");
328     }
329
330     @Test
331     public void testGenerateSequenceCheckMissingDependencyInfo() throws URISyntaxException, IOException {
332         String inputJSON = readInput("/input/MissingDependencyInfo.json");
333
334         Map<String,String> params = new HashMap<>();
335         SvcLogicContext context = new SvcLogicContext();
336         context.setAttribute("inputJSON",inputJSON);
337
338         SequenceGeneratorPlugin plugin = new SequenceGeneratorPluginImpl();
339         plugin.generateSequence(params,context);
340
341         String errorCode = context.getAttribute("error-code");
342         String errorMessage = context.getAttribute("error-message");
343         logger.debug("errorCode = " + errorCode);
344         Assert.assertEquals(errorCode,"401");
345         Assert.assertEquals(errorMessage,"Error generating sequence Dependency model is missing following vnfc type(s): [smp]");
346     }
347
348     @Test
349     public void testGenerateSequenceExtraVnfcInDependency() throws URISyntaxException, IOException {
350         String inputJSON = readInput("/input/WrongDependencyModel.json");
351
352         Map<String,String> params = new HashMap<>();
353         SvcLogicContext context = new SvcLogicContext();
354         context.setAttribute("inputJSON",inputJSON);
355
356         SequenceGeneratorPlugin plugin = new SequenceGeneratorPluginImpl();
357         plugin.generateSequence(params,context);
358
359         String errorCode = context.getAttribute("error-code");
360         String errorMessage = context.getAttribute("error-message");
361         logger.debug("errorCode = " + errorCode);
362         Assert.assertEquals(errorCode,"401");
363         Assert.assertEquals(errorMessage,"Error generating sequence Dependency model missing vnfc type SMP");
364     }
365
366     private String readInput(String inputFile) throws URISyntaxException, IOException {
367         File file = new File(this.getClass().getResource(inputFile).toURI());
368
369         byte[] bFile = new byte[(int) file.length()];
370         FileInputStream fileInputStream = new FileInputStream(file);
371         fileInputStream.read(bFile);
372         fileInputStream.close();
373         return new String(bFile);
374     }
375     private String readOutput(String outputFile) throws IOException,URISyntaxException {
376         File file = new File(this.getClass().getResource(outputFile).toURI());
377
378         byte[] bFile = new byte[(int) file.length()];
379         FileInputStream fileInputStream = new FileInputStream(file);
380         fileInputStream.read(bFile);
381         fileInputStream.close();
382         String output=new String(bFile);
383         int start=output.indexOf("[");
384         return output.substring(start,output.length());
385
386     }
387 }
388