summaryrefslogtreecommitdiffstats
path: root/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/ImagesTest.java
blob: 78cd45cdce6cc1842f769d6380f05ac9626a251b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
package org.openecomp.sdc.vendorsoftwareproduct;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.Spy;
import org.openecomp.sdc.common.errors.CoreException;
import org.openecomp.sdc.vendorsoftwareproduct.dao.VendorSoftwareProductInfoDao;
import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ImageEntity;
import org.openecomp.sdc.vendorsoftwareproduct.errors.VendorSoftwareProductErrorCodes;
import org.openecomp.sdc.vendorsoftwareproduct.impl.ImageManagerImpl;
import org.openecomp.sdc.versioning.dao.types.Version;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.verify;

public class ImagesTest {

  private static String VSP_ID = "VSP_ID";
  private static String COMP_ID = "COMP_ID";
  private static String ID = "ID";
  public static final Version VERSION01 = new Version("version_id");

  @Mock
  private VendorSoftwareProductInfoDao vendorSoftwareProductInfoDao;

  @Mock
  private CompositionEntityDataManager compositionEntityDataManager;

  @InjectMocks
  @Spy
  private ImageManagerImpl imageManager;

  @Before
  public void setUp() throws Exception {
    MockitoAnnotations.initMocks(this);
  }

  @After
  public void tearDown(){
    imageManager = null;
  }

  @Test
  public void createImage() {
    ImageEntity imageEntity = new ImageEntity(VSP_ID, VERSION01, COMP_ID, ID);
    doReturn(true).when(vendorSoftwareProductInfoDao).isManual(any(), any());

    imageManager.createImage(imageEntity);
    verify(compositionEntityDataManager).createImage(imageEntity);
  }

