Improve testing stability
[sdc.git] / openecomp-be / lib / openecomp-heat-lib / src / test / java / org / openecomp / sdc / heat / services / HeatStructureUtilTest.java
1 /*
2  *
3  *  Copyright © 2017-2018 European Support Limited
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *       http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  * Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  * /
17  *
18  */
19
20 package org.openecomp.sdc.heat.services;
21
22 import java.util.Collections;
23 import java.util.HashMap;
24 import java.util.HashSet;
25 import java.util.Map;
26 import java.util.Set;
27
28 import org.junit.Assert;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.mockito.Mock;
32 import org.mockito.Mockito;
33 import org.mockito.MockitoAnnotations;
34 import org.openecomp.core.validation.types.GlobalValidationContext;
35 import org.openecomp.sdc.heat.datatypes.model.ResourceReferenceFunctions;
36
37 public class HeatStructureUtilTest {
38
39     @Mock
40     private GlobalValidationContext globalValidationContextMock;
41
42     @Before
43     public void setUp() throws Exception {
44         MockitoAnnotations.openMocks(this);
45     }
46
47     @Test
48     public void testIsResourceNestedFalse() {
49         Assert.assertFalse(HeatStructureUtil.isNestedResource("Test.txt"));
50     }
51
52     @Test
53     public void testIsResourceNestedNull() {
54         Assert.assertFalse(HeatStructureUtil.isNestedResource(null));
55     }
56
57     @Test
58     public void testIsResourceNestedTrue() {
59         Assert.assertTrue(HeatStructureUtil.isNestedResource("Test.yml"));
60     }
61
62     @Test
63     public void testGetReferencedValuesByFunctionNameAddMessageCall() {
64         Map<String, Object> propertyMap = new HashMap<>();
65         propertyMap.put(ResourceReferenceFunctions.GET_RESOURCE.getFunction(), Collections.emptyList());
66
67         Mockito.doNothing().when(globalValidationContextMock).addMessage(Mockito.anyString(), Mockito.any(),
68                 Mockito.anyString());
69
70         Set<String> valueNames = HeatStructureUtil.getReferencedValuesByFunctionName("Main.yml",
71                 ResourceReferenceFunctions.GET_RESOURCE.getFunction(), propertyMap, globalValidationContextMock);
72
73         Mockito.verify(globalValidationContextMock, Mockito.times(1))
74                 .addMessage(Mockito.anyString(), Mockito.any(), Mockito.anyString());
75         Assert.assertTrue(valueNames.isEmpty());
76     }
77
78     @Test
79     public void testGetReferencedValuesByFunctionNameGetFile() {
80         Map<String, Object> propertyMap = new HashMap<>();
81         propertyMap.put(ResourceReferenceFunctions.GET_FILE.getFunction(), "file:///filename");
82
83         Set<String> valueNames = HeatStructureUtil.getReferencedValuesByFunctionName("Main.yml",
84                 ResourceReferenceFunctions.GET_FILE.getFunction(), propertyMap, globalValidationContextMock);
85
86         Assert.assertFalse(valueNames.isEmpty());
87         Assert.assertTrue(valueNames.contains("filename"));
88     }
89
90     @Test
91     public void testGetReferencedValuesByFunctionNameGetFileValueList() {
92         Map<String, Object> propertyMap = new HashMap<>();
93         propertyMap
94                 .put(ResourceReferenceFunctions.GET_FILE.getFunction(), Collections.singletonList("file:///filename"));
95
96         Set<String> valueNames = HeatStructureUtil.getReferencedValuesByFunctionName("Main.yml",
97                 ResourceReferenceFunctions.GET_FILE.getFunction(), propertyMap, globalValidationContextMock);
98
99         Assert.assertFalse(valueNames.isEmpty());
100         Assert.assertTrue(valueNames.contains("filename"));
101     }
102
103     @Test
104     public void testGetReferencedValuesByFunctionNameGetFileValueListWithAnotherList() {
105         Map<String, Object> propertyMap = new HashMap<>();
106         propertyMap.put(ResourceReferenceFunctions.GET_FILE.getFunction(),
107                 Collections.singletonList(Collections.emptyList()));
108
109         Set<String> valueNames = HeatStructureUtil.getReferencedValuesByFunctionName("Main.yml",
110                 ResourceReferenceFunctions.GET_FILE.getFunction(), propertyMap, globalValidationContextMock);
111
112         Assert.assertTrue(valueNames.isEmpty());
113     }
114
115     @Test
116     public void testGetReferencedValuesByFunctionNamePassingPropertyMapWithSet() {
117         Set<String> valueNames = HeatStructureUtil.getReferencedValuesByFunctionName("Main.yml",
118                 ResourceReferenceFunctions.GET_FILE.getFunction(), Collections.singletonList(new HashSet<>()),
119                 globalValidationContextMock);
120
121         Assert.assertTrue(valueNames.isEmpty());
122     }
123
124     @Test
125     public void testGetReferencedValuesByFunctionNameGetFileValueListSet() {
126         Map<String, Object> propertyMap = new HashMap<>();
127         propertyMap.put(ResourceReferenceFunctions.GET_FILE.getFunction(),
128                 new HashSet<>());
129
130         Set<String> valueNames = HeatStructureUtil.getReferencedValuesByFunctionName("Main.yml",
131                 ResourceReferenceFunctions.GET_FILE.getFunction(), propertyMap, globalValidationContextMock);
132
133         Assert.assertTrue(valueNames.isEmpty());
134     }
135
136     @Test
137     public void testGetReferencedValuesByFunctionNameIncorrectKeyWithSet() {
138         Map<String, Object> propertyMap = new HashMap<>();
139         propertyMap.put("test", new HashSet<>());
140
141         Set<String> valueNames = HeatStructureUtil.getReferencedValuesByFunctionName("Main.yml",
142                 ResourceReferenceFunctions.GET_FILE.getFunction(), propertyMap, globalValidationContextMock);
143
144         Assert.assertTrue(valueNames.isEmpty());
145     }
146 }