summaryrefslogtreecommitdiffstats
path: root/dictionaryvalidation/src/test/java/org/onap/validation/yaml/schema/YamlSchemaFactoryTest.java
blob: efc304caccedf715171688ec1867557884dc558f (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
/*
 * Copyright 2020 Nokia
 *
 * 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.
 *
 */

package org.onap.validation.yaml.schema;

import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.onap.validation.yaml.YamlLoadingUtils;
import org.onap.validation.yaml.exception.YamlProcessingException;
import org.onap.validation.yaml.model.YamlDocument;
import org.onap.validation.yaml.schema.node.YamlSchemaNode;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.onap.validation.yaml.model.YamlDocumentFactory.YamlDocumentParsingException;
import static org.onap.validation.yaml.schema.node.YamlSchemaNodeFactory.EMPTY_COMMENT;
import static org.onap.validation.yaml.schema.node.YamlSchemaNodeFactoryTest.assertThatBranchNodeIsValid;
import static org.onap.validation.yaml.schema.node.YamlSchemaNodeFactoryTest.assertThatLeafNodeIsValid;


class YamlSchemaFactoryTest {

    @Test
    void shouldCreateYamlSchemaFromYamlDocumentWithMultipleRoots()
            throws YamlProcessingException {
        // given
        YamlDocument documents = YamlLoadingUtils.loadSimpleValidYamlSchemaWithMultiRootFile();

        // when
        YamlSchema schema = new YamlSchemaFactory().createTreeStructuredYamlSchema(documents);

        // then
        assertThat(schema).isNotNull();
        assertThat(schema.getRootNodes())
                .extracting(YamlSchemaNode::getName)
                .containsExactly("root1", "root2", "root3");
    }


    @Test
    void shouldCreateYamlSchemaFromYamlDocument()
            throws YamlProcessingException {
        // given
        YamlDocument documents = YamlLoadingUtils.loadSimpleValidYamlSchemaFile();

        // when
        YamlSchema schema = new YamlSchemaFactory().createTreeStructuredYamlSchema(documents);

        // then
        assertThat(schema).isNotNull();
        assertThat(schema.getRootNodes()).hasSize(1);
        YamlSchemaNode pmMetaData = schema.getRootNodes().get(0);
        assertThatBranchNodeIsValid(pmMetaData, "pmMetaData", "/", true, EMPTY_COMMENT,
                2);

        YamlSchemaNode pmHeader = pmMetaData.getNextNodes().get(1);
        assertThatBranchNodeIsValid(pmHeader, "pmHeader", "/pmMetaData/", true, EMPTY_COMMENT,
                1);

        YamlSchemaNode nfType = pmHeader.getNextNodes().get(0);
        assertThatLeafNodeIsValid(nfType, "nfType", "/pmMetaData/pmHeader/", true, "nfType comment");

        YamlSchemaNode pmFields = pmMetaData.getNextNodes().get(0);
        assertThatBranchNodeIsValid(pmFields, "pmFields", "/pmMetaData/", true, EMPTY_COMMENT,
                2);

        YamlSchemaNode measChangeType = pmFields.getNextNodes().get(1);
        assertThatLeafNodeIsValid(measChangeType, "measChangeType", "/pmMetaData/pmFields/",
                true, "measChangeType comment",
                "added", "modified", "deleted");

        YamlSchemaNode measAdditionalFields = pmFields.getNextNodes().get(0);
        assertThatBranchNodeIsValid(measAdditionalFields, "measAdditionalFields", "/pmMetaData/pmFields/",
                true, "measAdditionalFields comment",
                2);

        YamlSchemaNode vendorField1 = measAdditionalFields.getNextNodes().get(0);
        assertThatLeafNodeIsValid(vendorField1, "vendorField1", "/pmMetaData/pmFields/measAdditionalFields/",
                true, "vendorField1 comment",
                "X", "Y", "Z");
        YamlSchemaNode vendorField2 = measAdditionalFields.getNextNodes().get(1);
        assertThatLeafNodeIsValid(vendorField2, "vendorField2", "/pmMetaData/pmFields/measAdditionalFields/",
                false, "vendorField2 comment",
                "A", "B");
    }

    @Test
    void shouldThrowYamlParsingExceptionWhenLoadedSchemaIsInvalid()
            throws YamlDocumentParsingException {
        // given
        YamlDocument documents = YamlLoadingUtils.loadSimpleInvalidYamlSchemaFile();

        //when then
        assertThatThrownBy(() -> new YamlSchemaFactory().createTreeStructuredYamlSchema(documents))
                .isInstanceOf(YamlDocumentParsingException.class)
                .hasMessageContaining(String.format(
                        "Fail to parse given objects: %s as yaml document",
                        documents.getSubStructure("pmMetaData").getYaml().get("structure"))
                );
    }
}