aboutsummaryrefslogtreecommitdiffstats
path: root/openecomp-be/backend/openecomp-sdc-vendor-license-manager/src/test/java/org/openecomp/sdc/vendorlicense/impl/VendorLicenseModelTest.java
blob: 7b0216d5b60e776063212239e7718b3e4d3a0995 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/*-
 * ============LICENSE_START=======================================================
 * SDC
 * ================================================================================
 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.openecomp.sdc.vendorlicense.impl;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.mockito.*;
import org.openecomp.sdc.activitylog.dao.type.ActivityLogEntity;
import org.openecomp.sdc.datatypes.model.ItemType;
import org.openecomp.sdc.vendorlicense.VendorLicenseConstants;
import org.openecomp.sdc.vendorlicense.dao.*;
import org.openecomp.sdc.vendorlicense.dao.types.VendorLicenseModelEntity;
import org.openecomp.sdc.vendorlicense.facade.VendorLicenseFacade;
import org.openecomp.sdc.versioning.VersioningManager;
import org.openecomp.sdc.versioning.dao.types.Version;
import org.openecomp.sdc.versioning.types.Item;
import org.openecomp.sdc.versioning.types.ItemStatus;

import java.util.Set;
import java.util.List;
import java.util.ArrayList;
import java.util.HashSet;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;
import static org.hamcrest.MatcherAssert.assertThat;

/**
 * Created by ayalaben on 7/19/2017
 */
public class VendorLicenseModelTest {

  private static final String USER1 = "TestUser1";
  private static final String USER2 = "TestUser2";

  private static String vlm1_id = "vlm1_id";
  private static String vlm2_id = "vlm2_id";
  private static String la1_id = "la1_id";
  private static String la2_id = "la2_id";
  private static String fg1_id = "fg1_id";
  private static String fg2_id = "fg2_id";
  public static final Version VERSION01 = new Version(0, 1);
  private static final Version VERSION10 = new Version(1, 0);

  private static final boolean MULTITENANCY_ENABLED = true;

  private static final String TEST_TENANT = "test_tenant";

  @Mock
  private VersioningManager versioningManagerMcok;
  @Mock
  private VendorLicenseFacade vendorLicenseFacadeMcok;
  @Mock
  private VendorLicenseModelDao vendorLicenseModelDaoMcok;
  @Mock
  private LicenseAgreementDao licenseAgreementDaoMcok;
  @Mock
  private FeatureGroupDao featureGroupDaoMcok;
  @Mock
  private EntitlementPoolDao entitlementPoolDaoMcok;
  @Mock
  private LicenseKeyGroupDao licenseKeyGroupDaoMcok;
  @Mock
  private LimitDao limitDaoMcok;

  @Spy
  @InjectMocks
  private VendorLicenseManagerImpl vendorLicenseManager;


  @Captor
  private ArgumentCaptor<ActivityLogEntity> activityLogEntityArg;


  @Before
  public void setUp() throws Exception {
    MockitoAnnotations.openMocks(this);
  }

  @After
  public void tearDown(){
    vendorLicenseManager = null;
  }

  @Test
  public void testValidate() {
    // TODO: 8/13/2017
    vendorLicenseManager.validate(vlm1_id, null);
    verify(vendorLicenseFacadeMcok).validate(vlm1_id, null);
  }

  @Test
  public void testCreate() {
    VendorLicenseModelEntity vlmEntity = new VendorLicenseModelEntity(vlm1_id, VERSION01);

    vendorLicenseManager.createVendorLicenseModel(vlmEntity);

    verify(vendorLicenseModelDaoMcok).create(vlmEntity);
  }

  @Test
  public void testUpdate() {

    VendorLicenseModelEntity existingVlm = new VendorLicenseModelEntity();
    existingVlm.setVersion(VERSION01);
    existingVlm.setId(vlm1_id);
    existingVlm.setIconRef("icon");
    existingVlm.setVendorName("VLM1");
    existingVlm.setDescription("decription");

    doReturn("VLM1").when(vendorLicenseModelDaoMcok).get(existingVlm);

    VendorLicenseModelEntity updatedVlm = new VendorLicenseModelEntity();
    updatedVlm.setVersion(VERSION01);
    updatedVlm.setId(vlm1_id);
    updatedVlm.setIconRef("icon");
    updatedVlm.setVendorName("VLM1_updated");
    updatedVlm.setDescription("decription");

    doNothing().when(vendorLicenseManager)
        .updateUniqueName(VendorLicenseConstants.UniqueValues.VENDOR_NAME,
            existingVlm.getVendorName(), updatedVlm.getVendorName());

    doReturn(existingVlm).when(vendorLicenseModelDaoMcok).get(any(VendorLicenseModelEntity.class));

    vendorLicenseManager.updateVendorLicenseModel(updatedVlm);

    verify(vendorLicenseModelDaoMcok).update(updatedVlm);
  }

