bc9275bb518b0a593b90bba8aae025daf87eb1c2
[so.git] / asdc-controller / src / test / java / org / onap / so / asdc / installer / heat / ToscaResourceInputTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2019 Huawei 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 package org.onap.so.asdc.installer.heat;
21
22 import org.junit.Rule;
23 import org.junit.Test;
24 import org.mockito.Mock;
25 import org.mockito.junit.MockitoJUnit;
26 import org.mockito.junit.MockitoRule;
27 import org.onap.sdc.tosca.parser.api.ISdcCsarHelper;
28 import org.onap.sdc.toscaparser.api.NodeTemplate;
29 import org.onap.sdc.toscaparser.api.Property;
30 import org.onap.sdc.toscaparser.api.elements.Metadata;
31 import org.onap.sdc.toscaparser.api.functions.GetInput;
32 import org.onap.sdc.toscaparser.api.parameters.Input;
33 import org.onap.so.asdc.client.exceptions.ArtifactInstallerException;
34 import org.onap.so.asdc.installer.ToscaResourceStructure;
35 import org.onap.so.db.catalog.beans.Service;
36
37 import java.util.ArrayList;
38 import java.util.Arrays;
39 import java.util.HashMap;
40 import java.util.LinkedHashMap;
41
42 import static org.junit.Assert.assertEquals;
43 import static org.mockito.Matchers.any;
44 import static org.mockito.Mockito.when;
45
46 public class ToscaResourceInputTest {
47     @Rule
48     public MockitoRule rule = MockitoJUnit.rule();
49
50     @Mock
51     ISdcCsarHelper sdcCsarHelper;
52
53     @Mock
54     NodeTemplate nodeTemplate;
55
56     @Mock
57     Property property;
58
59     @Mock
60     GetInput getInput;
61
62     @Mock
63     Input input;
64
65     @Test
66     public void processResourceSequenceTest() {
67         ToscaResourceInstaller toscaResourceInstaller = new ToscaResourceInstaller();
68         ToscaResourceStructure toscaResourceStructure = new ToscaResourceStructure();
69         toscaResourceStructure.setSdcCsarHelper(sdcCsarHelper);
70         ArrayList<Input> inputs = new ArrayList<>();
71         Service service = new Service();
72
73         HashMap<String, Object> hashMap = new HashMap();
74         hashMap.put("name", "node1");
75
76         Metadata metadata = new Metadata(hashMap);
77         when(nodeTemplate.getMetaData()).thenReturn(metadata);
78         when(sdcCsarHelper.getServiceInputs()).thenReturn(inputs);
79         when(sdcCsarHelper.getServiceNodeTemplates()).thenReturn(Arrays.asList(nodeTemplate));
80         when(sdcCsarHelper.getRequirementsOf(any())).thenReturn(null);
81
82
83         toscaResourceInstaller.processResourceSequence(toscaResourceStructure, service);
84         assertEquals(service.getResourceOrder(), "node1");
85     }
86
87     @Test
88     public void resouceInputTest() throws ArtifactInstallerException {
89         ToscaResourceInstaller toscaResourceInstaller = new ToscaResourceInstaller();
90         ToscaResourceStructure toscaResourceStructure = new ToscaResourceStructure();
91
92         toscaResourceStructure.setSdcCsarHelper(sdcCsarHelper);
93
94         HashMap hashMap = new HashMap();
95         hashMap.put("customizationUUID", "id1");
96         Metadata metadata = new Metadata(hashMap);
97
98         LinkedHashMap propertyMap = new LinkedHashMap();
99         propertyMap.put("prop1", property);
100
101         when(sdcCsarHelper.getServiceNodeTemplates()).thenReturn(Arrays.asList(nodeTemplate));
102         when(nodeTemplate.getMetaData()).thenReturn(metadata);
103         when(nodeTemplate.getProperties()).thenReturn(propertyMap);
104         when(property.getValue()).thenReturn("value1");
105
106         String resourceInput = toscaResourceInstaller.getResourceInput(toscaResourceStructure, "id1");
107         assertEquals("{\\\"prop1\\\":\\\"value1\\\"}", resourceInput);
108     }
109
110     @Test
111     public void resouceInputGetInputTest() throws ArtifactInstallerException {
112         ToscaResourceInstaller toscaResourceInstaller = new ToscaResourceInstaller();
113         ToscaResourceStructure toscaResourceStructure = new ToscaResourceStructure();
114
115         toscaResourceStructure.setSdcCsarHelper(sdcCsarHelper);
116
117         HashMap hashMap = new HashMap();
118         hashMap.put("customizationUUID", "id1");
119         Metadata metadata = new Metadata(hashMap);
120
121         LinkedHashMap propertyMap = new LinkedHashMap();
122         propertyMap.put("prop1", property);
123
124         when(sdcCsarHelper.getServiceNodeTemplates()).thenReturn(Arrays.asList(nodeTemplate));
125         when(sdcCsarHelper.getServiceInputs()).thenReturn(Arrays.asList(input));
126         when(nodeTemplate.getMetaData()).thenReturn(metadata);
127         when(nodeTemplate.getProperties()).thenReturn(propertyMap);
128         when(property.getValue()).thenReturn(getInput);
129         when(getInput.getInputName()).thenReturn("res_key");
130         when(input.getName()).thenReturn("res_key");
131         when(input.getDefault()).thenReturn("default_value");
132
133         String resourceInput = toscaResourceInstaller.getResourceInput(toscaResourceStructure, "id1");
134         assertEquals("{\\\"prop1\\\":\\\"res_key|default_value\\\"}", resourceInput);
135     }
136
137     @Test
138     public void resouceInputGetInputDefaultIntegerTest() throws ArtifactInstallerException {
139         ToscaResourceInstaller toscaResourceInstaller = new ToscaResourceInstaller();
140         ToscaResourceStructure toscaResourceStructure = new ToscaResourceStructure();
141
142         toscaResourceStructure.setSdcCsarHelper(sdcCsarHelper);
143
144         HashMap hashMap = new HashMap();
145         hashMap.put("customizationUUID", "id1");
146         Metadata metadata = new Metadata(hashMap);
147
148         LinkedHashMap propertyMap = new LinkedHashMap();
149         propertyMap.put("prop1", property);
150
151         when(sdcCsarHelper.getServiceNodeTemplates()).thenReturn(Arrays.asList(nodeTemplate));
152         when(sdcCsarHelper.getServiceInputs()).thenReturn(Arrays.asList(input));
153         when(nodeTemplate.getMetaData()).thenReturn(metadata);
154         when(nodeTemplate.getProperties()).thenReturn(propertyMap);
155         when(property.getValue()).thenReturn(getInput);
156         when(getInput.getInputName()).thenReturn("res_key");
157         when(input.getName()).thenReturn("res_key");
158         when(input.getDefault()).thenReturn(new Integer(10));
159
160         String resourceInput = toscaResourceInstaller.getResourceInput(toscaResourceStructure, "id1");
161         assertEquals("{\\\"prop1\\\":\\\"res_key|10\\\"}", resourceInput);
162     }
163
164     @Test
165     public void resouceInputGetInputNoPropertyTest() throws ArtifactInstallerException {
166         ToscaResourceInstaller toscaResourceInstaller = new ToscaResourceInstaller();
167         ToscaResourceStructure toscaResourceStructure = new ToscaResourceStructure();
168
169         toscaResourceStructure.setSdcCsarHelper(sdcCsarHelper);
170
171         HashMap hashMap = new HashMap();
172         hashMap.put("customizationUUID", "id1");
173         Metadata metadata = new Metadata(hashMap);
174
175         LinkedHashMap propertyMap = new LinkedHashMap();
176
177         when(sdcCsarHelper.getServiceNodeTemplates()).thenReturn(Arrays.asList(nodeTemplate));
178         when(sdcCsarHelper.getServiceInputs()).thenReturn(Arrays.asList(input));
179         when(nodeTemplate.getMetaData()).thenReturn(metadata);
180         when(nodeTemplate.getProperties()).thenReturn(propertyMap);
181
182         String resourceInput = toscaResourceInstaller.getResourceInput(toscaResourceStructure, "id1");
183         assertEquals("{}", resourceInput);
184     }
185 }