[SDC] Onboarding 1710 rebase.
[sdc.git] / openecomp-be / backend / openecomp-sdc-vendor-software-product-manager / src / test / java / org / openecomp / sdc / vendorsoftwareproduct / impl / ComputeManagerImplTest.java
1 package org.openecomp.sdc.vendorsoftwareproduct.impl;
2
3 import org.junit.runner.RunWith;
4 import org.mockito.InjectMocks;
5 import org.mockito.Mock;
6 import org.mockito.MockitoAnnotations;
7 import org.mockito.Spy;
8 import org.openecomp.sdc.common.errors.CoreException;
9 import org.openecomp.sdc.common.errors.ValidationErrorBuilder;
10 import org.openecomp.sdc.vendorsoftwareproduct.ComputeManager;
11 import org.openecomp.sdc.vendorsoftwareproduct.NetworkManager;
12 import org.openecomp.sdc.vendorsoftwareproduct.dao.ComputeDao;
13 import org.openecomp.sdc.vendorsoftwareproduct.dao.DeploymentFlavorDao;
14 import org.openecomp.sdc.vendorsoftwareproduct.dao.NicDao;
15 import org.openecomp.sdc.vendorsoftwareproduct.dao.VendorSoftwareProductInfoDao;
16 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.DeploymentFlavorEntity;
17 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.NetworkEntity;
18 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComputeEntity;
19 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails;
20 import org.openecomp.sdc.vendorsoftwareproduct.errors.DuplicateComputeInComponentErrorBuilder;
21 import org.openecomp.sdc.vendorsoftwareproduct.errors.VendorSoftwareProductErrorCodes;
22 import org.openecomp.sdc.vendorsoftwareproduct.services.composition.CompositionEntityDataManager;
23 import org.openecomp.sdc.vendorsoftwareproduct.services.schemagenerator.SchemaGenerator;
24 import org.openecomp.sdc.vendorsoftwareproduct.types.CompositionEntityResponse;
25 import org.openecomp.sdc.vendorsoftwareproduct.types.ListComputeResponse;
26 import org.openecomp.sdc.vendorsoftwareproduct.types.QuestionnaireResponse;
27 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.*;
28 import org.openecomp.sdc.versioning.dao.types.Version;
29 import org.openecomp.sdc.versioning.errors.VersioningErrorCodes;
30 import org.testng.Assert;
31 import org.testng.annotations.BeforeMethod;
32 import org.testng.annotations.Test;
33
34 import java.util.ArrayList;
35 import java.util.Arrays;
36 import java.util.Collection;
37 import java.util.List;
38
39 import static org.mockito.Matchers.anyObject;
40 import static org.mockito.Mockito.doReturn;
41 import static org.mockito.Mockito.never;
42 import static org.mockito.Mockito.verify;
43 import static org.mockito.Mockito.when;
44
45 /**
46  * Created by DIVESHM on 5/16/2017.
47  */
48
49
50 public class ComputeManagerImplTest {
51
52     private static final String COMPUTE_NOT_EXIST_MSG =
53             "Vendor Software Product COMPUTE with Id compute1 does not exist for Vendor Software Product with " +
54                     "id VSP_ID and version 0.1";
55
56     private static final String USER = "computeTestUser";
57     private static final String VSP_ID = "VSP_ID";
58     private static final Version VERSION = new Version(0, 1);
59     private static final String COMPONENT_ID = "COMPONENT_ID";
60     private static final String COMPUTE1_ID = "compute1";
61     private static final String COMPUTE2_ID = "compute2";
62
63   @Mock
64   private ComputeDao computeDao;
65
66   @Mock
67   private CompositionEntityDataManager compositionEntityDataManagerMock;
68
69   @Mock
70   private NetworkManager networkManagerMock;
71
72   @Mock
73   private VendorSoftwareProductInfoDao vspInfoDao;
74
75   @Mock
76   private ComputeEntity computeEntity;
77
78   @Mock
79   private ListComputeResponse listComputeResponse;
80
81   @Mock
82   private DeploymentFlavorEntity deploymentFlavorEntity;
83
84   @Mock
85   private DeploymentFlavorDao deploymentFlavorDao;
86
87   @Mock
88   private ComponentComputeAssociation componentComputeAssociation;
89
90   @Mock
91   DeploymentFlavor deploymentFlavor;
92
93   @InjectMocks
94   @Spy
95   private ComputeManagerImpl computeManager;
96
97   @BeforeMethod
98   public void setUp() throws Exception {
99       MockitoAnnotations.initMocks(this);
100   }
101
102    @Test
103   public void testListWhenNone() {
104     Collection<ListComputeResponse> computes = computeManager.listCompute(VSP_ID, VERSION, COMPONENT_ID, USER);
105     Assert.assertEquals(computes.size(), 0);
106   }
107
108   @Test
109   public void testList(){
110       doReturn(Arrays.asList(
111         createCompute(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID),
112         createCompute(VSP_ID, VERSION, COMPONENT_ID, COMPUTE2_ID)))
113               .when(computeDao).list(anyObject());
114
115
116       Collection<ListComputeResponse> computes = computeManager.listCompute(VSP_ID, VERSION, COMPONENT_ID, USER);
117       Assert.assertEquals(computes.size(), 2);
118       for (ListComputeResponse compute : computes) {
119           Assert.assertEquals(compute.getComputeEntity().getComputeCompositionData().getName(),
120                   COMPUTE1_ID.equals(compute.getComputeEntity().getId())
121                           ? "compute1name"
122                           : "compute2name");
123       };
124   }
125
126     @Test
127     public void testCreateOnNotManualCompute_negative() {
128
129         testCreate_negative(new ComputeEntity(VSP_ID, VERSION, COMPONENT_ID, null), USER,
130                 VendorSoftwareProductErrorCodes.ADD_COMPUTE_NOT_ALLOWED_IN_HEAT_ONBOARDING);
131     }
132
133     @Test
134     public void testCreateManualCompute() {
135         ComputeEntity expected = createCompute(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID);
136         doReturn(true).when(vspInfoDao).isManual(anyObject(), anyObject());
137         computeManager.createCompute(expected, USER);
138         verify(compositionEntityDataManagerMock).createCompute(expected);
139         verify(compositionEntityDataManagerMock).createCompute(expected);
140     }
141
142     @Test
143     public void testCreateManualImageWithDuplicateName() {
144         ComputeEntity expected = createCompute(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID);
145         doReturn(true).when(vspInfoDao).isManual(anyObject(), anyObject());
146
147         ComputeEntity expectedDiffName = createCompute(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID);
148         ComputeData computeData = expectedDiffName.getComputeCompositionData();
149         computeData.setName(COMPUTE1_ID + "Name");
150         expectedDiffName.setComputeCompositionData(computeData);
151         List<ComputeEntity> vfcImageList = new ArrayList<ComputeEntity>();
152         vfcImageList.add(expectedDiffName);
153         doReturn(vfcImageList).when(computeDao).list(anyObject());
154
155         try {
156             computeManager.createCompute(expected, USER);
157             Assert.fail();
158         }
159         catch (CoreException ex) {
160             Assert.assertEquals(VendorSoftwareProductErrorCodes.DUPLICATE_COMPUTE_NAME_NOT_ALLOWED,
161                     ex.code().id());
162         }
163     }
164
165
166     @Test
167     public void testUpdateNonExistingComputeId_negative() {
168
169         testUpdate_negative(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID, USER,
170                 VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND);
171     }
172
173     @Test
174     public void testUpdateCompute() {
175         doReturn(createCompute(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID))
176                 .when(computeDao).get(anyObject());
177
178         doReturn(new CompositionEntityValidationData(CompositionEntityType.compute, COMPUTE1_ID))
179                 .when(compositionEntityDataManagerMock)
180                 .validateEntity(anyObject(), anyObject(), anyObject());
181
182         ComputeEntity computeEntity = new ComputeEntity(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID);
183         ComputeData computeData = new ComputeData();
184         computeData.setName(COMPUTE1_ID + "name");
185         computeData.setDescription(COMPUTE1_ID + "desc updated");
186         computeEntity.setComputeCompositionData(computeData);
187
188         CompositionEntityValidationData validationData =
189                 computeManager.updateCompute(computeEntity, USER);
190         Assert.assertTrue(validationData == null || validationData.getErrors() == null);
191         verify(computeDao).update(computeEntity);
192     }
193
194     @Test
195     public void testIllegalComputeUpdate() {
196         doReturn(createCompute(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID))
197                 .when(computeDao).get(anyObject());
198
199         doReturn(true).when(vspInfoDao).isManual(anyObject(), anyObject());
200
201         CompositionEntityValidationData toBeReturned =
202                 new CompositionEntityValidationData(CompositionEntityType.compute, COMPUTE1_ID);
203         toBeReturned.setErrors(Arrays.asList("error1", "error2"));
204         doReturn(toBeReturned)
205                 .when(compositionEntityDataManagerMock)
206                 .validateEntity(anyObject(), anyObject(), anyObject());
207
208         ComputeEntity computeEntity = new ComputeEntity(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID);
209         ComputeData computeData = new ComputeData();
210         computeData.setName(COMPUTE1_ID + " name updated");
211         computeData.setDescription(COMPUTE1_ID + " desc updated");
212         computeEntity.setComputeCompositionData(computeData);
213
214         CompositionEntityValidationData validationData = computeManager.updateCompute(computeEntity, USER);
215         Assert.assertNotNull(validationData);
216         Assert.assertEquals(validationData.getErrors().size(), 2);
217
218         verify(computeDao, never()).update(computeEntity);
219     }
220
221     @Test
222     public void testUpdateHEATComputeName() throws Exception {
223         doReturn(createCompute(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID))
224                 .when(computeDao).get(anyObject());
225         ComputeEntity computeEntity = new ComputeEntity(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID);
226         ComputeData computeData = new ComputeData();
227         computeData.setName(COMPUTE1_ID + " name updated");
228         computeData.setDescription(COMPUTE1_ID + " desc updated");
229         computeEntity.setComputeCompositionData(computeData);
230
231         try {
232             computeManager.updateCompute(computeEntity, USER);
233         }
234         catch (CoreException ex) {
235             Assert.assertEquals(ex.code().id(), VendorSoftwareProductErrorCodes.UPDATE_COMPUTE_NOT_ALLOWED);
236         }
237
238     }
239
240     @Test
241     public void testUpdateManualComputeQuestionnaire() throws Exception {
242         String json = "{\"md5\" :\"FFDSD33SS\"}";
243         doReturn(true).when(vspInfoDao).isManual(anyObject(), anyObject());
244         doReturn(new ComputeEntity(null,null,null,null)).when(computeDao).get(anyObject());
245
246         computeManager.updateComputeQuestionnaire(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID, json, USER);
247         verify(computeDao).updateQuestionnaireData(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID, json);
248     }
249
250     @Test
251     public void testGetNonExistingComputeId_negative() {
252         testGet_negative(VSP_ID, VERSION, COMPONENT_ID, "non existing compute id", USER,
253                 VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND);
254     }
255
256     @Test
257     public void testGet() {
258         ComputeEntity expected = createCompute(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID);
259         doReturn(expected).when(computeDao).get(anyObject());
260         String compositionSchema = "schema string";
261         doReturn(compositionSchema).when(computeManager).getComputeCompositionSchema(anyObject());
262
263         CompositionEntityResponse<ComputeData> response =
264                 computeManager.getCompute(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID, USER);
265         Assert.assertEquals(response.getId(), expected.getId());
266         Assert.assertEquals(response.getData().getName(), expected.getComputeCompositionData().getName());
267         Assert.assertEquals(response.getData().getDescription(), expected.getComputeCompositionData().
268                 getDescription());
269         Assert.assertEquals(response.getSchema(), compositionSchema);
270     }
271
272     @Test
273     public void testGetQuestionnaire() throws Exception {
274         ComputeEntity compute = new ComputeEntity(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID);
275         compute.setQuestionnaireData("{}");
276         doReturn(compute).when(computeDao).getQuestionnaireData(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID);
277
278         String schema = "schema string";
279
280
281         doReturn(schema).when(computeManager).getComputeQuestionnaireSchema(anyObject());
282
283         QuestionnaireResponse questionnaire =
284                 computeManager.getComputeQuestionnaire(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID, USER);
285
286         Assert.assertNotNull(questionnaire);
287         Assert.assertEquals(questionnaire.getData(), compute.getQuestionnaireData());
288         Assert.assertEquals(questionnaire.getSchema(), schema);
289         Assert.assertNull(questionnaire.getErrorMessage());
290     }
291
292     @Test
293     public void testDeleteOnNotManualCompute() {
294         ComputeEntity expected = createCompute(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID);
295         doReturn(expected).when(computeDao).get(anyObject());
296         testDelete_negative(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID, USER,
297                 VendorSoftwareProductErrorCodes.VSP_COMPOSITION_EDIT_NOT_ALLOWED);
298     }
299
300     @Test
301     public void testDeleteOnManualCompute() {
302         ComputeEntity expected = createCompute(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID);
303         doReturn(expected).when(computeDao).get(anyObject());
304         doReturn(true).when(vspInfoDao).isManual(anyObject(), anyObject());
305         computeManager.deleteCompute(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID, USER);
306         verify(computeDao).delete(anyObject());
307     }
308
309     @Test
310     public void testDeleteOnNotExistCompute() {
311         testDelete_negative(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID, USER,
312                 VendorSoftwareProductErrorCodes.VSP_COMPOSITION_EDIT_NOT_ALLOWED);
313     }
314
315
316     private void testDelete_negative(String vspId, Version version, String componentId, String computeId,
317                                      String user,
318                                      String expectedErrorCode) {
319         try {
320             computeManager.deleteCompute(vspId, version, componentId, computeId, user);
321             Assert.fail();
322         } catch (CoreException exception) {
323             Assert.assertEquals(exception.code().id(), expectedErrorCode);
324         }
325     }
326
327     private void testGet_negative(String vspId, Version version, String componentId, String computeId,
328                                 String user, String expectedErrorCode) {
329       try {
330           computeManager.getCompute(vspId, version, componentId, computeId, user);
331           Assert.fail();
332       } catch (CoreException exception) {
333           Assert.assertEquals(exception.code().id(), expectedErrorCode);
334       }
335   }
336
337     private void testList_negative(String vspId, Version version, String componentId, String user,
338                                    String expectedErrorCode, String expectedErrorMsg) {
339         try {
340             computeManager.listCompute(vspId, version, componentId, user);
341             Assert.fail();
342         } catch (CoreException exception) {
343             Assert.assertEquals(exception.code().id(), expectedErrorCode);
344             Assert.assertEquals(exception.getMessage(), expectedErrorMsg);
345         }
346     }
347
348
349
350     private void testUpdate_negative(String vspId, Version version, String componentId, String computeId,
351                                  String user, String expectedErrorCode) {
352   try {
353     computeManager.updateCompute(new ComputeEntity(vspId, version, componentId, computeId), user);
354     Assert.fail();
355   } catch (CoreException exception) {
356       Assert.assertEquals(exception.code().id(), expectedErrorCode);
357     }
358   }
359
360   private void testCreate_negative(ComputeEntity computeEntity1, String user, String expectedErrorCode) {
361         try {
362             computeManager.createCompute(computeEntity1, user);
363             Assert.fail();
364         } catch (CoreException exception) {
365             Assert.assertEquals(exception.code().id(), expectedErrorCode);
366         }
367     }
368
369   static ComputeEntity createCompute(String vspId, Version version, String compId, String computeId){
370       ComputeEntity computeEntity1 = new ComputeEntity(vspId, version, compId, computeId);
371       ComputeData computeData = new ComputeData();
372       computeData.setName(computeId+"name");
373       computeData.setDescription(computeId+"desc");
374       computeEntity1.setComputeCompositionData(computeData);
375       return computeEntity1;
376   }
377 }