2 * ============LICENSE_START=======================================================
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
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.
19 * ============LICENSE_END=========================================================
22 package org.onap.appc.seqgen;
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;
35 import java.io.FileInputStream;
36 import java.io.IOException;
37 import java.net.URISyntaxException;
38 import java.util.HashMap;
41 public class TestSequenceGeneratorPlugin {
43 private static final EELFLogger logger = EELFManager.getInstance().getLogger(TestSequenceGeneratorPlugin.class);
44 private SequenceGeneratorPlugin seqImpl;
47 public void testGenerateSequenceStart() throws URISyntaxException, IOException {
48 String inputJSON = readInput("/input/start.json");
50 Map<String,String> params = new HashMap<>();
51 SvcLogicContext context = new SvcLogicContext();
52 context.setAttribute("inputJSON",inputJSON);
54 seqImpl = new SequenceGeneratorPluginImpl();
55 seqImpl.generateSequence(params,context);
57 String outputJSON = context.getAttribute("output");
58 String actualOutput = readOutput("/output/Start2.json");
59 Assert.assertEquals(outputJSON.trim(),actualOutput.trim());
63 public void testGenerateSequenceWODependencyInfo()throws URISyntaxException, IOException {
64 String inputJSON = readInput("/input/start-withoutDependency.json");
66 Map<String,String> params = new HashMap<>();
67 SvcLogicContext context = new SvcLogicContext();
68 context.setAttribute("inputJSON",inputJSON);
70 SequenceGeneratorPlugin plugin = new SequenceGeneratorPluginImpl();
71 plugin.generateSequence(params,context);
73 String outputJSON = context.getAttribute("output");
74 String actualOutput = readOutput("/output/start-withoutDependency.json");
75 Assert.assertEquals(outputJSON.trim(),actualOutput.trim());
79 public void testGenerateSequenceSingleVM()throws URISyntaxException, IOException {
80 String inputJSON = readInput("/input/start-singleVM-.json");
82 Map<String,String> params = new HashMap<>();
83 SvcLogicContext context = new SvcLogicContext();
84 context.setAttribute("inputJSON",inputJSON);
86 SequenceGeneratorPlugin plugin = new SequenceGeneratorPluginImpl();
87 plugin.generateSequence(params,context);
89 String outputJSON = context.getAttribute("output");
90 String actualOutput = readOutput("/output/start-singleVM-.json");
91 Assert.assertEquals(outputJSON.trim(),actualOutput.trim());
95 public void testGenerateSequenceNoStrategy() throws URISyntaxException, IOException {
96 String inputJSON = readInput("/input/no-strategy.json");
98 Map<String,String> params = new HashMap<>();
99 SvcLogicContext context = new SvcLogicContext();
100 context.setAttribute("inputJSON",inputJSON);
102 SequenceGeneratorPlugin plugin = new SequenceGeneratorPluginImpl();
103 plugin.generateSequence(params,context);
105 String outputJSON = context.getAttribute("output");
106 String actualOutput = readOutput("/output/Start2.json");
108 Assert.assertEquals(outputJSON.trim(),actualOutput.trim());
112 public void testGenerateSequenceStop() throws URISyntaxException, IOException {
113 String inputJSON = readInput("/input/stop.json");
115 Map<String,String> params = new HashMap<>();
116 SvcLogicContext context = new SvcLogicContext();
117 context.setAttribute("inputJSON",inputJSON);
119 SequenceGeneratorPlugin plugin = new SequenceGeneratorPluginImpl();
120 plugin.generateSequence(params,context);
122 String outputJSON = context.getAttribute("output");
123 String actualOutput = readOutput("/output/Output-stop.json");
125 Assert.assertEquals(outputJSON.trim(),actualOutput.trim());
129 public void testGenerateSequenceWrongNumber() throws URISyntaxException, IOException {
130 String inputJSON = readInput("/input/wrongnumber.json");
132 Map<String,String> params = new HashMap<>();
133 SvcLogicContext context = new SvcLogicContext();
134 context.setAttribute("inputJSON",inputJSON);
136 SequenceGeneratorPlugin plugin = new SequenceGeneratorPluginImpl();
137 plugin.generateSequence(params,context);
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");
148 public void testGenerateSequenceCyclic() throws URISyntaxException, IOException {
149 String inputJSON = readInput("/input/cyclic.json");
151 Map<String,String> params = new HashMap<>();
152 SvcLogicContext context = new SvcLogicContext();
153 context.setAttribute("inputJSON",inputJSON);
155 SequenceGeneratorPlugin plugin = new SequenceGeneratorPluginImpl();
156 plugin.generateSequence(params,context);
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");
166 public void testGenerateSequenceWrongAction() throws URISyntaxException, IOException {
167 String inputJSON = readInput("/input/wrongaction.json");
169 Map<String,String> params = new HashMap<>();
170 SvcLogicContext context = new SvcLogicContext();
171 context.setAttribute("inputJSON",inputJSON);
173 SequenceGeneratorPlugin plugin = new SequenceGeneratorPluginImpl();
174 plugin.generateSequence(params,context);
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");
185 public void testGenerateSequenceMissingRequestInfo() throws URISyntaxException, IOException {
186 String inputJSON = readInput("/input/missingrequestinfo.json");
188 Map<String,String> params = new HashMap<>();
189 SvcLogicContext context = new SvcLogicContext();
190 context.setAttribute("inputJSON",inputJSON);
192 SequenceGeneratorPlugin plugin = new SequenceGeneratorPluginImpl();
193 plugin.generateSequence(params,context);
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");
203 public void testGenerateSequenceStopSingleVM() throws URISyntaxException, IOException{
204 String inputJSON = readInput("/input/stop-singleVM.json");
206 Map<String,String> params = new HashMap<>();
207 SvcLogicContext context = new SvcLogicContext();
208 context.setAttribute("inputJSON",inputJSON);
210 SequenceGeneratorPlugin plugin = new SequenceGeneratorPluginImpl();
211 plugin.generateSequence(params,context);
213 String outputJSON = context.getAttribute("output");
214 String actualOutput = readOutput("/output/stop-singleVM.json");
215 Assert.assertEquals(outputJSON.trim(),actualOutput.trim());
219 public void testGenerateSequenceStopSingleVmPerVnfc() throws URISyntaxException, IOException{
220 String inputJSON = readInput("/input/stop-singleVmPerVnfc.json");
222 Map<String,String> params = new HashMap<>();
223 SvcLogicContext context = new SvcLogicContext();
224 context.setAttribute("inputJSON",inputJSON);
226 SequenceGeneratorPlugin plugin = new SequenceGeneratorPluginImpl();
227 plugin.generateSequence(params,context);
229 String outputJSON = context.getAttribute("output");
230 String actualOutput = readOutput("/output/stop-singleVmPerVnfc.json");
231 Assert.assertEquals(outputJSON.trim(),actualOutput.trim());
235 public void testGenerateSequenceRestartNoDep() throws URISyntaxException, IOException {
236 String inputJSON = readInput("/input/restartNodep.json");
238 Map<String,String> params = new HashMap<>();
239 SvcLogicContext context = new SvcLogicContext();
240 context.setAttribute("inputJSON",inputJSON);
242 SequenceGeneratorPlugin plugin = new SequenceGeneratorPluginImpl();
243 plugin.generateSequence(params,context);
245 String outputJSON = context.getAttribute("output");
246 String actualOutput = readInput("/output/restart-NoDep.json");
248 Assert.assertEquals(outputJSON.trim(),actualOutput.trim());
252 public void testGenerateSequenceRestartNoDepSingleVM() throws URISyntaxException, IOException {
253 String inputJSON = readInput("/input/NoDep-SingleVM.json");
255 Map<String,String> params = new HashMap<>();
256 SvcLogicContext context = new SvcLogicContext();
257 context.setAttribute("inputJSON",inputJSON);
259 SequenceGeneratorPlugin plugin = new SequenceGeneratorPluginImpl();
260 plugin.generateSequence(params,context);
262 String outputJSON = context.getAttribute("output");
263 String actualOutput = readInput("/output/restart-Nodep-SingleVM.json");
264 Assert.assertEquals(outputJSON.trim(),actualOutput.trim());
268 public void testGenerateSequenceStartSingleVmPerVnfc() throws URISyntaxException, IOException{
269 String inputJSON = readInput("/input/start-singleVmPerVnfc-.json");
271 Map<String,String> params = new HashMap<>();
272 SvcLogicContext context = new SvcLogicContext();
273 context.setAttribute("inputJSON",inputJSON);
275 SequenceGeneratorPlugin plugin = new SequenceGeneratorPluginImpl();
276 plugin.generateSequence(params,context);
278 String outputJSON = context.getAttribute("output");
279 String actualOutput = readOutput("/output/start-singleVmPerVnfc.json");
280 Assert.assertEquals(outputJSON.trim(),actualOutput.trim());
284 public void testGenerateSequenceVnfcNotPresentInInventory() throws URISyntaxException, IOException {
285 String inputJSON = readInput("/input/CheckVNfcInInventory.json");
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);
293 String outputJSON = context.getAttribute("output");
294 String actualOutput = readOutput("/output/CheckVnfcInInventory.json");
296 Assert.assertEquals(outputJSON.trim(),actualOutput.trim());
300 public void testGenerateSequenceCheckMandatoryVnfc() throws URISyntaxException, IOException {
301 String inputJSON = readInput("/input/CheckMandatoryVnfc.json");
303 Map<String,String> params = new HashMap<>();
304 SvcLogicContext context = new SvcLogicContext();
305 context.setAttribute("inputJSON",inputJSON);
307 SequenceGeneratorPlugin plugin = new SequenceGeneratorPluginImpl();
308 plugin.generateSequence(params,context);
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]");
318 public void testGenerateSequenceCheckMissingDependencyInfo() throws URISyntaxException, IOException {
319 String inputJSON = readInput("/input/MissingDependencyInfo.json");
321 Map<String,String> params = new HashMap<>();
322 SvcLogicContext context = new SvcLogicContext();
323 context.setAttribute("inputJSON",inputJSON);
325 SequenceGeneratorPlugin plugin = new SequenceGeneratorPluginImpl();
326 plugin.generateSequence(params,context);
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]");
336 public void testGenerateSequenceExtraVnfcInDependency() throws URISyntaxException, IOException {
337 String inputJSON = readInput("/input/WrongDependencyModel.json");
339 Map<String,String> params = new HashMap<>();
340 SvcLogicContext context = new SvcLogicContext();
341 context.setAttribute("inputJSON",inputJSON);
343 SequenceGeneratorPlugin plugin = new SequenceGeneratorPluginImpl();
344 plugin.generateSequence(params,context);
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");
353 private String readInput(String inputFile) throws URISyntaxException, IOException {
354 File file = new File(this.getClass().getResource(inputFile).toURI());
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);
362 private String readOutput(String outputFile) throws IOException,URISyntaxException {
363 File file = new File(this.getClass().getResource(outputFile).toURI());
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());