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