Catalog alignment
[sdc.git] / ui-ci / src / main / java / org / openecomp / sdc / ci / tests / US / AddComponentInstancesArtifactsInCsar.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 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.openecomp.sdc.ci.tests.US;
22
23 import org.apache.commons.lang3.tuple.ImmutablePair;
24 import org.apache.http.HttpStatus;
25 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
26 import org.openecomp.sdc.be.model.ArtifactDefinition;
27 import org.openecomp.sdc.be.model.Component;
28 import org.openecomp.sdc.be.model.ComponentInstance;
29 import org.openecomp.sdc.be.model.Resource;
30 import org.openecomp.sdc.be.model.User;
31 import org.openecomp.sdc.ci.tests.datatypes.ArtifactReqDetails;
32 import org.openecomp.sdc.ci.tests.datatypes.HeatMetaFirstLevelDefinition;
33 import org.openecomp.sdc.ci.tests.datatypes.ResourceReqDetails;
34 import org.openecomp.sdc.ci.tests.datatypes.VendorLicenseModel;
35 import org.openecomp.sdc.ci.tests.datatypes.VendorSoftwareProductObject;
36 import org.openecomp.sdc.ci.tests.datatypes.enums.ArtifactTypeEnum;
37 import org.openecomp.sdc.ci.tests.datatypes.enums.UserRoleEnum;
38 import org.openecomp.sdc.ci.tests.datatypes.http.RestResponse;
39 import org.openecomp.sdc.ci.tests.execute.devCI.ArtifactFromCsar;
40 import org.openecomp.sdc.ci.tests.execute.setup.SetupCDTest;
41 import org.openecomp.sdc.ci.tests.pages.HomePage;
42 import org.openecomp.sdc.ci.tests.pages.ResourceGeneralPage;
43 import org.openecomp.sdc.ci.tests.pages.ToscaArtifactsPage;
44 import org.openecomp.sdc.ci.tests.utilities.FileHandling;
45 import org.openecomp.sdc.ci.tests.utilities.OnboardingUiUtils;
46 import org.openecomp.sdc.ci.tests.utils.general.AtomicOperationUtils;
47 import org.openecomp.sdc.ci.tests.utils.general.ElementFactory;
48 import org.openecomp.sdc.ci.tests.utils.general.VendorLicenseModelRestUtils;
49 import org.openecomp.sdc.ci.tests.utils.general.VendorSoftwareProductRestUtils;
50 import org.openecomp.sdc.ci.tests.utils.rest.ArtifactRestUtils;
51 import org.openecomp.sdc.ci.tests.utils.rest.ResponseParser;
52 import org.testng.Assert;
53 import org.testng.annotations.BeforeClass;
54 import org.testng.annotations.Test;
55
56 import java.io.File;
57 import java.util.ArrayList;
58 import java.util.Arrays;
59 import java.util.HashMap;
60 import java.util.LinkedList;
61 import java.util.List;
62 import java.util.Map;
63 import java.util.Random;
64
65 public class AddComponentInstancesArtifactsInCsar extends SetupCDTest {
66
67     public static final String DEPLOYMENT = "Deployment";
68     public static final String INFORMATIONAL = "Informational";
69     private String filePath;
70
71     @BeforeClass
72     public void beforeClass() {
73         filePath = System.getProperty("filePath");
74         if (filePath == null && System.getProperty("os.name").contains("Windows")) {
75             filePath = FileHandling.getResourcesFilesPath() + "AddComponentInstancesArtifactsInCsar" + File.separator;
76         } else if (filePath.isEmpty() && !System.getProperty("os.name").contains("Windows")) {
77             filePath = FileHandling.getBasePath() + File.separator + "Files" + File.separator + "AddComponentInstancesArtifactsInCsar" + File.separator;
78         }
79     }
80
81     // US847439 - Story [BE] - Add Component Instance's artifacts in CSAR
82     // TC1521795 - VF CSAR - The Flow
83     @Test
84     public void vfAndServiceCsarTheFlow() throws Exception {
85         ResourceReqDetails resourceMetaData = ElementFactory.getDefaultResourceByType(ResourceTypeEnum.VF, getUser());
86
87         String vnfFile = "FDNT.zip";
88         String snmpFile = "Fault-alarms-ASDC-vprobes-vLB.zip";
89
90         VendorLicenseModel vendorLicenseModel = VendorLicenseModelRestUtils.createVendorLicense(getUser());
91         ResourceReqDetails resourceReqDetails = ElementFactory.getDefaultResource();//getResourceReqDetails(ComponentConfigurationTypeEnum.DEFAULT);
92         VendorSoftwareProductObject createVSP = VendorSoftwareProductRestUtils.createVSP(resourceReqDetails, vnfFile, filePath, getUser(),
93             vendorLicenseModel);
94         String vspName = createVSP.getName();
95         resourceMetaData.setName(vspName);
96         VendorSoftwareProductRestUtils.addVFCArtifacts(filePath, snmpFile, null, createVSP, getUser());
97         VendorSoftwareProductRestUtils.prepareVspForUse(getUser(), createVSP, true);
98
99         HomePage.showVspRepository();
100         OnboardingUiUtils.importVSP(createVSP);
101         resourceMetaData.setVersion("0.1");
102         Resource vfResource = AtomicOperationUtils.getResourceObjectByNameAndVersion(UserRoleEnum.DESIGNER, resourceMetaData.getName(), resourceMetaData.getVersion());
103
104         Map<String, Object> artifacts = getArtifactsOfComponentAndComponentsInstance(vfResource);
105
106         List<ImmutablePair<ComponentInstance, ArtifactDefinition>> artifactsUploadedToComponentInstance = new LinkedList<>();
107         Random random = new Random();
108         final int randomIntForLoop = random.nextInt(10) + 10;
109         for (int i = 0; i < randomIntForLoop; i++) {
110             ImmutablePair<ComponentInstance, ArtifactDefinition> uploadArtifactOnRandomVfc = uploadArtifactOnRandomRI(vfResource);
111
112             if (uploadArtifactOnRandomVfc.getRight().getArtifactName() != null) {
113                 artifactsUploadedToComponentInstance.add(uploadArtifactOnRandomVfc);
114             }
115         }
116
117         if (!artifactsUploadedToComponentInstance.isEmpty()) {
118             Map<String, Object> artifactsOfResourceInstance = getArtifactsOfResourceInstance(artifactsUploadedToComponentInstance);
119             artifacts.put("Resources", artifactsOfResourceInstance);
120         }
121
122         ResourceGeneralPage.getLeftMenu().moveToToscaArtifactsScreen();
123         ToscaArtifactsPage.downloadCsar();
124         File latestFilefromDir = FileHandling.getLastModifiedFileNameFromDir();
125         Map<String, Object> combineHeatArtifacstWithFolderArtifacsToMap = ArtifactFromCsar.getVFCArtifacts(latestFilefromDir.getAbsolutePath());
126
127         compareArtifactFromFileStructureToArtifactsFromJavaObject(artifacts, combineHeatArtifacstWithFolderArtifacsToMap);
128
129     }
130
131     public void compareArtifactFromFileStructureToArtifactsFromJavaObject(Map<String, Object> artifactFromJavaObject, Map<String, Object> artifactsFromFileStructure) {
132         for (String key : artifactFromJavaObject.keySet()) {
133             if ((!key.equals(DEPLOYMENT)) && (!key.equals(INFORMATIONAL))) {
134                 Map<String, Object> newArtifactFromJavaObject = (Map<String, Object>) artifactFromJavaObject.get(key);
135                 Map<String, Object> newArtifactsFromFileStructure = (Map<String, Object>) artifactsFromFileStructure.get(key);
136                 compareArtifactFromFileStructureToArtifactsFromJavaObject(newArtifactFromJavaObject, newArtifactsFromFileStructure);
137             } else {
138                 compareArtifacts(artifactFromJavaObject.get(key), artifactsFromFileStructure.get(key));
139             }
140         }
141     }
142
143
144     private void compareArtifacts(Object artifactFromJavaObject, Object artifactsFromFileStructure) {
145         Map<String, List<String>> artifactsMap = (Map<String, List<String>>) artifactFromJavaObject;
146         List<HeatMetaFirstLevelDefinition> artifactsList = (List<HeatMetaFirstLevelDefinition>) artifactsFromFileStructure;
147
148         for (HeatMetaFirstLevelDefinition heatMetaFirstLevelDefinition : artifactsList) {
149             Assert.assertTrue(artifactsMap.get(heatMetaFirstLevelDefinition.getType()).contains(heatMetaFirstLevelDefinition.getFileName()),
150                     "Expected that artifacts will be the same. Not exists: " + heatMetaFirstLevelDefinition.getFileName() + " of type: " + heatMetaFirstLevelDefinition.getType());
151         }
152
153         for (String key : artifactsMap.keySet()) {
154             List<String> artifacts = artifactsMap.get(key);
155
156             for (HeatMetaFirstLevelDefinition heatMetaFirstLevelDefinition : artifactsList) {
157                 if (heatMetaFirstLevelDefinition.getType().equals(key)) {
158                     if (artifacts.contains(heatMetaFirstLevelDefinition.getFileName())) {
159                         artifacts.remove(heatMetaFirstLevelDefinition.getFileName());
160                     }
161                 }
162             }
163
164             Assert.assertEquals(artifacts.size(), 0, "Expected that all artifacts equal. There is artifacts which not equal: " + artifacts.toString());
165         }
166     }
167
168
169     public Map<String, Object> getArtifactsOfResourceInstance(List<ImmutablePair<ComponentInstance, ArtifactDefinition>> riList) {
170         Map<String, Object> artifacts = new HashMap<>();
171
172         for (ImmutablePair<ComponentInstance, ArtifactDefinition> ri : riList) {
173             ArtifactDefinition artifactDefinition = ri.getRight();
174             ComponentInstance componentInstance = ri.getLeft();
175             if (artifacts.containsKey(componentInstance.getNormalizedName())) {
176                 if (((Map<String, ArrayList<String>>) ((Map<String, Object>) artifacts.get(componentInstance.getNormalizedName())).get(DEPLOYMENT)).containsKey(artifactDefinition.getArtifactType())) {
177
178                     ((Map<String, ArrayList<String>>) ((Map<String, Object>) artifacts.get(componentInstance.getNormalizedName())).get(DEPLOYMENT)).get(artifactDefinition.getArtifactType()).add(artifactDefinition.getArtifactName());
179
180                 } else {
181                     ArrayList<String> list = new ArrayList<>();
182                     list.add(artifactDefinition.getArtifactName());
183                     ((Map<String, ArrayList<String>>) ((Map<String, Object>) artifacts.get(componentInstance.getNormalizedName())).get(DEPLOYMENT)).put(artifactDefinition.getArtifactType(), list);
184                 }
185
186             } else {
187                 try {
188
189
190                     ArrayList<String> list = new ArrayList<>();
191                     list.add(artifactDefinition.getArtifactName());
192
193                     Map<String, ArrayList<String>> map = new HashMap<>();
194                     map.put(artifactDefinition.getArtifactType(), list);
195
196                     Map<String, Map<String, ArrayList<String>>> addMap = new HashMap<>();
197                     addMap.put(DEPLOYMENT, map);
198
199                     artifacts.put(componentInstance.getNormalizedName(), addMap);
200                 } catch (Exception e) {
201                     Assert.fail("Artifact name is null for componentInstance: " + componentInstance.getNormalizedName());
202                 }
203             }
204         }
205         return artifacts;
206     }
207
208     public Map<String, Object> getArtifactsOfComponentAndComponentsInstance(Component component) {
209         Map<String, Object> artifacts = getArtifactsOfComponent(component);
210
211         for (ComponentInstance componentInstance : component.getComponentInstances()) {
212             Map<String, Object> artifactsOfComponentInstance = getArtifactsOfComponentInstance(componentInstance);
213             if (!artifactsOfComponentInstance.isEmpty()) {
214                 artifacts.put(componentInstance.getToscaComponentName() + "." + componentInstance.getComponentVersion(), artifactsOfComponentInstance);
215             }
216         }
217
218         return artifacts;
219     }
220
221     public Map<String, Object> getArtifactsOfComponentInstance(ComponentInstance componentInstance) {
222         Map<String, Object> map = new HashMap<>();
223
224         if (componentInstance.getArtifacts() != null) {
225             Map<String, Object> informationalArtifacts = getArtifacts(componentInstance.getArtifacts());
226             if (!informationalArtifacts.isEmpty()) {
227                 map.put(INFORMATIONAL, informationalArtifacts);
228             }
229         }
230
231         if (componentInstance.getDeploymentArtifacts() != null) {
232             Map<String, Object> deploymentArtifacts = getArtifacts(componentInstance.getDeploymentArtifacts());
233             if (!deploymentArtifacts.isEmpty()) {
234                 map.put(DEPLOYMENT, deploymentArtifacts);
235             }
236         }
237
238         return map;
239     }
240
241     public Map<String, Object> getArtifactsOfComponent(Component component) {
242         Map<String, Object> map = new HashMap<>();
243
244         if (component.getArtifacts() != null) {
245             Map<String, Object> informationalArtifacts = getArtifacts(component.getArtifacts());
246             if (!informationalArtifacts.isEmpty()) {
247                 map.put(INFORMATIONAL, informationalArtifacts);
248             }
249         }
250
251         if (component.getDeploymentArtifacts() != null) {
252             Map<String, Object> deploymentArtifacts = getArtifacts(component.getDeploymentArtifacts());
253             if (!deploymentArtifacts.isEmpty()) {
254                 map.put(DEPLOYMENT, deploymentArtifacts);
255             }
256         }
257
258         return map;
259     }
260
261     public Map<String, Object> getArtifacts(Map<String, ArtifactDefinition> artifacts) {
262         Map<String, Object> map = new HashMap<>();
263
264         for (String artifact : artifacts.keySet()) {
265             ArtifactDefinition artifactDefinition = artifacts.get(artifact);
266             if ((artifactDefinition.getEsId() != null) && (!artifactDefinition.getEsId().equals("")) && (!artifactDefinition.getArtifactType().equals("HEAT_ENV"))) {
267                 if (map.containsKey(artifactDefinition.getArtifactType())) {
268                     ((List<String>) map.get(artifactDefinition.getArtifactType())).add(artifactDefinition.getArtifactName());
269                 } else {
270                     ArrayList<String> list = new ArrayList<>();
271                     list.add(artifactDefinition.getArtifactName());
272                     map.put(artifactDefinition.getArtifactType(), list);
273                 }
274             }
275         }
276
277         return map;
278     }
279
280     public ImmutablePair<ComponentInstance, ArtifactDefinition> uploadArtifactOnRandomRI(Component component) throws Exception {
281         ArtifactReqDetails artifactReqDetails = getRandomArtifact();
282         Random random = new Random();
283         int randInt = random.nextInt(component.getComponentInstances().size());
284         User defaultUser = ElementFactory.getDefaultUser(getRole());
285         ComponentInstance componentInstance = component.getComponentInstances().get(randInt);
286
287         RestResponse uploadArtifactRestResponse = ArtifactRestUtils.externalAPIUploadArtifactOfComponentInstanceOnAsset(component, defaultUser, artifactReqDetails, componentInstance);
288
289         // Check response of external API
290         Integer responseCode = uploadArtifactRestResponse.getErrorCode();
291         Assert.assertEquals(responseCode, (Integer) HttpStatus.SC_OK, "Response code is not correct.");
292
293         ImmutablePair<ComponentInstance, ArtifactDefinition> pair = ImmutablePair.of(componentInstance, ResponseParser.convertArtifactDefinitionResponseToJavaObject(uploadArtifactRestResponse.getResponse()));
294
295         return pair;
296     }
297
298     public ImmutablePair<ComponentInstance, ArtifactDefinition> uploadArtifactOnRandomRI(Resource resource) throws Exception {
299         ArtifactReqDetails artifactReqDetails = getRandomVfcArtifact();
300         Random random = new Random();
301         int randInt = random.nextInt(resource.getComponentInstances().size());
302         User defaultUser = ElementFactory.getDefaultUser(getRole());
303         ComponentInstance componentInstance = resource.getComponentInstances().get(randInt);
304
305         RestResponse uploadArtifactRestResponse = ArtifactRestUtils.externalAPIUploadArtifactOfComponentInstanceOnAsset(resource, defaultUser, artifactReqDetails, componentInstance);
306         // Check response of external API
307         Integer responseCode = uploadArtifactRestResponse.getErrorCode();
308         Assert.assertEquals(responseCode, (Integer) HttpStatus.SC_OK, "Response code is not correct.");
309         ImmutablePair<ComponentInstance, ArtifactDefinition> pair = ImmutablePair.of(componentInstance, ResponseParser.convertArtifactDefinitionResponseToJavaObject(uploadArtifactRestResponse.getResponse()));
310         return pair;
311     }
312
313     public ArtifactReqDetails getRandomArtifact() throws Exception {
314         List<String> artifactsTypeList = Arrays.asList("Other");
315         return getRandomArtifact(artifactsTypeList);
316     }
317
318     public ArtifactReqDetails getRandomVfcArtifact() throws Exception {
319         List<String> vfcArtifactsTypeList = Arrays.asList(
320                 ArtifactTypeEnum.DCAE_INVENTORY_TOSCA.getType(),
321                 ArtifactTypeEnum.DCAE_INVENTORY_JSON.getType(),
322                 ArtifactTypeEnum.DCAE_INVENTORY_POLICY.getType(),
323                 ArtifactTypeEnum.DCAE_INVENTORY_DOC.getType(),
324                 ArtifactTypeEnum.DCAE_INVENTORY_BLUEPRINT.getType(),
325                 ArtifactTypeEnum.DCAE_INVENTORY_EVENT.getType(),
326                 ArtifactTypeEnum.SNMP_POLL.getType(),
327                 ArtifactTypeEnum.SNMP_TRAP.getType());
328         return getRandomArtifact(vfcArtifactsTypeList);
329     }
330
331     public ArtifactReqDetails getRandomArtifact(List<String> artifactType) throws Exception {
332         Random random = new Random();
333         return ElementFactory.getArtifactByType("ci", artifactType.get(random.nextInt(artifactType.size())), true, false);
334     }
335
336     @Override
337     protected UserRoleEnum getRole() {
338         return UserRoleEnum.DESIGNER;
339     }
340
341 }