aboutsummaryrefslogtreecommitdiffstats
path: root/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/services/SchemaGeneratorTest.java
blob: fd293c66dd5d2cac0d5abaae805f444b50b2c0f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package org.openecomp.sdc.vendorsoftwareproduct.services;

import org.openecomp.core.utilities.json.JsonUtil;
import org.everit.json.schema.EmptySchema;
import org.everit.json.schema.loader.SchemaLoader;
import org.json.JSONObject;
import org.openecomp.sdc.vendorsoftwareproduct.types.composition.ComponentData;
import org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionEntityType;
import org.openecomp.sdc.vendorsoftwareproduct.types.composition.Network;
import org.openecomp.sdc.vendorsoftwareproduct.types.composition.NetworkType;
import org.openecomp.sdc.vendorsoftwareproduct.types.composition.Nic;
import org.openecomp.sdc.vendorsoftwareproduct.types.schemagenerator.ComponentCompositionSchemaInput;
import org.openecomp.sdc.vendorsoftwareproduct.types.schemagenerator.ComponentQuestionnaireSchemaInput;
import org.openecomp.sdc.vendorsoftwareproduct.types.schemagenerator.NetworkCompositionSchemaInput;
import org.openecomp.sdc.vendorsoftwareproduct.types.schemagenerator.NicCompositionSchemaInput;
import org.openecomp.sdc.vendorsoftwareproduct.types.schemagenerator.SchemaTemplateContext;
import org.testng.Assert;
import org.testng.annotations.Test;

import java.util.Arrays;
import java.util.Map;

public class SchemaGeneratorTest {

  private static int getMinOfVmMax(JSONObject schemaJson) {
    return schemaJson.getJSONObject("properties").getJSONObject("compute")
        .getJSONObject("properties").getJSONObject("numOfVMs").getJSONObject("properties")
        .getJSONObject("maximum").getInt("minimum");
  }

  private static JSONObject validateSchema(String schema) {
    System.out.println(schema);
    Assert.assertNotNull(schema);
    Assert.assertTrue(JsonUtil.isValidJson(schema));
    JSONObject schemaJson = new JSONObject(schema);
    Assert.assertFalse(SchemaLoader.load(schemaJson) instanceof EmptySchema);
    return schemaJson;
  }

  @Test
  public void testGenerateVspQuestionnaire() {
    String schema = SchemaGenerator
        .generate(SchemaTemplateContext.questionnaire, CompositionEntityType.vsp, null);
    validateSchema(schema);
  }

  @Test
  public void testGenerateNetworkCompositionUpload() {
    Network network = new Network();
    network.setName("upload network1 name");
    network.setDhcp(true);

    NetworkCompositionSchemaInput input = new NetworkCompositionSchemaInput();
    input.setManual(false);
    input.setNetwork(network);

    String schema = SchemaGenerator
        .generate(SchemaTemplateContext.composition, CompositionEntityType.network, input);
    validateSchema(schema);
  }

  @Test
  public void testGenerateNetworkCompositionManual() {
    NetworkCompositionSchemaInput input = new NetworkCompositionSchemaInput();
    input.setManual(true);

    String schema = SchemaGenerator
        .generate(SchemaTemplateContext.composition, CompositionEntityType.network, input);

    validateSchema(schema);
  }

  @Test
  public void testGenerateComponentQuestionnaireWithoutInput() {
    String schema = SchemaGenerator
        .generate(SchemaTemplateContext.questionnaire, CompositionEntityType.component, null);
    validateSchema(schema);
  }

  @Test
  public void testGenerateComponentQuestionnaireWithMissingInput() {
    ComponentQuestionnaireSchemaInput input =
        new ComponentQuestionnaireSchemaInput(Arrays.asList("nic1", "nic2"),
            JsonUtil.json2Object("{\n" +
                "  \"compute\": {\n" +
                "    \"numOfVMs\": {\n" +
                "      \"blabla\": 70\n" + // no minimum
                "    }\n" +
                "  }\n" +
                "}", Map.class));
    String schema = SchemaGenerator
        .generate(SchemaTemplateContext.questionnaire, CompositionEntityType.component, input);
    JSONObject schemaJson = validateSchema(schema);
    //Assert.assertEquals(getMinOfVmMax(schemaJson), 0);
  }

  @Test
  public void testGenerateComponentQuestionnaireWithInvalidTypeInput() {
    ComponentQuestionnaireSchemaInput input =
        new ComponentQuestionnaireSchemaInput(Arrays.asList("nic1", "nic2"),
            JsonUtil.json2Object("{\n" +
                "  \"compute\": {\n" +
                "    \"numOfVMs\": {\n" +
                "      \"minimum\": \"some string instead of integer\"\n" +
                // invalid minimum - string
                "    }\n" +
                "  }\n" +
                "}", Map.class));
    String schema = SchemaGenerator
        .generate(SchemaTemplateContext.questionnaire, CompositionEntityType.component, input);
    JSONObject schemaJson = validateSchema(schema);
    Assert.assertEquals(getMinOfVmMax(schemaJson), 0);
  }

