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