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