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