04a9ea0a1e964a73e4caf297db69f3f0a4d09183
[so.git] /
1 /*-\r
2  * ============LICENSE_START=======================================================\r
3  * ONAP - SO\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
10  *\r
11  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  *\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
19  */\r
20 \r
21 package org.openecomp.mso.asdc.installer.heat.tests;\r
22 \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 \r
33 import org.apache.commons.codec.binary.Base64;\r
34 import org.junit.AfterClass;\r
35 import org.junit.Before;\r
36 import org.junit.BeforeClass;\r
37 import org.junit.Test;\r
38 import org.mockito.Mockito;\r
39 import org.openecomp.mso.asdc.client.ASDCConfiguration;\r
40 import org.openecomp.mso.asdc.client.exceptions.ArtifactInstallerException;\r
41 import org.openecomp.mso.asdc.client.tests.ASDCControllerTest;\r
42 import org.openecomp.mso.asdc.installer.VfResourceStructure;\r
43 import org.openecomp.mso.asdc.installer.heat.VfResourceInstaller;\r
44 import org.openecomp.mso.db.catalog.CatalogDatabase;\r
45 import org.openecomp.mso.db.catalog.beans.AllottedResourceCustomization;\r
46 import org.openecomp.mso.db.catalog.beans.NetworkResourceCustomization;\r
47 import org.openecomp.mso.db.catalog.beans.Service;\r
48 import org.openecomp.mso.db.catalog.beans.ServiceToAllottedResources;\r
49 import org.openecomp.mso.db.catalog.beans.ServiceToNetworks;\r
50 import org.openecomp.mso.db.catalog.beans.VnfResource;\r
51 import org.openecomp.mso.properties.MsoJavaProperties;\r
52 import org.openecomp.mso.properties.MsoPropertiesException;\r
53 import org.openecomp.mso.properties.MsoPropertiesFactory;\r
54 import org.openecomp.sdc.api.IDistributionClient;\r
55 import org.openecomp.sdc.api.notification.IArtifactInfo;\r
56 import org.openecomp.sdc.api.notification.INotificationData;\r
57 import org.openecomp.sdc.api.notification.IResourceInstance;\r
58 import org.openecomp.sdc.api.results.IDistributionClientDownloadResult;\r
59 import org.openecomp.sdc.api.results.IDistributionClientResult;\r
60 import org.openecomp.sdc.utils.DistributionActionResultEnum;\r
61 \r
62 import mockit.Mock;\r
63 import mockit.MockUp;\r
64 \r
65 public class VfResourceInstallerTest {\r
66     private static MsoPropertiesFactory msoPropertiesFactory = new MsoPropertiesFactory();\r
67 \r
68     private static String heatExample;\r
69     private static String heatExampleMD5HashBase64;\r
70 \r
71     private static INotificationData iNotif;\r
72 \r
73     private static IDistributionClientDownloadResult downloadResult;\r
74     private static IDistributionClientDownloadResult downloadCorruptedResult;\r
75 \r
76     private static IDistributionClientResult successfulClientInitResult;\r
77     private static IDistributionClientResult unsuccessfulClientInitResult;\r
78 \r
79     private static IDistributionClient distributionClient;\r
80 \r
81     private static IArtifactInfo artifactInfo1;\r
82 \r
83     private static IResourceInstance resource1;\r
84 \r
85     private static VfResourceStructure vrs;\r
86 \r
87     public static final String ASDC_PROP = MsoJavaProperties.class.getClassLoader().getResource("mso.json").toString()\r
88             .substring(5);\r
89     public static final String ASDC_PROP2 = MsoJavaProperties.class.getClassLoader().getResource("mso2.json").toString()\r
90             .substring(5);\r
91     public static final String ASDC_PROP3 = MsoJavaProperties.class.getClassLoader().getResource("mso3.json").toString()\r
92             .substring(5);\r
93     public static final String ASDC_PROP_BAD = MsoJavaProperties.class.getClassLoader().getResource("mso-bad.json")\r
94             .toString().substring(5);\r
95     public static final String ASDC_PROP_WITH_NULL = MsoJavaProperties.class.getClassLoader()\r
96             .getResource("mso-with-NULL.json").toString().substring(5);\r
97 \r
98     @BeforeClass\r
99     public static final void prepareMockNotification() throws MsoPropertiesException, IOException, URISyntaxException,\r
100             NoSuchAlgorithmException, ArtifactInstallerException {\r
101 \r
102         heatExample = new String(Files.readAllBytes(Paths.get(\r
103                 ASDCControllerTest.class.getClassLoader().getResource("resource-examples/autoscaling.yaml").toURI())));\r
104         MessageDigest md = MessageDigest.getInstance("MD5");\r
105         byte[] md5Hash = md.digest(heatExample.getBytes());\r
106         heatExampleMD5HashBase64 = Base64.encodeBase64String(md5Hash);\r
107 \r
108         iNotif = Mockito.mock(INotificationData.class);\r
109 \r
110         // Create fake ArtifactInfo\r
111         artifactInfo1 = Mockito.mock(IArtifactInfo.class);\r
112         Mockito.when(artifactInfo1.getArtifactChecksum()).thenReturn(VfResourceInstallerTest.heatExampleMD5HashBase64);\r
113 \r
114         Mockito.when(artifactInfo1.getArtifactName()).thenReturn("artifact1");\r
115         Mockito.when(artifactInfo1.getArtifactType()).thenReturn(ASDCConfiguration.HEAT);\r
116         Mockito.when(artifactInfo1.getArtifactURL())\r
117                 .thenReturn("https://localhost:8080/v1/catalog/services/srv1/2.0/resources/aaa/1.0/artifacts/aaa.yml");\r
118         Mockito.when(artifactInfo1.getArtifactUUID()).thenReturn("UUID1");\r
119         Mockito.when(artifactInfo1.getArtifactDescription()).thenReturn("testos artifact1");\r
120 \r
121         distributionClient = Mockito.mock(IDistributionClient.class);\r
122 \r
123         // Now provision the NotificationData mock\r
124         List<IArtifactInfo> listArtifact = new ArrayList<>();\r
125         listArtifact.add(artifactInfo1);\r
126 \r
127         // Create fake resource Instance\r
128         resource1 = Mockito.mock(IResourceInstance.class);\r
129 //              Mockito.when(resource1.getResourceType()).thenReturn("VF");\r
130         Mockito.when(resource1.getResourceName()).thenReturn("resourceName");\r
131         Mockito.when(resource1.getArtifacts()).thenReturn(listArtifact);\r
132 \r
133         List<IResourceInstance> resources = new ArrayList<>();\r
134         resources.add(resource1);\r
135 \r
136         Mockito.when(iNotif.getResources()).thenReturn(resources);\r
137         Mockito.when(iNotif.getDistributionID()).thenReturn("distributionID1");\r
138         Mockito.when(iNotif.getServiceName()).thenReturn("serviceName1");\r
139         Mockito.when(iNotif.getServiceUUID()).thenReturn("serviceNameUUID1");\r
140         Mockito.when(iNotif.getServiceVersion()).thenReturn("1.0");\r
141 \r
142         downloadResult = Mockito.mock(IDistributionClientDownloadResult.class);\r
143         Mockito.when(downloadResult.getArtifactPayload()).thenReturn(heatExample.getBytes());\r
144         Mockito.when(downloadResult.getDistributionActionResult()).thenReturn(DistributionActionResultEnum.SUCCESS);\r
145         Mockito.when(downloadResult.getDistributionMessageResult()).thenReturn("Success");\r
146 \r
147         downloadCorruptedResult = Mockito.mock(IDistributionClientDownloadResult.class);\r
148         Mockito.when(downloadCorruptedResult.getArtifactPayload()).thenReturn((heatExample + "badone").getBytes());\r
149         Mockito.when(downloadCorruptedResult.getDistributionActionResult())\r
150                 .thenReturn(DistributionActionResultEnum.SUCCESS);\r
151         Mockito.when(downloadCorruptedResult.getDistributionMessageResult()).thenReturn("Success");\r
152 \r
153         vrs = new VfResourceStructure(iNotif, resource1);\r
154         try {\r
155             vrs.addArtifactToStructure(distributionClient, artifactInfo1, downloadResult);\r
156         } catch (UnsupportedEncodingException e) {\r
157             e.printStackTrace();\r
158         }\r
159         try {\r
160             vrs.createVfModuleStructures();\r
161         } catch (ArtifactInstallerException e) {\r
162             e.printStackTrace();\r
163         }\r
164         vrs.getNotification();\r
165         vrs.getArtifactsMapByUUID();\r
166         vrs.getCatalogNetworkResourceCustomization();\r
167         vrs.getCatalogResourceCustomization();\r
168         vrs.getCatalogService();\r
169         vrs.getCatalogServiceToAllottedResources();\r
170         vrs.getCatalogServiceToNetworks();\r
171         vrs.getCatalogVnfResource();\r
172         vrs.getResourceInstance();\r
173         vrs.getVfModulesStructureList();\r
174         vrs.getVfModuleStructure();\r
175         vrs.setCatalogNetworkResourceCustomization(new NetworkResourceCustomization());\r
176         vrs.setCatalogResourceCustomization(new AllottedResourceCustomization());\r
177         vrs.setCatalogService(new Service());\r
178         vrs.setCatalogServiceToAllottedResources(new ServiceToAllottedResources());\r
179         vrs.setCatalogServiceToNetworks(new ServiceToNetworks());\r
180         vrs.setCatalogVnfResource(new VnfResource());\r
181         vrs.setSuccessfulDeployment();\r
182 \r
183         AllottedResourceCustomization arc = new AllottedResourceCustomization();\r
184         arc.setModelCustomizationUuid("modelCustomizationUuid");\r
185         List<AllottedResourceCustomization> allottedResources = new ArrayList<>();\r
186         allottedResources.add(arc);\r
187 \r
188         NetworkResourceCustomization nrc = new NetworkResourceCustomization();\r
189         nrc.setModelCustomizationUuid("modelCustomizationUuid");\r
190         List<NetworkResourceCustomization> networkResources = new ArrayList<>();\r
191         networkResources.add(nrc);\r
192 \r
193         new MockUp<CatalogDatabase>() {\r
194             @Mock\r
195             public List<AllottedResourceCustomization> getAllAllottedResourcesByServiceModelUuid(String serviceModelUuid) {\r
196                 return allottedResources;\r
197             }\r
198         };\r
199         new MockUp<CatalogDatabase>() {\r
200             @Mock\r
201             public List<NetworkResourceCustomization> getAllNetworksByServiceModelUuid(String serviceModelUuid) {\r
202                 return networkResources;\r
203             }\r
204         };\r
205 \r
206         // Mock now the ASDC distribution client behavior\r
207         successfulClientInitResult = Mockito.mock(IDistributionClientResult.class);\r
208         Mockito.when(successfulClientInitResult.getDistributionActionResult())\r
209                 .thenReturn(DistributionActionResultEnum.SUCCESS);\r
210 \r
211         unsuccessfulClientInitResult = Mockito.mock(IDistributionClientResult.class);\r
212         Mockito.when(unsuccessfulClientInitResult.getDistributionActionResult())\r
213                 .thenReturn(DistributionActionResultEnum.GENERAL_ERROR);\r
214 \r
215     }\r
216 \r
217     @Before\r
218     public final void initBeforeEachTest() throws MsoPropertiesException {\r
219         // load the config\r
220         msoPropertiesFactory.removeAllMsoProperties();\r
221         msoPropertiesFactory.initializeMsoProperties(ASDCConfiguration.MSO_PROP_ASDC, ASDC_PROP);\r
222     }\r
223 \r
224     @AfterClass\r
225     public static final void kill() throws MsoPropertiesException {\r
226 \r
227         msoPropertiesFactory.removeMsoProperties(ASDCConfiguration.MSO_PROP_ASDC);\r
228 \r
229     }\r
230 \r
231     @Test\r
232     public void isResourceAlreadyDeployedAllotedResourceTest() {\r
233 \r
234         Mockito.when(resource1.getResourceType()).thenReturn("VF");\r
235         Mockito.when(resource1.getCategory()).thenReturn("Allotted Resource");\r
236         VfResourceInstaller vfri = new VfResourceInstaller();\r
237 \r
238         try {\r
239             vfri.isResourceAlreadyDeployed(vrs);\r
240         } catch (ArtifactInstallerException e) {\r
241         }\r
242 \r
243     }\r
244 \r
245     @Test\r
246     public void isResourceAlreadyDeployedTest() {\r
247 \r
248         Mockito.when(resource1.getResourceType()).thenReturn("VF");\r
249         Mockito.when(resource1.getCategory()).thenReturn("Not Allotted Resource");\r
250         VfResourceInstaller vfri = new VfResourceInstaller();\r
251 \r
252         try {\r
253             vfri.isResourceAlreadyDeployed(vrs);\r
254         } catch (ArtifactInstallerException e) {\r
255         }\r
256 \r
257     }\r
258 \r
259     @Test\r
260     public void isResourceAlreadyDeployedDuplicateNtwrkTest() {\r
261 \r
262         Mockito.when(resource1.getResourceType()).thenReturn("VL");\r
263         Mockito.when(resource1.getCategory()).thenReturn("Not Allotted Resource");\r
264         VfResourceInstaller vfri = new VfResourceInstaller();\r
265 \r
266         try {\r
267             vfri.isResourceAlreadyDeployed(vrs);\r
268         } catch (ArtifactInstallerException e) {\r
269         }\r
270 \r
271     }\r
272 \r
273     @Test(expected = Exception.class)\r
274     public void installTheResourceTest() {\r
275         VfResourceInstaller vfri = new VfResourceInstaller();\r
276         try {\r
277             vfri.installTheResource(vrs);\r
278         } catch (ArtifactInstallerException e) {\r
279         }\r
280     }\r
281 }\r