Adding unit tests
[sdc.git] / common / onap-tosca-datatype / src / test / java / org / onap / sdc / tosca / services / YamlUtilTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2021 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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.sdc.tosca.services;
22
23 import static org.junit.jupiter.api.Assertions.assertEquals;
24 import static org.junit.jupiter.api.Assertions.assertFalse;
25 import static org.junit.jupiter.api.Assertions.assertNotNull;
26 import static org.junit.jupiter.api.Assertions.assertTrue;
27 import org.junit.jupiter.api.Test;
28
29 import java.io.InputStream;
30 import java.util.LinkedHashMap;
31 import java.util.List;
32 import java.util.Map;
33 import java.util.Optional;
34
35
36 public class YamlUtilTest {
37
38     private YamlUtil yamlUtil = new YamlUtil();
39
40     @Test
41     public void testYamlToMap() {
42         InputStream is = yamlUtil.loadYamlFileIs("/yamlMap.yaml");
43         Map<String, LinkedHashMap<String, Object>> res = yamlUtil.yamlToMap(is);
44         assertNotNull(res);
45         assertEquals(res.size(), 3);
46         assertEquals(res.get("parameter1"), "value1");
47         assertEquals(res.get("parameter2"), "value2");
48         assertEquals(res.get("parameter3"), "value3");
49     }
50
51     @Test
52     public void testYamlToList() {
53         InputStream is = yamlUtil.loadYamlFileIs("/yamlList.yaml");
54         Optional<List<Object>> res = yamlUtil.yamlToList(is);
55         assertEquals(res.get().size(), 3);
56         assertEquals(res.get().get(0), "value1");
57
58         InputStream is2 = yamlUtil.loadYamlFileIs("/yamlListError.yaml");
59         Optional<List<Object>> res2 = yamlUtil.yamlToList(is2);
60         assertTrue(res2.isEmpty());
61     }
62
63     @Test
64     public void testIsYamlFileContentValid() {
65         String yamlString = "tosca_definitions_version: tosca_simple_yaml_1_1\n" +
66                 "imports: []\n" +
67                 "node_types:\n" +
68                 "  tosca.nodes.Root:\n" +
69                 "    description: The TOSCA Node Type all other TOSCA base Node Types derive from";
70         boolean res = yamlUtil.isYamlFileContentValid(yamlString);
71         assertTrue(res);
72
73         String yamlString2 = "{tosca_definitions_version: tosca_simple_yaml_1_1\n" +
74                 "node_types:\n" +
75                 "  tosca.nodes.Root:\n" +
76                 "    description: The TOSCA Node Type all other TOSCA base Node Types derive from}";
77         boolean res2 = yamlUtil.isYamlFileContentValid(yamlString2);
78         assertFalse(res2);
79     }
80 }