  @Test
  public void testGenerateComponentQuestionnaireWithInvalidRangeInput() {
    ComponentQuestionnaireSchemaInput input =
        new ComponentQuestionnaireSchemaInput(Arrays.asList("nic1", "nic2"),
            JsonUtil.json2Object("{\n" +
                "  \"compute\": {\n" +
                "    \"numOfVMs\": {\n" +
                "      \"minimum\": 150\n" + // invalid minimum - integer out of range (0-100)
                "    }\n" +
                "  }\n" +
                "}", Map.class));
    String schema = SchemaGenerator
        .generate(SchemaTemplateContext.questionnaire, CompositionEntityType.component, input);
    JSONObject schemaJson = validateSchema(schema);
    Assert.assertEquals(getMinOfVmMax(schemaJson), 0);
  }

  @Test
  public void testGenerateComponentQuestionnaireWithValidInput() {
    ComponentQuestionnaireSchemaInput input =
        new ComponentQuestionnaireSchemaInput(Arrays.asList("nic1", "nic2"),
            JsonUtil.json2Object("{\n" +
                "  \"compute\": {\n" +
                "    \"numOfVMs\": {\n" +
                "      \"minimum\": 30\n" + // valid minimum - integer at the correct range (0-100)
                "    }\n" +
                "  }\n" +
                "}", Map.class));
    String schema = SchemaGenerator
        .generate(SchemaTemplateContext.questionnaire, CompositionEntityType.component, input);
    JSONObject schemaJson = validateSchema(schema);
    Assert.assertEquals(getMinOfVmMax(schemaJson), 30);
  }

  @Test
  public void testGenerateNicQuestionnaire() {
    String schema = SchemaGenerator
        .generate(SchemaTemplateContext.questionnaire, CompositionEntityType.nic, null);
    validateSchema(schema);
  }

  @Test
  public void testGenerateComponentCompositionUpload() {
    ComponentData component = new ComponentData();
    component.setName("upload comp1 name");
    component.setDescription("upload comp1 desc");

    ComponentCompositionSchemaInput input = new ComponentCompositionSchemaInput();
    input.setManual(false);
    input.setComponent(component);

    String schema = SchemaGenerator
        .generate(SchemaTemplateContext.composition, CompositionEntityType.component, input);
    validateSchema(schema);
  }

  @Test
  public void testGenerateComponentCompositionManual() {
    ComponentCompositionSchemaInput input = new ComponentCompositionSchemaInput();
    input.setManual(true);

    String schema = SchemaGenerator
        .generate(SchemaTemplateContext.composition, CompositionEntityType.component, input);
    validateSchema(schema);
  }

  @Test
  public void testGenerateNicCompositionUpload() {
    Nic nic = new Nic();
    nic.setName("upload nic1 name");
    nic.setDescription("upload nic1 desc");
    nic.setNetworkId("upload nic1 networkId");
    //nic.setNetworkName("upload nic1 networkName");
    nic.setNetworkType(NetworkType.External);

    NicCompositionSchemaInput input = new NicCompositionSchemaInput();
    input.setManual(false);
    input.setNic(nic);

    String schema = SchemaGenerator
        .generate(SchemaTemplateContext.composition, CompositionEntityType.nic, input);
    validateSchema(schema);
  }


//    @Test
//    public void testGenerateNicCompositionManualWithoutNetworkId() {
//        Nic nic = new Nic();
//        nic.setName("upload nic1 name");
//        nic.setDescription("upload nic1 desc");
//        //nic.setNetworkName("upload nic1 networkName");
//        nic.setNetworkType(NetworkType.External);
//
//        NicCompositionSchemaInput input = new NicCompositionSchemaInput();
//        input.setManual(true);
//        input.setNic(nic);
//
//        String schema = SchemaGenerator.generate(SchemaTemplateContext.composition, CompositionEntityType.nic, input);
//        validateSchema(schema);
//    }

  @Test
  public void testGenerateNicCompositionUploadWithoutNetworkId() {
    Nic nic = new Nic();
    nic.setName("upload nic1 name");
    nic.setDescription("upload nic1 desc");
    //nic.setNetworkName("upload nic1 networkName");
    nic.setNetworkType(NetworkType.External);

    NicCompositionSchemaInput input = new NicCompositionSchemaInput();
    input.setManual(false);
    input.setNic(nic);

    String schema = SchemaGenerator
        .generate(SchemaTemplateContext.composition, CompositionEntityType.nic, input);
    validateSchema(schema);
  }

  @Test
  public void testGenerateNicCompositionManual() {
    NicCompositionSchemaInput input = new NicCompositionSchemaInput();
    input.setManual(true);
    input.setNetworkIds(
        Arrays.asList("manual networkId1", "manual networkId2", "manual networkId3"));

    String schema = SchemaGenerator
        .generate(SchemaTemplateContext.composition, CompositionEntityType.nic, input);
    validateSchema(schema);
  }
}