[SDC] Onboarding 1710 rebase.
[sdc.git] / openecomp-be / backend / openecomp-sdc-vendor-software-product-manager / src / test / java / org / openecomp / sdc / vendorsoftwareproduct / ComputeTest.java
1 package org.openecomp.sdc.vendorsoftwareproduct;
2
3 import static org.openecomp.sdc.vendorsoftwareproduct.errors.VendorSoftwareProductErrorCodes.ADD_COMPUTE_NOT_ALLOWED_IN_HEAT_ONBOARDING;
4 import static org.openecomp.sdc.vendorsoftwareproduct.errors.VendorSoftwareProductErrorCodes.DUPLICATE_COMPUTE_NAME_NOT_ALLOWED;
5
6 import org.openecomp.core.utilities.CommonMethods;
7 import org.openecomp.sdc.common.errors.CoreException;
8 import org.openecomp.sdc.vendorsoftwareproduct.dao.VendorSoftwareProductDao;
9 import org.openecomp.sdc.vendorsoftwareproduct.dao.VendorSoftwareProductDaoFactory;
10 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentEntity;
11 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComputeEntity;
12 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.DeploymentFlavorEntity;
13 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails;
14 import org.openecomp.sdc.vendorsoftwareproduct.impl.VendorSoftwareProductManagerImpl;
15 import org.openecomp.sdc.vendorsoftwareproduct.types.CompositionEntityResponse;
16 import org.openecomp.sdc.vendorsoftwareproduct.types.ListComputeResponse;
17 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.ComponentComputeAssociation;
18 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.ComponentData;
19 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.ComputeData;
20 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.DeploymentFlavor;
21 import org.openecomp.sdc.versioning.dao.types.Version;
22 import org.openecomp.sdc.versioning.errors.VersioningErrorCodes;
23 import org.testng.Assert;
24 import org.testng.annotations.BeforeClass;
25 import org.testng.annotations.Test;
26
27 import java.util.ArrayList;
28 import java.util.Collection;
29 import java.util.List;
30
31 public class ComputeTest {
32
33   /*private static final String USER1 = "componentsTestUser1";
34   private static final String USER2 = "componentsTestUser2";
35   private static final Version VERSION01 = new Version(0, 1);
36   private static final VendorSoftwareProductManager vendorSoftwareProductManager =
37       new VendorSoftwareProductManagerImpl();
38   private static final VendorSoftwareProductDao vendorSoftwareProductDao =
39       VendorSoftwareProductDaoFactory
40           .getInstance().createInterface();
41
42   private static String vsp1Id;
43   private static String vsp2Id;
44   private static String comp1 = "{\"displayName\": \"VFC_Manual\", " +
45       "\"description\": \"desc manual\"}";
46   private static String compute1 = "{\"name\": \"Compute_A\", " +
47       "\"description\": \"desc manual compute\"}";
48   private static String computeDelete = "{\"name\": \"Compute_Delete\", " +
49       "\"description\": \"desc manual compute delete\"}";
50
51   private static String comp1Id;
52   private static String compute1Id;
53   private ComputeEntity createdCompute;
54
55   static ComponentEntity createComponent(String vspId, Version version, String compId) {
56     ComponentEntity componentEntity = new ComponentEntity(vspId, version, compId);
57     ComponentData compData = new ComponentData();
58     compData.setName(compId + " name");
59     compData.setDisplayName(compId + " display name");
60     compData.setDescription(compId + " desc");
61     componentEntity.setComponentCompositionData(compData);
62     vendorSoftwareProductDao.createComponent(componentEntity);
63     return componentEntity;
64   }
65
66   static ComputeEntity createComputeEntity(String vspId, String componentId, String data ){
67     ComputeEntity comp = new ComputeEntity();
68     comp.setVspId(vspId);
69     comp.setComponentId(componentId);
70     comp.setCompositionData(data);
71     return comp;
72   }
73
74   @BeforeClass
75   private void init() {
76     VspDetails
77         vsp1 = vendorSoftwareProductManager.createNewVsp(VSPCommon
78         .createVspDetails(null, null, "VSP_" + CommonMethods.nextUuId(), "Test-vsp1", "vendorName",
79             "vlm1Id", "icon", "category", "subCategory", "123", null,
80             VSPCommon.OnboardingMethod.HEAT.name()), USER1
81     );
82     vsp1Id = vsp1.getId(); //HEAT onboarded
83
84     VspDetails vsp2 = vendorSoftwareProductManager.createNewVsp(VSPCommon
85         .createVspDetails(null, null, "VSP_" + CommonMethods.nextUuId(), "Test-vsp3",
86             "vendorName",
87             "vlm1Id", "icon", "category", "subCategory", "123", null, VSPCommon
88                 .OnboardingMethod.Manual.name()), USER1);
89     vsp2Id = vsp2.getId(); //MANUAL onboarded
90
91     ComponentEntity component = new ComponentEntity();
92     component.setVspId(vsp2Id);
93     component.setCompositionData(comp1);
94     ComponentEntity createdComp = vendorSoftwareProductManager.createComponent(component, USER1);
95     comp1Id = createdComp.getId();
96   }
97
98   @Test
99   public void testListWhenNone() {
100
101     final Collection<ListComputeResponse> listComputeResponses =
102         vendorSoftwareProductManager.listCompute(vsp2Id, null, comp1Id, USER1);
103     Assert.assertEquals(listComputeResponses.size(), 0);
104   }
105
106   @Test
107   public void testCreateComputeInHeatOnboardedVsp_negative() {
108     ComputeEntity comp = createComputeEntity(vsp1Id,comp1Id,compute1);
109     try {
110       createdCompute = vendorSoftwareProductManager.createCompute(comp, USER1);
111       Assert.fail();
112     }
113     catch(CoreException exception){
114       Assert.assertEquals(exception.code().id(),ADD_COMPUTE_NOT_ALLOWED_IN_HEAT_ONBOARDING);
115     }
116   }
117
118   @Test(dependsOnMethods = "testListWhenNone")
119   public void testCreateCompute() {
120     ComputeEntity comp = createComputeEntity(vsp2Id,comp1Id,compute1);
121
122     createdCompute = vendorSoftwareProductManager.createCompute(comp, USER1);
123     compute1Id = createdCompute.getId();
124     Assert.assertNotNull(compute1Id);
125     Assert.assertNotNull(createdCompute.getCompositionData());
126     Assert.assertNotNull(
127         vendorSoftwareProductManager.getCompute(vsp2Id, VERSION01, comp1Id,compute1Id,
128             USER1).getData());
129   }
130
131   @Test(dependsOnMethods = "testCreateCompute")
132   public void testCreateComputeNegative() {
133     ComputeEntity comp = createComputeEntity(vsp2Id,comp1Id,compute1);
134
135     try {
136       ComputeEntity createdCompute = vendorSoftwareProductManager.createCompute(comp, USER1);
137       Assert.fail();
138     }
139     catch (CoreException exception) {
140       Assert.assertEquals(exception.code().id(),DUPLICATE_COMPUTE_NAME_NOT_ALLOWED);
141     }
142   }
143
144   @Test
145   public void testGetNonExistingComponentId_negative() {
146     testGet_negative(vsp1Id, null, "non existing component id", compute1Id, USER1,
147         VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND);
148   }
149
150   @Test
151   public void testGetNonExistingVspId_negative() {
152     testGet_negative("non existing vsp id", null, comp1Id, compute1Id, USER1,
153         VersioningErrorCodes.VERSIONABLE_ENTITY_NOT_EXIST);
154   }
155
156   @Test
157   public void testGetNonExistingComputeId_negative() {
158     testGet_negative(vsp1Id, null, comp1Id, "non existing compute id", USER1,
159         VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND);
160   }
161
162   @Test(dependsOnMethods = "testCreateCompute")
163   public void testGetCompute() {
164     testGet(vsp2Id, VERSION01, comp1Id, compute1Id, USER1, createdCompute);
165   }
166
167
168   @Test(dependsOnMethods = "testCreateCompute")
169   public void testListCompute() {
170
171     final Collection<ListComputeResponse> actual =
172         vendorSoftwareProductManager.listCompute(vsp2Id, null, comp1Id, USER1);
173     Assert.assertEquals(actual.size(), 1);
174     actual.forEach(listComputeResponse -> {
175       Assert.assertEquals(listComputeResponse.isAssociatedWithDeploymentFlavor(), false);
176     } );
177   }
178
179
180   @Test(dependsOnMethods = "testListCompute")
181   public void testListComputeAssociatedWithDeployment() {
182
183     //Create DF and associate compute1Id CF to it
184     String deployment1Id = null;
185     DeploymentFlavorEntity deploymentFlavorEntity = new DeploymentFlavorEntity(vsp2Id,
186         VERSION01, deployment1Id);
187     DeploymentFlavor deploymentFlavor = new DeploymentFlavor();
188     deploymentFlavor.setModel("DF_testListComputeAssociatedWithDeployment");
189     deploymentFlavor.setDescription("creating a deployment flavor with compute flavor association");
190     ComponentComputeAssociation association = new ComponentComputeAssociation();
191     association.setComponentId(comp1Id);
192     association.setComputeFlavorId(compute1Id);
193     List<ComponentComputeAssociation> associations = new ArrayList<ComponentComputeAssociation>();
194     associations.add(association);
195     deploymentFlavor.setComponentComputeAssociations(associations);
196     deploymentFlavorEntity.setDeploymentFlavorCompositionData(deploymentFlavor);
197
198     DeploymentFlavorEntity createddeployment = vendorSoftwareProductManager.createDeploymentFlavor
199         (deploymentFlavorEntity, USER1);
200     Assert.assertEquals((createddeployment.getId() != null), true);
201     deployment1Id = createddeployment.getId();
202
203     final Collection<ListComputeResponse> actual =
204         vendorSoftwareProductManager.listCompute(vsp2Id, null, comp1Id, USER1);
205     Assert.assertEquals(actual.size(), 1);
206     actual.forEach(listComputeResponse -> {
207       Assert.assertEquals(listComputeResponse.isAssociatedWithDeploymentFlavor(), true);
208     } );
209   }
210
211   @Test
212   public void testUpdateNonExistingComponentId_negative() {
213     testUpdate_negative(vsp1Id, "non existing component id", USER1,
214         VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND);
215   }
216
217   @Test
218   public void testUpdateNonExistingVspId_negative() {
219     testUpdate_negative("non existing vsp id", comp1Id, USER1,
220         VersioningErrorCodes.VERSIONABLE_ENTITY_NOT_EXIST);
221   }
222
223   @Test
224   public void testDelete() {
225     ComputeEntity comp = createComputeEntity(vsp2Id,comp1Id,computeDelete);
226
227     ComputeEntity created = vendorSoftwareProductManager.createCompute(comp, USER1);
228
229     vendorSoftwareProductManager.deleteCompute(vsp2Id,comp1Id,created.getId(),USER1);
230     testGet_negative(vsp2Id,null, comp1Id, created.getId(),USER1,
231         VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND);
232   }
233
234   @Test
235   public void testDeleteNonExistingComputeId_negative() {
236     testDelete_negative(vsp2Id,comp1Id,"InvalidComputeId",USER1,
237         VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND);
238   }
239
240   @Test
241   public void testDeleteNonExistingComponentId_negative() {
242     testDelete_negative(vsp2Id,"InvalidComponentId",compute1Id,USER1,
243         VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND);
244   }
245
246   @Test
247   public void testDeleteNonExistingVspId_negative() {
248     testDelete_negative("InvalidVspId",comp1Id,compute1Id,USER1,
249         VersioningErrorCodes.VERSIONABLE_ENTITY_NOT_EXIST);
250   }
251
252   private void testGet(String vspId, Version version, String componentId, String computeId, String
253       user, ComputeEntity expected) {
254     CompositionEntityResponse<ComputeData>
255         response = vendorSoftwareProductManager.getCompute(vspId, null, componentId, computeId,
256         user);
257     Assert.assertEquals(response.getId(), expected.getId());
258     Assert.assertEquals(response.getData(), expected.getComputeCompositionData());
259     Assert.assertNotNull(response.getSchema());
260   }
261
262   private void testGet_negative(String vspId, Version version, String componentId, String computeId,
263                                 String user, String expectedErrorCode) {
264     try {
265       vendorSoftwareProductManager.getCompute(vspId, version, componentId, computeId, user);
266       Assert.fail();
267     } catch (CoreException exception) {
268       Assert.assertEquals(exception.code().id(), expectedErrorCode);
269     }
270   }
271
272   private void testDelete_negative(String vspId, String componentId, String computeId, String user,
273                                    String expectedErrorCode){
274     try {
275       vendorSoftwareProductManager.deleteCompute(vspId, componentId, computeId, user);
276       Assert.fail();
277     }
278     catch(CoreException exception){
279       Assert.assertEquals(exception.code().id(), expectedErrorCode);
280     }
281   }
282
283   private void testUpdate_negative(String vspId, String componentId, String user,
284                                    String expectedErrorCode) {
285     try {
286       vendorSoftwareProductManager
287           .updateComponent(new ComponentEntity(vspId, null, componentId), user);
288       Assert.fail();
289     } catch (CoreException exception) {
290       Assert.assertEquals(exception.code().id(), expectedErrorCode);
291     }
292   }*/
293 }