addd81a4b7539f686a3f2ae8d8741f09048f1224
[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-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 com.att.eelf.configuration.EELFLogger;
25 import com.att.eelf.configuration.EELFManager;
26 import org.junit.Assert;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.onap.appc.seqgen.dbservices.SequenceGeneratorDBServices;
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 import static org.mockito.Mockito.mock;
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     private SequenceGeneratorPlugin seqImpl;
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         seqImpl = new SequenceGeneratorPluginImpl();
55         seqImpl.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 testGenerateSequenceVnfcNotPresentInInventory() throws URISyntaxException, IOException {
285         String inputJSON = readInput("/input/CheckVNfcInInventory.json");
286
287         Map<String,String> params = new HashMap<>();
288         SvcLogicContext context = new SvcLogicContext();
289         context.setAttribute("inputJSON",inputJSON);
290         SequenceGeneratorPlugin plugin = new SequenceGeneratorPluginImpl();
291         plugin.generateSequence(params,context);
292
293         String outputJSON = context.getAttribute("output");
294         String actualOutput = readOutput("/output/CheckVnfcInInventory.json");
295
296         Assert.assertEquals(outputJSON.trim(),actualOutput.trim());
297     }
298
299     @Test
300     public void testGenerateSequenceCheckMandatoryVnfc() throws URISyntaxException, IOException {
301         String inputJSON = readInput("/input/CheckMandatoryVnfc.json");
302
303         Map<String,String> params = new HashMap<>();
304         SvcLogicContext context = new SvcLogicContext();
305         context.setAttribute("inputJSON",inputJSON);
306
307         SequenceGeneratorPlugin plugin = new SequenceGeneratorPluginImpl();
308         plugin.generateSequence(params,context);
309
310         String errorCode = context.getAttribute("error-code");
311         String errorMessage = context.getAttribute("error-message");
312         logger.debug("errorCode = " + errorCode);
313         Assert.assertEquals(errorCode,"401");
314         Assert.assertEquals(errorMessage,"Error generating sequence VMs missing for the mandatory VNFC : [smp]");
315     }
316
317     @Test
318     public void testGenerateSequenceCheckMissingDependencyInfo() throws URISyntaxException, IOException {
319         String inputJSON = readInput("/input/MissingDependencyInfo.json");
320
321         Map<String,String> params = new HashMap<>();
322         SvcLogicContext context = new SvcLogicContext();
323         context.setAttribute("inputJSON",inputJSON);
324
325         SequenceGeneratorPlugin plugin = new SequenceGeneratorPluginImpl();
326         plugin.generateSequence(params,context);
327
328         String errorCode = context.getAttribute("error-code");
329         String errorMessage = context.getAttribute("error-message");
330         logger.debug("errorCode = " + errorCode);
331         Assert.assertEquals(errorCode,"401");
332         Assert.assertEquals(errorMessage,"Error generating sequence Dependency model is missing following vnfc type(s): [smp]");
333     }
334
335     @Test
336     public void testGenerateSequenceExtraVnfcInDependency() throws URISyntaxException, IOException {
337         String inputJSON = readInput("/input/WrongDependencyModel.json");
338
339         Map<String,String> params = new HashMap<>();
340         SvcLogicContext context = new SvcLogicContext();
341         context.setAttribute("inputJSON",inputJSON);
342
343         SequenceGeneratorPlugin plugin = new SequenceGeneratorPluginImpl();
344         plugin.generateSequence(params,context);
345
346         String errorCode = context.getAttribute("error-code");
347         String errorMessage = context.getAttribute("error-message");
348         logger.debug("errorCode = " + errorCode);
349         Assert.assertEquals(errorCode,"401");
350         Assert.assertEquals(errorMessage,"Error generating sequence Dependency model missing vnfc type SMP");
351     }
352
353     private String readInput(String inputFile) throws URISyntaxException, IOException {
354         File file = new File(this.getClass().getResource(inputFile).toURI());
355
356         byte[] bFile = new byte[(int) file.length()];
357         FileInputStream fileInputStream = new FileInputStream(file);
358         fileInputStream.read(bFile);
359         fileInputStream.close();
360         return new String(bFile);
361     }
362     private String readOutput(String outputFile) throws IOException,URISyntaxException {
363         File file = new File(this.getClass().getResource(outputFile).toURI());
364
365         byte[] bFile = new byte[(int) file.length()];
366         FileInputStream fileInputStream = new FileInputStream(file);
367         fileInputStream.read(bFile);
368         fileInputStream.close();
369         String output=new String(bFile);
370         int start=output.indexOf("[");
371         return output.substring(start,output.length());
372
373     }
374 }
375