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