1 package org.openecomp.sdc.vendorsoftwareproduct.impl;
3 import org.junit.Assert;
4 import org.junit.Before;
6 import org.mockito.InjectMocks;
7 import org.mockito.Mock;
8 import org.mockito.Mockito;
9 import org.mockito.MockitoAnnotations;
10 import org.mockito.Spy;
11 import org.openecomp.sdc.common.errors.CoreException;
12 import org.openecomp.sdc.vendorsoftwareproduct.ComponentManager;
13 import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentDependencyModelDao;
14 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentDependencyModelEntity;
15 import org.openecomp.sdc.vendorsoftwareproduct.errors.ComponentDependencyModelErrorBuilder;
16 import org.openecomp.sdc.versioning.dao.types.Version;
18 import java.util.ArrayList;
19 import java.util.Collection;
20 import java.util.List;
22 import static org.mockito.ArgumentMatchers.any;
23 import static org.mockito.Mockito.when;
25 public class ComponentDependencyModelTest {
27 private static final String VSP_ID = "vsp_id";
28 private static final Version VERSION = new Version("version_id");
29 private static final String COMP_DEP_ID = "comp_dep_id";
31 private static final String COMP_ID_1 = "comp_id_1";
32 private static final String COMP_ID_2 = "comp_id_2";
33 private static final String COMP_ID_3 = "comp_id_3";
34 private static final String COMP_ID_4 = "comp_id_4";
38 private ComponentDependencyModelManagerImpl componentDependencyModelManager;
40 private ComponentManager componentManager;
42 private ComponentDependencyModelDao componentDependencyModelDao;
46 MockitoAnnotations.initMocks(this);
50 public void testListDependency() {
51 List<ComponentDependencyModelEntity> entities = new ArrayList<>();
52 entities.add(createModelEntity(COMP_ID_1, COMP_ID_2));
53 entities.add(createModelEntity(COMP_ID_3, COMP_ID_4));
55 Mockito.when(componentDependencyModelDao
56 .list(new ComponentDependencyModelEntity(VSP_ID, VERSION, null)))
57 .thenReturn(entities);
59 Collection<ComponentDependencyModelEntity> list =
60 componentDependencyModelManager.list(VSP_ID, VERSION);
62 Mockito.verify(componentDependencyModelDao, Mockito.times(1))
63 .list(new ComponentDependencyModelEntity(VSP_ID, VERSION, null));
65 Assert.assertEquals(2, list.size());
69 public void testCreateDependency() {
70 ComponentDependencyModelEntity modelEntity =
71 createModelEntity(COMP_ID_1, COMP_ID_2);
73 componentDependencyModelManager.createComponentDependency(modelEntity, VSP_ID, VERSION);
74 Mockito.verify(componentDependencyModelDao, Mockito.times(1)).create(modelEntity);
78 public void testCreateDependencyNegative_SameSourceTarget() {
79 ComponentDependencyModelEntity modelEntity =
80 createModelEntity(COMP_ID_1, COMP_ID_1);
81 testCreateDependency_negative(modelEntity, VSP_ID, VERSION,
82 ComponentDependencyModelErrorBuilder.getSourceTargetComponentEqualErrorBuilder().id(),
83 ComponentDependencyModelErrorBuilder.getSourceTargetComponentEqualErrorBuilder().message());
87 public void testCreateDependencyNegative_NoSourceId() {
89 ComponentDependencyModelEntity modelEntity = createModelEntity(null, COMP_ID_1);
90 testCreateDependency_negative(modelEntity, VSP_ID, VERSION,
91 ComponentDependencyModelErrorBuilder.getNoSourceComponentErrorBuilder().id(),
92 ComponentDependencyModelErrorBuilder.getNoSourceComponentErrorBuilder().message());
95 ComponentDependencyModelEntity modelEntity1 = createModelEntity("", COMP_ID_1);
96 testCreateDependency_negative(modelEntity1, VSP_ID, VERSION,
97 ComponentDependencyModelErrorBuilder.getNoSourceComponentErrorBuilder().id(),
98 ComponentDependencyModelErrorBuilder.getNoSourceComponentErrorBuilder().message());
102 public void testUpdateDependency() {
103 ComponentDependencyModelEntity modelEntity =
104 createModelEntity(COMP_ID_1, COMP_ID_2);
105 modelEntity.setId(COMP_DEP_ID);
107 when(componentDependencyModelDao.get(any())).thenReturn(modelEntity);
109 componentDependencyModelManager.update(modelEntity);
110 Mockito.verify(componentDependencyModelDao, Mockito.times(1)).update(modelEntity);
114 public void testUpdateDependencyNegative_NoSourceId() {
116 ComponentDependencyModelEntity modelEntity = createModelEntity(null, COMP_ID_1);
117 modelEntity.setId(COMP_DEP_ID);
119 when(componentDependencyModelDao.get(any())).thenReturn(modelEntity);
121 testUpdateDependency_negative(modelEntity,
122 ComponentDependencyModelErrorBuilder.getNoSourceComponentErrorBuilder().id(),
123 ComponentDependencyModelErrorBuilder.getNoSourceComponentErrorBuilder().message());
125 ComponentDependencyModelEntity modelEntity1 = createModelEntity("", COMP_ID_1);
126 modelEntity1.setId(COMP_DEP_ID);
128 when(componentDependencyModelDao.get(any())).thenReturn(modelEntity1);
130 testUpdateDependency_negative(modelEntity1,
131 ComponentDependencyModelErrorBuilder.getNoSourceComponentErrorBuilder().id(),
132 ComponentDependencyModelErrorBuilder.getNoSourceComponentErrorBuilder().message());
136 public void testUpdateDependencyNegative_SameSourceTarget() {
137 ComponentDependencyModelEntity modelEntity =
138 createModelEntity(COMP_ID_1, COMP_ID_1);
139 modelEntity.setId(COMP_DEP_ID);
141 when(componentDependencyModelDao.get(any())).thenReturn(modelEntity);
142 testUpdateDependency_negative(modelEntity,
143 ComponentDependencyModelErrorBuilder.getSourceTargetComponentEqualErrorBuilder().id(),
144 ComponentDependencyModelErrorBuilder.getSourceTargetComponentEqualErrorBuilder().message());
148 public void testDeleteDependency() {
149 ComponentDependencyModelEntity modelEntity =
150 createModelEntity(COMP_ID_1, COMP_ID_2);
151 modelEntity.setId(COMP_DEP_ID);
153 when(componentDependencyModelDao.get(any())).thenReturn(modelEntity);
155 componentDependencyModelManager.delete(VSP_ID, VERSION, COMP_DEP_ID);
156 Mockito.verify(componentDependencyModelDao, Mockito.times(1)).delete(modelEntity);
160 public void testDeleteInvalidDependency() {
161 ComponentDependencyModelEntity delModelEntity =
162 createModelEntity(COMP_ID_1, COMP_ID_2);
163 delModelEntity.setId(COMP_DEP_ID);
166 componentDependencyModelManager.delete(VSP_ID, VERSION, COMP_DEP_ID);
168 } catch (CoreException exception) {
169 Assert.assertEquals(exception.code().id(), "VERSIONABLE_SUB_ENTITY_NOT_FOUND");
170 Assert.assertEquals(exception.getMessage(),
171 String.format("Vendor Software Product Component Dependency Model with Id %s " +
172 "does not exist for Vendor Software Product with id %s and version %s",
173 COMP_DEP_ID, VSP_ID, VERSION.getId()));
179 public void testGetDependency() {
180 ComponentDependencyModelEntity modelEntity =
181 createModelEntity(COMP_ID_1, COMP_ID_2);
182 modelEntity.setId(COMP_DEP_ID);
184 when(componentDependencyModelDao.get(any())).thenReturn(modelEntity);
186 ComponentDependencyModelEntity retrieved =
187 componentDependencyModelManager.get(VSP_ID, VERSION, COMP_DEP_ID);
189 Assert.assertEquals(retrieved.getSourceComponentId(), COMP_ID_1);
193 private ComponentDependencyModelEntity createModelEntity(String sourceId, String targetId) {
194 ComponentDependencyModelEntity entity =
195 new ComponentDependencyModelEntity(VSP_ID, VERSION, COMP_DEP_ID);
196 entity.setSourceComponentId(sourceId);
197 entity.setTargetComponentId(targetId);
198 entity.setRelation("dependsOn");
202 private void testCreateDependency_negative(ComponentDependencyModelEntity entity, String vspId,
203 Version version, String expectedErrorCode,
204 String expectedErrorMsg) {
206 componentDependencyModelManager.createComponentDependency(entity, vspId, version);
208 } catch (CoreException exception) {
209 Assert.assertEquals(exception.code().id(), expectedErrorCode);
210 Assert.assertEquals(exception.getMessage(), expectedErrorMsg);
214 private void testUpdateDependency_negative(ComponentDependencyModelEntity entity,
215 String expectedErrorCode, String expectedErrorMsg) {
217 componentDependencyModelManager.update(entity);
219 } catch (CoreException exception) {
220 Assert.assertEquals(exception.code().id(), expectedErrorCode);
221 Assert.assertEquals(exception.getMessage(), expectedErrorMsg);