Add collaboration feature
[sdc.git] / openecomp-be / backend / openecomp-sdc-vendor-software-product-manager / src / test / java / org / openecomp / sdc / vendorsoftwareproduct / impl / ComponentDependencyModelTest.java
1 package org.openecomp.sdc.vendorsoftwareproduct.impl;
2
3 import org.mockito.InjectMocks;
4 import org.mockito.Mock;
5 import org.mockito.Mockito;
6 import org.mockito.MockitoAnnotations;
7 import org.mockito.Spy;
8 import org.openecomp.sdc.common.errors.CoreException;
9 import org.openecomp.sdc.vendorsoftwareproduct.ComponentManager;
10 import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentDependencyModelDao;
11 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentDependencyModelEntity;
12 import org.openecomp.sdc.vendorsoftwareproduct.errors.ComponentDependencyModelErrorBuilder;
13 import org.openecomp.sdc.versioning.dao.types.Version;
14 import org.testng.Assert;
15 import org.testng.annotations.BeforeMethod;
16 import org.testng.annotations.Test;
17
18 import java.util.ArrayList;
19 import java.util.Collection;
20 import java.util.List;
21
22 import static org.mockito.Matchers.anyObject;
23 import static org.mockito.Mockito.when;
24
25 public class ComponentDependencyModelTest {
26
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";
30
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";
35
36   @Spy
37   @InjectMocks
38   private ComponentDependencyModelManagerImpl componentDependencyModelManager;
39   @Mock
40   private ComponentManager componentManager;
41   @Mock
42   private ComponentDependencyModelDao componentDependencyModelDao;
43
44   @BeforeMethod
45   private void init() {
46     MockitoAnnotations.initMocks(this);
47   }
48
49   @Test
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));
54
55     Mockito.when(componentDependencyModelDao
56         .list(new ComponentDependencyModelEntity(VSP_ID, VERSION, null)))
57         .thenReturn(entities);
58
59     Collection<ComponentDependencyModelEntity> list =
60         componentDependencyModelManager.list(VSP_ID, VERSION);
61
62     Mockito.verify(componentDependencyModelDao, Mockito.times(1))
63         .list(new ComponentDependencyModelEntity(VSP_ID, VERSION, null));
64
65     Assert.assertEquals(2, list.size());
66   }
67
68   @Test
69   public void testCreateDependency() {
70     ComponentDependencyModelEntity modelEntity =
71         createModelEntity(COMP_ID_1, COMP_ID_2);
72
73     componentDependencyModelManager.createComponentDependency(modelEntity, VSP_ID, VERSION);
74     Mockito.verify(componentDependencyModelDao, Mockito.times(1)).create(modelEntity);
75   }
76
77   @Test
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());
84   }
85
86   @Test
87   public void testCreateDependencyNegative_NoSourceId() {
88
89     ComponentDependencyModelEntity modelEntity = createModelEntity(null, COMP_ID_1);
90     testCreateDependency_negative(modelEntity, VSP_ID, VERSION,
91         ComponentDependencyModelErrorBuilder.getNoSourceComponentErrorBuilder().id(),
92         ComponentDependencyModelErrorBuilder.getNoSourceComponentErrorBuilder().message());
93
94
95     ComponentDependencyModelEntity modelEntity1 = createModelEntity("", COMP_ID_1);
96     testCreateDependency_negative(modelEntity1, VSP_ID, VERSION,
97         ComponentDependencyModelErrorBuilder.getNoSourceComponentErrorBuilder().id(),
98         ComponentDependencyModelErrorBuilder.getNoSourceComponentErrorBuilder().message());
99   }
100
101   @Test
102   public void testUpdateDependency() {
103     ComponentDependencyModelEntity modelEntity =
104         createModelEntity(COMP_ID_1, COMP_ID_2);
105     modelEntity.setId(COMP_DEP_ID);
106
107     when(componentDependencyModelDao.get(anyObject())).thenReturn(modelEntity);
108
109     componentDependencyModelManager.update(modelEntity);
110     Mockito.verify(componentDependencyModelDao, Mockito.times(1)).update(modelEntity);
111   }
112
113   @Test
114   public void testUpdateDependencyNegative_NoSourceId() {
115
116     ComponentDependencyModelEntity modelEntity = createModelEntity(null, COMP_ID_1);
117     modelEntity.setId(COMP_DEP_ID);
118
119     when(componentDependencyModelDao.get(anyObject())).thenReturn(modelEntity);
120
121     testUpdateDependency_negative(modelEntity,
122         ComponentDependencyModelErrorBuilder.getNoSourceComponentErrorBuilder().id(),
123         ComponentDependencyModelErrorBuilder.getNoSourceComponentErrorBuilder().message());
124
125     ComponentDependencyModelEntity modelEntity1 = createModelEntity("", COMP_ID_1);
126     modelEntity1.setId(COMP_DEP_ID);
127
128     when(componentDependencyModelDao.get(anyObject())).thenReturn(modelEntity1);
129
130     testUpdateDependency_negative(modelEntity1,
131         ComponentDependencyModelErrorBuilder.getNoSourceComponentErrorBuilder().id(),
132         ComponentDependencyModelErrorBuilder.getNoSourceComponentErrorBuilder().message());
133   }
134
135   @Test
136   public void testUpdateDependencyNegative_SameSourceTarget() {
137     ComponentDependencyModelEntity modelEntity =
138         createModelEntity(COMP_ID_1, COMP_ID_1);
139     modelEntity.setId(COMP_DEP_ID);
140
141     when(componentDependencyModelDao.get(anyObject())).thenReturn(modelEntity);
142     testUpdateDependency_negative(modelEntity,
143         ComponentDependencyModelErrorBuilder.getSourceTargetComponentEqualErrorBuilder().id(),
144         ComponentDependencyModelErrorBuilder.getSourceTargetComponentEqualErrorBuilder().message());
145   }
146
147   @Test
148   public void testDeleteDependency() {
149     ComponentDependencyModelEntity modelEntity =
150         createModelEntity(COMP_ID_1, COMP_ID_2);
151     modelEntity.setId(COMP_DEP_ID);
152
153     when(componentDependencyModelDao.get(anyObject())).thenReturn(modelEntity);
154
155     componentDependencyModelManager.delete(VSP_ID, VERSION, COMP_DEP_ID);
156     Mockito.verify(componentDependencyModelDao, Mockito.times(1)).delete(modelEntity);
157   }
158
159   @Test
160   public void testDeleteInvalidDependency() {
161     ComponentDependencyModelEntity delModelEntity =
162         createModelEntity(COMP_ID_1, COMP_ID_2);
163     delModelEntity.setId(COMP_DEP_ID);
164
165     try {
166       componentDependencyModelManager.delete(VSP_ID, VERSION, COMP_DEP_ID);
167       Assert.fail();
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()));
174     }
175   }
176
177
178   @Test
179   public void testGetDependency() {
180     ComponentDependencyModelEntity modelEntity =
181         createModelEntity(COMP_ID_1, COMP_ID_2);
182     modelEntity.setId(COMP_DEP_ID);
183
184     when(componentDependencyModelDao.get(anyObject())).thenReturn(modelEntity);
185
186     ComponentDependencyModelEntity retrieved =
187         componentDependencyModelManager.get(VSP_ID, VERSION, COMP_DEP_ID);
188
189     Assert.assertEquals(retrieved.getSourceComponentId(), COMP_ID_1);
190
191   }
192
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");
199     return entity;
200   }
201
202   private void testCreateDependency_negative(ComponentDependencyModelEntity entity, String vspId,
203                                              Version version, String expectedErrorCode,
204                                              String expectedErrorMsg) {
205     try {
206       componentDependencyModelManager.createComponentDependency(entity, vspId, version);
207       Assert.fail();
208     } catch (CoreException exception) {
209       Assert.assertEquals(exception.code().id(), expectedErrorCode);
210       Assert.assertEquals(exception.getMessage(), expectedErrorMsg);
211     }
212   }
213
214   private void testUpdateDependency_negative(ComponentDependencyModelEntity entity,
215                                              String expectedErrorCode, String expectedErrorMsg) {
216     try {
217       componentDependencyModelManager.update(entity);
218       Assert.fail();
219     } catch (CoreException exception) {
220       Assert.assertEquals(exception.code().id(), expectedErrorCode);
221       Assert.assertEquals(exception.getMessage(), expectedErrorMsg);
222     }
223   }
224 }