aboutsummaryrefslogtreecommitdiffstats
path: root/openecomp-be/backend/openecomp-sdc-vendor-license-manager/src/test/java/org/openecomp/sdc/vendorlicense/impl/LicenseAgreementTest.java
blob: 4e5045256cac7361b6cf61772c72d95eed4a0d39 (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
/*
 * Copyright © 2016-2018 European Support Limited
 *
 * 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.
 */

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.vendorlicense.VendorLicenseConstants;
import org.openecomp.sdc.vendorlicense.dao.*;
import org.openecomp.sdc.vendorlicense.dao.types.ChoiceOrOther;
import org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupEntity;
import org.openecomp.sdc.vendorlicense.dao.types.LicenseAgreementEntity;
import org.openecomp.sdc.vendorlicense.dao.types.LicenseTerm;
import org.openecomp.sdc.vendorlicense.facade.VendorLicenseFacade;
import org.openecomp.sdc.versioning.VersioningManager;
import org.openecomp.sdc.versioning.dao.types.Version;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

import static org.mockito.Matchers.anyObject;
import static org.mockito.Mockito.*;

public class LicenseAgreementTest {

  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);

  @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;
  }

  public static LicenseAgreementEntity createLicenseAgreement(String vlmId, Version version,
                                                              String id, String name, String desc,
                                                              String requirementsAndConstrains,
                                                              ChoiceOrOther<LicenseTerm> term,
                                                              String... fgIds) {
    LicenseAgreementEntity la = new LicenseAgreementEntity();
    la.setVendorLicenseModelId(vlmId);
    la.setVersion(version);
    la.setId(id);
    la.setName(name);
    la.setDescription(desc);
    la.setLicenseTerm(term);
    la.setRequirementsAndConstrains(requirementsAndConstrains);
    for (String fgId : fgIds) {
      la.getFeatureGroupIds().add(fgId);
    }
    return la;
  }

  public static FeatureGroupEntity createFeatureGroup(String vendorId, Version version, String id,
                                                      String name, String description,
                                                      Set<String> entitlementPoolIds,
                                                      Set<String> licenseKeyGroupIds) {
    FeatureGroupEntity featureGroup = new FeatureGroupEntity();
    featureGroup.setVendorLicenseModelId(vendorId);
    featureGroup.setVersion(version);
    featureGroup.setId(id);
    featureGroup.setName(name);
    featureGroup.setDescription(description);
    featureGroup.setEntitlementPoolIds(entitlementPoolIds);
    featureGroup.setLicenseKeyGroupIds(licenseKeyGroupIds);
    return featureGroup;
  }


  @Test
  public void listLicenseAgreementsTest() {

    LicenseAgreementEntity la =
        new LicenseAgreementEntity(vlm1_id, VERSION01, null); // TODO: 8/13/2017

    doReturn(Arrays.asList(
        createLicenseAgreement(vlm1_id, VERSION01, la1_id, "LA1", "LA1 " +
                "desc", "RequirementsAndConstrains2", new ChoiceOrOther<>(LicenseTerm.Unlimited, null),
            "fg1"),
        createLicenseAgreement(vlm1_id, VERSION01, la2_id, "LA2", "LA2 desc",
            "RequirementsAndConstrains2", new ChoiceOrOther<>(LicenseTerm.Unlimited, null),
            "fg2")))
        .when(licenseAgreementDaoMcok).list(la);

    Collection<LicenseAgreementEntity> LAs =
        vendorLicenseManager.listLicenseAgreements(vlm1_id, VERSION01);
    Assert.assertEquals(LAs.size(), 2);
    LAs.forEach(
        licseAgreement -> Assert.assertTrue(licseAgreement.getId().matches(la1_id + "|" + la2_id)));
  }

  @Test
  public void testListLicenseAgreementsWhenNone() {

    LicenseAgreementEntity la =
        new LicenseAgreementEntity(vlm1_id, VERSION01, null); // TODO: 8/13/2017

    doReturn(new ArrayList<>())
        .when(licenseAgreementDaoMcok).list(la);

    Collection<LicenseAgreementEntity> LAs =
        vendorLicenseManager.listLicenseAgreements(vlm1_id, VERSION01);

    verify(licenseAgreementDaoMcok).list(la);
    Assert.assertEquals(LAs.size(), 0);
  }


  @Test
  public void testCreateLicenseAgreement() {

    LicenseAgreementEntity licenseAgreementEntity = new LicenseAgreementEntity(vlm1_id, VERSION01,
        la2_id);

    doReturn(licenseAgreementEntity).when(vendorLicenseFacadeMcok).createLicenseAgreement
        (licenseAgreementEntity);

    vendorLicenseManager.createLicenseAgreement(licenseAgreementEntity);

    verify(vendorLicenseFacadeMcok).createLicenseAgreement(licenseAgreementEntity);
  }

  @Test
  public void testUpdateLicenseAgreement() {
    LicenseAgreementEntity existingLA = new LicenseAgreementEntity(vlm1_id, VERSION01, la1_id);

    existingLA.setFeatureGroupIds(new HashSet<>());

    doReturn(existingLA).when(licenseAgreementDaoMcok).get(existingLA);

    Set<String> removedFGs = new HashSet<>();
    Set<String> addedFGs = new HashSet<>();
    addedFGs.add(fg1_id);
    addedFGs.add(fg2_id);
    FeatureGroupEntity fg1 = new FeatureGroupEntity(vlm1_id, VERSION01, fg1_id);
    FeatureGroupEntity fg2 = new FeatureGroupEntity(vlm1_id, VERSION01, fg2_id);
    doReturn(fg1).when(featureGroupDaoMcok).get(fg1);
    doReturn(fg2).when(featureGroupDaoMcok).get(fg2);
    doNothing().when(vendorLicenseManager).updateUniqueName(anyObject(), anyObject(), anyObject(),
        anyObject(), anyObject());

    vendorLicenseManager.updateLicenseAgreement(existingLA, addedFGs, removedFGs);

    verify(licenseAgreementDaoMcok)
        .updateColumnsAndDeltaFeatureGroupIds(existingLA, addedFGs, removedFGs);
    verify(vendorLicenseManager).addFeatureGroupsToLicenseAgreementRef(addedFGs, existingLA);
    verify(vendorLicenseManager).removeFeatureGroupsToLicenseAgreementRef(removedFGs, existingLA);

  }

  @Test
  public void deleteLicenseAgreementsTest() {
    LicenseAgreementEntity existingLA = new LicenseAgreementEntity(vlm1_id, VERSION01, la1_id);
    existingLA.setName("LA");
    existingLA.setFeatureGroupIds(new HashSet<>());

    doReturn(existingLA).when(licenseAgreementDaoMcok).get(anyObject());

    doNothing().when(vendorLicenseManager).deleteUniqueName(VendorLicenseConstants.UniqueValues
        .LICENSE_AGREEMENT_NAME, vlm1_id, VERSION01.toString(), existingLA.getName());

    vendorLicenseManager.deleteLicenseAgreement(vlm1_id, VERSION01, la1_id);

    verify(licenseAgreementDaoMcok).delete(existingLA);
    verify(vendorLicenseManager).removeFeatureGroupsToLicenseAgreementRef(existingLA
        .getFeatureGroupIds(), existingLA);
  }

  @Test
  public void testGetLicenseAgreement() {
    vendorLicenseManager.getLicenseAgreementModel(vlm1_id, VERSION01, la1_id);
    verify(vendorLicenseFacadeMcok).getLicenseAgreementModel(vlm1_id, VERSION01, la1_id);
  }

/*
  @Test(dependsOnMethods = {"listLicenseAgreementsTest"})
  public void featureGroupDeletedLicenseAgreementUpdated() {
    LicenseAgreementEntity licenseAgreement =
        createLicenseAgreement(vlm1Id, VERSION01, "laId", "LA2", "LA2 desc",
            "RequirementsAndConstrains2", new ChoiceOrOther<>(LicenseTerm.Unlimited, null), "fg2");
    licenseAgreementDao.create(licenseAgreement);
    String featureGroupId = "FeatureGroupId";
    FeatureGroupEntity created =
        createFeatureGroup(vlm1Id, VERSION01, "fg11", "FG1", "FG1 desc", null, null);
    featureGroupDao.create(created);
    featureGroupDao.addReferencingLicenseAgreement(created, licenseAgreement.getId());

    vendorLicenseManager.deleteFeatureGroup(created);
    LicenseAgreementEntity afterDeletingFG = licenseAgreementDao.get(licenseAgreement);
    Assert.assertEquals(afterDeletingFG.getFeatureGroupIds().size(), 1);
    Assert.assertTrue(afterDeletingFG.getFeatureGroupIds().contains("fg2"));
  }

  */
}