  @Test
  public void createImageHeat() {
    ImageEntity imageEntity = new ImageEntity(VSP_ID, VERSION01, COMP_ID, ID);
    doReturn(false).when(vendorSoftwareProductInfoDao).isManual(any(), any());

    try {
      imageManager.createImage(imageEntity);
      Assert.fail();
    } catch (CoreException exception) {
      Assert.assertEquals(exception.code().id(),
          VendorSoftwareProductErrorCodes.ADD_IMAGE_NOT_ALLOWED_IN_HEAT_ONBOARDING);
    }
  }
  /*private static final String USER1 = "imageTestUser1";
  private static final String USER2 = "imageTestUser2";
  private static final Version VERSION01 = new Version(0, 1);
  private static final VendorSoftwareProductManager vendorSoftwareProductManager =
      new VendorSoftwareProductManagerImpl();
  private static final VendorSoftwareProductDao vendorSoftwareProductDao =
      VendorSoftwareProductDaoFactory
          .getInstance().createInterface();

  private static String image1Id;


  private static String comp1 = "{\"displayName\": \"VFC_Manual\", " +
      "\"description\": \"desc manual\"}";

  private static String vsp1Id;
  private static String vsp2Id;
  private static String vsp3Id;
  private static String comp1Id;

  private static String image2Id;

  @BeforeClass
  private void init() {
    VspDetails
        vsp1 = vendorSoftwareProductManager.createNewVsp(VSPCommon
        .createVspDetails(null, null, "VSP_" + CommonMethods.nextUuId(), "Test-vsp1", "vendorName",
            "vlm1Id", "icon", "category", "subCategory", "123", null,
            VSPCommon.OnboardingMethod.Manual.name())1
        );
    vsp1Id = vsp1.getId();

    VspDetails vsp2 = vendorSoftwareProductManager.createNewVsp(VSPCommon
        .createVspDetails(null, null, "VSP_" + CommonMethods.nextUuId(), "Test-vsp2", "vendorName",
            "vlm1Id", "icon", "category", "subCategory", "123", null, VSPCommon.OnboardingMethod.
                Manual.name())1);
    vsp2Id = vsp2.getId();

    VspDetails vsp3 = vendorSoftwareProductManager.createNewVsp(VSPCommon
        .createVspDetails(null, null, "VSP_" + CommonMethods.nextUuId(), "Test-vsp3",
            "vendorName",
            "vlm1Id", "icon", "category", "subCategory", "123", null, VSPCommon
                .OnboardingMethod.HEAT.name())1);
    vsp3Id = vsp3.getId();

    ComponentEntity comp = new ComponentEntity();
    comp.setVspId(vsp2Id);
    comp.setCompositionData(comp1);
    ComponentEntity createdComp = vendorSoftwareProductManager.createComponent(comp1);
    comp1Id = createdComp.getId();
  }

  @Test
  public void testListImagesNonExistingVspId_negative() {
    testList_negative("non existing vsp id", null, image1Id1,
        VersioningErrorCodes.VERSIONABLE_ENTITY_NOT_EXIST,
        "Versionable entity VendorSoftwareProduct with id non existing vsp id does not exist." );
  }

  @Test
  public void testListImagesNonExistingVfcId_negative() {
    testList_negative(vsp1Id, VERSION01, "444"1,
        VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND,
        "Vendor Software Product Component with Id 444 does not exist for Vendor Software Product "
            + "with id "+vsp1Id+ " and version "+VERSION01);
  }

  @Test
  public void testListImages() {
    createImageEntity("media-vsrx-vmdisk-15.1X49-D40.6.aki", "aki");
    createImageEntity("riverbed-15.1X49-D40.6.vdi", "vdi");
    final Collection<ImageEntity> imageEntities =
        vendorSoftwareProductManager.listImages(vsp2Id, VERSION01, comp1Id1);
    System.out.println("size::"+imageEntities.size());
  }

  @Test
  public void testCreateNonExistingVspId_negative() {
    testCreate_negative(new ImageEntity("non existing vsp id", null, null, null)1,
        VersioningErrorCodes.VERSIONABLE_ENTITY_NOT_EXIST,
        "Versionable entity VendorSoftwareProduct with id non existing vsp id does not exist.");
  }

  @Test
  public void testCreateNonExistingVfcId_negative() {
    testCreate_negative(new ImageEntity(vsp1Id, VERSION01, "222", null)1,
        VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND,
        "Vendor Software Product Component with Id 222 does not exist for Vendor Software Product "
            + "with id "+vsp1Id + " and version "+VERSION01);
  }

  @Test(dependsOnMethods = "testUpdateNonExistingImageId_negative")
  public void testCreateOnAvailableVsp_negative() {
    vendorSoftwareProductManager.checkin(vsp1Id1);
    testCreate_negative(new ImageEntity(vsp1Id, null, null, null)1,
        VersioningErrorCodes.EDIT_ON_UNLOCKED_ENTITY,
        "Can not edit versionable entity VendorSoftwareProduct with id "+vsp1Id+ " since it is not"
            + " checked out.");
  }

  @Test(dependsOnMethods = "testCreateOnAvailableVsp_negative")
  public void testCreateOnVspOtherUser_negative() {
    vendorSoftwareProductManager.checkout(vsp1Id1);
    testCreate_negative(new ImageEntity(vsp1Id, null, null, null)2,
        VersioningErrorCodes.EDIT_ON_ENTITY_LOCKED_BY_OTHER_USER,
        "Versionable entity VendorSoftwareProduct with id " +vsp1Id+
            " can not be edited since it is locked by other user "+ USER1+ ".");
  }

  @Test(dependsOnMethods = "testCreateOnVspOtherUser_negative")
  public void testOnUndoCheckoutImagesDeleted() {

    ComponentEntity comp = new ComponentEntity();
    comp.setVspId(vsp1Id);
    comp.setCompositionData(comp1);
    ComponentEntity createdComp = vendorSoftwareProductManager.createComponent(comp1);
    String compId = createdComp.getId();

    vendorSoftwareProductManager.checkin(vsp1Id1);
    vendorSoftwareProductManager.checkout(vsp1Id1);

    for(int i = 1; i<=3; i++) {
      ImageEntity imageEntity = new ImageEntity();
      imageEntity.setVspId(vsp1Id);
      imageEntity.setComponentId(compId);
      Image image = new Image();
      image.setFileName(i + "testimage.aki");
      //image.setVersion("9.2.0");
      image.setDescription("riverbed image");
      //image.setFormat("aki");
      //image.setMd5("233343DDDD");
      imageEntity.setImageCompositionData(image);
      ImageEntity createdImage = vendorSoftwareProductManager.createImage(imageEntity1);
    }

    Collection<ImageEntity> imageEntities =
        vendorSoftwareProductManager.listImages(vsp1Id, null, compId1);

    Assert.assertEquals(imageEntities.size(), 3);

    vendorSoftwareProductManager.undoCheckout(vsp1Id1);

    imageEntities = vendorSoftwareProductManager.listImages(vsp1Id, null, compId1);

    Assert.assertEquals(imageEntities.size(), 0);
  }

  @Test
  public void testCreateOnHeatVsp_negative() {
    final ErrorCode addImageNotSupportedHeatOnboardMethodErrorBuilder =
        NotSupportedHeatOnboardMethodErrorBuilder
            .getAddImageNotSupportedHeatOnboardMethodErrorBuilder();
    testCreate_negative(new ImageEntity(vsp3Id, null, null, null)1,
        addImageNotSupportedHeatOnboardMethodErrorBuilder.id(),
        addImageNotSupportedHeatOnboardMethodErrorBuilder.message()
        );
  }

  @Test(dependsOnMethods = "testListImages")
  public void testCreateImage() {
    ImageEntity createdImage = createImageEntity("riverbed-WX-IMG-9.2.0.qcow2", "qcow2");
    Assert.assertNotNull(image1Id);
    Assert.assertNotNull(createdImage.getCompositionData());
    Assert.assertNotNull(
        vendorSoftwareProductManager.getImage(vsp2Id, VERSION01, comp1Id,image1Id,
            USER1).getData());
  }

  @Test(dependsOnMethods = "testCreateImage")
  public void testCreateDupImageNameForSameComponent_negative() {
    ImageEntity createdImage = null;
    try {
      createdImage = createImageEntity("riverbed-WX-IMG-9.2.0.qcow2", "qcow2");
      Assert.fail();
    }
    catch(CoreException exception) {
      Assert.assertEquals(exception.code().id(), VendorSoftwareProductErrorCodes.
          DUPLICATE_IMAGE_NAME_NOT_ALLOWED);
      Assert.assertEquals(exception.getMessage(),
          String.format("Invalid request, Image with name riverbed-WX-IMG-9.2.0.qcow2 already " +
              "exists for component with ID "+comp1Id)+".");
    }
  }

  @Test
  public void testGet() {
    ImageEntity createdImage = createImageEntity("read-riverbed-WX-IMG-9.2.0.qcow2", "qcow2");
    testGet(vsp2Id, VERSION01, comp1Id, createdImage.getId()1, createdImage);
  }

  @Test
  public void testGetNonExistingVspId_negative() {
    testGet_negative("non existing vsp id", null, null, image1Id1,
        VersioningErrorCodes.VERSIONABLE_ENTITY_NOT_EXIST,
        "Versionable entity VendorSoftwareProduct with id non existing vsp id does not exist." );
  }

  @Test
  public void testGetNonExistingVfcId_negative() {
    testGet_negative(vsp1Id, VERSION01, "111", null1,
        VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND,
        "Vendor Software Product Component with Id 111 does not exist for Vendor Software Product "
            + "with id "+vsp1Id + " and version "+VERSION01);
  }

  @Test
  public void testUpdateNonExistingVspId_negative() {
    ImageEntity imageEntity = new ImageEntity("non existing vsp id", null, null, image1Id);

    testUpdate_negative(imageEntity1,
        VersioningErrorCodes.VERSIONABLE_ENTITY_NOT_EXIST,
        "Versionable entity VendorSoftwareProduct with id non existing vsp id does not exist." );
  }

  @Test(dependsOnMethods =  "testUpdateNonExistingVspId_negative")
  public void testUpdateNonExistingVfcId_negative() {
    ImageEntity imageEntity = new ImageEntity(vsp1Id, VERSION01, "111", null);

    testUpdate_negative(imageEntity1,
        VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND,
        "Vendor Software Product Component with Id 111 does not exist for Vendor Software Product "
            + "with id "+vsp1Id + " and version "+VERSION01);
  }

  @Test(dependsOnMethods =  "testUpdateNonExistingVfcId_negative")
  public void testUpdateNonExistingImageId_negative() {
    ImageEntity imageEntity = new ImageEntity(vsp2Id, VERSION01, comp1Id, "222");

    testUpdate_negative(imageEntity1,
        VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND,
        "Vendor Software Product Component Image with Id 222 does not exist for Vendor " +
            "Software Product with id "+vsp2Id+ " and version "+VERSION01 );
  }

  @Test(dependsOnMethods = "testCreateImage")
  public void testUpdate() {
    ImageEntity imageEntity = createImageEntity("testimage1","qcow2");
    testGet(vsp2Id, VERSION01, comp1Id, imageEntity.getId(),USER1, imageEntity );

    final Image imageCompositionData = imageEntity.getImageCompositionData();
    //imageCompositionData.setVersion("10.0");
    imageCompositionData.setDescription("updated image");

    vendorSoftwareProductManager.updateImage(imageEntity1);

    testGet(vsp2Id, VERSION01, comp1Id, imageEntity.getId(),USER1, imageEntity );
    image2Id = imageEntity.getId();
  }

  @Test(dependsOnMethods = "testUpdate")
  public void testUpdateNegative_UniqueName() {
    final CompositionEntityResponse<Image> image =
        vendorSoftwareProductManager.getImage(vsp2Id, VERSION01, comp1Id,
            image2Id1);
    final Image data = image.getData();

    final Image imageCompositionData = data;
    imageCompositionData.setFileName("riverbed-WX-IMG-9.2.0.qcow2");

    ImageEntity entity = new ImageEntity(vsp2Id, VERSION01, comp1Id, image2Id );
    entity.setImageCompositionData(imageCompositionData);
    testUpdate_negative(entity1, ImageErrorBuilder.getDuplicateImageNameErrorBuilder(
        "riverbed-WX-IMG-9.2.0.qcow2", comp1Id).id()
        ,ImageErrorBuilder.getDuplicateImageNameErrorBuilder("riverbed-WX-IMG-9.2.0.qcow2", comp1Id)
            .message() );
  }

  @Test(dependsOnMethods = "testUpdateNegative_UniqueName")
  public void testDeleteImage() {
    CompositionEntityResponse<Image> image =
        vendorSoftwareProductManager.getImage(vsp2Id, VERSION01, comp1Id, image2Id1);

    Assert.assertNotNull(image.getData());

    vendorSoftwareProductManager.deleteImage(vsp2Id, comp1Id, image2Id1);

    testGet_negative(vsp2Id, VERSION01, comp1Id, image2Id1,
        VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND,
        "Vendor Software Product Component Image with Id "+image2Id+ " does not exist for " +
            "Vendor Software Product with id "+vsp2Id+ " and version "+VERSION01 );

  }

  @Test
  public void testDeleteNonExistingVspId_negative() {
    ImageEntity imageEntity = new ImageEntity("non existing vsp id", null, null, image1Id);

    testDelete_negative(imageEntity1,
        VersioningErrorCodes.VERSIONABLE_ENTITY_NOT_EXIST,
        "Versionable entity VendorSoftwareProduct with id non existing vsp id does not exist." );
  }

  @Test(dependsOnMethods =  "testDeleteNonExistingVspId_negative")
  public void testDeleteNonExistingVfcId_negative() {
    ImageEntity imageEntity = new ImageEntity(vsp1Id, VERSION01, "111", null);

    testDelete_negative(imageEntity1,
        VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND,
        "Vendor Software Product Component with Id 111 does not exist for Vendor Software Product "
            + "with id "+vsp1Id + " and version "+VERSION01);
  }

  @Test(dependsOnMethods =  "testDeleteNonExistingVfcId_negative")
  public void testDeleteNonExistingImageId_negative() {
    ImageEntity imageEntity = new ImageEntity(vsp2Id, VERSION01, comp1Id, "222");

    testDelete_negative(imageEntity1,
        VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND,
        "Vendor Software Product Component Image with Id 222 does not exist for Vendor " +
            "Software Product with id "+vsp2Id+ " and version "+VERSION01 );
  }

  @Test
  public void testUpdateandGetQuestionnaire() {
    ImageEntity imageEntity = createImageEntity("newimage_testUpdateandGetQuestionnaire.vdi","vdi");
    vendorSoftwareProductManager.updateImageQuestionnaire(vsp2Id, comp1Id, imageEntity.getId(),
        "{\"format\":\"vdi\",\"version\":\"1.2\",\"md5\":\"ssd3344\"}",
        USER1);

    final QuestionnaireResponse imageQuestionnaire =
        vendorSoftwareProductManager.getImageQuestionnaire(vsp2Id, VERSION01, comp1Id,
            imageEntity.getId()1);

    String imageDetails = imageQuestionnaire.getData();
    Assert.assertEquals("vdi", JsonUtil.json2Object(imageDetails, ImageDetails.class).getFormat());
    Assert.assertEquals("1.2", JsonUtil.json2Object(imageDetails, ImageDetails.class).getVersion());
    Assert.assertEquals("ssd3344", JsonUtil.json2Object(imageDetails, ImageDetails.class).getMd5());
  }

  @Test
  public void testUpdateQuestionnaireInvalidFormat_negative() {
    ImageEntity imageEntity = createImageEntity("newimage.vdi","vdi");
    try {
      vendorSoftwareProductManager.updateImageQuestionnaire(vsp2Id, comp1Id, imageEntity.getId(),
          "{\"format\":\"invalidformat\",\"version\":\"1.2\",\"md5\":\"ssd3344\"}",
          USER1);
      Assert.fail();
    }
    catch(CoreException exception) {
      Assert.assertEquals(exception.code().id(), "VFC_IMAGE_INVALID_FORMAT");
      Assert.assertEquals(exception.getMessage(), "The format value doesn't meet the expected "
          + "attribute value.");
    }
  }

  private ImageEntity createImageEntity(String fileName, String format) {
    ImageEntity imageEntity = new ImageEntity();
    imageEntity.setVspId(vsp2Id);
    imageEntity.setComponentId(comp1Id);
    Image image = new Image();
    image.setFileName(fileName);
    //image.setVersion("9.2.0");
    image.setDescription("riverbed image");
    //image.setFormat(format);
    // image.setMd5("233343DDDD");
    imageEntity.setImageCompositionData(image);

    ImageEntity createdImage = vendorSoftwareProductManager.createImage(imageEntity1);
    image1Id = createdImage.getId();
    return createdImage;
  }

  private void testGet(String vspId, Version version, String componentId, String imageId, String
      user, ImageEntity expected) {
    CompositionEntityResponse<Image>
        response = vendorSoftwareProductManager.getImage(vspId, null, componentId, imageId);
    Assert.assertEquals(response.getId(), expected.getId());
    Assert.assertEquals(expected.getImageCompositionData().getFileName(), response.getData().
        getFileName());
    Assert.assertEquals(expected.getImageCompositionData().getDescription(), response.getData().
        getDescription());
    Assert.assertNotNull(response.getSchema());
  }

  private void testCreate_negative(ImageEntity image, String user,
                                   String expectedErrorCode, String expectedErrorMsg) {
    try {
      vendorSoftwareProductManager.createImage(image);
      Assert.fail();
    } catch (CoreException exception) {
      Assert.assertEquals(exception.code().id(), expectedErrorCode);
      Assert.assertEquals(exception.getMessage(), expectedErrorMsg);
    }
  }

  private void testGet_negative(String vspId, Version version, String componentId, String imageId,
                                String user,
                                String expectedErrorCode, String expectedErrorMsg) {
    try {
      vendorSoftwareProductManager.getImage(vspId, version, componentId, imageId);
      Assert.fail();
    } catch (CoreException exception) {
      Assert.assertEquals(exception.code().id(), expectedErrorCode);
      Assert.assertEquals(exception.getMessage(), expectedErrorMsg);
    }
  }

  private void testList_negative(String vspId, Version version, String componentId, String user,
                                 String expectedErrorCode, String expectedErrorMsg) {
    try {
      vendorSoftwareProductManager.listImages(vspId, version, componentId);
      Assert.fail();
    } catch (CoreException exception) {
      Assert.assertEquals(exception.code().id(), expectedErrorCode);
      Assert.assertEquals(exception.getMessage(), expectedErrorMsg);
    }
  }

  private void testUpdate_negative(ImageEntity imageEntity,
                                String user,
                                String expectedErrorCode, String expectedErrorMsg) {
    try {

      vendorSoftwareProductManager.updateImage(imageEntity);
      Assert.fail();
    } catch (CoreException exception) {
      Assert.assertEquals(exception.code().id(), expectedErrorCode);
      Assert.assertEquals(exception.getMessage(), expectedErrorMsg);
    }
  }

  private void testDelete_negative(ImageEntity imageEntity,
                                   String user,
                                   String expectedErrorCode, String expectedErrorMsg) {
    try {

      vendorSoftwareProductManager.updateImage(imageEntity);
      Assert.fail();
    } catch (CoreException exception) {
      Assert.assertEquals(exception.code().id(), expectedErrorCode);
      Assert.assertEquals(exception.getMessage(), expectedErrorMsg);
    }
  }*/
}