re base code
[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.*;
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;
13
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.List;
17
18 import static org.mockito.Matchers.anyObject;
19 import static org.mockito.Mockito.when;
20
21 public class ComponentDependencyModelTest {
22
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";
26
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";
31
32   @Spy
33   @InjectMocks
34   private ComponentDependencyModelManagerImpl componentDependencyModelManager;
35   @Mock
36   private ComponentManager componentManager;
37   @Mock
38   private ComponentDependencyModelDao componentDependencyModelDao;
39
40   @BeforeMethod
41   private void init() {
42     MockitoAnnotations.initMocks(this);
43   }
44
45   @Test
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));
50
51     Mockito.when(componentDependencyModelDao
52         .list(new ComponentDependencyModelEntity(VSP_ID, VERSION, null)))
53         .thenReturn(entities);
54
55     Collection<ComponentDependencyModelEntity> list =
56         componentDependencyModelManager.list(VSP_ID, VERSION);
57
58     Mockito.verify(componentDependencyModelDao, Mockito.times(1))
59         .list(new ComponentDependencyModelEntity(VSP_ID, VERSION, null));
60
61     Assert.assertEquals(2, list.size());
62   }
63
64   @Test
65   public void testCreateDependency() {
66     ComponentDependencyModelEntity modelEntity =
67         createModelEntity(COMP_ID_1, COMP_ID_2);
68
69     componentDependencyModelManager.createComponentDependency(modelEntity, VSP_ID, VERSION);
70     Mockito.verify(componentDependencyModelDao, Mockito.times(1)).create(modelEntity);
71   }
72
73   @Test
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());
80   }
81
82   @Test
83   public void testCreateDependencyNegative_NoSourceId() {
84
85     ComponentDependencyModelEntity modelEntity = createModelEntity(null, COMP_ID_1);
86     testCreateDependency_negative(modelEntity, VSP_ID, VERSION,
87         ComponentDependencyModelErrorBuilder.getNoSourceComponentErrorBuilder().id(),
88         ComponentDependencyModelErrorBuilder.getNoSourceComponentErrorBuilder().message());
89
90
91     ComponentDependencyModelEntity modelEntity1 = createModelEntity("", COMP_ID_1);
92     testCreateDependency_negative(modelEntity1, VSP_ID, VERSION,
93         ComponentDependencyModelErrorBuilder.getNoSourceComponentErrorBuilder().id(),
94         ComponentDependencyModelErrorBuilder.getNoSourceComponentErrorBuilder().message());
95   }
96
97   @Test
98   public void testUpdateDependency() {
99     ComponentDependencyModelEntity modelEntity =
100         createModelEntity(COMP_ID_1, COMP_ID_2);
101     modelEntity.setId(COMP_DEP_ID);
102
103     when(componentDependencyModelDao.get(anyObject())).thenReturn(modelEntity);
104
105     componentDependencyModelManager.update(modelEntity);
106     Mockito.verify(componentDependencyModelDao, Mockito.times(1)).update(modelEntity);
107   }
108
109   @Test
110   public void testUpdateDependencyNegative_NoSourceId() {
111
112     ComponentDependencyModelEntity modelEntity = createModelEntity(null, COMP_ID_1);
113     modelEntity.setId(COMP_DEP_ID);
114
115     when(componentDependencyModelDao.get(anyObject())).thenReturn(modelEntity);
116
117     testUpdateDependency_negative(modelEntity,
118         ComponentDependencyModelErrorBuilder.getNoSourceComponentErrorBuilder().id(),
119         ComponentDependencyModelErrorBuilder.getNoSourceComponentErrorBuilder().message());
120
121     ComponentDependencyModelEntity modelEntity1 = createModelEntity("", COMP_ID_1);
122     modelEntity1.setId(COMP_DEP_ID);
123
124     when(componentDependencyModelDao.get(anyObject())).thenReturn(modelEntity1);
125
126     testUpdateDependency_negative(modelEntity1,
127         ComponentDependencyModelErrorBuilder.getNoSourceComponentErrorBuilder().id(),
128         ComponentDependencyModelErrorBuilder.getNoSourceComponentErrorBuilder().message());
129   }
130
131   @Test
132   public void testUpdateDependencyNegative_SameSourceTarget() {
133     ComponentDependencyModelEntity modelEntity =
134         createModelEntity(COMP_ID_1, COMP_ID_1);
135     modelEntity.setId(COMP_DEP_ID);
136
137     when(componentDependencyModelDao.get(anyObject())).thenReturn(modelEntity);
138     testUpdateDependency_negative(modelEntity,
139         ComponentDependencyModelErrorBuilder.getSourceTargetComponentEqualErrorBuilder().id(),
140         ComponentDependencyModelErrorBuilder.getSourceTargetComponentEqualErrorBuilder().message());
141   }
142
143   @Test
144   public void testDeleteDependency() {
145     ComponentDependencyModelEntity modelEntity =
146         createModelEntity(COMP_ID_1, COMP_ID_2);
147     modelEntity.setId(COMP_DEP_ID);
148
149     when(componentDependencyModelDao.get(anyObject())).thenReturn(modelEntity);
150
151     componentDependencyModelManager.delete(VSP_ID, VERSION, COMP_DEP_ID);
152     Mockito.verify(componentDependencyModelDao, Mockito.times(1)).delete(modelEntity);
153   }
154
155   @Test
156   public void testDeleteInvalidDependency() {
157     ComponentDependencyModelEntity delModelEntity =
158         createModelEntity(COMP_ID_1, COMP_ID_2);
159     delModelEntity.setId(COMP_DEP_ID);
160
161     try {
162       componentDependencyModelManager.delete(VSP_ID, VERSION, COMP_DEP_ID);
163       Assert.fail();
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()));
170     }
171   }
172
173
174   @Test
175   public void testGetDependency() {
176     ComponentDependencyModelEntity modelEntity =
177         createModelEntity(COMP_ID_1, COMP_ID_2);
178     modelEntity.setId(COMP_DEP_ID);
179
180     when(componentDependencyModelDao.get(anyObject())).thenReturn(modelEntity);
181
182     ComponentDependencyModelEntity retrieved =
183         componentDependencyModelManager.get(VSP_ID, VERSION, COMP_DEP_ID);
184
185     Assert.assertEquals(retrieved.getSourceComponentId(), COMP_ID_1);
186
187   }
188
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");
195     return entity;
196   }
197
198   private void testCreateDependency_negative(ComponentDependencyModelEntity entity, String vspId,
199                                              Version version, String expectedErrorCode,
200                                              String expectedErrorMsg) {
201     try {
202       componentDependencyModelManager.createComponentDependency(entity, vspId, version);
203       Assert.fail();
204     } catch (CoreException exception) {
205       Assert.assertEquals(exception.code().id(), expectedErrorCode);
206       Assert.assertEquals(exception.getMessage(), expectedErrorMsg);
207     }
208   }
209
210   private void testUpdateDependency_negative(ComponentDependencyModelEntity entity,
211                                              String expectedErrorCode, String expectedErrorMsg) {
212     try {
213       componentDependencyModelManager.update(entity);
214       Assert.fail();
215     } catch (CoreException exception) {
216       Assert.assertEquals(exception.code().id(), expectedErrorCode);
217       Assert.assertEquals(exception.getMessage(), expectedErrorMsg);
218     }
219   }
220 }