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