[SDC-29] rebase continue work to align source
[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 java.io.File;
24 import java.io.IOException;
25 import java.util.ArrayList;
26 import java.util.Arrays;
27 import java.util.HashMap;
28 import java.util.LinkedList;
29 import java.util.List;
30 import java.util.Map;
31 import java.util.Random;
32
33 import org.apache.commons.lang3.tuple.ImmutablePair;
34 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
35 import org.openecomp.sdc.be.model.ArtifactDefinition;
36 import org.openecomp.sdc.be.model.Component;
37 import org.openecomp.sdc.be.model.ComponentInstance;
38 import org.openecomp.sdc.be.model.Resource;
39 import org.openecomp.sdc.be.model.User;
40 import org.openecomp.sdc.ci.tests.datatypes.ArtifactReqDetails;
41 import org.openecomp.sdc.ci.tests.datatypes.HeatMetaFirstLevelDefinition;
42 import org.openecomp.sdc.ci.tests.datatypes.ResourceReqDetails;
43 import org.openecomp.sdc.ci.tests.datatypes.enums.UserRoleEnum;
44 import org.openecomp.sdc.ci.tests.datatypes.http.RestResponse;
45 import org.openecomp.sdc.ci.tests.execute.devCI.ArtifactFromCsar;
46 import org.openecomp.sdc.ci.tests.execute.setup.SetupCDTest;
47 import org.openecomp.sdc.ci.tests.pages.HomePage;
48 import org.openecomp.sdc.ci.tests.pages.ResourceGeneralPage;
49 import org.openecomp.sdc.ci.tests.pages.ToscaArtifactsPage;
50 import org.openecomp.sdc.ci.tests.utilities.FileHandling;
51 import org.openecomp.sdc.ci.tests.utilities.OnboardingUtils;
52 import org.openecomp.sdc.ci.tests.utils.general.AtomicOperationUtils;
53 import org.openecomp.sdc.ci.tests.utils.general.ElementFactory;
54 import org.openecomp.sdc.ci.tests.utils.rest.ArtifactRestUtils;
55 import org.testng.Assert;
56 import org.testng.SkipException;
57 import org.testng.annotations.BeforeClass;
58 import org.testng.annotations.Test;
59
60 import com.aventstack.extentreports.Status;
61 import com.clearspring.analytics.util.Pair;
62
63
64 public class AddComponentInstancesArtifactsInCsar extends SetupCDTest {
65         
66         private String filePath;
67         @BeforeClass
68         public void beforeClass(){
69                 filePath = System.getProperty("filepath");
70                 if (filePath == null && System.getProperty("os.name").contains("Windows")) {
71                         filePath = FileHandling.getResourcesFilesPath() + "AddComponentInstancesArtifactsInCsar"+ File.separator;
72                 }
73                 else if(filePath.isEmpty() && !System.getProperty("os.name").contains("Windows")){
74                         filePath = FileHandling.getBasePath() + File.separator + "Files" + File.separator + "AddComponentInstancesArtifactsInCsar"+ File.separator;
75                 }
76         }
77         
78         // US847439 - Story [BE] - Add Component Instance's artifacts in CSAR
79         // TC1521795 - VF CSAR - The Flow
80         @Test
81         public void vfAndServicerCsarTheFlow() throws Exception{
82                 ResourceReqDetails resourceMetaData = ElementFactory.getDefaultResourceByType(ResourceTypeEnum.VF, getUser());
83                 
84                 String vnfFile = "FDNT.zip";
85                 String snmpFile = "Fault-alarms-ASDC-vprobes-vLB.zip";
86                 
87                 OnboardingUtils.createVendorLicense(getUser());
88                 Pair<String, Map<String, String>> createVSP = OnboardingUtils.createVSP(vnfFile, filePath, getUser());
89                 String vspName = createVSP.left;
90                 resourceMetaData.setName(vspName);
91                 Map<String, String> resourceMeta = createVSP.right;
92                 String vspid = resourceMeta.get("vspId");
93                 OnboardingUtils.addVFCArtifacts(filePath, snmpFile, null, vspid, getUser());
94                 OnboardingUtils.prepareVspForUse(getUser(), vspid);
95
96                 HomePage.showVspRepository();
97                 OnboardingUtils.importVSP(createVSP);
98                 resourceMetaData.setVersion("0.1");
99                 Resource vfResource = AtomicOperationUtils.getResourceObjectByNameAndVersion(UserRoleEnum.DESIGNER, resourceMetaData.getName(), resourceMetaData.getVersion());
100
101                 
102                 Map<String, Object> artifacts = getArtifactsOfComponentAndComponentsInstance(vfResource);
103
104                 List<ImmutablePair<ComponentInstance, ArtifactDefinition>> artifactsUploadedToComponentInstance = new LinkedList<>();
105                 Random random = new Random();
106                 for(int i=0; i<random.nextInt(10) + 10; i++) {
107                         ImmutablePair<ComponentInstance, ArtifactDefinition> uploadArtifactOnRandomVfc = uploadArtifactOnRandomRI(vfResource);
108
109                         if(uploadArtifactOnRandomVfc.getRight().getArtifactName() != null) {
110                                 artifactsUploadedToComponentInstance.add(uploadArtifactOnRandomVfc);
111                         }
112                 }
113                 
114                 if(artifactsUploadedToComponentInstance.size() > 0) {
115                         Map<String, Object> artifactsOfResourceInstance = getArtifactsOfResourceInstance(artifactsUploadedToComponentInstance);
116                         artifacts.put("Resources", artifactsOfResourceInstance);
117                 }
118                 
119                 
120                 ResourceGeneralPage.getLeftMenu().moveToToscaArtifactsScreen();
121                 ToscaArtifactsPage.downloadCsar();
122                 File latestFilefromDir = FileHandling.getLastModifiedFileFromDir();
123                 Map<String, Object> combineHeatArtifacstWithFolderArtifacsToMap = ArtifactFromCsar.getVFCArtifacts(latestFilefromDir.getAbsolutePath());
124                 
125                 compareArtifactFromFileStructureToArtifactsFromJavaObject(artifacts, combineHeatArtifacstWithFolderArtifacsToMap);
126                 
127                 
128 //              ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
129 //              ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
130 //              ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
131 //              
132 //              // Submit for testing + certify
133 //              DeploymentArtifactPage.clickSubmitForTestingButton(vspName);
134 //
135 //              reloginWithNewRole(UserRoleEnum.TESTER);
136 //              GeneralUIUtils.findComponentAndClick(vspName);
137 //              TesterOperationPage.certifyComponent(vspName);
138 //
139 //              reloginWithNewRole(UserRoleEnum.DESIGNER);
140 //              // create service
141 //              ServiceReqDetails serviceMetadata = ElementFactory.getDefaultService();
142 //              ServiceUIUtils.createService(serviceMetadata, getUser());
143 //              serviceMetadata.setVersion("0.1");
144 //              
145 //              
146 //              // Upload informationl artifact to service
147 //              ServiceGeneralPage.getLeftMenu().moveToCompositionScreen();
148 //              
149 //              String HEAT_FILE_YAML_NAME = "Heat-File.yaml";
150 //              String DESCRIPTION = "kuku";
151 //              String ARTIFACT_LABEL = "artifact3";
152 //              
153 //              ArtifactInfo artifact = new ArtifactInfo(filePath, HEAT_FILE_YAML_NAME, DESCRIPTION, ARTIFACT_LABEL,"OTHER");
154 //              CompositionPage.showDeploymentArtifactTab();
155 //              CompositionPage.clickAddArtifactButton();
156 //              ArtifactUIUtils.fillAndAddNewArtifactParameters(artifact, CompositionPage.artifactPopup());
157 //              
158 //              ArtifactInfo informationArtifact = new ArtifactInfo(filePath, "asc_heat 0 2.yaml", "kuku", "artifact1", "GUIDE");
159 //              CompositionPage.showInformationArtifactTab();
160 //              CompositionPage.clickAddArtifactButton();
161 //              ArtifactUIUtils.fillAndAddNewArtifactParameters(informationArtifact, CompositionPage.artifactPopup());
162 //              
163 //              
164 //              
165 //              // Add component instance to canvas of the service
166 //              CompositionPage.searchForElement(vspName);
167 //              CanvasManager serviceCanvasManager = CanvasManager.getCanvasManager();
168 //              CanvasElement vfElement = serviceCanvasManager.createElementOnCanvas(vspName);
169 //              
170 //              Service service = AtomicOperationUtils.getServiceObjectByNameAndVersion(UserRoleEnum.DESIGNER, serviceMetadata.getName(), serviceMetadata.getVersion());
171 //              
172 ////            ArtifactReqDetails artifactReqDetails = ElementFactory.getArtifactByType("ci", "OTHER", true, false);
173 ////            RestResponse restResponse = ArtifactRestUtils.externalAPIUploadArtifactOfTheAsset(service, getUser(), artifactReqDetails);
174 ////            Integer responseCode = restResponse.getErrorCode();
175 ////            Assert.assertEquals(responseCode, (Integer)200, "Response code is not correct.");
176 ////            
177 ////            service = AtomicOperationUtils.getServiceObjectByNameAndVersion(UserRoleEnum.DESIGNER, serviceMetadata.getName(), serviceMetadata.getVersion());
178 //              
179 //              Map<String, Object> artifactsService = getArtifactsOfComponentAndComponentsInstance(service);
180 //              
181 //              System.out.println("12354");
182 //              
183 //              artifactsService.put(vfResource.getToscaResourceName(), artifacts);
184 //              
185 //              System.out.println("1234");
186
187         }
188         
189         public void compareArtifactFromFileStructureToArtifactsFromJavaObject(Map<String, Object> artifactFromJavaObject, Map<String, Object> artifactsFromFileStructure) {
190                 for(String key: artifactFromJavaObject.keySet()) {
191                         if((!key.equals("Deployment")) && (!key.equals("Informational"))) {
192                                 Map<String, Object> newArtifactFromJavaObject = (Map<String, Object>) artifactFromJavaObject.get(key);
193                                 Map<String, Object> newArtifactsFromFileStructure = (Map<String, Object>) artifactsFromFileStructure.get(key);
194                                 compareArtifactFromFileStructureToArtifactsFromJavaObject(newArtifactFromJavaObject, newArtifactsFromFileStructure);
195                         } else {
196                                 compareArtifacts(artifactFromJavaObject.get(key), artifactsFromFileStructure.get(key));
197                         }
198                         
199                         
200                         
201                         
202                 }
203         }
204         
205         
206         private void compareArtifacts(Object artifactFromJavaObject, Object artifactsFromFileStructure) {       
207                 Map<String, List<String>> artifactsMap = (Map<String, List<String>>) artifactFromJavaObject;
208                 List<HeatMetaFirstLevelDefinition> artifactsList = (List<HeatMetaFirstLevelDefinition>) artifactsFromFileStructure;
209
210                 for(HeatMetaFirstLevelDefinition heatMetaFirstLevelDefinition: artifactsList) {
211                         Assert.assertTrue(artifactsMap.get(heatMetaFirstLevelDefinition.getType()).contains(heatMetaFirstLevelDefinition.getFileName()), 
212                                         "Expected that artifacts will be the same. Not exists: " + heatMetaFirstLevelDefinition.getFileName() + " of type: " + heatMetaFirstLevelDefinition.getType());
213                 }
214                 
215                 for(String key: artifactsMap.keySet()) {
216                         List<String> artifacts = artifactsMap.get(key);
217                         
218                         for(HeatMetaFirstLevelDefinition heatMetaFirstLevelDefinition: artifactsList) {
219                                 if(heatMetaFirstLevelDefinition.getType().equals(key)) {
220                                         if(artifacts.contains(heatMetaFirstLevelDefinition.getFileName())) {
221                                                 artifacts.remove(heatMetaFirstLevelDefinition.getFileName());
222                                         }
223                                 }
224                         }
225                         
226                         Assert.assertEquals(artifacts.size(), 0, "Expected that all artifacts equal. There is artifacts which not equal: " + artifacts.toString());
227                 }
228         }
229         
230
231         public Map<String, Object> getArtifactsOfResourceInstance(List<ImmutablePair<ComponentInstance, ArtifactDefinition>> riList) {
232                 Map<String, Object> artifacts = new HashMap<>();
233                 
234                 for(ImmutablePair<ComponentInstance, ArtifactDefinition> ri: riList) {
235                         ArtifactDefinition artifactDefinition = ri.getRight();
236                         ComponentInstance componentInstance = ri.getLeft();
237                         if(artifacts.containsKey(componentInstance.getNormalizedName())) {
238                                 if( ((Map<String, ArrayList<String>>)((Map<String, Object>)artifacts.get(componentInstance.getNormalizedName())).get("Deployment")).containsKey(artifactDefinition.getArtifactType()) ) {
239
240                                         ((Map<String, ArrayList<String>>)((Map<String, Object>) artifacts.get(componentInstance.getNormalizedName())).get("Deployment")).get(artifactDefinition.getArtifactType()).add(artifactDefinition.getArtifactName());
241
242                                 } else {
243                                         ArrayList<String> list = new ArrayList<String>();
244                                         list.add(artifactDefinition.getArtifactName());                         
245                                         ((Map<String, ArrayList<String>>)((Map<String, Object>) artifacts.get(componentInstance.getNormalizedName())).get("Deployment")).put(artifactDefinition.getArtifactType(), list);
246                                 }       
247                 
248                         } else {
249                                 try {
250                                         
251                                         
252                                         ArrayList<String> list = new ArrayList<String>();
253                                         list.add(artifactDefinition.getArtifactName());
254                                         
255                                         Map<String, ArrayList<String>> map = new HashMap<>();
256                                         map.put(artifactDefinition.getArtifactType(), list);
257                                         
258                                         Map<String, Map<String, ArrayList<String>>> addMap = new HashMap<>();
259                                         addMap.put("Deployment", map);
260                                         
261                                         artifacts.put(componentInstance.getNormalizedName(), addMap);
262                                         
263 //                                      if(artifacts.size() == 0) {
264 //                                              artifacts.put("Deployment", addMap);
265 //                                      } else {
266 //                                              ((Map<String, Map<String, ArrayList<String>>>) artifacts.get("Deployment")).putAll(addMap);
267 //                                      }
268                                 } catch (Exception e) {
269                                         Assert.fail("Artifact name is null for componentInstance: " + componentInstance.getNormalizedName());
270                                 }
271                         }
272                 }
273                 return artifacts;
274         }
275         
276         public Map<String, Object> getArtifactsOfComponentAndComponentsInstance(Component component) {
277                 Map<String, Object> artifacts = getArtifacstOfComponent(component);
278                 
279                 for(ComponentInstance componentInstance: component.getComponentInstances()) {
280                         Map<String, Object> artifacstOfComponentInstance = getArtifacstOfComponentInstance(componentInstance);
281                         if(artifacstOfComponentInstance.size() > 0) {
282                                 artifacts.put(componentInstance.getToscaComponentName() + "." + componentInstance.getComponentVersion(), artifacstOfComponentInstance);
283                         }
284                 }
285                 
286                 return artifacts;
287         }
288         
289         public Map<String, Object> getArtifacstOfComponentInstance(ComponentInstance componentInstance) {
290                 Map<String, Object> map = new HashMap<>();
291                 
292                 if(componentInstance.getArtifacts() != null) {
293                         Map<String, Object> informationalArtifacts = getArtifacts(componentInstance.getArtifacts());
294                         if(informationalArtifacts.size() > 0) {
295                                 map.put("Informational", informationalArtifacts);
296                         }
297                 }
298                 
299                 if(componentInstance.getDeploymentArtifacts() != null) {
300                         Map<String, Object> deploymentArtifacts = getArtifacts(componentInstance.getDeploymentArtifacts());
301                         if(deploymentArtifacts.size() > 0) {
302                                 map.put("Deployment", deploymentArtifacts);
303                         }
304                 }
305                 
306                 return map;
307         }
308         
309         public Map<String, Object> getArtifacstOfComponent(Component component) {
310                 Map<String, Object> map = new HashMap<>();
311                 
312                 if(component.getArtifacts() != null) {
313                         Map<String, Object> informationalArtifacts = getArtifacts(component.getArtifacts());
314                         if(informationalArtifacts.size() > 0) {
315                                 map.put("Informational", informationalArtifacts);
316                         }
317                 }
318                 
319                 if(component.getDeploymentArtifacts() != null) {
320                         Map<String, Object> deploymentArtifacts = getArtifacts(component.getDeploymentArtifacts());
321                         if(deploymentArtifacts.size() > 0) {
322                                 map.put("Deployment", deploymentArtifacts);
323                         }
324                 }
325                 
326                 return map;
327         }
328         
329         public Map<String, Object> getArtifacts(Map<String, ArtifactDefinition> artifacts) {
330                 Map<String, Object> map = new HashMap<>();
331                 
332                 for(String artifact: artifacts.keySet()) {
333                         ArtifactDefinition artifactDefinition = artifacts.get(artifact);
334                         if((artifactDefinition.getEsId() != null) && (!artifactDefinition.getEsId().equals("")) && (!artifactDefinition.getArtifactType().equals("HEAT_ENV"))) {
335                                 if(map.containsKey(artifactDefinition.getArtifactType())) {
336                                         ((List<String>) map.get(artifactDefinition.getArtifactType())).add(artifactDefinition.getArtifactName());
337                                 } else {
338                                         ArrayList<String> list = new ArrayList<String>();
339                                         list.add(artifactDefinition.getArtifactName());
340                                         map.put(artifactDefinition.getArtifactType(), list);
341                                 }
342                         }
343                 }
344                 
345                 return map;
346         }
347         
348         public ImmutablePair<ComponentInstance, ArtifactDefinition> uploadArtifactOnRandomRI(Component component) throws IOException, Exception {
349                 ArtifactReqDetails artifactReqDetails = getRandomArtifact();
350                 Random random = new Random();
351                 int randInt = random.nextInt(component.getComponentInstances().size());
352                 User defaultUser = ElementFactory.getDefaultUser(getRole());
353                 ComponentInstance componentInstance = component.getComponentInstances().get(randInt);
354                 
355                 RestResponse uploadArtifactRestResponse = ArtifactRestUtils.externalAPIUploadArtifactOfComponentInstanceOnAsset(component, defaultUser, artifactReqDetails, componentInstance);
356                 
357                 // Check response of external API
358                 Integer responseCode = uploadArtifactRestResponse.getErrorCode();
359                 Assert.assertEquals(responseCode, (Integer)200, "Response code is not correct.");
360                 
361                 ImmutablePair<ComponentInstance, ArtifactDefinition> pair = ImmutablePair.of(componentInstance, ArtifactRestUtils.getArtifactDataFromJson(uploadArtifactRestResponse.getResponse()));
362         
363                 return pair;
364         }
365         
366         public ImmutablePair<ComponentInstance, ArtifactDefinition> uploadArtifactOnRandomRI(Resource resource) throws IOException, Exception {
367                 ArtifactReqDetails artifactReqDetails = getRandomVfcArtifact();
368                 Random random = new Random();
369                 int randInt = random.nextInt(resource.getComponentInstances().size());
370                 User defaultUser = ElementFactory.getDefaultUser(getRole());
371                 ComponentInstance componentInstance = resource.getComponentInstances().get(randInt);
372                 
373                 RestResponse uploadArtifactRestResponse = ArtifactRestUtils.externalAPIUploadArtifactOfComponentInstanceOnAsset(resource, defaultUser, artifactReqDetails, componentInstance);
374                 
375                 
376                 
377                 // Check response of external API
378                 Integer responseCode = uploadArtifactRestResponse.getErrorCode();
379                 
380                 if(responseCode.equals(404)) {
381                         getExtendTest().log(Status.SKIP, String.format("DE271521"));
382                         throw new SkipException("DE271521");                    
383                 }
384                 
385                 Assert.assertEquals(responseCode, (Integer)200, "Response code is not correct.");
386                 
387                 ImmutablePair<ComponentInstance, ArtifactDefinition> pair = ImmutablePair.of(componentInstance, ArtifactRestUtils.getArtifactDataFromJson(uploadArtifactRestResponse.getResponse()));
388         
389                 return pair;
390         }
391         
392         public ArtifactReqDetails getRandomArtifact() throws IOException, Exception {
393                 List<String> artifactsTypeList = Arrays.asList("Other");
394                 return getRandomArtifact(artifactsTypeList);
395         }
396         
397         public ArtifactReqDetails getRandomVfcArtifact() throws IOException, Exception {
398                 List<String> vfcArtifactsTypeList = Arrays.asList("DCAE_INVENTORY_TOSCA", "DCAE_INVENTORY_JSON", "DCAE_INVENTORY_POLICY", "DCAE_INVENTORY_DOC",
399                                 "DCAE_INVENTORY_BLUEPRINT", "DCAE_INVENTORY_EVENT", "SNMP_POLL", "SNMP_TRAP");
400                 return getRandomArtifact(vfcArtifactsTypeList);
401         }
402         
403         public ArtifactReqDetails getRandomArtifact(List<String> artifactType) throws IOException, Exception {
404                 Random random = new Random();
405                 
406                 ArtifactReqDetails artifactReqDetails = ElementFactory.getArtifactByType("ci", artifactType.get(random.nextInt(artifactType.size())), true, false);
407                 return artifactReqDetails;
408         }
409
410         @Override
411         protected UserRoleEnum getRole() {
412                 return UserRoleEnum.DESIGNER;
413         }
414
415 }