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