Improve testing stability
[sdc.git] / openecomp-be / backend / openecomp-sdc-vendor-software-product-manager / src / test / java / org / openecomp / sdc / vendorsoftwareproduct / impl / ComponentDependencyModelTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.vendorsoftwareproduct.impl;
22
23 import org.junit.Assert;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.mockito.InjectMocks;
27 import org.mockito.Mock;
28 import org.mockito.Mockito;
29 import org.mockito.MockitoAnnotations;
30 import org.mockito.Spy;
31 import org.openecomp.sdc.common.errors.CoreException;
32 import org.openecomp.sdc.vendorsoftwareproduct.ComponentManager;
33 import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentDependencyModelDao;
34 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentDependencyModelEntity;
35 import org.openecomp.sdc.vendorsoftwareproduct.errors.ComponentDependencyModelErrorBuilder;
36 import org.openecomp.sdc.versioning.dao.types.Version;
37
38 import java.util.ArrayList;
39 import java.util.Collection;
40 import java.util.List;
41
42 import static org.mockito.ArgumentMatchers.any;
43 import static org.mockito.Mockito.when;
44
45 public class ComponentDependencyModelTest {
46
47   private static final String VSP_ID = "vsp_id";
48   private static final Version VERSION = new Version("version_id");
49   private static final String COMP_DEP_ID = "comp_dep_id";
50
51   private static final String COMP_ID_1 = "comp_id_1";
52   private static final String COMP_ID_2 = "comp_id_2";
53   private static final String COMP_ID_3 = "comp_id_3";
54   private static final String COMP_ID_4 = "comp_id_4";
55
56   @Spy
57   @InjectMocks
58   private ComponentDependencyModelManagerImpl componentDependencyModelManager;
59   @Mock
60   private ComponentManager componentManager;
61   @Mock
62   private ComponentDependencyModelDao componentDependencyModelDao;
63
64   @Before
65   public void init() {
66     MockitoAnnotations.openMocks(this);
67   }
68
69   @Test
70   public void testListDependency() {
71     List<ComponentDependencyModelEntity> entities = new ArrayList<>();
72     entities.add(createModelEntity(COMP_ID_1, COMP_ID_2));
73     entities.add(createModelEntity(COMP_ID_3, COMP_ID_4));
74
75     Mockito.when(componentDependencyModelDao
76         .list(new ComponentDependencyModelEntity(VSP_ID, VERSION, null)))
77         .thenReturn(entities);
78
79     Collection<ComponentDependencyModelEntity> list =
80         componentDependencyModelManager.list(VSP_ID, VERSION);
81
82     Mockito.verify(componentDependencyModelDao, Mockito.times(1))
83         .list(new ComponentDependencyModelEntity(VSP_ID, VERSION, null));
84
85     Assert.assertEquals(2, list.size());
86   }
87
88   @Test
89   public void testCreateDependency() {
90     ComponentDependencyModelEntity modelEntity =
91         createModelEntity(COMP_ID_1, COMP_ID_2);
92
93     componentDependencyModelManager.createComponentDependency(modelEntity, VSP_ID, VERSION);
94     Mockito.verify(componentDependencyModelDao, Mockito.times(1)).create(modelEntity);
95   }
96
97   @Test
98   public void testCreateDependencyNegative_SameSourceTarget() {
99     ComponentDependencyModelEntity modelEntity =
100         createModelEntity(COMP_ID_1, COMP_ID_1);
101     testCreateDependency_negative(modelEntity, VSP_ID, VERSION,
102         ComponentDependencyModelErrorBuilder.getSourceTargetComponentEqualErrorBuilder().id(),
103         ComponentDependencyModelErrorBuilder.getSourceTargetComponentEqualErrorBuilder().message());
104   }
105
106   @Test
107   public void testCreateDependencyNegative_NoSourceId() {
108
109     ComponentDependencyModelEntity modelEntity = createModelEntity(null, COMP_ID_1);
110     testCreateDependency_negative(modelEntity, VSP_ID, VERSION,
111         ComponentDependencyModelErrorBuilder.getNoSourceComponentErrorBuilder().id(),
112         ComponentDependencyModelErrorBuilder.getNoSourceComponentErrorBuilder().message());
113
114
115     ComponentDependencyModelEntity modelEntity1 = createModelEntity("", COMP_ID_1);
116     testCreateDependency_negative(modelEntity1, VSP_ID, VERSION,
117         ComponentDependencyModelErrorBuilder.getNoSourceComponentErrorBuilder().id(),
118         ComponentDependencyModelErrorBuilder.getNoSourceComponentErrorBuilder().message());
119   }
120
121   @Test
122   public void testUpdateDependency() {
123     ComponentDependencyModelEntity modelEntity =
124         createModelEntity(COMP_ID_1, COMP_ID_2);
125     modelEntity.setId(COMP_DEP_ID);
126
127     when(componentDependencyModelDao.get(any())).thenReturn(modelEntity);
128
129     componentDependencyModelManager.update(modelEntity);
130     Mockito.verify(componentDependencyModelDao, Mockito.times(1)).update(modelEntity);
131   }
132
133   @Test
134   public void testUpdateDependencyNegative_NoSourceId() {
135
136     ComponentDependencyModelEntity modelEntity = createModelEntity(null, COMP_ID_1);
137     modelEntity.setId(COMP_DEP_ID);
138
139     when(componentDependencyModelDao.get(any())).thenReturn(modelEntity);
140
141     testUpdateDependency_negative(modelEntity,
142         ComponentDependencyModelErrorBuilder.getNoSourceComponentErrorBuilder().id(),
143         ComponentDependencyModelErrorBuilder.getNoSourceComponentErrorBuilder().message());
144
145     ComponentDependencyModelEntity modelEntity1 = createModelEntity("", COMP_ID_1);
146     modelEntity1.setId(COMP_DEP_ID);
147
148     when(componentDependencyModelDao.get(any())).thenReturn(modelEntity1);
149
150     testUpdateDependency_negative(modelEntity1,
151         ComponentDependencyModelErrorBuilder.getNoSourceComponentErrorBuilder().id(),
152         ComponentDependencyModelErrorBuilder.getNoSourceComponentErrorBuilder().message());
153   }
154
155   @Test
156   public void testUpdateDependencyNegative_SameSourceTarget() {
157     ComponentDependencyModelEntity modelEntity =
158         createModelEntity(COMP_ID_1, COMP_ID_1);
159     modelEntity.setId(COMP_DEP_ID);
160
161     when(componentDependencyModelDao.get(any())).thenReturn(modelEntity);
162     testUpdateDependency_negative(modelEntity,
163         ComponentDependencyModelErrorBuilder.getSourceTargetComponentEqualErrorBuilder().id(),
164         ComponentDependencyModelErrorBuilder.getSourceTargetComponentEqualErrorBuilder().message());
165   }
166
167   @Test
168   public void testDeleteDependency() {
169     ComponentDependencyModelEntity modelEntity =
170         createModelEntity(COMP_ID_1, COMP_ID_2);
171     modelEntity.setId(COMP_DEP_ID);
172
173     when(componentDependencyModelDao.get(any())).thenReturn(modelEntity);
174
175     componentDependencyModelManager.delete(VSP_ID, VERSION, COMP_DEP_ID);
176     Mockito.verify(componentDependencyModelDao, Mockito.times(1)).delete(modelEntity);
177   }
178
179   @Test
180   public void testDeleteInvalidDependency() {
181     ComponentDependencyModelEntity delModelEntity =
182         createModelEntity(COMP_ID_1, COMP_ID_2);
183     delModelEntity.setId(COMP_DEP_ID);
184
185     try {
186       componentDependencyModelManager.delete(VSP_ID, VERSION, COMP_DEP_ID);
187       Assert.fail();
188     } catch (CoreException exception) {
189       Assert.assertEquals(exception.code().id(), "VERSIONABLE_SUB_ENTITY_NOT_FOUND");
190       Assert.assertEquals(exception.getMessage(),
191           String.format("Vendor Software Product Component Dependency Model with Id %s " +
192                   "does not exist for Vendor Software Product with id %s and version %s",
193               COMP_DEP_ID, VSP_ID, VERSION.getId()));
194     }
195   }
196
197
198   @Test
199   public void testGetDependency() {
200     ComponentDependencyModelEntity modelEntity =
201         createModelEntity(COMP_ID_1, COMP_ID_2);
202     modelEntity.setId(COMP_DEP_ID);
203
204     when(componentDependencyModelDao.get(any())).thenReturn(modelEntity);
205
206     ComponentDependencyModelEntity retrieved =
207         componentDependencyModelManager.get(VSP_ID, VERSION, COMP_DEP_ID);
208
209     Assert.assertEquals(retrieved.getSourceComponentId(), COMP_ID_1);
210
211   }
212
213   private ComponentDependencyModelEntity createModelEntity(String sourceId, String targetId) {
214     ComponentDependencyModelEntity entity =
215         new ComponentDependencyModelEntity(VSP_ID, VERSION, COMP_DEP_ID);
216     entity.setSourceComponentId(sourceId);
217     entity.setTargetComponentId(targetId);
218     entity.setRelation("dependsOn");
219     return entity;
220   }
221
222   private void testCreateDependency_negative(ComponentDependencyModelEntity entity, String vspId,
223                                              Version version, String expectedErrorCode,
224                                              String expectedErrorMsg) {
225     try {
226       componentDependencyModelManager.createComponentDependency(entity, vspId, version);
227       Assert.fail();
228     } catch (CoreException exception) {
229       Assert.assertEquals(exception.code().id(), expectedErrorCode);
230       Assert.assertEquals(exception.getMessage(), expectedErrorMsg);
231     }
232   }
233
234   private void testUpdateDependency_negative(ComponentDependencyModelEntity entity,
235                                              String expectedErrorCode, String expectedErrorMsg) {
236     try {
237       componentDependencyModelManager.update(entity);
238       Assert.fail();
239     } catch (CoreException exception) {
240       Assert.assertEquals(exception.code().id(), expectedErrorCode);
241       Assert.assertEquals(exception.getMessage(), expectedErrorMsg);
242     }
243   }
244 }