2 * ============LICENSE_START=======================================================
\r
4 * ================================================================================
\r
5 * Copyright (C) 2017 Huawei Technologies Co., Ltd. All rights reserved.
\r
6 * ================================================================================
\r
7 * Licensed under the Apache License, Version 2.0 (the "License");
\r
8 * you may not use this file except in compliance with the License.
\r
9 * You may obtain a copy of the License at
\r
11 * http://www.apache.org/licenses/LICENSE-2.0
\r
13 * Unless required by applicable law or agreed to in writing, software
\r
14 * distributed under the License is distributed on an "AS IS" BASIS,
\r
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
16 * See the License for the specific language governing permissions and
\r
17 * limitations under the License.
\r
18 * ============LICENSE_END=========================================================
\r
21 package org.openecomp.mso.asdc.installer.heat.tests;
\r
23 import java.io.IOException;
\r
24 import java.io.UnsupportedEncodingException;
\r
25 import java.net.URISyntaxException;
\r
26 import java.nio.file.Files;
\r
27 import java.nio.file.Paths;
\r
28 import java.security.MessageDigest;
\r
29 import java.security.NoSuchAlgorithmException;
\r
30 import java.util.ArrayList;
\r
31 import java.util.List;
\r
32 import java.util.Map;
\r
34 import org.apache.commons.codec.binary.Base64;
\r
35 import org.apache.commons.lang3.tuple.Pair;
\r
36 import org.junit.AfterClass;
\r
37 import org.junit.Before;
\r
38 import org.junit.BeforeClass;
\r
39 import org.junit.Test;
\r
40 import org.mockito.Mockito;
\r
41 import org.openecomp.mso.asdc.client.ASDCConfiguration;
\r
42 import org.openecomp.mso.asdc.client.exceptions.ArtifactInstallerException;
\r
43 import org.openecomp.mso.asdc.client.tests.ASDCControllerTest;
\r
44 import org.openecomp.mso.asdc.installer.ToscaResourceStructure;
\r
45 import org.openecomp.mso.asdc.installer.VfResourceStructure;
\r
46 import org.openecomp.mso.asdc.installer.heat.ToscaResourceInstaller;
\r
47 import org.openecomp.mso.db.catalog.CatalogDatabase;
\r
48 import org.openecomp.mso.db.catalog.beans.AllottedResource;
\r
49 import org.openecomp.mso.db.catalog.beans.AllottedResourceCustomization;
\r
50 import org.openecomp.mso.db.catalog.beans.NetworkResource;
\r
51 import org.openecomp.mso.db.catalog.beans.NetworkResourceCustomization;
\r
52 import org.openecomp.mso.db.catalog.beans.Service;
\r
53 import org.openecomp.mso.db.catalog.beans.ServiceToAllottedResources;
\r
54 import org.openecomp.mso.db.catalog.beans.ServiceToNetworks;
\r
55 import org.openecomp.mso.db.catalog.beans.ServiceToResourceCustomization;
\r
56 import org.openecomp.mso.db.catalog.beans.TempNetworkHeatTemplateLookup;
\r
57 import org.openecomp.mso.db.catalog.beans.ToscaCsar;
\r
58 import org.openecomp.mso.db.catalog.beans.VfModule;
\r
59 import org.openecomp.mso.db.catalog.beans.VfModuleCustomization;
\r
60 import org.openecomp.mso.db.catalog.beans.VfModuleToHeatFiles;
\r
61 import org.openecomp.mso.db.catalog.beans.VnfResCustomToVfModuleCustom;
\r
62 import org.openecomp.mso.db.catalog.beans.VnfResource;
\r
63 import org.openecomp.mso.db.catalog.beans.VnfResourceCustomization;
\r
64 import org.openecomp.mso.properties.MsoJavaProperties;
\r
65 import org.openecomp.mso.properties.MsoPropertiesException;
\r
66 import org.openecomp.mso.properties.MsoPropertiesFactory;
\r
67 import org.openecomp.sdc.api.IDistributionClient;
\r
68 import org.openecomp.sdc.api.notification.IArtifactInfo;
\r
69 import org.openecomp.sdc.api.notification.INotificationData;
\r
70 import org.openecomp.sdc.api.notification.IResourceInstance;
\r
71 import org.openecomp.sdc.api.results.IDistributionClientDownloadResult;
\r
72 import org.openecomp.sdc.api.results.IDistributionClientResult;
\r
73 import org.openecomp.sdc.tosca.parser.api.ISdcCsarHelper;
\r
74 import org.openecomp.sdc.tosca.parser.impl.FilterType;
\r
75 import org.openecomp.sdc.tosca.parser.impl.SdcTypes;
\r
76 import org.openecomp.sdc.toscaparser.api.Group;
\r
77 import org.openecomp.sdc.toscaparser.api.NodeTemplate;
\r
78 import org.openecomp.sdc.toscaparser.api.elements.Metadata;
\r
79 import org.openecomp.sdc.toscaparser.api.parameters.Input;
\r
80 import org.openecomp.sdc.utils.DistributionActionResultEnum;
\r
83 import mockit.MockUp;
\r
85 public class ToscaResourceInstallerTest {
\r
87 private static MsoPropertiesFactory msoPropertiesFactory = new MsoPropertiesFactory();
\r
89 private static String heatExample;
\r
90 private static String heatExampleMD5HashBase64;
\r
92 private static INotificationData iNotif;
\r
94 private static IDistributionClientDownloadResult downloadResult;
\r
95 private static IDistributionClientDownloadResult downloadCorruptedResult;
\r
97 private static IDistributionClientResult successfulClientInitResult;
\r
98 private static IDistributionClientResult unsuccessfulClientInitResult;
\r
100 private static IDistributionClient distributionClient;
\r
102 private static IArtifactInfo artifactInfo1;
\r
104 private static IResourceInstance resource1;
\r
106 private static VfResourceStructure vrs;
\r
108 public static final String ASDC_PROP = MsoJavaProperties.class.getClassLoader().getResource("mso.json").toString()
\r
110 public static final String ASDC_PROP2 = MsoJavaProperties.class.getClassLoader().getResource("mso2.json").toString()
\r
112 public static final String ASDC_PROP3 = MsoJavaProperties.class.getClassLoader().getResource("mso3.json").toString()
\r
114 public static final String ASDC_PROP_BAD = MsoJavaProperties.class.getClassLoader().getResource("mso-bad.json")
\r
115 .toString().substring(5);
\r
116 public static final String ASDC_PROP_WITH_NULL = MsoJavaProperties.class.getClassLoader()
\r
117 .getResource("mso-with-NULL.json").toString().substring(5);
\r
120 public static final void prepareMockNotification() throws MsoPropertiesException, IOException, URISyntaxException,
\r
121 NoSuchAlgorithmException, ArtifactInstallerException {
\r
123 heatExample = new String(Files.readAllBytes(Paths.get(
\r
124 ASDCControllerTest.class.getClassLoader().getResource("resource-examples/autoscaling.yaml").toURI())));
\r
125 MessageDigest md = MessageDigest.getInstance("MD5");
\r
126 byte[] md5Hash = md.digest(heatExample.getBytes());
\r
127 heatExampleMD5HashBase64 = Base64.encodeBase64String(md5Hash);
\r
129 iNotif = Mockito.mock(INotificationData.class);
\r
131 // Create fake ArtifactInfo
\r
132 artifactInfo1 = Mockito.mock(IArtifactInfo.class);
\r
133 Mockito.when(artifactInfo1.getArtifactChecksum())
\r
134 .thenReturn(ToscaResourceInstallerTest.heatExampleMD5HashBase64);
\r
136 Mockito.when(artifactInfo1.getArtifactName()).thenReturn("artifact1");
\r
137 Mockito.when(artifactInfo1.getArtifactType()).thenReturn(ASDCConfiguration.HEAT);
\r
138 Mockito.when(artifactInfo1.getArtifactURL())
\r
139 .thenReturn("https://localhost:8080/v1/catalog/services/srv1/2.0/resources/aaa/1.0/artifacts/aaa.yml");
\r
140 Mockito.when(artifactInfo1.getArtifactUUID()).thenReturn("UUID1");
\r
141 Mockito.when(artifactInfo1.getArtifactDescription()).thenReturn("testos artifact1");
\r
143 distributionClient = Mockito.mock(IDistributionClient.class);
\r
145 // Now provision the NotificationData mock
\r
146 List<IArtifactInfo> listArtifact = new ArrayList<IArtifactInfo>();
\r
147 listArtifact.add(artifactInfo1);
\r
149 // Create fake resource Instance
\r
150 resource1 = Mockito.mock(IResourceInstance.class);
\r
151 // Mockito.when(resource1.getResourceType()).thenReturn("VF");
\r
152 Mockito.when(resource1.getResourceName()).thenReturn("resourceName");
\r
153 Mockito.when(resource1.getArtifacts()).thenReturn(listArtifact);
\r
155 List<IResourceInstance> resources = new ArrayList<>();
\r
156 resources.add(resource1);
\r
158 Mockito.when(iNotif.getResources()).thenReturn(resources);
\r
159 Mockito.when(iNotif.getDistributionID()).thenReturn("distributionID1");
\r
160 Mockito.when(iNotif.getServiceName()).thenReturn("serviceName1");
\r
161 Mockito.when(iNotif.getServiceUUID()).thenReturn("serviceNameUUID1");
\r
162 Mockito.when(iNotif.getServiceVersion()).thenReturn("1.0");
\r
164 downloadResult = Mockito.mock(IDistributionClientDownloadResult.class);
\r
165 Mockito.when(downloadResult.getArtifactPayload()).thenReturn(heatExample.getBytes());
\r
166 Mockito.when(downloadResult.getDistributionActionResult()).thenReturn(DistributionActionResultEnum.SUCCESS);
\r
167 Mockito.when(downloadResult.getDistributionMessageResult()).thenReturn("Success");
\r
169 downloadCorruptedResult = Mockito.mock(IDistributionClientDownloadResult.class);
\r
170 Mockito.when(downloadCorruptedResult.getArtifactPayload()).thenReturn((heatExample + "badone").getBytes());
\r
171 Mockito.when(downloadCorruptedResult.getDistributionActionResult())
\r
172 .thenReturn(DistributionActionResultEnum.SUCCESS);
\r
173 Mockito.when(downloadCorruptedResult.getDistributionMessageResult()).thenReturn("Success");
\r
175 vrs = new VfResourceStructure(iNotif, resource1);
\r
177 vrs.addArtifactToStructure(distributionClient, artifactInfo1, downloadResult);
\r
178 } catch (UnsupportedEncodingException e) {
\r
179 e.printStackTrace();
\r
182 vrs.createVfModuleStructures();
\r
183 } catch (ArtifactInstallerException e) {
\r
184 e.printStackTrace();
\r
186 vrs.getNotification();
\r
187 vrs.getArtifactsMapByUUID();
\r
188 vrs.getCatalogNetworkResourceCustomization();
\r
189 vrs.getCatalogResourceCustomization();
\r
190 vrs.getCatalogService();
\r
191 vrs.getCatalogServiceToAllottedResources();
\r
192 vrs.getCatalogServiceToNetworks();
\r
193 vrs.getCatalogVnfResource();
\r
194 vrs.getResourceInstance();
\r
195 vrs.getVfModulesStructureList();
\r
196 vrs.getVfModuleStructure();
\r
197 vrs.setCatalogNetworkResourceCustomization(new NetworkResourceCustomization());
\r
198 vrs.setCatalogResourceCustomization(new AllottedResourceCustomization());
\r
199 vrs.setCatalogService(new Service());
\r
200 vrs.setCatalogServiceToAllottedResources(new ServiceToAllottedResources());
\r
201 vrs.setCatalogServiceToNetworks(new ServiceToNetworks());
\r
202 vrs.setCatalogVnfResource(new VnfResource());
\r
203 vrs.setSuccessfulDeployment();
\r
205 AllottedResourceCustomization arc = new AllottedResourceCustomization();
\r
206 arc.setModelCustomizationUuid("modelCustomizationUuid");
\r
207 List<AllottedResourceCustomization> allottedResources = new ArrayList<>();
\r
208 allottedResources.add(arc);
\r
210 NetworkResourceCustomization nrc = new NetworkResourceCustomization();
\r
211 nrc.setModelCustomizationUuid("modelCustomizationUuid");
\r
212 List<NetworkResourceCustomization> networkResources = new ArrayList<>();
\r
213 networkResources.add(nrc);
\r
215 new MockUp<CatalogDatabase>() {
\r
217 public List<AllottedResourceCustomization> getAllAllottedResourcesByServiceModelUuid(
\r
218 String serviceModelUuid) {
\r
219 return allottedResources;
\r
222 new MockUp<CatalogDatabase>() {
\r
224 public List<NetworkResourceCustomization> getAllNetworksByServiceModelUuid(String serviceModelUuid) {
\r
225 return networkResources;
\r
229 // Mock now the ASDC distribution client behavior
\r
230 successfulClientInitResult = Mockito.mock(IDistributionClientResult.class);
\r
231 Mockito.when(successfulClientInitResult.getDistributionActionResult())
\r
232 .thenReturn(DistributionActionResultEnum.SUCCESS);
\r
234 unsuccessfulClientInitResult = Mockito.mock(IDistributionClientResult.class);
\r
235 Mockito.when(unsuccessfulClientInitResult.getDistributionActionResult())
\r
236 .thenReturn(DistributionActionResultEnum.GENERAL_ERROR);
\r
241 public final void initBeforeEachTest() throws MsoPropertiesException {
\r
243 msoPropertiesFactory.removeAllMsoProperties();
\r
244 msoPropertiesFactory.initializeMsoProperties(ASDCConfiguration.MSO_PROP_ASDC, ASDC_PROP);
\r
248 public static final void kill() throws MsoPropertiesException {
\r
250 msoPropertiesFactory.removeMsoProperties(ASDCConfiguration.MSO_PROP_ASDC);
\r
255 public void isResourceAlreadyDeployedAllotedResourceTest() {
\r
256 Mockito.when(resource1.getResourceType()).thenReturn("VF");
\r
257 Mockito.when(resource1.getCategory()).thenReturn("Allotted Resource");
\r
258 ToscaResourceInstaller tri = new ToscaResourceInstaller();
\r
261 tri.isResourceAlreadyDeployed(vrs);
\r
262 } catch (ArtifactInstallerException e) {
\r
266 @Test(expected=Exception.class)
\r
267 public void installTheResourceTest() {
\r
269 ToscaResourceStructure trs = new ToscaResourceStructure();
\r
270 trs.getAllottedResource();
\r
271 trs.getAllottedList();
\r
272 trs.getCatalogAllottedResourceCustomization();
\r
273 trs.getCatalogAllottedServiceToResourceCustomization();
\r
274 trs.getCatalogNetworkResource();
\r
275 trs.getCatalogNetworkResourceCustomization();
\r
276 trs.getCatalogResourceCustomization();
\r
277 trs.getCatalogService();
\r
278 trs.getCatalogTempNetworkHeatTemplateLookup();
\r
279 trs.getCatalogToscaCsar();
\r
280 trs.getCatalogVfModule();
\r
281 trs.getCatalogVfModuleCustomization();
\r
282 trs.getCatalogVfModuleToHeatFiles();
\r
283 trs.getCatalogVfServiceToResourceCustomization();
\r
284 trs.getCatalogVlServiceToResourceCustomization();
\r
285 trs.getCatalogVnfResCustomToVfModuleCustom();
\r
286 trs.getCatalogVnfResource();
\r
287 trs.getCatalogVnfResourceCustomization();
\r
288 trs.getEnvHeatTemplateUUID();
\r
289 trs.getHeatFilesUUID();
\r
290 trs.getHeatTemplateUUID();
\r
291 trs.getNetworkTypes();
\r
292 trs.getSdcCsarHelper();
\r
293 trs.getServiceMetadata();
\r
294 trs.getServiceToResourceCustomization();
\r
295 trs.getServiceVersion();
\r
296 trs.getToscaArtifact();
\r
298 trs.getVolHeatEnvTemplateUUID();
\r
299 trs.getVolHeatTemplateUUID();
\r
301 NodeTemplate nodeTemplate = Mockito.mock(NodeTemplate.class);
\r
302 List<NodeTemplate> alnt = new ArrayList<>();
\r
303 trs.setAllottedList(alnt);
\r
304 trs.setAllottedResource(new AllottedResource());
\r
305 trs.setCatalogAllottedResourceCustomization(new AllottedResourceCustomization());
\r
306 trs.setCatalogAllottedServiceToResourceCustomization(new ServiceToResourceCustomization());
\r
307 trs.setCatalogNetworkResource(new NetworkResource());
\r
308 trs.setCatalogNetworkResourceCustomization(new NetworkResourceCustomization());
\r
309 trs.setCatalogResourceCustomization(new AllottedResourceCustomization());
\r
310 trs.setCatalogService(new Service());
\r
311 trs.setCatalogTempNetworkHeatTemplateLookup(new TempNetworkHeatTemplateLookup());
\r
312 trs.setCatalogToscaCsar(new ToscaCsar());
\r
313 trs.setCatalogVfModule(new VfModule());
\r
314 trs.setCatalogVfModuleCustomization(new VfModuleCustomization());
\r
315 trs.setCatalogVfModuleToHeatFiles(new VfModuleToHeatFiles());
\r
316 trs.setCatalogVfServiceToResourceCustomization(new ServiceToResourceCustomization());
\r
317 trs.setCatalogVlServiceToResourceCustomization(new ServiceToResourceCustomization());
\r
318 trs.setCatalogVnfResCustomToVfModuleCustom(new VnfResCustomToVfModuleCustom());
\r
319 trs.setCatalogVnfResource(new VnfResource());
\r
320 trs.setCatalogVnfResourceCustomization(new VnfResourceCustomization());
\r
321 trs.setEnvHeatTemplateUUID("envHeatTemplateUUID");
\r
322 trs.setHeatFilesUUID("heatFilesUUID");
\r
323 trs.setHeatTemplateUUID("heatTemplateUUID");
\r
324 trs.setNetworkTypes(alnt);
\r
325 trs.setVolHeatTemplateUUID("volHeatTemplateUUID");
\r
326 trs.setSdcCsarHelper(new ISdcCsarHelper() {
\r
329 public boolean hasTopology(NodeTemplate arg0) {
\r
334 public NodeTemplate getVnfConfig(String arg0) {
\r
339 public List<NodeTemplate> getVfcListByVf(String arg0) {
\r
344 public List<Group> getVfModulesByVf(String arg0) {
\r
349 public String getTypeOfNodeTemplate(NodeTemplate arg0) {
\r
354 public List<NodeTemplate> getServiceVlList() {
\r
359 public List<NodeTemplate> getServiceVfList() {
\r
364 public String getServiceSubstitutionMappingsTypeName() {
\r
369 public List<NodeTemplate> getServiceNodeTemplatesByType(String arg0) {
\r
374 public List<NodeTemplate> getServiceNodeTemplates() {
\r
379 public List<NodeTemplate> getServiceNodeTemplateBySdcType(SdcTypes arg0) {
\r
384 public Map<String, Object> getServiceMetadataProperties() {
\r
389 public Metadata getServiceMetadata() {
\r
394 public List<Input> getServiceInputs() {
\r
399 public Object getServiceInputLeafValueOfDefaultAsObject(String arg0) {
\r
404 public String getServiceInputLeafValueOfDefault(String arg0) {
\r
409 public String getNodeTemplatePropertyLeafValue(NodeTemplate arg0, String arg1) {
\r
414 public Object getNodeTemplatePropertyAsObject(NodeTemplate arg0, String arg1) {
\r
419 public List<Pair<NodeTemplate, NodeTemplate>> getNodeTemplatePairsByReqName(List<NodeTemplate> arg0,
\r
420 List<NodeTemplate> arg1, String arg2) {
\r
425 public String getNodeTemplateCustomizationUuid(NodeTemplate arg0) {
\r
430 public List<NodeTemplate> getNodeTemplateChildren(NodeTemplate arg0) {
\r
435 public List<NodeTemplate> getNodeTemplateBySdcType(NodeTemplate arg0, SdcTypes arg1) {
\r
440 public String getMetadataPropertyValue(Metadata arg0, String arg1) {
\r
445 public List<NodeTemplate> getMembersOfVfModule(NodeTemplate arg0, Group arg1) {
\r
450 public String getGroupPropertyLeafValue(Group arg0, String arg1) {
\r
455 public Object getGroupPropertyAsObject(Group arg0, String arg1) {
\r
460 public Map<String, Map<String, Object>> getCpPropertiesFromVfcAsObject(NodeTemplate arg0) {
\r
465 public Map<String, Map<String, Object>> getCpPropertiesFromVfc(NodeTemplate arg0) {
\r
470 public List<NodeTemplate> getCpListByVf(String arg0) {
\r
475 public String getConformanceLevel() {
\r
480 public List<NodeTemplate> getAllottedResources() {
\r
485 public Map<String, String> filterNodeTemplatePropertiesByValue(NodeTemplate arg0, FilterType arg1,
\r
490 // trs.setServiceMetadata(new Metadata(new HashMap<>()));
\r
491 trs.setServiceToResourceCustomization(new ServiceToResourceCustomization());
\r
492 trs.setServiceVersion("1.0");
\r
493 trs.setToscaArtifact(new IArtifactInfo() {
\r
496 public List<IArtifactInfo> getRelatedArtifacts() {
\r
501 public IArtifactInfo getGeneratedArtifact() {
\r
506 public String getArtifactVersion() {
\r
511 public String getArtifactUUID() {
\r
516 public String getArtifactURL() {
\r
521 public String getArtifactType() {
\r
526 public Integer getArtifactTimeout() {
\r
531 public String getArtifactName() {
\r
536 public String getArtifactDescription() {
\r
541 public String getArtifactChecksum() {
\r
545 trs.setVfTypes(alnt);
\r
546 trs.setVnfAlreadyInstalled(true);
\r
547 trs.setVolHeatEnvTemplateUUID("volHeatEnvTemplateUUID");
\r
548 trs.isVnfAlreadyInstalled();
\r
550 trs.updateResourceStructure(artifactInfo1);
\r
551 ToscaResourceInstaller tri = new ToscaResourceInstaller();
\r
554 tri.installTheResource(trs, vrs);
\r
555 } catch (ArtifactInstallerException e) {
\r