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