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