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.sdc.vendorsoftwareproduct.services.schemagenerator;
23 public class SchemaGeneratorTest {
26 private static int getMinOfVmMax(JSONObject schemaJson) {
27 return schemaJson.getJSONObject("properties").getJSONObject("compute")
28 .getJSONObject("properties").getJSONObject("numOfVMs").getJSONObject("properties")
29 .getJSONObject("maximum").getInt("minimum");
32 private static JSONObject validateSchema(String schema) {
33 System.out.println(schema);
34 Assert.assertNotNull(schema);
35 Assert.assertTrue(JsonUtil.isValidJson(schema));
36 JSONObject schemaJson = new JSONObject(schema);
37 Assert.assertFalse(SchemaLoader.load(schemaJson) instanceof EmptySchema);
41 // TODO: 3/15/2017 fix and enable //@Test
42 public void testGenerateVspQuestionnaire() {
43 String schema = SchemaGenerator
44 .generate(SchemaTemplateContext.questionnaire, CompositionEntityType.vsp, null);
45 validateSchema(schema);
49 public void testGenerateNetworkCompositionUpload() {
50 Network network = new Network();
51 network.setName("upload network1 name");
52 network.setDhcp(true);
54 NetworkCompositionSchemaInput input = new NetworkCompositionSchemaInput();
55 input.setManual(false);
56 input.setNetwork(network);
58 String schema = SchemaGenerator
59 .generate(SchemaTemplateContext.composition, CompositionEntityType.network, input);
60 validateSchema(schema);
64 public void testGenerateNetworkCompositionManual() {
65 NetworkCompositionSchemaInput input = new NetworkCompositionSchemaInput();
66 input.setManual(true);
68 String schema = SchemaGenerator
69 .generate(SchemaTemplateContext.composition, CompositionEntityType.network, input);
71 validateSchema(schema);
75 public void testGenerateComponentQuestionnaireWithoutInput() {
76 String schema = SchemaGenerator
77 .generate(SchemaTemplateContext.questionnaire, CompositionEntityType.component, null);
78 validateSchema(schema);
82 public void testGenerateComponentQuestionnaireWithMissingInput() {
83 ComponentQuestionnaireSchemaInput
84 input = new ComponentQuestionnaireSchemaInput(Arrays.asList("nic1", "nic2"),
85 JsonUtil.json2Object("{\n" +
87 " \"numOfVMs\": {\n" +
88 " \"blabla\": 70\n" + // no minimum
92 String schema = SchemaGenerator
93 .generate(SchemaTemplateContext.questionnaire, CompositionEntityType.component, input);
94 JSONObject schemaJson = validateSchema(schema);
95 //Assert.assertEquals(getMinOfVmMax(schemaJson), 0);
99 public void testGenerateComponentQuestionnaireWithInvalidTypeInput() {
100 ComponentQuestionnaireSchemaInput input =
101 new ComponentQuestionnaireSchemaInput(Arrays.asList("nic1", "nic2"),
102 JsonUtil.json2Object("{\n" +
103 " \"compute\": {\n" +
104 " \"numOfVMs\": {\n" +
105 " \"minimum\": \"some string instead of integer\"\n" +
106 // invalid minimum - string
110 String schema = SchemaGenerator
111 .generate(SchemaTemplateContext.questionnaire, CompositionEntityType.component, input);
112 JSONObject schemaJson = validateSchema(schema);
113 Assert.assertEquals(getMinOfVmMax(schemaJson), 0);
117 public void testGenerateComponentQuestionnaireWithInvalidRangeInput() {
118 ComponentQuestionnaireSchemaInput input =
119 new ComponentQuestionnaireSchemaInput(Arrays.asList("nic1", "nic2"),
120 JsonUtil.json2Object("{\n" +
121 " \"compute\": {\n" +
122 " \"numOfVMs\": {\n" +
123 " \"minimum\": 150\n" + // invalid minimum - integer out of range (0-100)
127 String schema = SchemaGenerator
128 .generate(SchemaTemplateContext.questionnaire, CompositionEntityType.component, input);
129 JSONObject schemaJson = validateSchema(schema);
130 Assert.assertEquals(getMinOfVmMax(schemaJson), 0);
134 public void testGenerateComponentQuestionnaireWithValidInput() {
135 ComponentQuestionnaireSchemaInput input =
136 new ComponentQuestionnaireSchemaInput(Arrays.asList("nic1", "nic2"),
137 JsonUtil.json2Object("{\n" +
138 " \"compute\": {\n" +
139 " \"numOfVMs\": {\n" +
140 " \"minimum\": 30\n" + // valid minimum - integer at the correct range (0-100)
144 String schema = SchemaGenerator
145 .generate(SchemaTemplateContext.questionnaire, CompositionEntityType.component, input);
146 JSONObject schemaJson = validateSchema(schema);
147 Assert.assertEquals(getMinOfVmMax(schemaJson), 30);
151 public void testGenerateNicQuestionnaire() {
152 String schema = SchemaGenerator
153 .generate(SchemaTemplateContext.questionnaire, CompositionEntityType.nic, null);
154 validateSchema(schema);
158 public void testGenerateComponentCompositionUpload() {
159 ComponentData component = new ComponentData();
160 component.setName("upload comp1 name");
161 component.setDescription("upload comp1 desc");
163 ComponentCompositionSchemaInput input = new ComponentCompositionSchemaInput();
164 input.setManual(false);
165 input.setComponent(component);
167 String schema = SchemaGenerator
168 .generate(SchemaTemplateContext.composition, CompositionEntityType.component, input);
169 validateSchema(schema);
173 public void testGenerateComponentCompositionManual() {
174 ComponentCompositionSchemaInput input = new ComponentCompositionSchemaInput();
175 input.setManual(true);
177 String schema = SchemaGenerator
178 .generate(SchemaTemplateContext.composition, CompositionEntityType.component, input);
179 validateSchema(schema);
183 public void testGenerateNicCompositionUpload() {
185 nic.setName("upload nic1 name");
186 nic.setDescription("upload nic1 desc");
187 nic.setNetworkId("upload nic1 networkId");
188 //nic.setNetworkName("upload nic1 networkName");
189 nic.setNetworkType(NetworkType.External);
191 NicCompositionSchemaInput input = new NicCompositionSchemaInput();
192 input.setManual(false);
195 String schema = SchemaGenerator
196 .generate(SchemaTemplateContext.composition, CompositionEntityType.nic, input);
197 validateSchema(schema);
202 // public void testGenerateNicCompositionManualWithoutNetworkId() {
203 // Nic nic = new Nic();
204 // nic.setName("upload nic1 name");
205 // nic.setDescription("upload nic1 desc");
206 // //nic.setNetworkName("upload nic1 networkName");
207 // nic.setNetworkType(NetworkType.External);
209 // NicCompositionSchemaInput input = new NicCompositionSchemaInput();
210 // input.setManual(true);
211 // input.setNic(nic);
213 // String schema = SchemaGenerator.generate(SchemaTemplateContext.composition, CompositionEntityType.nic, input);
214 // validateSchema(schema);
218 public void testGenerateNicCompositionUploadWithoutNetworkId() {
220 nic.setName("upload nic1 name");
221 nic.setDescription("upload nic1 desc");
222 //nic.setNetworkName("upload nic1 networkName");
223 nic.setNetworkType(NetworkType.External);
225 NicCompositionSchemaInput input = new NicCompositionSchemaInput();
226 input.setManual(false);
229 String schema = SchemaGenerator
230 .generate(SchemaTemplateContext.composition, CompositionEntityType.nic, input);
231 validateSchema(schema);
235 public void testGenerateNicCompositionManual() {Nic nic = new Nic();
236 nic.setName("upload nic1 name");
237 nic.setDescription("upload nic1 desc");
238 nic.setNetworkType(NetworkType.Internal);
239 NicCompositionSchemaInput input = new NicCompositionSchemaInput();
241 input.setManual(true);
243 Arrays.asList("manual networkId1", "manual networkId2", "manual networkId3"));
245 String schema = SchemaGenerator
246 .generate(SchemaTemplateContext.composition, CompositionEntityType.nic, input);
247 validateSchema(schema);