2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.vendorsoftwareproduct.impl;
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;
38 import java.util.ArrayList;
39 import java.util.Collection;
40 import java.util.List;
42 import static org.mockito.ArgumentMatchers.any;
43 import static org.mockito.Mockito.when;
45 public class ComponentDependencyModelTest {
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";
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";
58 private ComponentDependencyModelManagerImpl componentDependencyModelManager;
60 private ComponentManager componentManager;
62 private ComponentDependencyModelDao componentDependencyModelDao;
66 MockitoAnnotations.openMocks(this);
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));
75 Mockito.when(componentDependencyModelDao
76 .list(new ComponentDependencyModelEntity(VSP_ID, VERSION, null)))
77 .thenReturn(entities);
79 Collection<ComponentDependencyModelEntity> list =
80 componentDependencyModelManager.list(VSP_ID, VERSION);
82 Mockito.verify(componentDependencyModelDao, Mockito.times(1))
83 .list(new ComponentDependencyModelEntity(VSP_ID, VERSION, null));
85 Assert.assertEquals(2, list.size());
89 public void testCreateDependency() {
90 ComponentDependencyModelEntity modelEntity =
91 createModelEntity(COMP_ID_1, COMP_ID_2);
93 componentDependencyModelManager.createComponentDependency(modelEntity, VSP_ID, VERSION);
94 Mockito.verify(componentDependencyModelDao, Mockito.times(1)).create(modelEntity);
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());
107 public void testCreateDependencyNegative_NoSourceId() {
109 ComponentDependencyModelEntity modelEntity = createModelEntity(null, COMP_ID_1);
110 testCreateDependency_negative(modelEntity, VSP_ID, VERSION,
111 ComponentDependencyModelErrorBuilder.getNoSourceComponentErrorBuilder().id(),
112 ComponentDependencyModelErrorBuilder.getNoSourceComponentErrorBuilder().message());
115 ComponentDependencyModelEntity modelEntity1 = createModelEntity("", COMP_ID_1);
116 testCreateDependency_negative(modelEntity1, VSP_ID, VERSION,
117 ComponentDependencyModelErrorBuilder.getNoSourceComponentErrorBuilder().id(),
118 ComponentDependencyModelErrorBuilder.getNoSourceComponentErrorBuilder().message());
122 public void testUpdateDependency() {
123 ComponentDependencyModelEntity modelEntity =
124 createModelEntity(COMP_ID_1, COMP_ID_2);
125 modelEntity.setId(COMP_DEP_ID);
127 when(componentDependencyModelDao.get(any())).thenReturn(modelEntity);
129 componentDependencyModelManager.update(modelEntity);
130 Mockito.verify(componentDependencyModelDao, Mockito.times(1)).update(modelEntity);
134 public void testUpdateDependencyNegative_NoSourceId() {
136 ComponentDependencyModelEntity modelEntity = createModelEntity(null, COMP_ID_1);
137 modelEntity.setId(COMP_DEP_ID);
139 when(componentDependencyModelDao.get(any())).thenReturn(modelEntity);
141 testUpdateDependency_negative(modelEntity,
142 ComponentDependencyModelErrorBuilder.getNoSourceComponentErrorBuilder().id(),
143 ComponentDependencyModelErrorBuilder.getNoSourceComponentErrorBuilder().message());
145 ComponentDependencyModelEntity modelEntity1 = createModelEntity("", COMP_ID_1);
146 modelEntity1.setId(COMP_DEP_ID);
148 when(componentDependencyModelDao.get(any())).thenReturn(modelEntity1);
150 testUpdateDependency_negative(modelEntity1,
151 ComponentDependencyModelErrorBuilder.getNoSourceComponentErrorBuilder().id(),
152 ComponentDependencyModelErrorBuilder.getNoSourceComponentErrorBuilder().message());
156 public void testUpdateDependencyNegative_SameSourceTarget() {
157 ComponentDependencyModelEntity modelEntity =
158 createModelEntity(COMP_ID_1, COMP_ID_1);
159 modelEntity.setId(COMP_DEP_ID);
161 when(componentDependencyModelDao.get(any())).thenReturn(modelEntity);
162 testUpdateDependency_negative(modelEntity,
163 ComponentDependencyModelErrorBuilder.getSourceTargetComponentEqualErrorBuilder().id(),
164 ComponentDependencyModelErrorBuilder.getSourceTargetComponentEqualErrorBuilder().message());
168 public void testDeleteDependency() {
169 ComponentDependencyModelEntity modelEntity =
170 createModelEntity(COMP_ID_1, COMP_ID_2);
171 modelEntity.setId(COMP_DEP_ID);
173 when(componentDependencyModelDao.get(any())).thenReturn(modelEntity);
175 componentDependencyModelManager.delete(VSP_ID, VERSION, COMP_DEP_ID);
176 Mockito.verify(componentDependencyModelDao, Mockito.times(1)).delete(modelEntity);
180 public void testDeleteInvalidDependency() {
181 ComponentDependencyModelEntity delModelEntity =
182 createModelEntity(COMP_ID_1, COMP_ID_2);
183 delModelEntity.setId(COMP_DEP_ID);
186 componentDependencyModelManager.delete(VSP_ID, VERSION, COMP_DEP_ID);
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()));
199 public void testGetDependency() {
200 ComponentDependencyModelEntity modelEntity =
201 createModelEntity(COMP_ID_1, COMP_ID_2);
202 modelEntity.setId(COMP_DEP_ID);
204 when(componentDependencyModelDao.get(any())).thenReturn(modelEntity);
206 ComponentDependencyModelEntity retrieved =
207 componentDependencyModelManager.get(VSP_ID, VERSION, COMP_DEP_ID);
209 Assert.assertEquals(retrieved.getSourceComponentId(), COMP_ID_1);
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");
222 private void testCreateDependency_negative(ComponentDependencyModelEntity entity, String vspId,
223 Version version, String expectedErrorCode,
224 String expectedErrorMsg) {
226 componentDependencyModelManager.createComponentDependency(entity, vspId, version);
228 } catch (CoreException exception) {
229 Assert.assertEquals(exception.code().id(), expectedErrorCode);
230 Assert.assertEquals(exception.getMessage(), expectedErrorMsg);
234 private void testUpdateDependency_negative(ComponentDependencyModelEntity entity,
235 String expectedErrorCode, String expectedErrorMsg) {
237 componentDependencyModelManager.update(entity);
239 } catch (CoreException exception) {
240 Assert.assertEquals(exception.code().id(), expectedErrorCode);
241 Assert.assertEquals(exception.getMessage(), expectedErrorMsg);