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