7b0216d5b60e776063212239e7718b3e4d3a0995
[sdc.git] / openecomp-be / backend / openecomp-sdc-vendor-license-manager / src / test / java / org / openecomp / sdc / vendorlicense / impl / VendorLicenseModelTest.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.vendorlicense.impl;
22
23 import org.junit.After;
24 import org.junit.Assert;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.mockito.*;
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;
38
39 import java.util.Set;
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;
46
47 /**
48  * Created by ayalaben on 7/19/2017
49  */
50 public class VendorLicenseModelTest {
51
52   private static final String USER1 = "TestUser1";
53   private static final String USER2 = "TestUser2";
54
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);
63
64   private static final boolean MULTITENANCY_ENABLED = true;
65
66   private static final String TEST_TENANT = "test_tenant";
67
68   @Mock
69   private VersioningManager versioningManagerMcok;
70   @Mock
71   private VendorLicenseFacade vendorLicenseFacadeMcok;
72   @Mock
73   private VendorLicenseModelDao vendorLicenseModelDaoMcok;
74   @Mock
75   private LicenseAgreementDao licenseAgreementDaoMcok;
76   @Mock
77   private FeatureGroupDao featureGroupDaoMcok;
78   @Mock
79   private EntitlementPoolDao entitlementPoolDaoMcok;
80   @Mock
81   private LicenseKeyGroupDao licenseKeyGroupDaoMcok;
82   @Mock
83   private LimitDao limitDaoMcok;
84
85   @Spy
86   @InjectMocks
87   private VendorLicenseManagerImpl vendorLicenseManager;
88
89
90   @Captor
91   private ArgumentCaptor<ActivityLogEntity> activityLogEntityArg;
92
93
94   @Before
95   public void setUp() throws Exception {
96     MockitoAnnotations.openMocks(this);
97   }
98
99   @After
100   public void tearDown(){
101     vendorLicenseManager = null;
102   }
103
104   @Test
105   public void testValidate() {
106     // TODO: 8/13/2017
107     vendorLicenseManager.validate(vlm1_id, null);
108     verify(vendorLicenseFacadeMcok).validate(vlm1_id, null);
109   }
110
111   @Test
112   public void testCreate() {
113     VendorLicenseModelEntity vlmEntity = new VendorLicenseModelEntity(vlm1_id, VERSION01);
114
115     vendorLicenseManager.createVendorLicenseModel(vlmEntity);
116
117     verify(vendorLicenseModelDaoMcok).create(vlmEntity);
118   }
119
120   @Test
121   public void testUpdate() {
122
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");
129
130     doReturn("VLM1").when(vendorLicenseModelDaoMcok).get(existingVlm);
131
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");
138
139     doNothing().when(vendorLicenseManager)
140         .updateUniqueName(VendorLicenseConstants.UniqueValues.VENDOR_NAME,
141             existingVlm.getVendorName(), updatedVlm.getVendorName());
142
143     doReturn(existingVlm).when(vendorLicenseModelDaoMcok).get(any(VendorLicenseModelEntity.class));
144
145     vendorLicenseManager.updateVendorLicenseModel(updatedVlm);
146
147     verify(vendorLicenseModelDaoMcok).update(updatedVlm);
148   }
149
150   @Test
151   public void testGetVendorLicenseModel() {
152     vendorLicenseManager.getVendorLicenseModel(vlm1_id, VERSION01);
153     verify(vendorLicenseFacadeMcok).getVendorLicenseModel(vlm1_id, VERSION01);
154   }
155
156   @Test(expected = UnsupportedOperationException.class)
157   public void testDeleteVLMUnsupportedOperation() {
158     vendorLicenseManager.deleteVendorLicenseModel(vlm1_id, null); // TODO: 8/13/2017
159   }
160
161
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);
167 //  }
168
169
170   @Test
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);
180   }
181
182  @Test
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()));
194   }
195
196   @Test
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);
208   }
209
210   @Test
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)));
222
223     Assert.assertEquals(expectedItems.size(), 0);
224     Assert.assertNotEquals(expectedItems.containsAll(actualItems), actualItems.containsAll(expectedItems));
225   }
226
227   private Set<String> getTestRoles(){
228     Set<String> roles = new HashSet<>();
229     roles.add("test_admin");
230     roles.add("test_tenant");
231     return roles;
232   }
233
234   private List<Item> getLicenseModelItems(){
235     List<Item> items=new ArrayList<>();
236
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);
244
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");
252
253     items.add(itemOne);
254     items.add(itemTwo);
255     return items;
256   }
257
258 }