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.openecomp.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.openecomp.appc.seqgen.dgplugin.SequenceGeneratorPlugin;
28 import org.openecomp.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());
61 public void testGenerateSequenceSingleVM()throws URISyntaxException, IOException {
62 String inputJSON = readInput("/input/start-single-vm.json");
64 Map<String,String> params = new HashMap<>();
65 SvcLogicContext context = new SvcLogicContext();
66 context.setAttribute("inputJSON",inputJSON);
68 SequenceGeneratorPlugin plugin = new SequenceGeneratorPluginImpl();
69 plugin.generateSequence(params,context);
71 String outputJSON = context.getAttribute("output");
72 String actualOutput = readOutput("/output/start-single-vm.json");
73 Assert.assertEquals(outputJSON.trim(),actualOutput.trim());
77 public void testGenerateSequenceNoStrategy() throws URISyntaxException, IOException {
78 String inputJSON = readInput("/input/no-strategy.json");
80 Map<String,String> params = new HashMap<>();
81 SvcLogicContext context = new SvcLogicContext();
82 context.setAttribute("inputJSON",inputJSON);
84 SequenceGeneratorPlugin plugin = new SequenceGeneratorPluginImpl();
85 plugin.generateSequence(params,context);
87 String outputJSON = context.getAttribute("output");
88 String actualOutput = readOutput("/output/start.json");
90 Assert.assertEquals(outputJSON.trim(),actualOutput.trim());
94 public void testGenerateSequenceStop() throws URISyntaxException, IOException {
95 String inputJSON = readInput("/input/stop.json");
97 Map<String,String> params = new HashMap<>();
98 SvcLogicContext context = new SvcLogicContext();
99 context.setAttribute("inputJSON",inputJSON);
101 SequenceGeneratorPlugin plugin = new SequenceGeneratorPluginImpl();
102 plugin.generateSequence(params,context);
104 String outputJSON = context.getAttribute("output");
105 String actualOutput = readOutput("/output/stop.json");
107 Assert.assertEquals(outputJSON.trim(),actualOutput.trim());
111 public void testGenerateSequenceWrongNumber() throws URISyntaxException, IOException {
112 String inputJSON = readInput("/input/wrongnumber.json");
114 Map<String,String> params = new HashMap<>();
115 SvcLogicContext context = new SvcLogicContext();
116 context.setAttribute("inputJSON",inputJSON);
118 SequenceGeneratorPlugin plugin = new SequenceGeneratorPluginImpl();
119 plugin.generateSequence(params,context);
121 String errorCode = context.getAttribute("error-code");
122 String errorMessage = context.getAttribute("error-message");
123 logger.debug("errorCode = " + errorCode);
124 Assert.assertEquals(errorCode,"401");
125 Assert.assertEquals(errorMessage,"Error generating sequence Invalid Number for Wait Time 6a");
130 public void testGenerateSequenceCyclic() throws URISyntaxException, IOException {
131 String inputJSON = readInput("/input/cyclic.json");
133 Map<String,String> params = new HashMap<>();
134 SvcLogicContext context = new SvcLogicContext();
135 context.setAttribute("inputJSON",inputJSON);
137 SequenceGeneratorPlugin plugin = new SequenceGeneratorPluginImpl();
138 plugin.generateSequence(params,context);
140 String errorCode = context.getAttribute("error-code");
141 String errorMessage = context.getAttribute("error-message");
142 logger.debug("errorCode = " + errorCode);
143 Assert.assertEquals(errorCode,"401");
144 Assert.assertEquals(errorMessage,"Error generating sequence There seems to be no Root/Independent node for Vnfc dependencies");
149 public void testGenerateSequenceWrongAction() throws URISyntaxException, IOException {
150 String inputJSON = readInput("/input/wrongaction.json");
152 Map<String,String> params = new HashMap<>();
153 SvcLogicContext context = new SvcLogicContext();
154 context.setAttribute("inputJSON",inputJSON);
156 SequenceGeneratorPlugin plugin = new SequenceGeneratorPluginImpl();
157 plugin.generateSequence(params,context);
159 String errorCode = context.getAttribute("error-code");
160 String errorMessage = context.getAttribute("error-message");
161 logger.debug("errorCode = " + errorCode);
162 Assert.assertEquals(errorCode,"401");
163 Assert.assertEquals(errorMessage,"Error generating sequence Invalid Action start");
168 public void testGenerateSequenceMissingRequestInfo() throws URISyntaxException, IOException {
169 String inputJSON = readInput("/input/missingrequestinfo.json");
171 Map<String,String> params = new HashMap<>();
172 SvcLogicContext context = new SvcLogicContext();
173 context.setAttribute("inputJSON",inputJSON);
175 SequenceGeneratorPlugin plugin = new SequenceGeneratorPluginImpl();
176 plugin.generateSequence(params,context);
178 String errorCode = context.getAttribute("error-code");
179 String errorMessage = context.getAttribute("error-message");
180 logger.debug("errorCode = " + errorCode);
181 Assert.assertEquals(errorCode,"401");
182 Assert.assertEquals(errorMessage,"Error generating sequence Request info is not provided in the input");
186 public void testGenerateSequenceStopSingleVM() throws URISyntaxException, IOException{
187 String inputJSON = readInput("/input/stop-single-vm.json");
189 Map<String,String> params = new HashMap<>();
190 SvcLogicContext context = new SvcLogicContext();
191 context.setAttribute("inputJSON",inputJSON);
193 SequenceGeneratorPlugin plugin = new SequenceGeneratorPluginImpl();
194 plugin.generateSequence(params,context);
196 String outputJSON = context.getAttribute("output");
197 String actualOutput = readOutput("/output/stop-single-vm.json");
198 Assert.assertEquals(outputJSON.trim(),actualOutput.trim());
202 public void testGenerateSequenceStopSingleVmPerVnfc() throws URISyntaxException, IOException{
203 String inputJSON = readInput("/input/stop-single-vm-per-vnfc.json");
205 Map<String,String> params = new HashMap<>();
206 SvcLogicContext context = new SvcLogicContext();
207 context.setAttribute("inputJSON",inputJSON);
209 SequenceGeneratorPlugin plugin = new SequenceGeneratorPluginImpl();
210 plugin.generateSequence(params,context);
212 String outputJSON = context.getAttribute("output");
213 String actualOutput = readOutput("/output/stop-single-vm-per-vnfc.json");
214 Assert.assertEquals(outputJSON.trim(),actualOutput.trim());
218 public void testGenerateSequenceStartSingleVmPerVnfc() throws URISyntaxException, IOException{
219 String inputJSON = readInput("/input/start-single-vm-per-vnfc.json");
221 Map<String,String> params = new HashMap<>();
222 SvcLogicContext context = new SvcLogicContext();
223 context.setAttribute("inputJSON",inputJSON);
225 SequenceGeneratorPlugin plugin = new SequenceGeneratorPluginImpl();
226 plugin.generateSequence(params,context);
228 String outputJSON = context.getAttribute("output");
229 String actualOutput = readOutput("/output/start-single-vm-per-vnfc.json");
230 Assert.assertEquals(outputJSON.trim(),actualOutput.trim());
233 private String readInput(String inputFile) throws URISyntaxException, IOException {
234 File file = new File(this.getClass().getResource(inputFile).toURI());
236 byte[] bFile = new byte[(int) file.length()];
237 FileInputStream fileInputStream = new FileInputStream(file);
238 fileInputStream.read(bFile);
239 fileInputStream.close();
240 return new String(bFile);
242 private String readOutput(String outputFile) throws IOException,URISyntaxException {
243 File file = new File(this.getClass().getResource(outputFile).toURI());
245 byte[] bFile = new byte[(int) file.length()];
246 FileInputStream fileInputStream = new FileInputStream(file);
247 fileInputStream.read(bFile);
248 fileInputStream.close();
250 String output=new String(bFile);
251 int start=output.indexOf("[");
252 int last=output.length()-1;
253 return output.substring(start,last);