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