1 package org.openecomp.sdc.vendorsoftwareproduct.impl;
3 import static org.mockito.Matchers.anyObject;
4 import static org.mockito.Mockito.doReturn;
5 import static org.mockito.Mockito.never;
6 import static org.mockito.Mockito.verify;
8 import org.mockito.InjectMocks;
9 import org.mockito.Mock;
10 import org.mockito.MockitoAnnotations;
11 import org.mockito.Spy;
12 import org.openecomp.sdc.common.errors.CoreException;
13 import org.openecomp.sdc.logging.api.Logger;
14 import org.openecomp.sdc.logging.api.LoggerFactory;
15 import org.openecomp.sdc.vendorsoftwareproduct.NetworkManager;
16 import org.openecomp.sdc.vendorsoftwareproduct.dao.ComputeDao;
17 import org.openecomp.sdc.vendorsoftwareproduct.dao.DeploymentFlavorDao;
18 import org.openecomp.sdc.vendorsoftwareproduct.dao.VendorSoftwareProductInfoDao;
19 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComputeEntity;
20 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.DeploymentFlavorEntity;
21 import org.openecomp.sdc.vendorsoftwareproduct.errors.VendorSoftwareProductErrorCodes;
22 import org.openecomp.sdc.vendorsoftwareproduct.services.composition.CompositionEntityDataManager;
23 import org.openecomp.sdc.vendorsoftwareproduct.types.CompositionEntityResponse;
24 import org.openecomp.sdc.vendorsoftwareproduct.types.ListComputeResponse;
25 import org.openecomp.sdc.vendorsoftwareproduct.types.QuestionnaireResponse;
26 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.ComponentComputeAssociation;
27 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionEntityType;
28 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionEntityValidationData;
29 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.ComputeData;
30 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.DeploymentFlavor;
31 import org.openecomp.sdc.versioning.dao.types.Version;
32 import org.openecomp.sdc.versioning.errors.VersioningErrorCodes;
33 import org.testng.Assert;
34 import org.testng.annotations.BeforeMethod;
35 import org.testng.annotations.Test;
37 import java.util.ArrayList;
38 import java.util.Arrays;
39 import java.util.Collection;
40 import java.util.List;
42 public class ComputeManagerImplTest {
44 private final Logger log = (Logger) LoggerFactory.getLogger(this.getClass().getName());
46 private static final String COMPUTE_NOT_EXIST_MSG =
47 "Vendor Software Product COMPUTE with Id compute1 does not exist for Vendor Software Product with " +
48 "id VSP_ID and version 0.1";
50 private static final String USER = "computeTestUser";
51 private static final String VSP_ID = "VSP_ID";
52 private static final Version VERSION = new Version(0, 1);
53 private static final String COMPONENT_ID = "COMPONENT_ID";
54 private static final String COMPUTE1_ID = "compute1";
55 private static final String COMPUTE2_ID = "compute2";
58 private ComputeDao computeDao;
61 private CompositionEntityDataManager compositionEntityDataManagerMock;
64 private NetworkManager networkManagerMock;
67 private VendorSoftwareProductInfoDao vspInfoDao;
70 private ComputeEntity computeEntity;
73 private ListComputeResponse listComputeResponse;
76 private DeploymentFlavorEntity deploymentFlavorEntity;
79 private DeploymentFlavorDao deploymentFlavorDao;
82 private ComponentComputeAssociation componentComputeAssociation;
85 DeploymentFlavor deploymentFlavor;
89 private ComputeManagerImpl computeManager;
92 public void setUp() throws Exception {
93 MockitoAnnotations.initMocks(this);
97 public void testListWhenNone() {
98 Collection<ListComputeResponse> computes = computeManager.listCompute(VSP_ID, VERSION, COMPONENT_ID, USER);
99 Assert.assertEquals(computes.size(), 0);
103 public void testList(){
104 doReturn(Arrays.asList(
105 createCompute(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID),
106 createCompute(VSP_ID, VERSION, COMPONENT_ID, COMPUTE2_ID)))
107 .when(computeDao).list(anyObject());
110 Collection<ListComputeResponse> computes = computeManager.listCompute(VSP_ID, VERSION, COMPONENT_ID, USER);
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())
121 public void testCreateOnNotManualCompute_negative() {
123 testCreate_negative(new ComputeEntity(VSP_ID, VERSION, COMPONENT_ID, null), USER,
124 VendorSoftwareProductErrorCodes.ADD_COMPUTE_NOT_ALLOWED_IN_HEAT_ONBOARDING);
128 public void testCreateManualCompute() {
129 ComputeEntity expected = createCompute(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID);
130 doReturn(true).when(vspInfoDao).isManual(anyObject(), anyObject());
131 computeManager.createCompute(expected, USER);
132 verify(compositionEntityDataManagerMock).createCompute(expected);
133 verify(compositionEntityDataManagerMock).createCompute(expected);
137 public void testCreateManualComputeWithDuplicateName() {
138 ComputeEntity expected = createCompute(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID);
139 doReturn(true).when(vspInfoDao).isManual(anyObject(), anyObject());
141 ComputeEntity expectedDiffName = createCompute(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID);
142 expectedDiffName.setId(COMPUTE1_ID + "Name");
143 ComputeData computeData = expectedDiffName.getComputeCompositionData();
144 computeData.setName(COMPUTE1_ID + "Name");
145 expectedDiffName.setComputeCompositionData(computeData);
146 List<ComputeEntity> vfcImageList = new ArrayList<ComputeEntity>();
147 vfcImageList.add(expectedDiffName);
148 doReturn(vfcImageList).when(computeDao).list(anyObject());
151 computeManager.createCompute(expected, USER);
154 catch (CoreException ex) {
156 Assert.assertEquals(VendorSoftwareProductErrorCodes.DUPLICATE_COMPUTE_NAME_NOT_ALLOWED,
162 public void testUpdateNonExistingComputeId_negative() {
164 testUpdate_negative(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID, USER,
165 VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND);
169 public void testUpdateCompute() {
170 doReturn(createCompute(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID))
171 .when(computeDao).get(anyObject());
173 doReturn(new CompositionEntityValidationData(CompositionEntityType.compute, COMPUTE1_ID))
174 .when(compositionEntityDataManagerMock)
175 .validateEntity(anyObject(), anyObject(), anyObject());
177 ComputeEntity computeEntity = new ComputeEntity(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID);
178 ComputeData computeData = new ComputeData();
179 computeData.setName(COMPUTE1_ID + "name");
180 computeData.setDescription(COMPUTE1_ID + "desc updated");
181 computeEntity.setComputeCompositionData(computeData);
183 CompositionEntityValidationData validationData =
184 computeManager.updateCompute(computeEntity, USER);
185 Assert.assertTrue(validationData == null || validationData.getErrors() == null);
186 verify(computeDao).update(computeEntity);
190 public void testIllegalComputeUpdate() {
191 doReturn(createCompute(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID))
192 .when(computeDao).get(anyObject());
194 doReturn(true).when(vspInfoDao).isManual(anyObject(), anyObject());
196 CompositionEntityValidationData toBeReturned =
197 new CompositionEntityValidationData(CompositionEntityType.compute, COMPUTE1_ID);
198 toBeReturned.setErrors(Arrays.asList("error1", "error2"));
199 doReturn(toBeReturned)
200 .when(compositionEntityDataManagerMock)
201 .validateEntity(anyObject(), anyObject(), anyObject());
203 ComputeEntity computeEntity = new ComputeEntity(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID);
204 ComputeData computeData = new ComputeData();
205 computeData.setName(COMPUTE1_ID + "_name_updated");
206 computeData.setDescription(COMPUTE1_ID + " desc updated");
207 computeEntity.setComputeCompositionData(computeData);
209 CompositionEntityValidationData validationData = computeManager.updateCompute(computeEntity, USER);
210 Assert.assertNotNull(validationData);
211 Assert.assertEquals(validationData.getErrors().size(), 2);
213 verify(computeDao, never()).update(computeEntity);
217 public void testUpdateHEATComputeName() throws Exception {
218 doReturn(createCompute(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID))
219 .when(computeDao).get(anyObject());
220 ComputeEntity computeEntity = new ComputeEntity(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID);
221 ComputeData computeData = new ComputeData();
222 computeData.setName(COMPUTE1_ID + " name updated");
223 computeData.setDescription(COMPUTE1_ID + " desc updated");
224 computeEntity.setComputeCompositionData(computeData);
227 computeManager.updateCompute(computeEntity, USER);
229 catch (CoreException ex) {
231 Assert.assertEquals(ex.code().id(), VendorSoftwareProductErrorCodes.UPDATE_COMPUTE_NOT_ALLOWED);
237 public void testUpdateManualComputeQuestionnaire() throws Exception {
238 String json = "{\"md5\" :\"FFDSD33SS\"}";
239 doReturn(true).when(vspInfoDao).isManual(anyObject(), anyObject());
240 doReturn(new ComputeEntity(null,null,null,null)).when(computeDao).get(anyObject());
242 computeManager.updateComputeQuestionnaire(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID, json, USER);
243 verify(computeDao).updateQuestionnaireData(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID, json);
247 public void testGetNonExistingComputeId_negative() {
248 testGet_negative(VSP_ID, VERSION, COMPONENT_ID, "non existing compute id", USER,
249 VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND);
253 public void testGet() {
254 ComputeEntity expected = createCompute(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID);
255 doReturn(expected).when(computeDao).get(anyObject());
256 String compositionSchema = "schema string";
257 doReturn(compositionSchema).when(computeManager).getComputeCompositionSchema(anyObject());
259 CompositionEntityResponse<ComputeData> response =
260 computeManager.getCompute(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID, USER);
261 Assert.assertEquals(response.getId(), expected.getId());
262 Assert.assertEquals(response.getData().getName(), expected.getComputeCompositionData().getName());
263 Assert.assertEquals(response.getData().getDescription(), expected.getComputeCompositionData().
265 Assert.assertEquals(response.getSchema(), compositionSchema);
269 public void testGetQuestionnaire() throws Exception {
270 ComputeEntity compute = new ComputeEntity(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID);
271 compute.setQuestionnaireData("{}");
272 doReturn(compute).when(computeDao).getQuestionnaireData(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID);
274 String schema = "schema string";
277 doReturn(schema).when(computeManager).getComputeQuestionnaireSchema(anyObject());
279 QuestionnaireResponse questionnaire =
280 computeManager.getComputeQuestionnaire(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID, USER);
282 Assert.assertNotNull(questionnaire);
283 Assert.assertEquals(questionnaire.getData(), compute.getQuestionnaireData());
284 Assert.assertEquals(questionnaire.getSchema(), schema);
285 Assert.assertNull(questionnaire.getErrorMessage());
289 public void testDeleteOnNotManualCompute() {
290 ComputeEntity expected = createCompute(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID);
291 doReturn(expected).when(computeDao).get(anyObject());
292 testDelete_negative(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID, USER,
293 VendorSoftwareProductErrorCodes.VSP_COMPOSITION_EDIT_NOT_ALLOWED);
297 public void testDeleteOnManualCompute() {
298 ComputeEntity expected = createCompute(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID);
299 doReturn(expected).when(computeDao).get(anyObject());
300 doReturn(true).when(vspInfoDao).isManual(anyObject(), anyObject());
301 computeManager.deleteCompute(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID, USER);
302 verify(computeDao).delete(anyObject());
306 public void testDeleteOnNotExistCompute() {
307 testDelete_negative(VSP_ID, VERSION, COMPONENT_ID, COMPUTE1_ID, USER,
308 VendorSoftwareProductErrorCodes.VSP_COMPOSITION_EDIT_NOT_ALLOWED);
312 private void testDelete_negative(String vspId, Version version, String componentId, String computeId,
314 String expectedErrorCode) {
316 computeManager.deleteCompute(vspId, version, componentId, computeId, user);
318 } catch (CoreException exception) {
319 log.debug("",exception);
320 Assert.assertEquals(exception.code().id(), expectedErrorCode);
324 private void testGet_negative(String vspId, Version version, String componentId, String computeId,
325 String user, String expectedErrorCode) {
327 computeManager.getCompute(vspId, version, componentId, computeId, user);
329 } catch (CoreException exception) {
330 log.debug("",exception);
331 Assert.assertEquals(exception.code().id(), expectedErrorCode);
335 private void testList_negative(String vspId, Version version, String componentId, String user,
336 String expectedErrorCode, String expectedErrorMsg) {
338 computeManager.listCompute(vspId, version, componentId, user);
340 } catch (CoreException exception) {
341 log.debug("",exception);
342 Assert.assertEquals(exception.code().id(), expectedErrorCode);
343 Assert.assertEquals(exception.getMessage(), expectedErrorMsg);
349 private void testUpdate_negative(String vspId, Version version, String componentId, String computeId,
350 String user, String expectedErrorCode) {
352 computeManager.updateCompute(new ComputeEntity(vspId, version, componentId, computeId), user);
354 } catch (CoreException exception) {
355 log.debug("",exception);
356 Assert.assertEquals(exception.code().id(), expectedErrorCode);
360 private void testCreate_negative(ComputeEntity computeEntity1, String user, String expectedErrorCode) {
362 computeManager.createCompute(computeEntity1, user);
364 } catch (CoreException exception) {
365 log.debug("",exception);
366 Assert.assertEquals(exception.code().id(), expectedErrorCode);
370 static ComputeEntity createCompute(String vspId, Version version, String compId, String computeId){
371 ComputeEntity computeEntity1 = new ComputeEntity(vspId, version, compId, computeId);
372 ComputeData computeData = new ComputeData();
373 computeData.setName(computeId+"name");
374 computeData.setDescription(computeId+"desc");
375 computeEntity1.setComputeCompositionData(computeData);
376 return computeEntity1;