validate mandatry vnfc vm info is absent in invent
[appc.git] / appc-sequence-generator / appc-sequence-generator-bundle / src / test / java / org / openecomp / appc / seqgen / TestSequenceGeneratorPlugin.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APP-C
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
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  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.appc.seqgen;
22
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;
30
31 import java.io.File;
32 import java.io.FileInputStream;
33 import java.io.IOException;
34 import java.net.URISyntaxException;
35 import java.util.HashMap;
36 import java.util.Map;
37
38 public class TestSequenceGeneratorPlugin {
39
40     private static final EELFLogger logger = EELFManager.getInstance().getLogger(TestSequenceGeneratorPlugin.class);
41
42     @Test
43     public void testGenerateSequenceStart() throws URISyntaxException, IOException {
44         String inputJSON = readInput("/input/start.json");
45
46         Map<String,String> params = new HashMap<>();
47         SvcLogicContext context = new SvcLogicContext();
48         context.setAttribute("inputJSON",inputJSON);
49
50         SequenceGeneratorPlugin plugin = new SequenceGeneratorPluginImpl();
51         plugin.generateSequence(params,context);
52
53         String outputJSON = context.getAttribute("output");
54         String actualOutput = readOutput("/output/start.json");
55         Assert.assertEquals(outputJSON.trim(),actualOutput.trim());
56     }
57
58
59
60     @Test
61     public void testGenerateSequenceSingleVM()throws URISyntaxException, IOException {
62         String inputJSON = readInput("/input/start-single-vm.json");
63
64         Map<String,String> params = new HashMap<>();
65         SvcLogicContext context = new SvcLogicContext();
66         context.setAttribute("inputJSON",inputJSON);
67
68         SequenceGeneratorPlugin plugin = new SequenceGeneratorPluginImpl();
69         plugin.generateSequence(params,context);
70
71         String outputJSON = context.getAttribute("output");
72         String actualOutput = readOutput("/output/start-single-vm.json");
73         Assert.assertEquals(outputJSON.trim(),actualOutput.trim());
74     }
75
76     @Test
77     public void testGenerateSequenceNoStrategy() throws URISyntaxException, IOException {
78         String inputJSON = readInput("/input/no-strategy.json");
79
80         Map<String,String> params = new HashMap<>();
81         SvcLogicContext context = new SvcLogicContext();
82         context.setAttribute("inputJSON",inputJSON);
83
84         SequenceGeneratorPlugin plugin = new SequenceGeneratorPluginImpl();
85         plugin.generateSequence(params,context);
86
87         String outputJSON = context.getAttribute("output");
88         String actualOutput = readOutput("/output/start.json");
89
90         Assert.assertEquals(outputJSON.trim(),actualOutput.trim());
91     }
92
93     @Test
94     public void testGenerateSequenceStop() throws URISyntaxException, IOException {
95         String inputJSON = readInput("/input/stop.json");
96
97         Map<String,String> params = new HashMap<>();
98         SvcLogicContext context = new SvcLogicContext();
99         context.setAttribute("inputJSON",inputJSON);
100
101         SequenceGeneratorPlugin plugin = new SequenceGeneratorPluginImpl();
102         plugin.generateSequence(params,context);
103
104         String outputJSON = context.getAttribute("output");
105         String actualOutput = readOutput("/output/stop.json");
106
107         Assert.assertEquals(outputJSON.trim(),actualOutput.trim());
108     }
109
110     @Test
111     public void testGenerateSequenceWrongNumber() throws URISyntaxException, IOException {
112         String inputJSON = readInput("/input/wrongnumber.json");
113
114         Map<String,String> params = new HashMap<>();
115         SvcLogicContext context = new SvcLogicContext();
116         context.setAttribute("inputJSON",inputJSON);
117
118         SequenceGeneratorPlugin plugin = new SequenceGeneratorPluginImpl();
119         plugin.generateSequence(params,context);
120
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");
126     }
127
128
129     @Test
130     public void testGenerateSequenceCyclic() throws URISyntaxException, IOException {
131         String inputJSON = readInput("/input/cyclic.json");
132
133         Map<String,String> params = new HashMap<>();
134         SvcLogicContext context = new SvcLogicContext();
135         context.setAttribute("inputJSON",inputJSON);
136
137         SequenceGeneratorPlugin plugin = new SequenceGeneratorPluginImpl();
138         plugin.generateSequence(params,context);
139
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");
145     }
146
147
148     @Test
149     public void testGenerateSequenceWrongAction() throws URISyntaxException, IOException {
150         String inputJSON = readInput("/input/wrongaction.json");
151
152         Map<String,String> params = new HashMap<>();
153         SvcLogicContext context = new SvcLogicContext();
154         context.setAttribute("inputJSON",inputJSON);
155
156         SequenceGeneratorPlugin plugin = new SequenceGeneratorPluginImpl();
157         plugin.generateSequence(params,context);
158
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");
164     }
165
166
167     @Test
168     public void testGenerateSequenceMissingRequestInfo() throws URISyntaxException, IOException {
169         String inputJSON = readInput("/input/missingrequestinfo.json");
170
171         Map<String,String> params = new HashMap<>();
172         SvcLogicContext context = new SvcLogicContext();
173         context.setAttribute("inputJSON",inputJSON);
174
175         SequenceGeneratorPlugin plugin = new SequenceGeneratorPluginImpl();
176         plugin.generateSequence(params,context);
177
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");
183     }
184
185     @Test
186     public void testGenerateSequenceStopSingleVM() throws URISyntaxException, IOException{
187         String inputJSON = readInput("/input/stop-single-vm.json");
188
189         Map<String,String> params = new HashMap<>();
190         SvcLogicContext context = new SvcLogicContext();
191         context.setAttribute("inputJSON",inputJSON);
192
193         SequenceGeneratorPlugin plugin = new SequenceGeneratorPluginImpl();
194         plugin.generateSequence(params,context);
195
196         String outputJSON = context.getAttribute("output");
197         String actualOutput = readOutput("/output/stop-single-vm.json");
198         Assert.assertEquals(outputJSON.trim(),actualOutput.trim());
199     }
200
201     @Test
202     public void testGenerateSequenceStopSingleVmPerVnfc() throws URISyntaxException, IOException{
203         String inputJSON = readInput("/input/stop-single-vm-per-vnfc.json");
204
205         Map<String,String> params = new HashMap<>();
206         SvcLogicContext context = new SvcLogicContext();
207         context.setAttribute("inputJSON",inputJSON);
208
209         SequenceGeneratorPlugin plugin = new SequenceGeneratorPluginImpl();
210         plugin.generateSequence(params,context);
211
212         String outputJSON = context.getAttribute("output");
213         String actualOutput = readOutput("/output/stop-single-vm-per-vnfc.json");
214         Assert.assertEquals(outputJSON.trim(),actualOutput.trim());
215     }
216
217     @Test
218     public void testGenerateSequenceStartSingleVmPerVnfc() throws URISyntaxException, IOException{
219         String inputJSON = readInput("/input/start-single-vm-per-vnfc.json");
220
221         Map<String,String> params = new HashMap<>();
222         SvcLogicContext context = new SvcLogicContext();
223         context.setAttribute("inputJSON",inputJSON);
224
225         SequenceGeneratorPlugin plugin = new SequenceGeneratorPluginImpl();
226         plugin.generateSequence(params,context);
227
228         String outputJSON = context.getAttribute("output");
229         String actualOutput = readOutput("/output/start-single-vm-per-vnfc.json");
230         Assert.assertEquals(outputJSON.trim(),actualOutput.trim());
231     }
232
233     @Test
234     public void testGenerateSequenceVnfcNotPresentInInventory() throws URISyntaxException, IOException {
235         String inputJSON = readInput("/input/CheckVNfcInInventory.json");
236
237         Map<String,String> params = new HashMap<>();
238         SvcLogicContext context = new SvcLogicContext();
239         context.setAttribute("inputJSON",inputJSON);
240         SequenceGeneratorPlugin plugin = new SequenceGeneratorPluginImpl();
241         plugin.generateSequence(params,context);
242
243         String outputJSON = context.getAttribute("output");
244         String actualOutput = readOutput("/output/CheckVnfcInInventory.json");
245
246         Assert.assertEquals(outputJSON.trim(),actualOutput.trim());
247     }
248
249     private String readInput(String inputFile) throws URISyntaxException, IOException {
250         File file = new File(this.getClass().getResource(inputFile).toURI());
251
252         byte[] bFile = new byte[(int) file.length()];
253         FileInputStream fileInputStream = new FileInputStream(file);
254         fileInputStream.read(bFile);
255         fileInputStream.close();
256         return new String(bFile);
257     }
258     private String readOutput(String outputFile) throws IOException,URISyntaxException {
259         File file = new File(this.getClass().getResource(outputFile).toURI());
260
261         byte[] bFile = new byte[(int) file.length()];
262         FileInputStream fileInputStream = new FileInputStream(file);
263         fileInputStream.read(bFile);
264         fileInputStream.close();
265
266         String output=new String(bFile);
267         int start=output.indexOf("[");
268         int last=output.length();
269         return output.substring(start,last);
270
271     }
272 }