aboutsummaryrefslogtreecommitdiffstats
path: root/src/test/java/org/onap/policy/clamp/clds/tosca/ToscaYamlToJsonConvertorTestItCase.java
blob: 6ff82b6d7a556bf83356b37b173c904ef50b8dc1 (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
/*-
 * ============LICENSE_START=======================================================
 * ONAP CLAMP
 * ================================================================================
 * Copyright (C) 2018 AT&T Intellectual Property. All rights
 *                             reserved.
 * Modifications Copyright (C) 2019 Huawei Technologies Co., Ltd.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END============================================
 * ===================================================================
 *
 */

package org.onap.policy.clamp.clds.tosca;

import static org.junit.Assert.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertEquals;

import com.google.gson.JsonObject;
import java.io.IOException;
import javax.transaction.Transactional;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.onap.policy.clamp.clds.Application;
import org.onap.policy.clamp.clds.util.ResourceFileUtils;
import org.onap.policy.clamp.tosca.Dictionary;
import org.onap.policy.clamp.tosca.DictionaryElement;
import org.onap.policy.clamp.tosca.DictionaryService;
import org.skyscreamer.jsonassert.JSONAssert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = Application.class)
public class ToscaYamlToJsonConvertorTestItCase {

    @Autowired
    private DictionaryService dictionaryService;

    @Autowired
    private ToscaYamlToJsonConvertor toscaYamlToJsonConvertor;

    /**
     * This Test validates TOSCA yaml to JSON Schema conversion based on JSON Editor
     * Schema.
     *
     * @throws IOException In case of issue when opening the tosca yaml file and
     *         converted json file
     */
    @Test
    public final void testParseToscaYaml() throws IOException {
        String toscaModelYaml = ResourceFileUtils.getResourceAsString("tosca/tosca_example.yaml");
        ToscaYamlToJsonConvertor convertor = new ToscaYamlToJsonConvertor();

        String parsedJsonSchema =
            convertor.parseToscaYaml(toscaModelYaml, "onap.policies.monitoring.cdap.tca.hi.lo.app");
        assertNotNull(parsedJsonSchema);
        JSONAssert.assertEquals(
            ResourceFileUtils.getResourceAsString("tosca/policy-yaml-to-json.json"),
            parsedJsonSchema, true);
    }

    /**
     * This Test validates TOSCA yaml with constraints to JSON Schema conversion
     * based on JSON Editor Schema.
     *
     * @throws IOException In case of issue when opening the tosca yaml file and
     *         converted json file
     */
    @Test
    public final void testParseToscaYamlWithConstraints() throws IOException {
        String toscaModelYaml =
            ResourceFileUtils.getResourceAsString("tosca/tosca-with-constraints.yaml");
        ToscaYamlToJsonConvertor convertor = new ToscaYamlToJsonConvertor();

        String parsedJsonSchema =
            convertor.parseToscaYaml(toscaModelYaml, "onap.policies.monitoring.example.app");
        assertNotNull(parsedJsonSchema);
        JSONAssert.assertEquals(
            ResourceFileUtils.getResourceAsString("tosca/policy-yaml-to-json-with-constraints.json"),
            parsedJsonSchema, true);
    }

    /**
     * This Test validates TOSCA yaml with different datatypes to JSON Schema
     * conversion based on JSON Editor Schema.
     *
     * @throws IOException In case of issue when opening the tosca yaml file and
     *         converted json file
     */
    @Test
    public final void testParseToscaYamlWithTypes() throws IOException {
        String toscaModelYaml =
            ResourceFileUtils.getResourceAsString("tosca/tosca-with-datatypes.yaml");
        ToscaYamlToJsonConvertor convertor = new ToscaYamlToJsonConvertor();

        String parsedJsonSchema =
            convertor.parseToscaYaml(toscaModelYaml, "onap.policies.monitoring.example.app");
        assertNotNull(parsedJsonSchema);
        JSONAssert.assertEquals(
            ResourceFileUtils.getResourceAsString("tosca/policy-yaml-to-json-with-datatypes.json"),
            parsedJsonSchema, true);
    }

    /**
     * This Test validates Tosca yaml with metadata tag that contains policy_model_type and acronym
     * parameters which defines the Tosca Policy name and its short name.
     *
     * @throws IOException In case of issue when opening the tosca yaml file and
     *         converted json file
     */
    @Test
    @Transactional
    public final void testMetadataClampPossibleValues() throws IOException {
        setupDictionary();
        String toscaModelYaml =
            ResourceFileUtils.getResourceAsString("tosca/tosca_metadata_clamp_possible_values.yaml");

        JsonObject jsonObject = toscaYamlToJsonConvertor.validateAndConvertToJson(toscaModelYaml);
        assertNotNull(jsonObject);
        String policyModelType = toscaYamlToJsonConvertor.getValueFromMetadata(jsonObject,
            ToscaSchemaConstants.METADATA_POLICY_MODEL_TYPE);
        String acronym = toscaYamlToJsonConvertor.getValueFromMetadata(jsonObject,
            ToscaSchemaConstants.METADATA_ACRONYM);
        String parsedJsonSchema =
            toscaYamlToJsonConvertor.parseToscaYaml(toscaModelYaml, policyModelType);

        assertNotNull(parsedJsonSchema);
        assertEquals("onap.policies.monitoring.cdap.tca.hi.lo.app", policyModelType);
        assertEquals("tca", acronym);
        JSONAssert.assertEquals(
            ResourceFileUtils
                .getResourceAsString("tosca/tosca_metadata_clamp_possible_values_json_schema.json"),
            parsedJsonSchema, true);

    }

    private void setupDictionary() {

        // Set up dictionary elements
        Dictionary dictionaryTest = new Dictionary();
        dictionaryTest.setName("Context");
        dictionaryTest.setSecondLevelDictionary(0);

        DictionaryElement element = new DictionaryElement();
        element.setName("PROD");
        element.setShortName("PROD");
        element.setType("string");
        element.setDescription("Production");
        dictionaryTest.addDictionaryElements(element);

        dictionaryService.saveOrUpdateDictionary(dictionaryTest);

        Dictionary dictionaryTest1 = new Dictionary();
        dictionaryTest1.setName("EventDictionary");
        dictionaryTest1.setSecondLevelDictionary(0);

        DictionaryElement element1 = new DictionaryElement();
        element1.setName("alarmCondition");
        element1.setShortName("alarmCondition");
        element1.setType("string");
        element1.setDescription("Alarm Condition");
        dictionaryTest1.addDictionaryElements(element1);

        dictionaryTest1 = dictionaryService.saveOrUpdateDictionary(dictionaryTest1);

        DictionaryElement element3 = new DictionaryElement();
        element3.setName("timeEpoch");
        element3.setShortName("timeEpoch");
        element3.setType("datetime");
        element3.setDescription("Time Epoch");
        dictionaryTest1.addDictionaryElements(element3);

        dictionaryService.saveOrUpdateDictionary(dictionaryTest1);

        Dictionary dictionaryTest2 = new Dictionary();
        dictionaryTest2.setName("Operators");
        dictionaryTest2.setSecondLevelDictionary(0);

        DictionaryElement element2 = new DictionaryElement();
        element2.setName("equals");
        element2.setShortName("equals");
        element2.setType("string|datetime");
        element2.setDescription("equals");
        dictionaryTest2.addDictionaryElements(element2);
        dictionaryService.saveOrUpdateDictionary(dictionaryTest2);
    }

}