[SDC] Onboarding 1710 rebase.
[sdc.git] / openecomp-be / backend / openecomp-sdc-vendor-software-product-manager / src / test / java / org / openecomp / sdc / vendorsoftwareproduct / ImagesTest.java
1 package org.openecomp.sdc.vendorsoftwareproduct;
2
3 import org.openecomp.core.utilities.CommonMethods;
4 import org.openecomp.core.utilities.json.JsonUtil;
5 import org.openecomp.sdc.common.errors.CoreException;
6 import org.openecomp.sdc.common.errors.ErrorCode;
7 import org.openecomp.sdc.vendorsoftwareproduct.dao.VendorSoftwareProductDao;
8 import org.openecomp.sdc.vendorsoftwareproduct.dao.VendorSoftwareProductDaoFactory;
9 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentEntity;
10 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ImageEntity;
11 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails;
12 import org.openecomp.sdc.vendorsoftwareproduct.errors.ImageErrorBuilder;
13 import org.openecomp.sdc.vendorsoftwareproduct.errors.NotSupportedHeatOnboardMethodErrorBuilder;
14 import org.openecomp.sdc.vendorsoftwareproduct.errors.VendorSoftwareProductErrorCodes;
15 import org.openecomp.sdc.vendorsoftwareproduct.impl.VendorSoftwareProductManagerImpl;
16 import org.openecomp.sdc.vendorsoftwareproduct.types.CompositionEntityResponse;
17 import org.openecomp.sdc.vendorsoftwareproduct.types.QuestionnaireResponse;
18 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.Image;
19 import org.openecomp.sdc.vendorsoftwareproduct.types.questionnaire.component.image.ImageDetails;
20 import org.openecomp.sdc.versioning.dao.types.Version;
21 import org.openecomp.sdc.versioning.errors.VersioningErrorCodes;
22 import org.testng.Assert;
23 import org.testng.annotations.BeforeClass;
24 import org.testng.annotations.Test;
25
26 import java.util.Collection;
27
28 public class ImagesTest {
29
30   /*private static final String USER1 = "imageTestUser1";
31   private static final String USER2 = "imageTestUser2";
32   private static final Version VERSION01 = new Version(0, 1);
33   private static final VendorSoftwareProductManager vendorSoftwareProductManager =
34       new VendorSoftwareProductManagerImpl();
35   private static final VendorSoftwareProductDao vendorSoftwareProductDao =
36       VendorSoftwareProductDaoFactory
37           .getInstance().createInterface();
38
39   private static String image1Id;
40
41
42   private static String comp1 = "{\"displayName\": \"VFC_Manual\", " +
43       "\"description\": \"desc manual\"}";
44
45   private static String vsp1Id;
46   private static String vsp2Id;
47   private static String vsp3Id;
48   private static String comp1Id;
49
50   private static String image2Id;
51
52   @BeforeClass
53   private void init() {
54     VspDetails
55         vsp1 = vendorSoftwareProductManager.createNewVsp(VSPCommon
56         .createVspDetails(null, null, "VSP_" + CommonMethods.nextUuId(), "Test-vsp1", "vendorName",
57             "vlm1Id", "icon", "category", "subCategory", "123", null,
58             VSPCommon.OnboardingMethod.Manual.name()), USER1
59         );
60     vsp1Id = vsp1.getId();
61
62     VspDetails vsp2 = vendorSoftwareProductManager.createNewVsp(VSPCommon
63         .createVspDetails(null, null, "VSP_" + CommonMethods.nextUuId(), "Test-vsp2", "vendorName",
64             "vlm1Id", "icon", "category", "subCategory", "123", null, VSPCommon.OnboardingMethod.
65                 Manual.name()), USER1);
66     vsp2Id = vsp2.getId();
67
68     VspDetails vsp3 = vendorSoftwareProductManager.createNewVsp(VSPCommon
69         .createVspDetails(null, null, "VSP_" + CommonMethods.nextUuId(), "Test-vsp3",
70             "vendorName",
71             "vlm1Id", "icon", "category", "subCategory", "123", null, VSPCommon
72                 .OnboardingMethod.HEAT.name()), USER1);
73     vsp3Id = vsp3.getId();
74
75     ComponentEntity comp = new ComponentEntity();
76     comp.setVspId(vsp2Id);
77     comp.setCompositionData(comp1);
78     ComponentEntity createdComp = vendorSoftwareProductManager.createComponent(comp, USER1);
79     comp1Id = createdComp.getId();
80   }
81
82   @Test
83   public void testListImagesNonExistingVspId_negative() {
84     testList_negative("non existing vsp id", null, image1Id, USER1,
85         VersioningErrorCodes.VERSIONABLE_ENTITY_NOT_EXIST,
86         "Versionable entity VendorSoftwareProduct with id non existing vsp id does not exist." );
87   }
88
89   @Test
90   public void testListImagesNonExistingVfcId_negative() {
91     testList_negative(vsp1Id, VERSION01, "444", USER1,
92         VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND,
93         "Vendor Software Product Component with Id 444 does not exist for Vendor Software Product "
94             + "with id "+vsp1Id+ " and version "+VERSION01);
95   }
96
97   @Test
98   public void testListImages() {
99     createImageEntity("media-vsrx-vmdisk-15.1X49-D40.6.aki", "aki");
100     createImageEntity("riverbed-15.1X49-D40.6.vdi", "vdi");
101     final Collection<ImageEntity> imageEntities =
102         vendorSoftwareProductManager.listImages(vsp2Id, VERSION01, comp1Id, USER1);
103     System.out.println("size::"+imageEntities.size());
104   }
105
106   @Test
107   public void testCreateNonExistingVspId_negative() {
108     testCreate_negative(new ImageEntity("non existing vsp id", null, null, null), USER1,
109         VersioningErrorCodes.VERSIONABLE_ENTITY_NOT_EXIST,
110         "Versionable entity VendorSoftwareProduct with id non existing vsp id does not exist.");
111   }
112
113   @Test
114   public void testCreateNonExistingVfcId_negative() {
115     testCreate_negative(new ImageEntity(vsp1Id, VERSION01, "222", null), USER1,
116         VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND,
117         "Vendor Software Product Component with Id 222 does not exist for Vendor Software Product "
118             + "with id "+vsp1Id + " and version "+VERSION01);
119   }
120
121   @Test(dependsOnMethods = "testUpdateNonExistingImageId_negative")
122   public void testCreateOnAvailableVsp_negative() {
123     vendorSoftwareProductManager.checkin(vsp1Id, USER1);
124     testCreate_negative(new ImageEntity(vsp1Id, null, null, null), USER1,
125         VersioningErrorCodes.EDIT_ON_UNLOCKED_ENTITY,
126         "Can not edit versionable entity VendorSoftwareProduct with id "+vsp1Id+ " since it is not"
127             + " checked out.");
128   }
129
130   @Test(dependsOnMethods = "testCreateOnAvailableVsp_negative")
131   public void testCreateOnVspOtherUser_negative() {
132     vendorSoftwareProductManager.checkout(vsp1Id, USER1);
133     testCreate_negative(new ImageEntity(vsp1Id, null, null, null), USER2,
134         VersioningErrorCodes.EDIT_ON_ENTITY_LOCKED_BY_OTHER_USER,
135         "Versionable entity VendorSoftwareProduct with id " +vsp1Id+
136             " can not be edited since it is locked by other user "+ USER1+ ".");
137   }
138
139   @Test(dependsOnMethods = "testCreateOnVspOtherUser_negative")
140   public void testOnUndoCheckoutImagesDeleted() {
141
142     ComponentEntity comp = new ComponentEntity();
143     comp.setVspId(vsp1Id);
144     comp.setCompositionData(comp1);
145     ComponentEntity createdComp = vendorSoftwareProductManager.createComponent(comp, USER1);
146     String compId = createdComp.getId();
147
148     vendorSoftwareProductManager.checkin(vsp1Id, USER1);
149     vendorSoftwareProductManager.checkout(vsp1Id, USER1);
150
151     for(int i = 1; i<=3; i++) {
152       ImageEntity imageEntity = new ImageEntity();
153       imageEntity.setVspId(vsp1Id);
154       imageEntity.setComponentId(compId);
155       Image image = new Image();
156       image.setFileName(i + "testimage.aki");
157       //image.setVersion("9.2.0");
158       image.setDescription("riverbed image");
159       //image.setFormat("aki");
160       //image.setMd5("233343DDDD");
161       imageEntity.setImageCompositionData(image);
162       ImageEntity createdImage = vendorSoftwareProductManager.createImage(imageEntity, USER1);
163     }
164
165     Collection<ImageEntity> imageEntities =
166         vendorSoftwareProductManager.listImages(vsp1Id, null, compId, USER1);
167
168     Assert.assertEquals(imageEntities.size(), 3);
169
170     vendorSoftwareProductManager.undoCheckout(vsp1Id, USER1);
171
172     imageEntities = vendorSoftwareProductManager.listImages(vsp1Id, null, compId, USER1);
173
174     Assert.assertEquals(imageEntities.size(), 0);
175   }
176
177   @Test
178   public void testCreateOnHeatVsp_negative() {
179     final ErrorCode addImageNotSupportedHeatOnboardMethodErrorBuilder =
180         NotSupportedHeatOnboardMethodErrorBuilder
181             .getAddImageNotSupportedHeatOnboardMethodErrorBuilder();
182     testCreate_negative(new ImageEntity(vsp3Id, null, null, null), USER1,
183         addImageNotSupportedHeatOnboardMethodErrorBuilder.id(),
184         addImageNotSupportedHeatOnboardMethodErrorBuilder.message()
185         );
186   }
187
188   @Test(dependsOnMethods = "testListImages")
189   public void testCreateImage() {
190     ImageEntity createdImage = createImageEntity("riverbed-WX-IMG-9.2.0.qcow2", "qcow2");
191     Assert.assertNotNull(image1Id);
192     Assert.assertNotNull(createdImage.getCompositionData());
193     Assert.assertNotNull(
194         vendorSoftwareProductManager.getImage(vsp2Id, VERSION01, comp1Id,image1Id,
195             USER1).getData());
196   }
197
198   @Test(dependsOnMethods = "testCreateImage")
199   public void testCreateDupImageNameForSameComponent_negative() {
200     ImageEntity createdImage = null;
201     try {
202       createdImage = createImageEntity("riverbed-WX-IMG-9.2.0.qcow2", "qcow2");
203       Assert.fail();
204     }
205     catch(CoreException exception) {
206       Assert.assertEquals(exception.code().id(), VendorSoftwareProductErrorCodes.
207           DUPLICATE_IMAGE_NAME_NOT_ALLOWED);
208       Assert.assertEquals(exception.getMessage(),
209           String.format("Invalid request, Image with name riverbed-WX-IMG-9.2.0.qcow2 already " +
210               "exists for component with ID "+comp1Id)+".");
211     }
212   }
213
214   @Test
215   public void testGet() {
216     ImageEntity createdImage = createImageEntity("read-riverbed-WX-IMG-9.2.0.qcow2", "qcow2");
217     testGet(vsp2Id, VERSION01, comp1Id, createdImage.getId(), USER1, createdImage);
218   }
219
220   @Test
221   public void testGetNonExistingVspId_negative() {
222     testGet_negative("non existing vsp id", null, null, image1Id, USER1,
223         VersioningErrorCodes.VERSIONABLE_ENTITY_NOT_EXIST,
224         "Versionable entity VendorSoftwareProduct with id non existing vsp id does not exist." );
225   }
226
227   @Test
228   public void testGetNonExistingVfcId_negative() {
229     testGet_negative(vsp1Id, VERSION01, "111", null, USER1,
230         VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND,
231         "Vendor Software Product Component with Id 111 does not exist for Vendor Software Product "
232             + "with id "+vsp1Id + " and version "+VERSION01);
233   }
234
235   @Test
236   public void testUpdateNonExistingVspId_negative() {
237     ImageEntity imageEntity = new ImageEntity("non existing vsp id", null, null, image1Id);
238
239     testUpdate_negative(imageEntity, USER1,
240         VersioningErrorCodes.VERSIONABLE_ENTITY_NOT_EXIST,
241         "Versionable entity VendorSoftwareProduct with id non existing vsp id does not exist." );
242   }
243
244   @Test(dependsOnMethods =  "testUpdateNonExistingVspId_negative")
245   public void testUpdateNonExistingVfcId_negative() {
246     ImageEntity imageEntity = new ImageEntity(vsp1Id, VERSION01, "111", null);
247
248     testUpdate_negative(imageEntity, USER1,
249         VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND,
250         "Vendor Software Product Component with Id 111 does not exist for Vendor Software Product "
251             + "with id "+vsp1Id + " and version "+VERSION01);
252   }
253
254   @Test(dependsOnMethods =  "testUpdateNonExistingVfcId_negative")
255   public void testUpdateNonExistingImageId_negative() {
256     ImageEntity imageEntity = new ImageEntity(vsp2Id, VERSION01, comp1Id, "222");
257
258     testUpdate_negative(imageEntity, USER1,
259         VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND,
260         "Vendor Software Product Component Image with Id 222 does not exist for Vendor " +
261             "Software Product with id "+vsp2Id+ " and version "+VERSION01 );
262   }
263
264   @Test(dependsOnMethods = "testCreateImage")
265   public void testUpdate() {
266     ImageEntity imageEntity = createImageEntity("testimage1","qcow2");
267     testGet(vsp2Id, VERSION01, comp1Id, imageEntity.getId(),USER1, imageEntity );
268
269     final Image imageCompositionData = imageEntity.getImageCompositionData();
270     //imageCompositionData.setVersion("10.0");
271     imageCompositionData.setDescription("updated image");
272
273     vendorSoftwareProductManager.updateImage(imageEntity, USER1);
274
275     testGet(vsp2Id, VERSION01, comp1Id, imageEntity.getId(),USER1, imageEntity );
276     image2Id = imageEntity.getId();
277   }
278
279   @Test(dependsOnMethods = "testUpdate")
280   public void testUpdateNegative_UniqueName() {
281     final CompositionEntityResponse<Image> image =
282         vendorSoftwareProductManager.getImage(vsp2Id, VERSION01, comp1Id,
283             image2Id, USER1);
284     final Image data = image.getData();
285
286     final Image imageCompositionData = data;
287     imageCompositionData.setFileName("riverbed-WX-IMG-9.2.0.qcow2");
288
289     ImageEntity entity = new ImageEntity(vsp2Id, VERSION01, comp1Id, image2Id );
290     entity.setImageCompositionData(imageCompositionData);
291     testUpdate_negative(entity, USER1, ImageErrorBuilder.getDuplicateImageNameErrorBuilder(
292         "riverbed-WX-IMG-9.2.0.qcow2", comp1Id).id()
293         ,ImageErrorBuilder.getDuplicateImageNameErrorBuilder("riverbed-WX-IMG-9.2.0.qcow2", comp1Id)
294             .message() );
295   }
296
297   @Test(dependsOnMethods = "testUpdateNegative_UniqueName")
298   public void testDeleteImage() {
299     CompositionEntityResponse<Image> image =
300         vendorSoftwareProductManager.getImage(vsp2Id, VERSION01, comp1Id, image2Id, USER1);
301
302     Assert.assertNotNull(image.getData());
303
304     vendorSoftwareProductManager.deleteImage(vsp2Id, comp1Id, image2Id, USER1);
305
306     testGet_negative(vsp2Id, VERSION01, comp1Id, image2Id, USER1,
307         VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND,
308         "Vendor Software Product Component Image with Id "+image2Id+ " does not exist for " +
309             "Vendor Software Product with id "+vsp2Id+ " and version "+VERSION01 );
310
311   }
312
313   @Test
314   public void testDeleteNonExistingVspId_negative() {
315     ImageEntity imageEntity = new ImageEntity("non existing vsp id", null, null, image1Id);
316
317     testDelete_negative(imageEntity, USER1,
318         VersioningErrorCodes.VERSIONABLE_ENTITY_NOT_EXIST,
319         "Versionable entity VendorSoftwareProduct with id non existing vsp id does not exist." );
320   }
321
322   @Test(dependsOnMethods =  "testDeleteNonExistingVspId_negative")
323   public void testDeleteNonExistingVfcId_negative() {
324     ImageEntity imageEntity = new ImageEntity(vsp1Id, VERSION01, "111", null);
325
326     testDelete_negative(imageEntity, USER1,
327         VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND,
328         "Vendor Software Product Component with Id 111 does not exist for Vendor Software Product "
329             + "with id "+vsp1Id + " and version "+VERSION01);
330   }
331
332   @Test(dependsOnMethods =  "testDeleteNonExistingVfcId_negative")
333   public void testDeleteNonExistingImageId_negative() {
334     ImageEntity imageEntity = new ImageEntity(vsp2Id, VERSION01, comp1Id, "222");
335
336     testDelete_negative(imageEntity, USER1,
337         VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND,
338         "Vendor Software Product Component Image with Id 222 does not exist for Vendor " +
339             "Software Product with id "+vsp2Id+ " and version "+VERSION01 );
340   }
341
342   @Test
343   public void testUpdateandGetQuestionnaire() {
344     ImageEntity imageEntity = createImageEntity("newimage_testUpdateandGetQuestionnaire.vdi","vdi");
345     vendorSoftwareProductManager.updateImageQuestionnaire(vsp2Id, comp1Id, imageEntity.getId(),
346         "{\"format\":\"vdi\",\"version\":\"1.2\",\"md5\":\"ssd3344\"}",
347         USER1);
348
349     final QuestionnaireResponse imageQuestionnaire =
350         vendorSoftwareProductManager.getImageQuestionnaire(vsp2Id, VERSION01, comp1Id,
351             imageEntity.getId(), USER1);
352
353     String imageDetails = imageQuestionnaire.getData();
354     Assert.assertEquals("vdi", JsonUtil.json2Object(imageDetails, ImageDetails.class).getFormat());
355     Assert.assertEquals("1.2", JsonUtil.json2Object(imageDetails, ImageDetails.class).getVersion());
356     Assert.assertEquals("ssd3344", JsonUtil.json2Object(imageDetails, ImageDetails.class).getMd5());
357   }
358
359   @Test
360   public void testUpdateQuestionnaireInvalidFormat_negative() {
361     ImageEntity imageEntity = createImageEntity("newimage.vdi","vdi");
362     try {
363       vendorSoftwareProductManager.updateImageQuestionnaire(vsp2Id, comp1Id, imageEntity.getId(),
364           "{\"format\":\"invalidformat\",\"version\":\"1.2\",\"md5\":\"ssd3344\"}",
365           USER1);
366       Assert.fail();
367     }
368     catch(CoreException exception) {
369       Assert.assertEquals(exception.code().id(), "VFC_IMAGE_INVALID_FORMAT");
370       Assert.assertEquals(exception.getMessage(), "The format value doesn't meet the expected "
371           + "attribute value.");
372     }
373   }
374
375   private ImageEntity createImageEntity(String fileName, String format) {
376     ImageEntity imageEntity = new ImageEntity();
377     imageEntity.setVspId(vsp2Id);
378     imageEntity.setComponentId(comp1Id);
379     Image image = new Image();
380     image.setFileName(fileName);
381     //image.setVersion("9.2.0");
382     image.setDescription("riverbed image");
383     //image.setFormat(format);
384     // image.setMd5("233343DDDD");
385     imageEntity.setImageCompositionData(image);
386
387     ImageEntity createdImage = vendorSoftwareProductManager.createImage(imageEntity, USER1);
388     image1Id = createdImage.getId();
389     return createdImage;
390   }
391
392   private void testGet(String vspId, Version version, String componentId, String imageId, String
393       user, ImageEntity expected) {
394     CompositionEntityResponse<Image>
395         response = vendorSoftwareProductManager.getImage(vspId, null, componentId, imageId, user);
396     Assert.assertEquals(response.getId(), expected.getId());
397     Assert.assertEquals(expected.getImageCompositionData().getFileName(), response.getData().
398         getFileName());
399     Assert.assertEquals(expected.getImageCompositionData().getDescription(), response.getData().
400         getDescription());
401     Assert.assertNotNull(response.getSchema());
402   }
403
404   private void testCreate_negative(ImageEntity image, String user,
405                                    String expectedErrorCode, String expectedErrorMsg) {
406     try {
407       vendorSoftwareProductManager.createImage(image, user);
408       Assert.fail();
409     } catch (CoreException exception) {
410       Assert.assertEquals(exception.code().id(), expectedErrorCode);
411       Assert.assertEquals(exception.getMessage(), expectedErrorMsg);
412     }
413   }
414
415   private void testGet_negative(String vspId, Version version, String componentId, String imageId,
416                                 String user,
417                                 String expectedErrorCode, String expectedErrorMsg) {
418     try {
419       vendorSoftwareProductManager.getImage(vspId, version, componentId, imageId, user);
420       Assert.fail();
421     } catch (CoreException exception) {
422       Assert.assertEquals(exception.code().id(), expectedErrorCode);
423       Assert.assertEquals(exception.getMessage(), expectedErrorMsg);
424     }
425   }
426
427   private void testList_negative(String vspId, Version version, String componentId, String user,
428                                  String expectedErrorCode, String expectedErrorMsg) {
429     try {
430       vendorSoftwareProductManager.listImages(vspId, version, componentId, user);
431       Assert.fail();
432     } catch (CoreException exception) {
433       Assert.assertEquals(exception.code().id(), expectedErrorCode);
434       Assert.assertEquals(exception.getMessage(), expectedErrorMsg);
435     }
436   }
437
438   private void testUpdate_negative(ImageEntity imageEntity,
439                                 String user,
440                                 String expectedErrorCode, String expectedErrorMsg) {
441     try {
442
443       vendorSoftwareProductManager.updateImage(imageEntity, user);
444       Assert.fail();
445     } catch (CoreException exception) {
446       Assert.assertEquals(exception.code().id(), expectedErrorCode);
447       Assert.assertEquals(exception.getMessage(), expectedErrorMsg);
448     }
449   }
450
451   private void testDelete_negative(ImageEntity imageEntity,
452                                    String user,
453                                    String expectedErrorCode, String expectedErrorMsg) {
454     try {
455
456       vendorSoftwareProductManager.updateImage(imageEntity, user);
457       Assert.fail();
458     } catch (CoreException exception) {
459       Assert.assertEquals(exception.code().id(), expectedErrorCode);
460       Assert.assertEquals(exception.getMessage(), expectedErrorMsg);
461     }
462   }*/
463 }