  @Test
  public void testGetVendorLicenseModel() {
    vendorLicenseManager.getVendorLicenseModel(vlm1_id, VERSION01);
    verify(vendorLicenseFacadeMcok).getVendorLicenseModel(vlm1_id, VERSION01);
  }

  @Test(expected = UnsupportedOperationException.class)
  public void testDeleteVLMUnsupportedOperation() {
    vendorLicenseManager.deleteVendorLicenseModel(vlm1_id, null); // TODO: 8/13/2017
  }


//  @Test(expectedExceptions = CoreException.class)
//  public void testGetNonExistingVersion_negative() {
//    Version notExistversion = new Version(43, 8);
//    doReturn(null).when(vspInfoDaoMock).get(any(VspDetails.class));
//    vendorSoftwareProductManager.getVsp(VSP_ID, notExistversion);
//  }


  @Test
  public void testCreate_withMultitenancyValidTenant_Success() {
    Assert.assertEquals(MULTITENANCY_ENABLED,true);
    VendorLicenseModelEntity vlmEntity = new VendorLicenseModelEntity();
    vlmEntity.setId(vlm1_id);
    vlmEntity.setVersion(VERSION01);
    vlmEntity.setTenant(TEST_TENANT);
    assertThat("Unauthorized Tenant", getTestRoles().contains(vlmEntity.getTenant()));
    vendorLicenseManager.createVendorLicenseModel(vlmEntity);
    verify(vendorLicenseModelDaoMcok).create(vlmEntity);
  }

 @Test
  public void testCreate_withMultitenancyInvalidTenant_Failure() {
    String invalidTenant="invalid_tenant";
    Assert.assertEquals(MULTITENANCY_ENABLED,true);
    VendorLicenseModelEntity vlmEntity = new VendorLicenseModelEntity();
    vlmEntity.setId(vlm1_id);
    vlmEntity.setVersion(VERSION01);
    vlmEntity.setTenant(invalidTenant);
    Assert.assertFalse(getTestRoles().contains(invalidTenant));
    Assert.assertNotNull(vlmEntity.getTenant());
    vendorLicenseManager.createVendorLicenseModel(vlmEntity);
    assertThat("Unauthorized Tenant", !getTestRoles().contains(vlmEntity.getTenant()));
  }

  @Test
  public void testListVLM_multitenancyWithTenant_FilterList() {
    Assert.assertEquals(MULTITENANCY_ENABLED,true);
    Assert.assertNotNull(getTestRoles());
    assertThat("Unauthorized Tenant", getTestRoles().contains(TEST_TENANT));
    String tenant = TEST_TENANT;
    Assert.assertNotNull(tenant);
    List<Item> expectedItems=new ArrayList<>();
      getTestRoles().stream().forEach(role -> getLicenseModelItems().stream()
              .filter(item -> item.getTenant().contains(role))
              .forEach(item -> expectedItems.add(item)));
    Assert.assertEquals(expectedItems.size(), 1);
  }

  @Test
  public void testListVLM_multitenancyWithInvalidTenant_ReturnEmptylist() {
    Assert.assertEquals(MULTITENANCY_ENABLED,true);
    Assert.assertNotNull(getTestRoles());
    String tenant= "invalid_tenant";
    List<Item> expectedItems=new ArrayList<>();
    List<Item> actualItems=getLicenseModelItems();
    Assert.assertNotNull(tenant);
    getTestRoles().stream().forEach(role -> getLicenseModelItems().stream()
            .filter(item -> item.getTenant()!=null)
            .filter(item -> item.getTenant().contains(tenant))
            .forEach(item -> expectedItems.add(item)));

    Assert.assertEquals(expectedItems.size(), 0);
    Assert.assertNotEquals(expectedItems.containsAll(actualItems), actualItems.containsAll(expectedItems));
  }

  private Set<String> getTestRoles(){
    Set<String> roles = new HashSet<>();
    roles.add("test_admin");
    roles.add("test_tenant");
    return roles;
  }

  private List<Item> getLicenseModelItems(){
    List<Item> items=new ArrayList<>();

    Item itemOne = new Item();
    itemOne.setType(ItemType.vlm.name());
    itemOne.setOwner(USER1);
    itemOne.setStatus(ItemStatus.ACTIVE);
    itemOne.setName("TEST_VENDOR_ONE");
    itemOne.setDescription("TEST_DESCRIPTION");
    itemOne.setTenant(TEST_TENANT);

    Item itemTwo = new Item();
    itemTwo.setType(ItemType.vlm.name());
    itemTwo.setOwner(USER1);
    itemTwo.setStatus(ItemStatus.ACTIVE);
    itemTwo.setName("TEST_VENDOR_TWO");
    itemTwo.setDescription("TEST_DESCRIPTION");
    itemTwo.setTenant("admin_tenant");

    items.add(itemOne);
    items.add(itemTwo);
    return items;
  }

}