summaryrefslogtreecommitdiffstats
path: root/dictionaryvalidation/src/test/java/org/onap/validation/yaml/schema/node/YamlSchemaNodeFactoryTest.java
blob: 646b8a2b241660970603ea90506c4671bd817378 (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
/*
 * 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.node;

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.model.YamlDocumentFactory;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.catchThrowable;
import static org.onap.validation.yaml.schema.node.YamlSchemaNodeFactory.EMPTY_COMMENT;

public class YamlSchemaNodeFactoryTest {

    private static final String ROOT_PATH = "/";

    @Test
    void shouldThrowExceptionDuringLazyLoadingWhenLoadedSchemaHaveInvalidSubStructure()
            throws YamlProcessingException {
        // given
        String nodeName = "pmMetaData";
        YamlDocument document = YamlLoadingUtils.loadSimpleInvalidYamlSchemaForLazyLoadingFile();
        YamlSchemaNode node = new YamlSchemaNodeFactory()
                .createNode(nodeName, ROOT_PATH, document.getSubStructure(nodeName));

        // when
        Throwable ex = catchThrowable(node::getNextNodes);

        // then
        assertThat(ex)
                .isInstanceOf(YamlSchemaNode.YamlSchemaProcessingException.class)
                .hasMessageContaining("Lazy loading failed, due to yaml parsing exception.");
    }

    @Test
    void shouldCreateLeafNodeIfGivenYamlDocumentHaveNoSubStructure()
            throws YamlProcessingException {
        // given
        String nodeName = "leaf_test";
        String comment = "test leaf node";
        List<String> acceptedValues = List.of("val1", "val2");
        Map<Object, Object> nodeInYamlFormat = Map.of(
                YamlSchemaNodeFactory.PRESENCE_KEY, YamlSchemaNodeFactory.PRESENCE_REQUIRED_KEY,
                YamlSchemaNodeFactory.COMMENT_KEY, comment,
                YamlSchemaNodeFactory.VALUE_KET, acceptedValues);
        YamlDocument document = new YamlDocumentFactory().createYamlDocument(nodeInYamlFormat);

        // when
        YamlSchemaNode yamlSchemaNode = new YamlSchemaNodeFactory().createNode(nodeName, ROOT_PATH, document);

        // then
        assertThatLeafNodeIsValid(
                yamlSchemaNode, nodeName, ROOT_PATH, true, comment,
                acceptedValues.toArray(new String[0])
        );
    }

    @Test
    void shouldCreateBranchNodeIfGivenYamlDocumentHaveSubStructure()
            throws YamlProcessingException {
        // given
        String nodeName = "branch_test";
        String comment = "test branch node";

        String subNode1Name = "branch_test_node1";
        String subNode2Name = "branch_test_node2";

        Map<Object, Object> subStructure = Map.of(
                subNode1Name, new HashMap<>(),
                subNode2Name, new HashMap<>());

        Map<Object, Object> nodeInYamlFormat = Map.of(
                YamlSchemaNodeFactory.PRESENCE_KEY, YamlSchemaNodeFactory.PRESENCE_REQUIRED_KEY,
                YamlSchemaNodeFactory.COMMENT_KEY, comment,
                YamlSchemaNodeFactory.STRUCTURE_KEY, subStructure);
        YamlDocument document = new YamlDocumentFactory().createYamlDocument(nodeInYamlFormat);

        // when
        YamlSchemaNode yamlSchemaNode = new YamlSchemaNodeFactory().createNode(nodeName, ROOT_PATH, document);

        // then
        assertThatBranchNodeIsValid(yamlSchemaNode, nodeName, ROOT_PATH, true, comment, 2);
        List<YamlSchemaNode> subNodes = yamlSchemaNode.getNextNodes();
        assertThat(subNodes).hasSize(2);
        assertThatLeafNodeIsValid(
                subNodes.get(1), subNode1Name, ROOT_PATH + nodeName + "/", false, EMPTY_COMMENT);
        assertThatLeafNodeIsValid(
                subNodes.get(0), subNode2Name, ROOT_PATH + nodeName + "/", false, EMPTY_COMMENT);
    }

    public static void assertThatBranchNodeIsValid(
            YamlSchemaNode yamlSchemaNode, String name, String path, boolean isRequired, String comment,
            int numberOfSubNodes
    ) throws YamlSchemaNode.YamlSchemaProcessingException {
        assertThatNodeIsValid(yamlSchemaNode, name, path, isRequired, comment);

        assertThat(yamlSchemaNode.getClass()).isEqualTo(YamlSchemaBranchNode.class);
        assertThat(yamlSchemaNode.isContainingSubStructure()).isTrue();
        assertThat(yamlSchemaNode.getNextNodes()).hasSize(numberOfSubNodes);
        assertThat(yamlSchemaNode.getAcceptedValues()).isEmpty();
    }

    public static void assertThatLeafNodeIsValid(
            YamlSchemaNode yamlSchemaNode, String name, String path, boolean isRequired, String comment,
            String... acceptedValues
    ) throws YamlSchemaNode.YamlSchemaProcessingException {
        assertThatNodeIsValid(yamlSchemaNode, name, path, isRequired, comment);

        assertThat(yamlSchemaNode.getClass()).isEqualTo(YamlSchemaLeafNode.class);
        assertThat(yamlSchemaNode.isContainingSubStructure()).isFalse();
        assertThat(yamlSchemaNode.getAcceptedValues()).containsExactly(acceptedValues);
        assertThat(yamlSchemaNode.getNextNodes()).isEmpty();
    }

    private static void assertThatNodeIsValid(YamlSchemaNode yamlSchemaNode, String name, String path, boolean isRequired, String comment) {
        assertThat(yamlSchemaNode).isNotNull();
        assertThat(yamlSchemaNode.getName()).isEqualTo(name);
        assertThat(yamlSchemaNode.getPath()).isEqualTo(path);
        if (comment.isEmpty()) {
            assertThat(yamlSchemaNode.getComment()).isNotEmpty();
        } else {
            assertThat(yamlSchemaNode.getComment()).isEqualTo(comment);
        }
        assertThat(yamlSchemaNode.isRequired()).isEqualTo(isRequired);
    }
}