1 package org.openecomp.sdc.vendorlicense.impl;
3 import org.junit.rules.ExpectedException;
4 import org.openecomp.core.util.UniqueValueUtil;
5 import org.openecomp.sdc.activityLog.ActivityLogManager;
6 import org.openecomp.sdc.activitylog.dao.type.ActivityLogEntity;
7 import org.openecomp.sdc.common.errors.CoreException;
8 import org.openecomp.sdc.common.errors.ErrorCategory;
9 import org.openecomp.sdc.common.errors.ErrorCode;
10 import org.openecomp.sdc.vendorlicense.VendorLicenseConstants;
11 import org.openecomp.sdc.vendorlicense.dao.EntitlementPoolDao;
12 import org.openecomp.sdc.vendorlicense.dao.FeatureGroupDao;
13 import org.openecomp.sdc.vendorlicense.dao.LicenseAgreementDao;
14 import org.openecomp.sdc.vendorlicense.dao.LicenseKeyGroupDao;
15 import org.openecomp.sdc.vendorlicense.dao.LimitDao;
16 import org.openecomp.sdc.vendorlicense.dao.VendorLicenseModelDao;
17 import org.openecomp.sdc.vendorlicense.dao.types.ChoiceOrOther;
18 import org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupEntity;
19 import org.openecomp.sdc.vendorlicense.dao.types.LicenseAgreementEntity;
20 import org.openecomp.sdc.vendorlicense.dao.types.LicenseTerm;
21 import org.openecomp.sdc.vendorlicense.dao.types.VendorLicenseModelEntity;
22 import org.openecomp.sdc.vendorlicense.facade.VendorLicenseFacade;
23 import org.openecomp.sdc.vendorlicense.types.VersionedVendorLicenseModel;
24 import org.openecomp.sdc.vendorsoftwareproduct.VendorSoftwareProductConstants;
25 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails;
26 import org.openecomp.sdc.vendorsoftwareproduct.types.VersionedVendorSoftwareProductInfo;
27 import org.openecomp.sdc.versioning.VersioningManager;
28 import org.openecomp.sdc.versioning.VersioningUtil;
29 import org.openecomp.sdc.versioning.dao.types.Version;
30 import org.openecomp.sdc.versioning.dao.types.VersionStatus;
31 import org.openecomp.sdc.versioning.types.VersionInfo;
32 import org.openecomp.sdc.versioning.types.VersionableEntityAction;
33 import org.testng.annotations.BeforeMethod;
34 import org.mockito.ArgumentCaptor;
35 import org.mockito.Captor;
36 import org.mockito.InjectMocks;
37 import org.mockito.Mock;
38 import org.mockito.MockitoAnnotations;
39 import org.mockito.Spy;
40 import org.testng.Assert;
41 import org.testng.annotations.BeforeMethod;
42 import org.testng.annotations.Test;
44 import java.util.ArrayList;
45 import java.util.Arrays;
46 import java.util.Collection;
47 import java.util.HashMap;
48 import java.util.HashSet;
49 import java.util.List;
53 import static org.mockito.Matchers.any;
54 import static org.mockito.Matchers.anyObject;
55 import static org.mockito.Matchers.eq;
56 import static org.mockito.Mockito.doNothing;
57 import static org.mockito.Mockito.doReturn;
58 import static org.mockito.Mockito.doThrow;
59 import static org.mockito.Mockito.verify;
60 import static org.openecomp.sdc.vendorlicense.VendorLicenseConstants.VENDOR_LICENSE_MODEL_VERSIONABLE_TYPE;
64 * Created by ayalaben on 7/19/2017
66 public class VendorLicenseModelTest {
68 private static final String USER1 = "TestUser1";
69 private static final String USER2 = "TestUser2";
71 private static String vlm1_id = "vlm1_id";
72 private static String vlm2_id = "vlm2_id";
73 private static String la1_id = "la1_id";
74 private static String la2_id = "la2_id";
75 private static String fg1_id = "fg1_id";
76 private static String fg2_id = "fg2_id";
77 public static final Version VERSION01 = new Version(0, 1);
78 private static final Version VERSION10 = new Version(1, 0);
81 private VersioningManager versioningManagerMcok;
83 private VendorLicenseFacade vendorLicenseFacadeMcok;
85 private VendorLicenseModelDao vendorLicenseModelDaoMcok;
87 private LicenseAgreementDao licenseAgreementDaoMcok;
89 private FeatureGroupDao featureGroupDaoMcok;
91 private EntitlementPoolDao entitlementPoolDaoMcok;
93 private LicenseKeyGroupDao licenseKeyGroupDaoMcok;
95 private LimitDao limitDaoMcok;
97 private ActivityLogManager activityLogManagerMcok;
102 private VendorLicenseManagerImpl vendorLicenseManager;
106 private ArgumentCaptor<ActivityLogEntity> activityLogEntityArg;
110 public void setUp() throws Exception {
111 MockitoAnnotations.initMocks(this);
115 public void testCheckout() {
117 doReturn(VERSION01).when(versioningManagerMcok)
118 .checkout(VENDOR_LICENSE_MODEL_VERSIONABLE_TYPE, vlm1_id, USER1);
120 vendorLicenseManager.checkout(vlm1_id, USER1);
122 Assert.assertEquals(VERSION01.getStatus(), VersionStatus.Locked);
123 verify(vendorLicenseFacadeMcok).updateVlmLastModificationTime(vlm1_id, VERSION01);
125 verify(versioningManagerMcok)
126 .checkout(VENDOR_LICENSE_MODEL_VERSIONABLE_TYPE, vlm1_id, USER1);
128 verify(activityLogManagerMcok).addActionLog(activityLogEntityArg.capture(), eq(USER1));
129 ActivityLogEntity activityLogEntity = activityLogEntityArg.getValue();
130 Assert.assertEquals(activityLogEntity.getVersionId(), String.valueOf(VERSION01.getMajor() + 1));
131 Assert.assertTrue(activityLogEntity.isSuccess());
135 public void testUndoCheckout() {
136 Version existingVersion = new Version(0, 2);
137 doReturn(existingVersion).when(versioningManagerMcok).undoCheckout(
138 VENDOR_LICENSE_MODEL_VERSIONABLE_TYPE, vlm1_id, USER1);
140 vendorLicenseManager.undoCheckout(vlm1_id, USER1);
142 verify(vendorLicenseFacadeMcok).updateVlmLastModificationTime(vlm1_id, existingVersion);
146 public void testCheckin() {
148 doReturn(VERSION10).when(vendorLicenseFacadeMcok).checkin(vlm1_id, USER1);
150 vendorLicenseManager.checkin(vlm1_id, USER1);
151 verify(activityLogManagerMcok).addActionLog(activityLogEntityArg.capture(), eq(USER1));
152 ActivityLogEntity activityLogEntity = activityLogEntityArg.getValue();
153 Assert.assertEquals(activityLogEntity.getVersionId(), String.valueOf(VERSION10.getMajor() + 1));
154 verify(vendorLicenseFacadeMcok).checkin(vlm1_id, USER1);
159 public void testSubmit() {
161 doReturn(VERSION10).when(vendorLicenseFacadeMcok).submit(vlm1_id, USER1);
163 vendorLicenseManager.submit(vlm1_id, USER1);
164 verify(activityLogManagerMcok).addActionLog(activityLogEntityArg.capture(), eq(USER1));
165 ActivityLogEntity activityLogEntity = activityLogEntityArg.getValue();
166 Assert.assertEquals(activityLogEntity.getVersionId(), String.valueOf(VERSION10.getMajor()));
167 verify(vendorLicenseFacadeMcok).submit(vlm1_id, USER1);
172 public void testListWhenNone() {
173 doReturn(new HashMap<>()).when(versioningManagerMcok).listEntitiesVersionInfo
174 (VENDOR_LICENSE_MODEL_VERSIONABLE_TYPE, USER1, VersionableEntityAction.Read);
175 Collection<VersionedVendorLicenseModel> vlms = vendorLicenseManager.listVendorLicenseModels
177 Assert.assertEquals(vlms.size(), 0);
181 public void testList() {
183 Map<String, VersionInfo> vlmsTobeReturned = new HashMap<>();
185 VersionInfo versionInfo1 = new VersionInfo();
186 versionInfo1.setActiveVersion(VERSION01);
187 vlmsTobeReturned.put(vlm1_id, versionInfo1);
189 VersionInfo versionInfo2 = new VersionInfo();
190 versionInfo2.setActiveVersion(VERSION10);
191 vlmsTobeReturned.put(vlm2_id, versionInfo2);
193 doReturn(vlmsTobeReturned).when(versioningManagerMcok).listEntitiesVersionInfo
194 (VENDOR_LICENSE_MODEL_VERSIONABLE_TYPE, USER1, VersionableEntityAction.Read);
196 VendorLicenseModelEntity vlm1 = new VendorLicenseModelEntity(vlm1_id, VERSION01);
197 vlm1.setWritetimeMicroSeconds(8L);
198 doReturn(vlm1).when(vendorLicenseModelDaoMcok).get(any(VendorLicenseModelEntity.class));
200 Collection<VersionedVendorLicenseModel> vlms = vendorLicenseManager.listVendorLicenseModels
202 Assert.assertEquals(vlms.size(), 2);
206 public void testListFinalsWhenNone() {
208 Map<String, VersionInfo> vspsTobeReturned = new HashMap<>();
210 VersionInfo versionInfo1 = new VersionInfo();
211 versionInfo1.setActiveVersion(VERSION01);
212 vspsTobeReturned.put(vlm1_id, versionInfo1);
214 VersionInfo versionInfo2 = new VersionInfo();
215 versionInfo2.setActiveVersion(VERSION10);
216 vspsTobeReturned.put(vlm2_id, versionInfo2);
218 doReturn(vspsTobeReturned).when(versioningManagerMcok).listEntitiesVersionInfo
219 (VENDOR_LICENSE_MODEL_VERSIONABLE_TYPE, USER1, VersionableEntityAction.Read);
221 Collection<VersionedVendorLicenseModel> vlms = vendorLicenseManager.listVendorLicenseModels
222 (VersionStatus.Final.name(), USER1);
223 Assert.assertEquals(vlms.size(), 0);
227 public void testListFinals() {
228 Map<String, VersionInfo> vlmsTobeReturned = new HashMap<>();
230 VersionInfo versionInfo1 = new VersionInfo();
231 versionInfo1.setActiveVersion(VERSION01);
232 vlmsTobeReturned.put(vlm1_id, versionInfo1);
234 VersionInfo versionInfo2 = new VersionInfo();
236 versionInfo2.setActiveVersion(new Version(1, 3));
237 versionInfo2.setLatestFinalVersion(VERSION10);
238 vlmsTobeReturned.put(vlm2_id, versionInfo2);
240 doReturn(vlmsTobeReturned).when(versioningManagerMcok).listEntitiesVersionInfo
241 (VENDOR_LICENSE_MODEL_VERSIONABLE_TYPE, USER1, VersionableEntityAction.Read);
243 VendorLicenseModelEntity vlm1 = new VendorLicenseModelEntity(vlm1_id, VERSION01);
244 vlm1.setWritetimeMicroSeconds(8L);
245 doReturn(vlm1).when(vendorLicenseModelDaoMcok).get(any(VendorLicenseModelEntity.class));
247 Collection<VersionedVendorLicenseModel> vlms = vendorLicenseManager.listVendorLicenseModels
248 (VersionStatus.Final.name(), USER1);
250 Assert.assertEquals(vlms.size(), 1);
254 public void testCreate() {
256 VendorLicenseModelEntity vlmEntity = new VendorLicenseModelEntity(vlm1_id, VERSION01);
258 doReturn(vlmEntity).when(vendorLicenseFacadeMcok).createVendorLicenseModel(vlmEntity, USER1);
260 vendorLicenseManager.createVendorLicenseModel(vlmEntity, USER1);
262 verify(vendorLicenseFacadeMcok).createVendorLicenseModel(vlmEntity, USER1);
263 verify(activityLogManagerMcok).addActionLog(activityLogEntityArg.capture(), eq(USER1));
264 ActivityLogEntity activityLogEntity = activityLogEntityArg.getValue();
265 Assert.assertEquals(activityLogEntity.getVersionId(), String.valueOf(VERSION01.getMajor() + 1));
266 Assert.assertTrue(activityLogEntity.isSuccess());
271 public void testUpdate() {
273 VendorLicenseModelEntity existingVlm = new VendorLicenseModelEntity();
274 existingVlm.setVersion(VERSION01);
275 existingVlm.setId(vlm1_id);
276 existingVlm.setIconRef("icon");
277 existingVlm.setVendorName("VLM1");
278 existingVlm.setDescription("decription");
280 VersionInfo versionInfo = new VersionInfo();
281 versionInfo.setActiveVersion(VERSION01);
283 doReturn(versionInfo).when(vendorLicenseManager).getVersionInfo(existingVlm.getId(),
284 VersionableEntityAction.Write, USER1);
286 doReturn(VERSION01).when(vendorLicenseManager).resloveVersion(vlm1_id,null, versionInfo, USER1);
288 doReturn("VLM1").when(vendorLicenseModelDaoMcok).get(existingVlm);
290 VendorLicenseModelEntity updatedVlm = new VendorLicenseModelEntity();
291 updatedVlm.setVersion(VERSION01);
292 updatedVlm.setId(vlm1_id);
293 updatedVlm.setIconRef("icon");
294 updatedVlm.setVendorName("VLM1_updated");
295 updatedVlm.setDescription("decription");
297 doNothing().when(vendorLicenseManager)
298 .updateUniqueName(VendorLicenseConstants.UniqueValues.VENDOR_NAME,
299 existingVlm.getVendorName(), updatedVlm.getVendorName());
301 existingVlm.setWritetimeMicroSeconds(8L);
303 doReturn(existingVlm).when(vendorLicenseModelDaoMcok).get(any(VendorLicenseModelEntity.class));
305 vendorLicenseManager.updateVendorLicenseModel(updatedVlm, USER1);
307 verify(vendorLicenseModelDaoMcok).update(updatedVlm);
308 verify(vendorLicenseFacadeMcok).updateVlmLastModificationTime(vlm1_id, VERSION01);
312 public void testGetVendorLicenseModel(){
313 vendorLicenseManager.getVendorLicenseModel(vlm1_id,VERSION01,USER1);
314 verify(vendorLicenseFacadeMcok).getVendorLicenseModel(vlm1_id,VERSION01,USER1);
317 @Test(expectedExceptions = UnsupportedOperationException.class)
318 public void testDeleteVLMUnsupportedOperation() {
319 vendorLicenseManager.deleteVendorLicenseModel(vlm1_id, USER1);
323 // @Test(expectedExceptions = CoreException.class)
324 // public void testGetNonExistingVersion_negative() {
325 // Version notExistversion = new Version(43, 8);
326 // doReturn(null).when(vspInfoDaoMock).get(any(VspDetails.class));
327 // vendorSoftwareProductManager.getVsp(VSP_ID, notExistversion, USER1);