8e499264637b0dff7685affe0ae9dcbc166f7222
[sdc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 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 /*
22 package org.openecomp.sdc.vendorlicense;
23
24 import org.openecomp.core.util.UniqueValueUtil;
25 import org.openecomp.core.utilities.CommonMethods;
26 import org.openecomp.sdc.common.errors.CoreException;
27 import org.openecomp.sdc.vendorlicense.dao.FeatureGroupDao;
28 import org.openecomp.sdc.vendorlicense.dao.FeatureGroupDaoFactory;
29 import org.openecomp.sdc.vendorlicense.dao.LicenseAgreementDao;
30 import org.openecomp.sdc.vendorlicense.dao.LicenseAgreementDaoFactory;
31 import org.openecomp.sdc.vendorlicense.dao.types.ChoiceOrOther;
32 import org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupEntity;
33 import org.openecomp.sdc.vendorlicense.dao.types.LicenseAgreementEntity;
34 import org.openecomp.sdc.vendorlicense.dao.types.LicenseTerm;
35 import org.openecomp.sdc.vendorlicense.impl.VendorLicenseManagerImpl;
36 import org.openecomp.sdc.versioning.dao.types.Version;
37 import org.testng.Assert;
38 import org.testng.annotations.BeforeClass;
39 import org.testng.annotations.Test;
40
41 import java.util.Collection;
42 import java.util.HashSet;
43 import java.util.Set;
44
45 public class LicenseAgreementTest {
46   private static final Version VERSION01 = new Version(0, 1);
47   private static final String USER1 = "user1";
48   private static final String LA1_NAME = "LA1 Name";
49
50   private static VendorLicenseManager vendorLicenseManager = new VendorLicenseManagerImpl();
51   private static FeatureGroupDao featureGroupDao;
52   private static LicenseAgreementDao licenseAgreementDao;
53
54   private static String vlm1Id;
55   private static String vlm2Id;
56   private static String la1Id;
57   private static String la2Id;
58
59   public static LicenseAgreementEntity createLicenseAgreement(String vlmId, Version version,
60                                                               String id, String name, String desc,
61                                                               String requirementsAndConstrains,
62                                                               ChoiceOrOther<LicenseTerm> term,
63                                                               String... fgIds) {
64     LicenseAgreementEntity la = new LicenseAgreementEntity();
65     la.setVendorLicenseModelId(vlmId);
66     la.setVersion(version);
67     la.setId(id);
68     la.setName(name);
69     la.setDescription(desc);
70     la.setLicenseTerm(term);
71     la.setRequirementsAndConstrains(requirementsAndConstrains);
72     for (String fgId : fgIds) {
73       la.getFeatureGroupIds().add(fgId);
74     }
75     return la;
76   }
77
78   public static FeatureGroupEntity createFeatureGroup(String vendorId, Version version, String id,
79                                                       String name, String description,
80                                                       Set<String> entitlementPoolIds,
81                                                       Set<String> licenseKeyGroupIds) {
82     FeatureGroupEntity featureGroup = new FeatureGroupEntity();
83     featureGroup.setVendorLicenseModelId(vendorId);
84     featureGroup.setVersion(version);
85     featureGroup.setId(id);
86     featureGroup.setName(name);
87     featureGroup.setDescription(description);
88     featureGroup.setEntitlementPoolIds(entitlementPoolIds);
89     featureGroup.setLicenseKeyGroupIds(licenseKeyGroupIds);
90     return featureGroup;
91   }
92
93   @BeforeClass
94   private void init() {
95     licenseAgreementDao = LicenseAgreementDaoFactory.getInstance().createInterface();
96     featureGroupDao = FeatureGroupDaoFactory.getInstance().createInterface();
97     vlm1Id = vendorLicenseManager.createVendorLicenseModel(VendorLicenseModelTest
98             .createVendorLicenseModel("vendor1 name " + CommonMethods.nextUuId(), "vlm1 dec", "icon1"),
99         USER1).getId();
100     vlm2Id = vendorLicenseManager.createVendorLicenseModel(VendorLicenseModelTest
101             .createVendorLicenseModel("vendor2 name " + CommonMethods.nextUuId(), "vlm2 dec", "icon2"),
102         USER1).getId();
103   }
104
105   @Test
106   public void createLicenseAgreementTest() {
107     la1Id = testCreate(vlm1Id, LA1_NAME);
108   }
109
110   private String testCreate(String vlmId, String name) {
111     FeatureGroupEntity
112         fg1 = createFeatureGroup(vlmId, VERSION01, "fg11", "FG1", "FG1 desc", null, null);
113     featureGroupDao.create(fg1);
114
115     LicenseAgreementEntity la1 = createLicenseAgreement(vlmId, VERSION01, null, name, "LA1 desc",
116         "RequirementsAndConstrains1", new ChoiceOrOther<>(
117             LicenseTerm.Unlimited, null), "fg11");
118     la1 = vendorLicenseManager.createLicenseAgreement(la1, USER1);
119     String la1Id = la1.getId();
120
121     LicenseAgreementEntity loadedLa1 = licenseAgreementDao.get(la1);
122     Assert.assertTrue(loadedLa1.equals(la1));
123     return la1Id;
124   }
125
126   @Test(dependsOnMethods = {"createLicenseAgreementTest"})
127   public void testCreateWithExistingName_negative() {
128     try {
129       LicenseAgreementEntity la1 =
130           createLicenseAgreement(vlm1Id, VERSION01, null, LA1_NAME, "LA1 desc",
131               "RequirementsAndConstrains1", new ChoiceOrOther<>(LicenseTerm.Unlimited, null),
132               "fg11");
133       vendorLicenseManager.createLicenseAgreement(la1, USER1);
134       Assert.fail();
135     } catch (CoreException exception) {
136       Assert.assertEquals(exception.code().id(), UniqueValueUtil.UNIQUE_VALUE_VIOLATION);
137     }
138   }
139
140   @Test(dependsOnMethods = {"createLicenseAgreementTest"})
141   public void testCreateWithExistingNameUnderOtherVlm() {
142     testCreate(vlm2Id, LA1_NAME);
143   }
144
145   @Test(dependsOnMethods = {"testCreateWithExistingName_negative"})
146   public void updateLicenseAgreementTest() {
147     FeatureGroupEntity fg2 =
148         createFeatureGroup(vlm1Id, VERSION01, "fg2", "FG2", "FG2 desc", null, null);
149     featureGroupDao.create(fg2);
150
151     FeatureGroupEntity fg3 =
152         createFeatureGroup(vlm1Id, VERSION01, "fg3", "FG3", "FG3 desc", null, null);
153     featureGroupDao.create(fg3);
154
155     LicenseAgreementEntity la1 =
156         licenseAgreementDao.get(new LicenseAgreementEntity(vlm1Id, VERSION01, la1Id));
157     la1.setDescription("LA1 desc updated");
158     la1.setLicenseTerm(new ChoiceOrOther<>(LicenseTerm.Other, "bla bla term"));
159     la1.getFeatureGroupIds().add("fg2");
160     la1.getFeatureGroupIds().add("fg3");
161     la1.getFeatureGroupIds().remove("fg11");
162
163     Set<String> addedFeatureGroupIds = new HashSet<>();
164     addedFeatureGroupIds.add("fg2");
165     addedFeatureGroupIds.add("fg3");
166
167     Set<String> removedFeatureGroupIds = new HashSet<>();
168     removedFeatureGroupIds.add("fg11");
169
170     vendorLicenseManager
171         .updateLicenseAgreement(la1, addedFeatureGroupIds, removedFeatureGroupIds, USER1);
172
173     LicenseAgreementEntity loadedLa1 =
174         licenseAgreementDao.get(new LicenseAgreementEntity(vlm1Id, VERSION01, la1Id));
175     Assert.assertTrue(loadedLa1.equals(la1));
176
177   }
178
179   @Test(dependsOnMethods = {"updateLicenseAgreementTest"})
180   public void listLicenseAgreementsTest() {
181     LicenseAgreementEntity la2 = createLicenseAgreement(vlm1Id, VERSION01, null, "LA2", "LA2 desc",
182         "RequirementsAndConstrains2", new ChoiceOrOther<>(LicenseTerm.Unlimited, null), "fg2");
183     la2 = vendorLicenseManager.createLicenseAgreement(la2, USER1);
184     la2Id = la2.getId();
185
186     Collection<LicenseAgreementEntity> loadedLas =
187         vendorLicenseManager.listLicenseAgreements(vlm1Id, null, USER1);
188     Assert.assertEquals(loadedLas.size(), 2);
189     boolean la2Exists = false;
190     for (LicenseAgreementEntity loadedLa : loadedLas) {
191       if (la2Id.equals(loadedLa.getId())) {
192         Assert.assertTrue(loadedLa.equals(la2));
193         la2Exists = true;
194       }
195     }
196
197     Assert.assertTrue(la2Exists);
198   }
199
200   @Test(dependsOnMethods = {"listLicenseAgreementsTest"})
201   public void featureGroupDeletedLicenseAgreementUpdated() {
202     LicenseAgreementEntity licenseAgreement =
203         createLicenseAgreement(vlm1Id, VERSION01, "laId", "LA2", "LA2 desc",
204             "RequirementsAndConstrains2", new ChoiceOrOther<>(LicenseTerm.Unlimited, null), "fg2");
205     licenseAgreementDao.create(licenseAgreement);
206     String featureGroupId = "FeatureGroupId";
207     FeatureGroupEntity created =
208         createFeatureGroup(vlm1Id, VERSION01, "fg11", "FG1", "FG1 desc", null, null);
209     featureGroupDao.create(created);
210     featureGroupDao.addReferencingLicenseAgreement(created, licenseAgreement.getId());
211
212     vendorLicenseManager.deleteFeatureGroup(created, USER1);
213     LicenseAgreementEntity afterDeletingFG = licenseAgreementDao.get(licenseAgreement);
214     Assert.assertEquals(afterDeletingFG.getFeatureGroupIds().size(), 1);
215     Assert.assertTrue(afterDeletingFG.getFeatureGroupIds().contains("fg2"));
216   }
217
218   @Test(dependsOnMethods = {"listLicenseAgreementsTest"})
219   public void deleteLicenseAgreementsTest() {
220     vendorLicenseManager.deleteLicenseAgreement(vlm1Id, null, la1Id, USER1);
221
222     LicenseAgreementEntity loadedLa1 =
223         licenseAgreementDao.get(new LicenseAgreementEntity(vlm1Id, VERSION01, la1Id));
224     Assert.assertEquals(loadedLa1, null);
225
226     Collection<LicenseAgreementEntity> loadedLas =
227         licenseAgreementDao.list(new LicenseAgreementEntity(vlm1Id, VERSION01, null));
228     Assert.assertEquals(loadedLas.size(), 1);
229     Assert.assertEquals(loadedLas.iterator().next().getId(), la2Id);
230   }
231
232   @Test(dependsOnMethods = "deleteLicenseAgreementsTest")
233   public void testCreateWithRemovedName() {
234     testCreate(vlm1Id, LA1_NAME);
235   }
236 }
237
238 */