Added oparent to sdc main
[sdc.git] / openecomp-be / backend / openecomp-sdc-vendor-software-product-manager / src / test / java / org / openecomp / sdc / vendorsoftwareproduct / impl / ComputeManagerImplTest.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.impl;
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.common.errors.ErrorCategory;
33 import org.openecomp.sdc.common.errors.ErrorCode;
34 import org.openecomp.sdc.vendorsoftwareproduct.CompositionEntityDataManager;
35 import org.openecomp.sdc.vendorsoftwareproduct.dao.ComputeDao;
36 import org.openecomp.sdc.vendorsoftwareproduct.dao.DeploymentFlavorDao;
37 import org.openecomp.sdc.vendorsoftwareproduct.dao.VendorSoftwareProductInfoDao;
38 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComputeEntity;
39 import org.openecomp.sdc.vendorsoftwareproduct.errors.VendorSoftwareProductErrorCodes;
40 import org.openecomp.sdc.vendorsoftwareproduct.types.CompositionEntityResponse;
41 import org.openecomp.sdc.vendorsoftwareproduct.types.ListComputeResponse;
42 import org.openecomp.sdc.vendorsoftwareproduct.types.QuestionnaireResponse;
43 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionEntityType;
44 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionEntityValidationData;
45 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.ComputeData;
46 import org.openecomp.sdc.versioning.dao.types.Version;
47 import org.openecomp.sdc.versioning.errors.VersioningErrorCodes;
48
49 import java.util.Arrays;
50 import java.util.Collection;
51
52 import static org.mockito.ArgumentMatchers.any;
53 import static org.mockito.Mockito.doNothing;
54 import static org.mockito.Mockito.doReturn;
55 import static org.mockito.Mockito.doThrow;
56 import static org.mockito.Mockito.never;
57 import static org.mockito.Mockito.verify;
58
59
60 public class ComputeManagerImplTest {
61
62   private static final String COMPUTE_NOT_EXIST_MSG =
63       "Vendor Software Product COMPUTE with Id compute1 does not exist for Vendor Software Product with " +
64           "id VSP_ID and version version_id";
65
66   private static final String VSP_ID = "VSP_ID";
67   private static final Version VERSION = new Version("version_id");
68   private static final String COMPONENT_ID = "COMPONENT_ID";
69   private static final String COMPUTE1_ID = "compute1";
70   private static final String COMPUTE2_ID = "compute2";
71
72   @Mock
73   private ComputeDao computeDao;
74   @Mock
75   private CompositionEntityDataManager compositionEntityDataManagerMock;
76   @Mock
77   private VendorSoftwareProductInfoDao vspInfoDao;
78   @Mock
79   private DeploymentFlavorDao deploymentFlavorDao;
80   @InjectMocks
81   @Spy
82   private ComputeManagerImpl computeManager;
83
84   @Before
85   public void setUp() throws Exception {
86     MockitoAnnotations.initMocks(this);
87   }
88
89   @After
90   public void tearDown() {
91     computeManager = null;
92   }
93
94   @Test
95   public void testListWhenNone() {
96     Collection<ListComputeResponse> computes =
97         computeManager.listComputes(VSP_ID, VERSION, COMPONENT_ID);
98     Assert.assertEquals(computes.size(), 0);
99   }
100
101   @Test
102   public void testList() {
103     doReturn(Arrays.asList(
104         createCompute(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID),
105         createCompute(VSP_ID, VERSION, COMPONENT_ID, COMPUTE2_ID)))
106         .when(computeDao).list(any());
107
108
109     Collection<ListComputeResponse> computes =
110         computeManager.listComputes(VSP_ID, VERSION, COMPONENT_ID);
111     Assert.assertEquals(computes.size(), 2);
112     for (ListComputeResponse compute : computes) {
113       Assert.assertEquals(compute.getComputeEntity().getComputeCompositionData().getName(),
114           COMPUTE1_ID.equals(compute.getComputeEntity().getId())
115               ? "compute1name"
116               : "compute2name");
117     }
118   }
119
120   @Test
121   public void testCreateOnNotManualCompute_negative() {
122     testCreate_negative(new ComputeEntity(VSP_ID, VERSION, COMPONENT_ID, null),
123         VendorSoftwareProductErrorCodes.ADD_COMPUTE_NOT_ALLOWED_IN_HEAT_ONBOARDING);
124   }
125
126   @Test
127   public void testCreateManualCompute() {
128     ComputeEntity expected = createCompute(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID);
129     doReturn(true).when(vspInfoDao).isManual(any(), any());
130     doNothing().when(computeManager)
131         .validateUniqueName(VSP_ID, VERSION, COMPONENT_ID,
132             expected.getComputeCompositionData().getName());
133     doNothing().when(computeManager)
134         .createUniqueName(VSP_ID, VERSION, COMPONENT_ID,
135             expected.getComputeCompositionData().getName());
136     String questionnaireSchema = "{}";
137     doReturn(questionnaireSchema).when(computeManager).getComputeQuestionnaireSchema(any());
138
139     computeManager.createCompute(expected);
140     verify(computeDao).create(expected);
141   }
142
143   @Test(expected = CoreException.class)
144   public void testCreateManualComputeWithDuplicateName() {
145     ComputeEntity expected = createCompute(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID);
146     doReturn(true).when(vspInfoDao).isManual(any(), any());
147
148     doThrow(new CoreException(
149         new ErrorCode.ErrorCodeBuilder().withCategory(ErrorCategory.APPLICATION).build()))
150         .when(computeManager).validateUniqueName(VSP_ID, VERSION, COMPONENT_ID,
151         expected.getComputeCompositionData().getName());
152
153     computeManager.createCompute(expected);
154   }
155
156   @Test
157   public void testUpdateNonExistingComputeId_negative() {
158     testUpdate_negative(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID,
159         VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND);
160   }
161
162   @Test
163   public void testUpdateCompute() {
164     ComputeEntity retrieved = createCompute(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID);
165     doReturn(retrieved).when(computeDao).get(any());
166
167     doReturn(new CompositionEntityValidationData(CompositionEntityType.compute, COMPUTE1_ID))
168         .when(compositionEntityDataManagerMock)
169         .validateEntity(any(), any(), any());
170
171     ComputeEntity computeEntity = new ComputeEntity(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID);
172     ComputeData computeData = new ComputeData();
173     computeData.setName(COMPUTE1_ID + "name");
174     computeData.setDescription(COMPUTE1_ID + "desc updated");
175     computeEntity.setComputeCompositionData(computeData);
176
177     doNothing().when(computeManager)
178         .updateUniqueName(VSP_ID, VERSION, COMPONENT_ID, retrieved.getComputeCompositionData().getName(),
179             computeData.getName());
180
181     CompositionEntityValidationData validationData =
182         computeManager.updateCompute(computeEntity);
183     Assert.assertTrue(validationData == null || validationData.getErrors() == null);
184     verify(computeDao).update(computeEntity);
185   }
186
187   @Test
188   public void testIllegalComputeUpdate() {
189     doReturn(createCompute(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID))
190         .when(computeDao).get(any());
191
192     doReturn(true).when(vspInfoDao).isManual(any(), any());
193
194     CompositionEntityValidationData toBeReturned =
195         new CompositionEntityValidationData(CompositionEntityType.compute, COMPUTE1_ID);
196     toBeReturned.setErrors(Arrays.asList("error1", "error2"));
197     doReturn(toBeReturned)
198         .when(compositionEntityDataManagerMock)
199         .validateEntity(any(), any(), any());
200
201     ComputeEntity computeEntity = new ComputeEntity(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID);
202     ComputeData computeData = new ComputeData();
203     computeData.setName(COMPUTE1_ID + "_name_updated");
204     computeData.setDescription(COMPUTE1_ID + " desc updated");
205     computeEntity.setComputeCompositionData(computeData);
206
207     CompositionEntityValidationData validationData =
208         computeManager.updateCompute(computeEntity);
209     Assert.assertNotNull(validationData);
210     Assert.assertEquals(validationData.getErrors().size(), 2);
211
212     verify(computeDao, never()).update(computeEntity);
213   }
214
215   @Test
216   public void testUpdateHEATComputeName() throws Exception {
217     doReturn(createCompute(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID))
218         .when(computeDao).get(any());
219
220     String updatedName = COMPUTE1_ID + " name updated";
221     CompositionEntityValidationData toBeReturned =
222         new CompositionEntityValidationData(CompositionEntityType.compute, COMPUTE1_ID);
223
224     toBeReturned.setErrors(Arrays.asList("#/name: "+updatedName+" is not a valid value."+
225         COMPUTE1_ID+"is the only possible value for this field"));
226     doReturn(toBeReturned).when(compositionEntityDataManagerMock).validateEntity(any(),any(),any());
227
228     ComputeEntity computeEntity = new ComputeEntity(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID);
229     ComputeData computeData = new ComputeData();
230     computeData.setName(updatedName);
231     computeData.setDescription(COMPUTE1_ID + " desc updated");
232     computeEntity.setComputeCompositionData(computeData);
233
234     CompositionEntityValidationData output = computeManager.updateCompute(computeEntity);
235
236     Assert.assertEquals(output.getErrors(), toBeReturned.getErrors());
237   }
238
239   @Test
240   public void testUpdateManualComputeQuestionnaire() throws Exception {
241     String json = "{\"md5\" :\"FFDSD33SS\"}";
242     doReturn(true).when(vspInfoDao).isManual(any(), any());
243     doReturn(new ComputeEntity(null, null, null, null)).when(computeDao).get(any());
244
245     computeManager
246         .updateComputeQuestionnaire(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID, json);
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",
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(any());
260     String compositionSchema = "schema string";
261     doReturn(compositionSchema).when(computeManager).getComputeCompositionSchema(any());
262
263     CompositionEntityResponse<ComputeData> response =
264         computeManager.getCompute(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID);
265     Assert.assertEquals(response.getId(), expected.getId());
266     Assert
267         .assertEquals(response.getData().getName(), expected.getComputeCompositionData().getName());
268     Assert.assertEquals(response.getData().getDescription(), expected.getComputeCompositionData().
269         getDescription());
270     Assert.assertEquals(response.getSchema(), compositionSchema);
271   }
272
273   @Test
274   public void testGetQuestionnaire() throws Exception {
275     ComputeEntity compute = new ComputeEntity(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID);
276     compute.setQuestionnaireData("{}");
277     doReturn(compute).when(computeDao)
278         .getQuestionnaireData(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID);
279
280     String schema = "schema string";
281
282     doReturn(schema).when(computeManager).getComputeQuestionnaireSchema(any());
283
284     QuestionnaireResponse questionnaire =
285         computeManager.getComputeQuestionnaire(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID);
286
287     Assert.assertNotNull(questionnaire);
288     Assert.assertEquals(questionnaire.getData(), compute.getQuestionnaireData());
289     Assert.assertEquals(questionnaire.getSchema(), schema);
290     Assert.assertNull(questionnaire.getErrorMessage());
291   }
292
293   @Test
294   public void testDeleteOnNotManualCompute() {
295     ComputeEntity expected = createCompute(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID);
296     doReturn(expected).when(computeDao).get(any());
297     testDelete_negative(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID,
298         VendorSoftwareProductErrorCodes.VSP_COMPOSITION_EDIT_NOT_ALLOWED);
299   }
300
301   @Test
302   public void testDeleteOnManualCompute() {
303     ComputeEntity expected = createCompute(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID);
304     doReturn(expected).when(computeDao).get(any());
305     doReturn(true).when(vspInfoDao).isManual(any(), any());
306     doNothing().when(computeManager).deleteUniqueValue(VSP_ID, VERSION, COMPONENT_ID,
307         expected.getComputeCompositionData().getName());
308
309     computeManager.deleteCompute(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID);
310     verify(computeDao).delete(any());
311   }
312
313   @Test
314   public void testDeleteOnNotExistCompute() {
315     testDelete_negative(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID,
316         VendorSoftwareProductErrorCodes.VSP_COMPOSITION_EDIT_NOT_ALLOWED);
317   }
318
319
320   private void testDelete_negative(String vspId, Version version, String componentId,
321                                    String computeId,
322                                    String expectedErrorCode) {
323     try {
324       computeManager.deleteCompute(vspId, version, componentId, computeId);
325       Assert.fail();
326     } catch (CoreException exception) {
327       Assert.assertEquals(exception.code().id(), expectedErrorCode);
328     }
329   }
330
331   private void testGet_negative(String vspId, Version version, String componentId, String computeId,
332                                 String expectedErrorCode) {
333     try {
334       computeManager.getCompute(vspId, version, componentId, computeId);
335       Assert.fail();
336     } catch (CoreException exception) {
337       Assert.assertEquals(exception.code().id(), expectedErrorCode);
338     }
339   }
340
341   private void testList_negative(String vspId, Version version, String componentId,
342                                  String expectedErrorCode, String expectedErrorMsg) {
343     try {
344       computeManager.listComputes(vspId, version, componentId);
345       Assert.fail();
346     } catch (CoreException exception) {
347       Assert.assertEquals(exception.code().id(), expectedErrorCode);
348       Assert.assertEquals(exception.getMessage(), expectedErrorMsg);
349     }
350   }
351
352
353   private void testUpdate_negative(String vspId, Version version, String componentId,
354                                    String computeId, String expectedErrorCode) {
355     try {
356       computeManager.updateCompute(new ComputeEntity(vspId, version, componentId, computeId));
357       Assert.fail();
358     } catch (CoreException exception) {
359       Assert.assertEquals(exception.code().id(), expectedErrorCode);
360     }
361   }
362
363   private void testCreate_negative(ComputeEntity computeEntity1, String expectedErrorCode) {
364     try {
365       computeManager.createCompute(computeEntity1);
366       Assert.fail();
367     } catch (CoreException exception) {
368       Assert.assertEquals(exception.code().id(), expectedErrorCode);
369     }
370   }
371
372   private static ComputeEntity createCompute(String vspId, Version version, String compId,
373                                              String computeId) {
374     ComputeEntity computeEntity1 = new ComputeEntity(vspId, version, compId, computeId);
375     ComputeData computeData = new ComputeData();
376     computeData.setName(computeId + "name");
377     computeData.setDescription(computeId + "desc");
378     computeEntity1.setComputeCompositionData(computeData);
379     return computeEntity1;
380   }
381 }