acd76a151be2227ad00673b5a2537097c59f427c
[sdc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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.sdc.vendorsoftwareproduct.services.schemagenerator;
22
23 import org.everit.json.schema.EmptySchema;
24 import org.everit.json.schema.loader.SchemaLoader;
25 import org.json.JSONObject;
26 import org.openecomp.core.utilities.json.JsonUtil;
27 import org.openecomp.sdc.vendorsoftwareproduct.services.schemagenerator.SchemaGenerator;
28 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.ComponentData;
29 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionEntityType;
30 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.Network;
31 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.NetworkType;
32 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.Nic;
33 import org.openecomp.sdc.vendorsoftwareproduct.types.schemagenerator.ComponentCompositionSchemaInput;
34 import org.openecomp.sdc.vendorsoftwareproduct.types.schemagenerator.ComponentQuestionnaireSchemaInput;
35 import org.openecomp.sdc.vendorsoftwareproduct.types.schemagenerator.NetworkCompositionSchemaInput;
36 import org.openecomp.sdc.vendorsoftwareproduct.types.schemagenerator.NicCompositionSchemaInput;
37 import org.openecomp.sdc.vendorsoftwareproduct.types.schemagenerator.SchemaTemplateContext;
38 import org.testng.Assert;
39 import org.testng.annotations.Test;
40
41 import java.util.Arrays;
42 import java.util.Map;
43
44 public class SchemaGeneratorTest {
45   /*
46
47   private static int getMinOfVmMax(JSONObject schemaJson) {
48     return schemaJson.getJSONObject("properties").getJSONObject("compute")
49         .getJSONObject("properties").getJSONObject("numOfVMs").getJSONObject("properties")
50         .getJSONObject("maximum").getInt("minimum");
51   }
52
53   private static JSONObject validateSchema(String schema) {
54     System.out.println(schema);
55     Assert.assertNotNull(schema);
56     Assert.assertTrue(JsonUtil.isValidJson(schema));
57     JSONObject schemaJson = new JSONObject(schema);
58     Assert.assertFalse(SchemaLoader.load(schemaJson) instanceof EmptySchema);
59     return schemaJson;
60   }
61
62   // TODO: 3/15/2017 fix and enable   //@Test
63   public void testGenerateVspQuestionnaire() {
64     String schema = SchemaGenerator
65         .generate(SchemaTemplateContext.questionnaire, CompositionEntityType.vsp, null);
66     validateSchema(schema);
67   }
68
69   @Test
70   public void testGenerateNetworkCompositionUpload() {
71     Network network = new Network();
72     network.setName("upload network1 name");
73     network.setDhcp(true);
74
75     NetworkCompositionSchemaInput input = new NetworkCompositionSchemaInput();
76     input.setManual(false);
77     input.setNetwork(network);
78
79     String schema = SchemaGenerator
80         .generate(SchemaTemplateContext.composition, CompositionEntityType.network, input);
81     validateSchema(schema);
82   }
83
84   @Test
85   public void testGenerateNetworkCompositionManual() {
86     NetworkCompositionSchemaInput input = new NetworkCompositionSchemaInput();
87     input.setManual(true);
88
89     String schema = SchemaGenerator
90         .generate(SchemaTemplateContext.composition, CompositionEntityType.network, input);
91
92     validateSchema(schema);
93   }
94
95   @Test
96   public void testGenerateComponentQuestionnaireWithoutInput() {
97     String schema = SchemaGenerator
98         .generate(SchemaTemplateContext.questionnaire, CompositionEntityType.component, null);
99     validateSchema(schema);
100   }
101
102   @Test
103   public void testGenerateComponentQuestionnaireWithMissingInput() {
104     ComponentQuestionnaireSchemaInput
105         input = new ComponentQuestionnaireSchemaInput(Arrays.asList("nic1", "nic2"),
106         JsonUtil.json2Object("{\n" +
107             "  \"compute\": {\n" +
108             "    \"numOfVMs\": {\n" +
109             "      \"blabla\": 70\n" + // no minimum
110             "    }\n" +
111             "  }\n" +
112             "}", Map.class));
113     String schema = SchemaGenerator
114         .generate(SchemaTemplateContext.questionnaire, CompositionEntityType.component, input);
115     JSONObject schemaJson = validateSchema(schema);
116     //Assert.assertEquals(getMinOfVmMax(schemaJson), 0);
117   }
118
119   @Test
120   public void testGenerateComponentQuestionnaireWithInvalidTypeInput() {
121     ComponentQuestionnaireSchemaInput input =
122         new ComponentQuestionnaireSchemaInput(Arrays.asList("nic1", "nic2"),
123             JsonUtil.json2Object("{\n" +
124                 "  \"compute\": {\n" +
125                 "    \"numOfVMs\": {\n" +
126                 "      \"minimum\": \"some string instead of integer\"\n" +
127                 // invalid minimum - string
128                 "    }\n" +
129                 "  }\n" +
130                 "}", Map.class));
131     String schema = SchemaGenerator
132         .generate(SchemaTemplateContext.questionnaire, CompositionEntityType.component, input);
133     JSONObject schemaJson = validateSchema(schema);
134     Assert.assertEquals(getMinOfVmMax(schemaJson), 0);
135   }
136
137   @Test
138   public void testGenerateComponentQuestionnaireWithInvalidRangeInput() {
139     ComponentQuestionnaireSchemaInput input =
140         new ComponentQuestionnaireSchemaInput(Arrays.asList("nic1", "nic2"),
141             JsonUtil.json2Object("{\n" +
142                 "  \"compute\": {\n" +
143                 "    \"numOfVMs\": {\n" +
144                 "      \"minimum\": 150\n" + // invalid minimum - integer out of range (0-100)
145                 "    }\n" +
146                 "  }\n" +
147                 "}", Map.class));
148     String schema = SchemaGenerator
149         .generate(SchemaTemplateContext.questionnaire, CompositionEntityType.component, input);
150     JSONObject schemaJson = validateSchema(schema);
151     Assert.assertEquals(getMinOfVmMax(schemaJson), 0);
152   }
153
154   @Test
155   public void testGenerateComponentQuestionnaireWithValidInput() {
156     ComponentQuestionnaireSchemaInput input =
157         new ComponentQuestionnaireSchemaInput(Arrays.asList("nic1", "nic2"),
158             JsonUtil.json2Object("{\n" +
159                 "  \"compute\": {\n" +
160                 "    \"numOfVMs\": {\n" +
161                 "      \"minimum\": 30\n" + // valid minimum - integer at the correct range (0-100)
162                 "    }\n" +
163                 "  }\n" +
164                 "}", Map.class));
165     String schema = SchemaGenerator
166         .generate(SchemaTemplateContext.questionnaire, CompositionEntityType.component, input);
167     JSONObject schemaJson = validateSchema(schema);
168     Assert.assertEquals(getMinOfVmMax(schemaJson), 30);
169   }
170
171   @Test
172   public void testGenerateNicQuestionnaire() {
173     String schema = SchemaGenerator
174         .generate(SchemaTemplateContext.questionnaire, CompositionEntityType.nic, null);
175     validateSchema(schema);
176   }
177
178   @Test
179   public void testGenerateComponentCompositionUpload() {
180     ComponentData component = new ComponentData();
181     component.setName("upload comp1 name");
182     component.setDescription("upload comp1 desc");
183
184     ComponentCompositionSchemaInput input = new ComponentCompositionSchemaInput();
185     input.setManual(false);
186     input.setComponent(component);
187
188     String schema = SchemaGenerator
189         .generate(SchemaTemplateContext.composition, CompositionEntityType.component, input);
190     validateSchema(schema);
191   }
192
193   @Test
194   public void testGenerateComponentCompositionManual() {
195     ComponentCompositionSchemaInput input = new ComponentCompositionSchemaInput();
196     input.setManual(true);
197
198     String schema = SchemaGenerator
199         .generate(SchemaTemplateContext.composition, CompositionEntityType.component, input);
200     validateSchema(schema);
201   }
202
203   @Test
204   public void testGenerateNicCompositionUpload() {
205     Nic nic = new Nic();
206     nic.setName("upload nic1 name");
207     nic.setDescription("upload nic1 desc");
208     nic.setNetworkId("upload nic1 networkId");
209     //nic.setNetworkName("upload nic1 networkName");
210     nic.setNetworkType(NetworkType.External);
211
212     NicCompositionSchemaInput input = new NicCompositionSchemaInput();
213     input.setManual(false);
214     input.setNic(nic);
215
216     String schema = SchemaGenerator
217         .generate(SchemaTemplateContext.composition, CompositionEntityType.nic, input);
218     validateSchema(schema);
219   }
220
221
222 //    @Test
223 //    public void testGenerateNicCompositionManualWithoutNetworkId() {
224 //        Nic nic = new Nic();
225 //        nic.setName("upload nic1 name");
226 //        nic.setDescription("upload nic1 desc");
227 //        //nic.setNetworkName("upload nic1 networkName");
228 //        nic.setNetworkType(NetworkType.External);
229 //
230 //        NicCompositionSchemaInput input = new NicCompositionSchemaInput();
231 //        input.setManual(true);
232 //        input.setNic(nic);
233 //
234 //        String schema = SchemaGenerator.generate(SchemaTemplateContext.composition, CompositionEntityType.nic, input);
235 //        validateSchema(schema);
236 //    }
237
238   @Test
239   public void testGenerateNicCompositionUploadWithoutNetworkId() {
240     Nic nic = new Nic();
241     nic.setName("upload nic1 name");
242     nic.setDescription("upload nic1 desc");
243     //nic.setNetworkName("upload nic1 networkName");
244     nic.setNetworkType(NetworkType.External);
245
246     NicCompositionSchemaInput input = new NicCompositionSchemaInput();
247     input.setManual(false);
248     input.setNic(nic);
249
250     String schema = SchemaGenerator
251         .generate(SchemaTemplateContext.composition, CompositionEntityType.nic, input);
252     validateSchema(schema);
253   }
254
255   @Test
256   public void testGenerateNicCompositionManual() {Nic nic = new Nic();
257     nic.setName("upload nic1 name");
258     nic.setDescription("upload nic1 desc");
259     nic.setNetworkType(NetworkType.Internal);
260     NicCompositionSchemaInput input = new NicCompositionSchemaInput();
261
262     input.setManual(true);
263     input.setNetworkIds(
264         Arrays.asList("manual networkId1", "manual networkId2", "manual networkId3"));
265     input.setNic(nic);
266     String schema = SchemaGenerator
267         .generate(SchemaTemplateContext.composition, CompositionEntityType.nic, input);
268     validateSchema(schema);
269   }
270   */
271 }