79741d55208ef08f853dd7644f9754e9c002eb8c
[appc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017-2018 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  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.appc.seqgen;
25
26 import com.att.eelf.configuration.EELFLogger;
27 import com.att.eelf.configuration.EELFManager;
28 import org.junit.Assert;
29 import org.junit.Test;
30 import org.onap.appc.seqgen.dgplugin.SequenceGeneratorPlugin;
31 import org.onap.appc.seqgen.dgplugin.impl.SequenceGeneratorPluginImpl;
32 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
33
34 import java.io.File;
35 import java.io.FileInputStream;
36 import java.io.IOException;
37 import java.net.URISyntaxException;
38 import java.util.HashMap;
39 import java.util.Map;
40
41 public class TestSequenceGeneratorPlugin {
42
43     private static final EELFLogger logger = EELFManager.getInstance().getLogger(TestSequenceGeneratorPlugin.class);
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         SequenceGeneratorPlugin plugin = new SequenceGeneratorPluginImpl();
54         plugin.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 = readInput("/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 = readInput("/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 testGenerateSequenceStopWithoutDep() throws  URISyntaxException,IOException{
284         String inputJSON = readInput("/input/stop-WithoutDep.json");
285
286         Map<String,String> params = new HashMap<>();
287         SvcLogicContext context = new SvcLogicContext();
288         context.setAttribute("inputJSON",inputJSON);
289
290         SequenceGeneratorPlugin plugin = new SequenceGeneratorPluginImpl();
291         plugin.generateSequence(params,context);
292
293         String outputJSON = context.getAttribute("output");
294         String actualOutput = readOutput("/output/stop-WithoutDep.json");
295
296         Assert.assertEquals(outputJSON.trim(),actualOutput.trim());
297     }
298
299     @Test
300     public void testGenerateSequenceVnfcNotPresentInInventory() throws URISyntaxException, IOException {
301         String inputJSON = readInput("/input/CheckVNfcInInventory.json");
302
303         Map<String,String> params = new HashMap<>();
304         SvcLogicContext context = new SvcLogicContext();
305         context.setAttribute("inputJSON",inputJSON);
306         SequenceGeneratorPlugin plugin = new SequenceGeneratorPluginImpl();
307         plugin.generateSequence(params,context);
308
309         String outputJSON = context.getAttribute("output");
310         String actualOutput = readOutput("/output/CheckVnfcInInventory.json");
311
312         Assert.assertEquals(outputJSON.trim(),actualOutput.trim());
313     }
314
315     @Test
316     public void testGenerateSequenceCheckMandatoryVnfc() throws URISyntaxException, IOException {
317         String inputJSON = readInput("/input/CheckMandatoryVnfc.json");
318
319         Map<String,String> params = new HashMap<>();
320         SvcLogicContext context = new SvcLogicContext();
321         context.setAttribute("inputJSON",inputJSON);
322
323         SequenceGeneratorPlugin plugin = new SequenceGeneratorPluginImpl();
324         plugin.generateSequence(params,context);
325
326         String errorCode = context.getAttribute("error-code");
327         String errorMessage = context.getAttribute("error-message");
328         logger.debug("errorCode = " + errorCode);
329         Assert.assertEquals(errorCode,"401");
330         Assert.assertEquals(errorMessage,"Error generating sequence VMs missing for the mandatory VNFC : [smp]");
331     }
332
333     @Test
334     public void testGenerateSequenceCheckMissingDependencyInfo() throws URISyntaxException, IOException {
335         String inputJSON = readInput("/input/MissingDependencyInfo.json");
336
337         Map<String,String> params = new HashMap<>();
338         SvcLogicContext context = new SvcLogicContext();
339         context.setAttribute("inputJSON",inputJSON);
340
341         SequenceGeneratorPlugin plugin = new SequenceGeneratorPluginImpl();
342         plugin.generateSequence(params,context);
343
344         String errorCode = context.getAttribute("error-code");
345         String errorMessage = context.getAttribute("error-message");
346         logger.debug("errorCode = " + errorCode);
347         Assert.assertEquals(errorCode,"401");
348         Assert.assertEquals(errorMessage,"Error generating sequence Dependency model is missing following vnfc type(s): [smp]");
349     }
350
351     @Test
352     public void testGenerateSequenceExtraVnfcInDependency() throws URISyntaxException, IOException {
353         String inputJSON = readInput("/input/WrongDependencyModel.json");
354
355         Map<String,String> params = new HashMap<>();
356         SvcLogicContext context = new SvcLogicContext();
357         context.setAttribute("inputJSON",inputJSON);
358
359         SequenceGeneratorPlugin plugin = new SequenceGeneratorPluginImpl();
360         plugin.generateSequence(params,context);
361
362         String errorCode = context.getAttribute("error-code");
363         String errorMessage = context.getAttribute("error-message");
364         logger.debug("errorCode = " + errorCode);
365         Assert.assertEquals(errorCode,"401");
366         Assert.assertEquals(errorMessage,"Error generating sequence Dependency model missing vnfc type SMP");
367     }
368
369     private String readInput(String inputFile) throws URISyntaxException, IOException {
370         File file = new File(this.getClass().getResource(inputFile).toURI());
371
372         byte[] bFile = new byte[(int) file.length()];
373         FileInputStream fileInputStream = new FileInputStream(file);
374         fileInputStream.read(bFile);
375         fileInputStream.close();
376         return new String(bFile);
377     }
378     private String readOutput(String outputFile) throws IOException,URISyntaxException {
379         File file = new File(this.getClass().getResource(outputFile).toURI());
380
381         byte[] bFile = new byte[(int) file.length()];
382         FileInputStream fileInputStream = new FileInputStream(file);
383         fileInputStream.read(bFile);
384         fileInputStream.close();
385         String output=new String(bFile);
386         int start=output.indexOf("[");
387         return output.substring(start,output.length());
388
389     }
390 }
391