2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.appc.seqgen;
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;
32 import java.io.FileInputStream;
33 import java.io.IOException;
34 import java.net.URISyntaxException;
35 import java.util.HashMap;
38 public class TestSequenceGeneratorPlugin {
40 private static final EELFLogger logger = EELFManager.getInstance().getLogger(TestSequenceGeneratorPlugin.class);
43 public void testGenerateSequenceStart() throws URISyntaxException, IOException {
44 String inputJSON = readInput("/input/start.json");
46 Map<String,String> params = new HashMap<>();
47 SvcLogicContext context = new SvcLogicContext();
48 context.setAttribute("inputJSON",inputJSON);
50 SequenceGeneratorPlugin plugin = new SequenceGeneratorPluginImpl();
51 plugin.generateSequence(params,context);
53 String outputJSON = context.getAttribute("output");
54 String actualOutput = readOutput("/output/Start.json");
55 Assert.assertEquals(outputJSON.trim(),actualOutput.trim());
59 public void testGenerateSequenceWODependencyInfo()throws URISyntaxException, IOException {
60 String inputJSON = readInput("/input/start-withoutDependency.json");
62 Map<String,String> params = new HashMap<>();
63 SvcLogicContext context = new SvcLogicContext();
64 context.setAttribute("inputJSON",inputJSON);
66 SequenceGeneratorPlugin plugin = new SequenceGeneratorPluginImpl();
67 plugin.generateSequence(params,context);
69 String outputJSON = context.getAttribute("output");
70 String actualOutput = readOutput("/output/start-withoutDependency.json");
71 Assert.assertEquals(outputJSON.trim(),actualOutput.trim());
75 public void testGenerateSequenceSingleVM()throws URISyntaxException, IOException {
76 String inputJSON = readInput("/input/start-singleVM-.json");
78 Map<String,String> params = new HashMap<>();
79 SvcLogicContext context = new SvcLogicContext();
80 context.setAttribute("inputJSON",inputJSON);
82 SequenceGeneratorPlugin plugin = new SequenceGeneratorPluginImpl();
83 plugin.generateSequence(params,context);
85 String outputJSON = context.getAttribute("output");
86 String actualOutput = readOutput("/output/start-singleVM-.json");
87 Assert.assertEquals(outputJSON.trim(),actualOutput.trim());
91 public void testGenerateSequenceNoStrategy() throws URISyntaxException, IOException {
92 String inputJSON = readInput("/input/no-strategy.json");
94 Map<String,String> params = new HashMap<>();
95 SvcLogicContext context = new SvcLogicContext();
96 context.setAttribute("inputJSON",inputJSON);
98 SequenceGeneratorPlugin plugin = new SequenceGeneratorPluginImpl();
99 plugin.generateSequence(params,context);
101 String outputJSON = context.getAttribute("output");
102 String actualOutput = readOutput("/output/Start.json");
104 Assert.assertEquals(outputJSON.trim(),actualOutput.trim());
108 public void testGenerateSequenceStop() throws URISyntaxException, IOException {
109 String inputJSON = readInput("/input/stop.json");
111 Map<String,String> params = new HashMap<>();
112 SvcLogicContext context = new SvcLogicContext();
113 context.setAttribute("inputJSON",inputJSON);
115 SequenceGeneratorPlugin plugin = new SequenceGeneratorPluginImpl();
116 plugin.generateSequence(params,context);
118 String outputJSON = context.getAttribute("output");
119 String actualOutput = readOutput("/output/Output-stop.json");
121 Assert.assertEquals(outputJSON.trim(),actualOutput.trim());
125 public void testGenerateSequenceWrongNumber() throws URISyntaxException, IOException {
126 String inputJSON = readInput("/input/wrongnumber.json");
128 Map<String,String> params = new HashMap<>();
129 SvcLogicContext context = new SvcLogicContext();
130 context.setAttribute("inputJSON",inputJSON);
132 SequenceGeneratorPlugin plugin = new SequenceGeneratorPluginImpl();
133 plugin.generateSequence(params,context);
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");
144 public void testGenerateSequenceCyclic() throws URISyntaxException, IOException {
145 String inputJSON = readInput("/input/cyclic.json");
147 Map<String,String> params = new HashMap<>();
148 SvcLogicContext context = new SvcLogicContext();
149 context.setAttribute("inputJSON",inputJSON);
151 SequenceGeneratorPlugin plugin = new SequenceGeneratorPluginImpl();
152 plugin.generateSequence(params,context);
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");
162 public void testGenerateSequenceWrongAction() throws URISyntaxException, IOException {
163 String inputJSON = readInput("/input/wrongaction.json");
165 Map<String,String> params = new HashMap<>();
166 SvcLogicContext context = new SvcLogicContext();
167 context.setAttribute("inputJSON",inputJSON);
169 SequenceGeneratorPlugin plugin = new SequenceGeneratorPluginImpl();
170 plugin.generateSequence(params,context);
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");
181 public void testGenerateSequenceMissingRequestInfo() throws URISyntaxException, IOException {
182 String inputJSON = readInput("/input/missingrequestinfo.json");
184 Map<String,String> params = new HashMap<>();
185 SvcLogicContext context = new SvcLogicContext();
186 context.setAttribute("inputJSON",inputJSON);
188 SequenceGeneratorPlugin plugin = new SequenceGeneratorPluginImpl();
189 plugin.generateSequence(params,context);
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");
199 public void testGenerateSequenceStopSingleVM() throws URISyntaxException, IOException{
200 String inputJSON = readInput("/input/stop-singleVM.json");
202 Map<String,String> params = new HashMap<>();
203 SvcLogicContext context = new SvcLogicContext();
204 context.setAttribute("inputJSON",inputJSON);
206 SequenceGeneratorPlugin plugin = new SequenceGeneratorPluginImpl();
207 plugin.generateSequence(params,context);
209 String outputJSON = context.getAttribute("output");
210 String actualOutput = readOutput("/output/stop-singleVM.json");
211 Assert.assertEquals(outputJSON.trim(),actualOutput.trim());
215 public void testGenerateSequenceStopSingleVmPerVnfc() throws URISyntaxException, IOException{
216 String inputJSON = readInput("/input/stop-singleVmPerVnfc.json");
218 Map<String,String> params = new HashMap<>();
219 SvcLogicContext context = new SvcLogicContext();
220 context.setAttribute("inputJSON",inputJSON);
222 SequenceGeneratorPlugin plugin = new SequenceGeneratorPluginImpl();
223 plugin.generateSequence(params,context);
225 String outputJSON = context.getAttribute("output");
226 String actualOutput = readOutput("/output/stop-singleVmPerVnfc.json");
227 Assert.assertEquals(outputJSON.trim(),actualOutput.trim());
231 public void testGenerateSequenceRestartNoDep() throws URISyntaxException, IOException {
232 String inputJSON = readInput("/input/restartNodep.json");
234 Map<String,String> params = new HashMap<>();
235 SvcLogicContext context = new SvcLogicContext();
236 context.setAttribute("inputJSON",inputJSON);
238 SequenceGeneratorPlugin plugin = new SequenceGeneratorPluginImpl();
239 plugin.generateSequence(params,context);
241 String outputJSON = context.getAttribute("output");
242 String actualOutput = readInput("/output/restart-NoDep.json");
244 Assert.assertEquals(outputJSON.trim(),actualOutput.trim());
248 public void testGenerateSequenceRestartNoDepSingleVM() throws URISyntaxException, IOException {
249 String inputJSON = readInput("/input/NoDep-SingleVM.json");
251 Map<String,String> params = new HashMap<>();
252 SvcLogicContext context = new SvcLogicContext();
253 context.setAttribute("inputJSON",inputJSON);
255 SequenceGeneratorPlugin plugin = new SequenceGeneratorPluginImpl();
256 plugin.generateSequence(params,context);
258 String outputJSON = context.getAttribute("output");
259 String actualOutput = readInput("/output/restart-Nodep-SingleVM.json");
260 Assert.assertEquals(outputJSON.trim(),actualOutput.trim());
264 public void testGenerateSequenceStartSingleVmPerVnfc() throws URISyntaxException, IOException{
265 String inputJSON = readInput("/input/start-singleVmPerVnfc-.json");
267 Map<String,String> params = new HashMap<>();
268 SvcLogicContext context = new SvcLogicContext();
269 context.setAttribute("inputJSON",inputJSON);
271 SequenceGeneratorPlugin plugin = new SequenceGeneratorPluginImpl();
272 plugin.generateSequence(params,context);
274 String outputJSON = context.getAttribute("output");
275 String actualOutput = readOutput("/output/start-singleVmPerVnfc.json");
276 Assert.assertEquals(outputJSON.trim(),actualOutput.trim());
280 public void testGenerateSequenceStopWithoutDep() throws URISyntaxException,IOException{
281 String inputJSON = readInput("/input/stop-WithoutDep.json");
283 Map<String,String> params = new HashMap<>();
284 SvcLogicContext context = new SvcLogicContext();
285 context.setAttribute("inputJSON",inputJSON);
287 SequenceGeneratorPlugin plugin = new SequenceGeneratorPluginImpl();
288 plugin.generateSequence(params,context);
290 String outputJSON = context.getAttribute("output");
291 String actualOutput = readOutput("/output/stop-WithoutDep.json");
293 Assert.assertEquals(outputJSON.trim(),actualOutput.trim());
297 public void testGenerateSequenceVnfcNotPresentInInventory() throws URISyntaxException, IOException {
298 String inputJSON = readInput("/input/CheckVNfcInInventory.json");
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);
306 String outputJSON = context.getAttribute("output");
307 String actualOutput = readOutput("/output/CheckVnfcInInventory.json");
309 Assert.assertEquals(outputJSON.trim(),actualOutput.trim());
313 public void testGenerateSequenceCheckMandatoryVnfc() throws URISyntaxException, IOException {
314 String inputJSON = readInput("/input/CheckMandatoryVnfc.json");
316 Map<String,String> params = new HashMap<>();
317 SvcLogicContext context = new SvcLogicContext();
318 context.setAttribute("inputJSON",inputJSON);
320 SequenceGeneratorPlugin plugin = new SequenceGeneratorPluginImpl();
321 plugin.generateSequence(params,context);
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]");
331 public void testGenerateSequenceCheckMissingDependencyInfo() throws URISyntaxException, IOException {
332 String inputJSON = readInput("/input/MissingDependencyInfo.json");
334 Map<String,String> params = new HashMap<>();
335 SvcLogicContext context = new SvcLogicContext();
336 context.setAttribute("inputJSON",inputJSON);
338 SequenceGeneratorPlugin plugin = new SequenceGeneratorPluginImpl();
339 plugin.generateSequence(params,context);
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]");
349 public void testGenerateSequenceExtraVnfcInDependency() throws URISyntaxException, IOException {
350 String inputJSON = readInput("/input/WrongDependencyModel.json");
352 Map<String,String> params = new HashMap<>();
353 SvcLogicContext context = new SvcLogicContext();
354 context.setAttribute("inputJSON",inputJSON);
356 SequenceGeneratorPlugin plugin = new SequenceGeneratorPluginImpl();
357 plugin.generateSequence(params,context);
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");
366 private String readInput(String inputFile) throws URISyntaxException, IOException {
367 File file = new File(this.getClass().getResource(inputFile).toURI());
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);
375 private String readOutput(String outputFile) throws IOException,URISyntaxException {
376 File file = new File(this.getClass().getResource(outputFile).toURI());
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());