2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.ci.tests.US;
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;
50 public class AddComponentInstancesArtifactsInCsar extends SetupCDTest {
52 public static final String DEPLOYMENT = "Deployment";
53 public static final String INFORMATIONAL = "Informational";
54 private String filePath;
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;
61 else if(filePath.isEmpty() && !System.getProperty("os.name").contains("Windows")){
62 filePath = FileHandling.getBasePath() + File.separator + "Files" + File.separator + "AddComponentInstancesArtifactsInCsar"+ File.separator;
66 // US847439 - Story [BE] - Add Component Instance's artifacts in CSAR
67 // TC1521795 - VF CSAR - The Flow
69 public void vfAndServiceCsarTheFlow() throws Exception{
70 ResourceReqDetails resourceMetaData = ElementFactory.getDefaultResourceByType(ResourceTypeEnum.VF, getUser());
72 String vnfFile = "FDNT.zip";
73 String snmpFile = "Fault-alarms-ASDC-vprobes-vLB.zip";
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);
83 HomePage.showVspRepository();
84 OnboardingUiUtils.importVSP(createVSP);
85 resourceMetaData.setVersion("0.1");
86 Resource vfResource = AtomicOperationUtils.getResourceObjectByNameAndVersion(UserRoleEnum.DESIGNER, resourceMetaData.getName(), resourceMetaData.getVersion());
88 Map<String, Object> artifacts = getArtifactsOfComponentAndComponentsInstance(vfResource);
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);
95 if(uploadArtifactOnRandomVfc.getRight().getArtifactName() != null) {
96 artifactsUploadedToComponentInstance.add(uploadArtifactOnRandomVfc);
100 if(!artifactsUploadedToComponentInstance.isEmpty()) {
101 Map<String, Object> artifactsOfResourceInstance = getArtifactsOfResourceInstance(artifactsUploadedToComponentInstance);
102 artifacts.put("Resources", artifactsOfResourceInstance);
105 ResourceGeneralPage.getLeftMenu().moveToToscaArtifactsScreen();
106 ToscaArtifactsPage.downloadCsar();
107 File latestFilefromDir = FileHandling.getLastModifiedFileNameFromDir();
108 Map<String, Object> combineHeatArtifacstWithFolderArtifacsToMap = ArtifactFromCsar.getVFCArtifacts(latestFilefromDir.getAbsolutePath());
110 compareArtifactFromFileStructureToArtifactsFromJavaObject(artifacts, combineHeatArtifacstWithFolderArtifacsToMap);
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);
121 compareArtifacts(artifactFromJavaObject.get(key), artifactsFromFileStructure.get(key));
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;
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());
136 for(String key: artifactsMap.keySet()) {
137 List<String> artifacts = artifactsMap.get(key);
139 for(HeatMetaFirstLevelDefinition heatMetaFirstLevelDefinition: artifactsList) {
140 if(heatMetaFirstLevelDefinition.getType().equals(key)) {
141 if(artifacts.contains(heatMetaFirstLevelDefinition.getFileName())) {
142 artifacts.remove(heatMetaFirstLevelDefinition.getFileName());
147 Assert.assertEquals(artifacts.size(), 0, "Expected that all artifacts equal. There is artifacts which not equal: " + artifacts.toString());
152 public Map<String, Object> getArtifactsOfResourceInstance(List<ImmutablePair<ComponentInstance, ArtifactDefinition>> riList) {
153 Map<String, Object> artifacts = new HashMap<>();
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()) ) {
161 ((Map<String, ArrayList<String>>)((Map<String, Object>) artifacts.get(componentInstance.getNormalizedName())).get(DEPLOYMENT)).get(artifactDefinition.getArtifactType()).add(artifactDefinition.getArtifactName());
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);
173 ArrayList<String> list = new ArrayList<>();
174 list.add(artifactDefinition.getArtifactName());
176 Map<String, ArrayList<String>> map = new HashMap<>();
177 map.put(artifactDefinition.getArtifactType(), list);
179 Map<String, Map<String, ArrayList<String>>> addMap = new HashMap<>();
180 addMap.put(DEPLOYMENT, map);
182 artifacts.put(componentInstance.getNormalizedName(), addMap);
183 } catch (Exception e) {
184 Assert.fail("Artifact name is null for componentInstance: " + componentInstance.getNormalizedName());
191 public Map<String, Object> getArtifactsOfComponentAndComponentsInstance(Component component) {
192 Map<String, Object> artifacts = getArtifactsOfComponent(component);
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);
204 public Map<String, Object> getArtifactsOfComponentInstance(ComponentInstance componentInstance) {
205 Map<String, Object> map = new HashMap<>();
207 if(componentInstance.getArtifacts() != null) {
208 Map<String, Object> informationalArtifacts = getArtifacts(componentInstance.getArtifacts());
209 if(!informationalArtifacts.isEmpty()) {
210 map.put(INFORMATIONAL, informationalArtifacts);
214 if(componentInstance.getDeploymentArtifacts() != null) {
215 Map<String, Object> deploymentArtifacts = getArtifacts(componentInstance.getDeploymentArtifacts());
216 if(!deploymentArtifacts.isEmpty()) {
217 map.put(DEPLOYMENT, deploymentArtifacts);
224 public Map<String, Object> getArtifactsOfComponent(Component component) {
225 Map<String, Object> map = new HashMap<>();
227 if(component.getArtifacts() != null) {
228 Map<String, Object> informationalArtifacts = getArtifacts(component.getArtifacts());
229 if(!informationalArtifacts.isEmpty()) {
230 map.put(INFORMATIONAL, informationalArtifacts);
234 if(component.getDeploymentArtifacts() != null) {
235 Map<String, Object> deploymentArtifacts = getArtifacts(component.getDeploymentArtifacts());
236 if(!deploymentArtifacts.isEmpty()) {
237 map.put(DEPLOYMENT, deploymentArtifacts);
244 public Map<String, Object> getArtifacts(Map<String, ArtifactDefinition> artifacts) {
245 Map<String, Object> map = new HashMap<>();
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());
253 ArrayList<String> list = new ArrayList<>();
254 list.add(artifactDefinition.getArtifactName());
255 map.put(artifactDefinition.getArtifactType(), list);
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);
270 RestResponse uploadArtifactRestResponse = ArtifactRestUtils.externalAPIUploadArtifactOfComponentInstanceOnAsset(component, defaultUser, artifactReqDetails, componentInstance);
272 // Check response of external API
273 Integer responseCode = uploadArtifactRestResponse.getErrorCode();
274 Assert.assertEquals(responseCode, (Integer)200, "Response code is not correct.");
276 ImmutablePair<ComponentInstance, ArtifactDefinition> pair = ImmutablePair.of(componentInstance, ResponseParser.convertArtifactDefinitionResponseToJavaObject(uploadArtifactRestResponse.getResponse()));
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);
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()));
296 public ArtifactReqDetails getRandomArtifact() throws Exception {
297 List<String> artifactsTypeList = Arrays.asList("Other");
298 return getRandomArtifact(artifactsTypeList);
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);
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);
320 protected UserRoleEnum getRole() {
321 return UserRoleEnum.DESIGNER;