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.vendorlicense.impl;
23 import org.junit.After;
24 import org.junit.Assert;
25 import org.junit.Before;
26 import org.junit.Test;
28 import org.openecomp.sdc.activitylog.dao.type.ActivityLogEntity;
29 import org.openecomp.sdc.datatypes.model.ItemType;
30 import org.openecomp.sdc.vendorlicense.VendorLicenseConstants;
31 import org.openecomp.sdc.vendorlicense.dao.*;
32 import org.openecomp.sdc.vendorlicense.dao.types.VendorLicenseModelEntity;
33 import org.openecomp.sdc.vendorlicense.facade.VendorLicenseFacade;
34 import org.openecomp.sdc.versioning.VersioningManager;
35 import org.openecomp.sdc.versioning.dao.types.Version;
36 import org.openecomp.sdc.versioning.types.Item;
37 import org.openecomp.sdc.versioning.types.ItemStatus;
40 import java.util.List;
41 import java.util.ArrayList;
42 import java.util.HashSet;
43 import static org.mockito.ArgumentMatchers.any;
44 import static org.mockito.Mockito.*;
45 import static org.hamcrest.MatcherAssert.assertThat;
48 * Created by ayalaben on 7/19/2017
50 public class VendorLicenseModelTest {
52 private static final String USER1 = "TestUser1";
53 private static final String USER2 = "TestUser2";
55 private static String vlm1_id = "vlm1_id";
56 private static String vlm2_id = "vlm2_id";
57 private static String la1_id = "la1_id";
58 private static String la2_id = "la2_id";
59 private static String fg1_id = "fg1_id";
60 private static String fg2_id = "fg2_id";
61 public static final Version VERSION01 = new Version(0, 1);
62 private static final Version VERSION10 = new Version(1, 0);
64 private static final boolean MULTITENANCY_ENABLED = true;
66 private static final String TEST_TENANT = "test_tenant";
69 private VersioningManager versioningManagerMcok;
71 private VendorLicenseFacade vendorLicenseFacadeMcok;
73 private VendorLicenseModelDao vendorLicenseModelDaoMcok;
75 private LicenseAgreementDao licenseAgreementDaoMcok;
77 private FeatureGroupDao featureGroupDaoMcok;
79 private EntitlementPoolDao entitlementPoolDaoMcok;
81 private LicenseKeyGroupDao licenseKeyGroupDaoMcok;
83 private LimitDao limitDaoMcok;
87 private VendorLicenseManagerImpl vendorLicenseManager;
91 private ArgumentCaptor<ActivityLogEntity> activityLogEntityArg;
95 public void setUp() throws Exception {
96 MockitoAnnotations.openMocks(this);
100 public void tearDown(){
101 vendorLicenseManager = null;
105 public void testValidate() {
107 vendorLicenseManager.validate(vlm1_id, null);
108 verify(vendorLicenseFacadeMcok).validate(vlm1_id, null);
112 public void testCreate() {
113 VendorLicenseModelEntity vlmEntity = new VendorLicenseModelEntity(vlm1_id, VERSION01);
115 vendorLicenseManager.createVendorLicenseModel(vlmEntity);
117 verify(vendorLicenseModelDaoMcok).create(vlmEntity);
121 public void testUpdate() {
123 VendorLicenseModelEntity existingVlm = new VendorLicenseModelEntity();
124 existingVlm.setVersion(VERSION01);
125 existingVlm.setId(vlm1_id);
126 existingVlm.setIconRef("icon");
127 existingVlm.setVendorName("VLM1");
128 existingVlm.setDescription("decription");
130 doReturn("VLM1").when(vendorLicenseModelDaoMcok).get(existingVlm);
132 VendorLicenseModelEntity updatedVlm = new VendorLicenseModelEntity();
133 updatedVlm.setVersion(VERSION01);
134 updatedVlm.setId(vlm1_id);
135 updatedVlm.setIconRef("icon");
136 updatedVlm.setVendorName("VLM1_updated");
137 updatedVlm.setDescription("decription");
139 doNothing().when(vendorLicenseManager)
140 .updateUniqueName(VendorLicenseConstants.UniqueValues.VENDOR_NAME,
141 existingVlm.getVendorName(), updatedVlm.getVendorName());
143 doReturn(existingVlm).when(vendorLicenseModelDaoMcok).get(any(VendorLicenseModelEntity.class));
145 vendorLicenseManager.updateVendorLicenseModel(updatedVlm);
147 verify(vendorLicenseModelDaoMcok).update(updatedVlm);
151 public void testGetVendorLicenseModel() {
152 vendorLicenseManager.getVendorLicenseModel(vlm1_id, VERSION01);
153 verify(vendorLicenseFacadeMcok).getVendorLicenseModel(vlm1_id, VERSION01);
156 @Test(expected = UnsupportedOperationException.class)
157 public void testDeleteVLMUnsupportedOperation() {
158 vendorLicenseManager.deleteVendorLicenseModel(vlm1_id, null); // TODO: 8/13/2017
162 // @Test(expectedExceptions = CoreException.class)
163 // public void testGetNonExistingVersion_negative() {
164 // Version notExistversion = new Version(43, 8);
165 // doReturn(null).when(vspInfoDaoMock).get(any(VspDetails.class));
166 // vendorSoftwareProductManager.getVsp(VSP_ID, notExistversion);
171 public void testCreate_withMultitenancyValidTenant_Success() {
172 Assert.assertEquals(MULTITENANCY_ENABLED,true);
173 VendorLicenseModelEntity vlmEntity = new VendorLicenseModelEntity();
174 vlmEntity.setId(vlm1_id);
175 vlmEntity.setVersion(VERSION01);
176 vlmEntity.setTenant(TEST_TENANT);
177 assertThat("Unauthorized Tenant", getTestRoles().contains(vlmEntity.getTenant()));
178 vendorLicenseManager.createVendorLicenseModel(vlmEntity);
179 verify(vendorLicenseModelDaoMcok).create(vlmEntity);
183 public void testCreate_withMultitenancyInvalidTenant_Failure() {
184 String invalidTenant="invalid_tenant";
185 Assert.assertEquals(MULTITENANCY_ENABLED,true);
186 VendorLicenseModelEntity vlmEntity = new VendorLicenseModelEntity();
187 vlmEntity.setId(vlm1_id);
188 vlmEntity.setVersion(VERSION01);
189 vlmEntity.setTenant(invalidTenant);
190 Assert.assertFalse(getTestRoles().contains(invalidTenant));
191 Assert.assertNotNull(vlmEntity.getTenant());
192 vendorLicenseManager.createVendorLicenseModel(vlmEntity);
193 assertThat("Unauthorized Tenant", !getTestRoles().contains(vlmEntity.getTenant()));
197 public void testListVLM_multitenancyWithTenant_FilterList() {
198 Assert.assertEquals(MULTITENANCY_ENABLED,true);
199 Assert.assertNotNull(getTestRoles());
200 assertThat("Unauthorized Tenant", getTestRoles().contains(TEST_TENANT));
201 String tenant = TEST_TENANT;
202 Assert.assertNotNull(tenant);
203 List<Item> expectedItems=new ArrayList<>();
204 getTestRoles().stream().forEach(role -> getLicenseModelItems().stream()
205 .filter(item -> item.getTenant().contains(role))
206 .forEach(item -> expectedItems.add(item)));
207 Assert.assertEquals(expectedItems.size(), 1);
211 public void testListVLM_multitenancyWithInvalidTenant_ReturnEmptylist() {
212 Assert.assertEquals(MULTITENANCY_ENABLED,true);
213 Assert.assertNotNull(getTestRoles());
214 String tenant= "invalid_tenant";
215 List<Item> expectedItems=new ArrayList<>();
216 List<Item> actualItems=getLicenseModelItems();
217 Assert.assertNotNull(tenant);
218 getTestRoles().stream().forEach(role -> getLicenseModelItems().stream()
219 .filter(item -> item.getTenant()!=null)
220 .filter(item -> item.getTenant().contains(tenant))
221 .forEach(item -> expectedItems.add(item)));
223 Assert.assertEquals(expectedItems.size(), 0);
224 Assert.assertNotEquals(expectedItems.containsAll(actualItems), actualItems.containsAll(expectedItems));
227 private Set<String> getTestRoles(){
228 Set<String> roles = new HashSet<>();
229 roles.add("test_admin");
230 roles.add("test_tenant");
234 private List<Item> getLicenseModelItems(){
235 List<Item> items=new ArrayList<>();
237 Item itemOne = new Item();
238 itemOne.setType(ItemType.vlm.name());
239 itemOne.setOwner(USER1);
240 itemOne.setStatus(ItemStatus.ACTIVE);
241 itemOne.setName("TEST_VENDOR_ONE");
242 itemOne.setDescription("TEST_DESCRIPTION");
243 itemOne.setTenant(TEST_TENANT);
245 Item itemTwo = new Item();
246 itemTwo.setType(ItemType.vlm.name());
247 itemTwo.setOwner(USER1);
248 itemTwo.setStatus(ItemStatus.ACTIVE);
249 itemTwo.setName("TEST_VENDOR_TWO");
250 itemTwo.setDescription("TEST_DESCRIPTION");
251 itemTwo.setTenant("admin_tenant");