[SDC-29] Amdocs OnBoard 1707 initial commit.
[sdc.git] / openecomp-be / backend / openecomp-sdc-vendor-license-manager / src / test / java / org / openecomp / sdc / vendorlicense / LicenseKeyGroupTest.java
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.nosqldb.api.NoSqlDb;
25 import org.openecomp.core.nosqldb.factory.NoSqlDbFactory;
26 import org.openecomp.core.util.UniqueValueUtil;
27 import org.openecomp.core.utilities.CommonMethods;
28 import org.openecomp.sdc.common.errors.CoreException;
29 import org.openecomp.sdc.vendorlicense.dao.LicenseKeyGroupDao;
30 import org.openecomp.sdc.vendorlicense.dao.LicenseKeyGroupDaoFactory;
31 import org.openecomp.sdc.vendorlicense.dao.types.LicenseKeyGroupEntity;
32 import org.openecomp.sdc.vendorlicense.dao.types.LicenseKeyType;
33 import org.openecomp.sdc.vendorlicense.dao.types.MultiChoiceOrOther;
34 import org.openecomp.sdc.vendorlicense.dao.types.OperationalScope;
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.Collections;
43 import java.util.HashSet;
44 import java.util.Set;
45
46 public class LicenseKeyGroupTest {
47
48   public static final String LKG1_NAME = "LKG1 name";
49   private static final Version VERSION01 = new Version(0, 1);
50   private static final String USER1 = "user1";
51   public static String vlm1Id;
52   public static String vlm2Id;
53   private static VendorLicenseManager vendorLicenseManager = new VendorLicenseManagerImpl();
54   private static LicenseKeyGroupDao licenseKeyGroupDao;
55   private static NoSqlDb noSqlDb;
56   private static String lkg1Id;
57   private static String lkg2Id;
58
59   public static LicenseKeyGroupEntity createLicenseKeyGroup(String vlmId, Version version,
60                                                             String name, String desc,
61                                                             LicenseKeyType type,
62                                                             MultiChoiceOrOther<OperationalScope> operationalScope) {
63     LicenseKeyGroupEntity licenseKeyGroup = new LicenseKeyGroupEntity();
64     licenseKeyGroup.setVendorLicenseModelId(vlmId);
65     licenseKeyGroup.setVersion(version);
66     licenseKeyGroup.setName(name);
67     licenseKeyGroup.setDescription(desc);
68     licenseKeyGroup.setType(type);
69     licenseKeyGroup.setOperationalScope(operationalScope);
70     return licenseKeyGroup;
71   }
72
73   @BeforeClass
74   private void init() {
75     licenseKeyGroupDao = LicenseKeyGroupDaoFactory.getInstance().createInterface();
76     noSqlDb = NoSqlDbFactory.getInstance().createInterface();
77
78     vlm1Id = vendorLicenseManager.createVendorLicenseModel(VendorLicenseModelTest
79         .createVendorLicenseModel("vendor1 name " + CommonMethods.nextUuId(), "vlm1Id dec",
80             "icon1"), USER1).getId();
81     vlm2Id = vendorLicenseManager.createVendorLicenseModel(VendorLicenseModelTest
82             .createVendorLicenseModel("vendor2 name " + CommonMethods.nextUuId(), "vlm2 dec", "icon2"),
83         USER1).getId();
84   }
85
86   @Test
87   public void createTest() {
88     lkg1Id = testCreate(vlm1Id, LKG1_NAME);
89   }
90
91   private String testCreate(String vlmId, String name) {
92     Set<OperationalScope> opScopeChoices = new HashSet<>();
93     opScopeChoices.add(OperationalScope.CPU);
94     opScopeChoices.add(OperationalScope.VM);
95     opScopeChoices.add(OperationalScope.Tenant);
96     opScopeChoices.add(OperationalScope.Data_Center);
97     LicenseKeyGroupEntity
98         lkg1 = createLicenseKeyGroup(vlmId, VERSION01, name, "LKG1 dec", LicenseKeyType.One_Time,
99         new MultiChoiceOrOther<>(opScopeChoices, null));
100     String lkg1Id = vendorLicenseManager.createLicenseKeyGroup(lkg1, USER1).getId();
101     lkg1.setId(lkg1Id);
102
103     LicenseKeyGroupEntity loadedLkg1 = licenseKeyGroupDao.get(lkg1);
104     Assert.assertTrue(loadedLkg1.equals(lkg1));
105     return lkg1Id;
106   }
107
108   @Test(dependsOnMethods = {"createTest"})
109   public void testCreateWithExistingName_negative() {
110     try {
111       LicenseKeyGroupEntity lkg1 =
112           createLicenseKeyGroup(vlm1Id, VERSION01, LKG1_NAME, "LKG1 dec", LicenseKeyType.One_Time,
113               new MultiChoiceOrOther<>(Collections.singleton(OperationalScope.Other),
114                   "other op scope"));
115       vendorLicenseManager.createLicenseKeyGroup(lkg1, USER1).getId();
116       Assert.fail();
117     } catch (CoreException exception) {
118       Assert.assertEquals(exception.code().id(), UniqueValueUtil.UNIQUE_VALUE_VIOLATION);
119     }
120   }
121
122   @Test(dependsOnMethods = {"createTest"})
123   public void testCreateWithExistingNameUnderOtherVlm() {
124     testCreate(vlm2Id, LKG1_NAME);
125   }
126
127   @Test(dependsOnMethods = {"testCreateWithExistingName_negative"})
128   public void updateAndGetTest() {
129     LicenseKeyGroupEntity lkg1 =
130         licenseKeyGroupDao.get(new LicenseKeyGroupEntity(vlm1Id, VERSION01, lkg1Id));
131     Set<OperationalScope> opScopeChoices = new HashSet<>();
132     opScopeChoices.add(OperationalScope.Other);
133     lkg1.setOperationalScope(new MultiChoiceOrOther<>(opScopeChoices, "op scope1 updated"));
134     lkg1.setDescription("LKG1 dec updated");
135
136     vendorLicenseManager.updateLicenseKeyGroup(lkg1, USER1);
137
138     LicenseKeyGroupEntity loadedLkg1 = vendorLicenseManager.getLicenseKeyGroup(lkg1, USER1);
139     Assert.assertTrue(loadedLkg1.equals(lkg1));
140
141   }
142
143   @Test(dependsOnMethods = {"updateAndGetTest"})
144   public void listTest() {
145     Set<OperationalScope> opScopeChoices = new HashSet<>();
146     opScopeChoices.add(OperationalScope.Network_Wide);
147     LicenseKeyGroupEntity lkg2 =
148         createLicenseKeyGroup(vlm1Id, VERSION01, "LKG2", "LKG2 dec", LicenseKeyType.Universal,
149             new MultiChoiceOrOther<>(opScopeChoices, null));
150     lkg2Id = vendorLicenseManager.createLicenseKeyGroup(lkg2, USER1).getId();
151     lkg2.setId(lkg2Id);
152
153     Collection<LicenseKeyGroupEntity> loadedLkgs =
154         vendorLicenseManager.listLicenseKeyGroups(vlm1Id, null, USER1);
155     Assert.assertEquals(loadedLkgs.size(), 2);
156     for (LicenseKeyGroupEntity loadedLkg : loadedLkgs) {
157       if (lkg2Id.equals(loadedLkg.getId())) {
158         Assert.assertTrue(loadedLkg.equals(lkg2));
159       }
160     }
161   }
162
163   @Test(dependsOnMethods = {"listTest"})
164   public void deleteTest() {
165     vendorLicenseManager
166         .deleteLicenseKeyGroup(new LicenseKeyGroupEntity(vlm1Id, VERSION01, lkg1Id), USER1);
167
168     LicenseKeyGroupEntity loadedLkg1 =
169         licenseKeyGroupDao.get(new LicenseKeyGroupEntity(vlm1Id, VERSION01, lkg1Id));
170     Assert.assertEquals(loadedLkg1, null);
171
172     Collection<LicenseKeyGroupEntity> loadedLkgs =
173         licenseKeyGroupDao.list(new LicenseKeyGroupEntity(vlm1Id, VERSION01, null));
174     Assert.assertEquals(loadedLkgs.size(), 1);
175     Assert.assertEquals(loadedLkgs.iterator().next().getId(), lkg2Id);
176   }
177
178   @Test(dependsOnMethods = "deleteTest")
179   public void testCreateWithRemovedName() {
180     testCreate(vlm1Id, LKG1_NAME);
181   }
182 }
183
184 */