1 package org.openecomp.sdc.vendorsoftwareproduct.impl;
4 import org.openecomp.sdc.common.errors.CoreException;
5 import org.openecomp.sdc.vendorsoftwareproduct.ComponentManager;
6 import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentDependencyModelDao;
7 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentDependencyModelEntity;
8 import org.openecomp.sdc.vendorsoftwareproduct.errors.ComponentDependencyModelErrorBuilder;
9 import org.openecomp.sdc.versioning.dao.types.Version;
10 import org.testng.Assert;
11 import org.testng.annotations.BeforeMethod;
12 import org.testng.annotations.Test;
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.List;
18 import static org.mockito.Matchers.anyObject;
19 import static org.mockito.Mockito.when;
21 public class ComponentDependencyModelTest {
23 private static final String VSP_ID = "vsp_id";
24 private static final Version VERSION = new Version("version_id");
25 private static final String COMP_DEP_ID = "comp_dep_id";
27 private static final String COMP_ID_1 = "comp_id_1";
28 private static final String COMP_ID_2 = "comp_id_2";
29 private static final String COMP_ID_3 = "comp_id_3";
30 private static final String COMP_ID_4 = "comp_id_4";
34 private ComponentDependencyModelManagerImpl componentDependencyModelManager;
36 private ComponentManager componentManager;
38 private ComponentDependencyModelDao componentDependencyModelDao;
42 MockitoAnnotations.initMocks(this);
46 public void testListDependency() {
47 List<ComponentDependencyModelEntity> entities = new ArrayList<>();
48 entities.add(createModelEntity(COMP_ID_1, COMP_ID_2));
49 entities.add(createModelEntity(COMP_ID_3, COMP_ID_4));
51 Mockito.when(componentDependencyModelDao
52 .list(new ComponentDependencyModelEntity(VSP_ID, VERSION, null)))
53 .thenReturn(entities);
55 Collection<ComponentDependencyModelEntity> list =
56 componentDependencyModelManager.list(VSP_ID, VERSION);
58 Mockito.verify(componentDependencyModelDao, Mockito.times(1))
59 .list(new ComponentDependencyModelEntity(VSP_ID, VERSION, null));
61 Assert.assertEquals(2, list.size());
65 public void testCreateDependency() {
66 ComponentDependencyModelEntity modelEntity =
67 createModelEntity(COMP_ID_1, COMP_ID_2);
69 componentDependencyModelManager.createComponentDependency(modelEntity, VSP_ID, VERSION);
70 Mockito.verify(componentDependencyModelDao, Mockito.times(1)).create(modelEntity);
74 public void testCreateDependencyNegative_SameSourceTarget() {
75 ComponentDependencyModelEntity modelEntity =
76 createModelEntity(COMP_ID_1, COMP_ID_1);
77 testCreateDependency_negative(modelEntity, VSP_ID, VERSION,
78 ComponentDependencyModelErrorBuilder.getSourceTargetComponentEqualErrorBuilder().id(),
79 ComponentDependencyModelErrorBuilder.getSourceTargetComponentEqualErrorBuilder().message());
83 public void testCreateDependencyNegative_NoSourceId() {
85 ComponentDependencyModelEntity modelEntity = createModelEntity(null, COMP_ID_1);
86 testCreateDependency_negative(modelEntity, VSP_ID, VERSION,
87 ComponentDependencyModelErrorBuilder.getNoSourceComponentErrorBuilder().id(),
88 ComponentDependencyModelErrorBuilder.getNoSourceComponentErrorBuilder().message());
91 ComponentDependencyModelEntity modelEntity1 = createModelEntity("", COMP_ID_1);
92 testCreateDependency_negative(modelEntity1, VSP_ID, VERSION,
93 ComponentDependencyModelErrorBuilder.getNoSourceComponentErrorBuilder().id(),
94 ComponentDependencyModelErrorBuilder.getNoSourceComponentErrorBuilder().message());
98 public void testUpdateDependency() {
99 ComponentDependencyModelEntity modelEntity =
100 createModelEntity(COMP_ID_1, COMP_ID_2);
101 modelEntity.setId(COMP_DEP_ID);
103 when(componentDependencyModelDao.get(anyObject())).thenReturn(modelEntity);
105 componentDependencyModelManager.update(modelEntity);
106 Mockito.verify(componentDependencyModelDao, Mockito.times(1)).update(modelEntity);
110 public void testUpdateDependencyNegative_NoSourceId() {
112 ComponentDependencyModelEntity modelEntity = createModelEntity(null, COMP_ID_1);
113 modelEntity.setId(COMP_DEP_ID);
115 when(componentDependencyModelDao.get(anyObject())).thenReturn(modelEntity);
117 testUpdateDependency_negative(modelEntity,
118 ComponentDependencyModelErrorBuilder.getNoSourceComponentErrorBuilder().id(),
119 ComponentDependencyModelErrorBuilder.getNoSourceComponentErrorBuilder().message());
121 ComponentDependencyModelEntity modelEntity1 = createModelEntity("", COMP_ID_1);
122 modelEntity1.setId(COMP_DEP_ID);
124 when(componentDependencyModelDao.get(anyObject())).thenReturn(modelEntity1);
126 testUpdateDependency_negative(modelEntity1,
127 ComponentDependencyModelErrorBuilder.getNoSourceComponentErrorBuilder().id(),
128 ComponentDependencyModelErrorBuilder.getNoSourceComponentErrorBuilder().message());
132 public void testUpdateDependencyNegative_SameSourceTarget() {
133 ComponentDependencyModelEntity modelEntity =
134 createModelEntity(COMP_ID_1, COMP_ID_1);
135 modelEntity.setId(COMP_DEP_ID);
137 when(componentDependencyModelDao.get(anyObject())).thenReturn(modelEntity);
138 testUpdateDependency_negative(modelEntity,
139 ComponentDependencyModelErrorBuilder.getSourceTargetComponentEqualErrorBuilder().id(),
140 ComponentDependencyModelErrorBuilder.getSourceTargetComponentEqualErrorBuilder().message());
144 public void testDeleteDependency() {
145 ComponentDependencyModelEntity modelEntity =
146 createModelEntity(COMP_ID_1, COMP_ID_2);
147 modelEntity.setId(COMP_DEP_ID);
149 when(componentDependencyModelDao.get(anyObject())).thenReturn(modelEntity);
151 componentDependencyModelManager.delete(VSP_ID, VERSION, COMP_DEP_ID);
152 Mockito.verify(componentDependencyModelDao, Mockito.times(1)).delete(modelEntity);
156 public void testDeleteInvalidDependency() {
157 ComponentDependencyModelEntity delModelEntity =
158 createModelEntity(COMP_ID_1, COMP_ID_2);
159 delModelEntity.setId(COMP_DEP_ID);
162 componentDependencyModelManager.delete(VSP_ID, VERSION, COMP_DEP_ID);
164 } catch (CoreException exception) {
165 Assert.assertEquals(exception.code().id(), "VERSIONABLE_SUB_ENTITY_NOT_FOUND");
166 Assert.assertEquals(exception.getMessage(),
167 String.format("Vendor Software Product Component Dependency Model with Id %s " +
168 "does not exist for Vendor Software Product with id %s and version %s",
169 COMP_DEP_ID, VSP_ID, VERSION.getId()));
175 public void testGetDependency() {
176 ComponentDependencyModelEntity modelEntity =
177 createModelEntity(COMP_ID_1, COMP_ID_2);
178 modelEntity.setId(COMP_DEP_ID);
180 when(componentDependencyModelDao.get(anyObject())).thenReturn(modelEntity);
182 ComponentDependencyModelEntity retrieved =
183 componentDependencyModelManager.get(VSP_ID, VERSION, COMP_DEP_ID);
185 Assert.assertEquals(retrieved.getSourceComponentId(), COMP_ID_1);
189 private ComponentDependencyModelEntity createModelEntity(String sourceId, String targetId) {
190 ComponentDependencyModelEntity entity =
191 new ComponentDependencyModelEntity(VSP_ID, VERSION, COMP_DEP_ID);
192 entity.setSourceComponentId(sourceId);
193 entity.setTargetComponentId(targetId);
194 entity.setRelation("dependsOn");
198 private void testCreateDependency_negative(ComponentDependencyModelEntity entity, String vspId,
199 Version version, String expectedErrorCode,
200 String expectedErrorMsg) {
202 componentDependencyModelManager.createComponentDependency(entity, vspId, version);
204 } catch (CoreException exception) {
205 Assert.assertEquals(exception.code().id(), expectedErrorCode);
206 Assert.assertEquals(exception.getMessage(), expectedErrorMsg);
210 private void testUpdateDependency_negative(ComponentDependencyModelEntity entity,
211 String expectedErrorCode, String expectedErrorMsg) {
213 componentDependencyModelManager.update(entity);
215 } catch (CoreException exception) {
216 Assert.assertEquals(exception.code().id(), expectedErrorCode);
217 Assert.assertEquals(exception.getMessage(), expectedErrorMsg);