diff options
Diffstat (limited to 'openecomp-be/backend/openecomp-sdc-vendor-license-manager/src/test')
6 files changed, 1123 insertions, 64 deletions
diff --git a/openecomp-be/backend/openecomp-sdc-vendor-license-manager/src/test/java/org/openecomp/sdc/vendorlicense/ArtifactTestUtils.java b/openecomp-be/backend/openecomp-sdc-vendor-license-manager/src/test/java/org/openecomp/sdc/vendorlicense/ArtifactTestUtils.java index 40ea59f543..5d9729de53 100644 --- a/openecomp-be/backend/openecomp-sdc-vendor-license-manager/src/test/java/org/openecomp/sdc/vendorlicense/ArtifactTestUtils.java +++ b/openecomp-be/backend/openecomp-sdc-vendor-license-manager/src/test/java/org/openecomp/sdc/vendorlicense/ArtifactTestUtils.java @@ -7,9 +7,9 @@ * 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. @@ -126,12 +126,25 @@ public class ArtifactTestUtils { protected List featureGroupsforVlm3; protected LicenseAgreementEntity licenseAgreementVlm3; + public enum OnboardingMethod { + HEAT("HEAT"), + Manual("Manual"); + + + OnboardingMethod(String method) { + this.method = method; + } + + private String method; + } + protected static VspDetails createVspDetails(String id, Version version, String name, String desc, String vendorName, String vlm, String icon, String category, String subCategory, String licenseAgreement, - List<String> featureGroups) { + List<String> featureGroups, + String onboardingMethod) { VspDetails vspDetails = new VspDetails(id, version); vspDetails.setName(name); vspDetails.setDescription(desc); @@ -142,6 +155,7 @@ public class ArtifactTestUtils { vspDetails.setVendorId(vlm); vspDetails.setLicenseAgreement(licenseAgreement); vspDetails.setFeatureGroups(featureGroups); + vspDetails.setOnboardingMethod(onboardingMethod); return vspDetails; } @@ -238,7 +252,8 @@ public class ArtifactTestUtils { vendorLicenseFacade.getVersionInfo(vlm1Id, VersionableEntityAction.Read, ""); vspDetails = createVspDetails(null, null, "VSP1_" + CommonMethods.nextUuId(), "Test-vsp", "vendorName", - vlm1Id, "icon", "category", "subCategory", la11Id, fgs); + vlm1Id, "icon", "category", "subCategory", la11Id, fgs, ArtifactTestUtils + .OnboardingMethod.HEAT.name()); List<Version> finalVersions = versionInfo.getFinalVersions(); Version finalVersion = finalVersions.get(1); @@ -266,7 +281,8 @@ public class ArtifactTestUtils { vspDetailsVsp3 = createVspDetails(null, null, "VSP3_" + CommonMethods.nextUuId(), "VSP3", "vendorName", - vlm3Id, "icon", "category", "subCategory", licenceAgreementId, fgs); + vlm3Id, "icon", "category", "subCategory", licenceAgreementId, fgs, OnboardingMethod + .HEAT.name()); VersionInfo versionInfo = vendorLicenseFacade.getVersionInfo(vlmToUse, VersionableEntityAction.Read, ""); @@ -383,7 +399,7 @@ public class ArtifactTestUtils { fgs.add(fg21Id); vsp2 = createVspDetails(null, null, "VSP2_" + CommonMethods.nextUuId(), "Test-vsp", "vendorName", - vlm2Id, "icon", "category", "subCategory", la21Id, fgs); + vlm2Id, "icon", "category", "subCategory", la21Id, fgs, OnboardingMethod.HEAT.name()); vsp2 = vendorSoftwareProductManager.createVsp(vsp2, USER1); } diff --git a/openecomp-be/backend/openecomp-sdc-vendor-license-manager/src/test/java/org/openecomp/sdc/vendorlicense/EntitlementPoolTest.java b/openecomp-be/backend/openecomp-sdc-vendor-license-manager/src/test/java/org/openecomp/sdc/vendorlicense/EntitlementPoolTest.java index 95e0e519e1..9b079de3fa 100644 --- a/openecomp-be/backend/openecomp-sdc-vendor-license-manager/src/test/java/org/openecomp/sdc/vendorlicense/EntitlementPoolTest.java +++ b/openecomp-be/backend/openecomp-sdc-vendor-license-manager/src/test/java/org/openecomp/sdc/vendorlicense/EntitlementPoolTest.java @@ -17,38 +17,387 @@ * limitations under the License. * ============LICENSE_END========================================================= */ -/* + package org.openecomp.sdc.vendorlicense; -import org.openecomp.core.util.UniqueValueUtil; -import org.openecomp.core.utilities.CommonMethods; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.mockito.Spy; import org.openecomp.sdc.common.errors.CoreException; import org.openecomp.sdc.vendorlicense.dao.EntitlementPoolDao; -import org.openecomp.sdc.vendorlicense.dao.EntitlementPoolDaoFactory; -import org.openecomp.sdc.vendorlicense.dao.types.AggregationFunction; -import org.openecomp.sdc.vendorlicense.dao.types.ChoiceOrOther; -import org.openecomp.sdc.vendorlicense.dao.types.EntitlementMetric; -import org.openecomp.sdc.vendorlicense.dao.types.EntitlementPoolEntity; -import org.openecomp.sdc.vendorlicense.dao.types.EntitlementTime; -import org.openecomp.sdc.vendorlicense.dao.types.MultiChoiceOrOther; -import org.openecomp.sdc.vendorlicense.dao.types.OperationalScope; -import org.openecomp.sdc.vendorlicense.dao.types.ThresholdUnit; +import org.openecomp.sdc.vendorlicense.dao.LimitDao; +import org.openecomp.sdc.vendorlicense.dao.types.*; +import org.openecomp.sdc.vendorlicense.errors.VendorLicenseErrorCodes; +import org.openecomp.sdc.vendorlicense.facade.VendorLicenseFacade; import org.openecomp.sdc.vendorlicense.impl.VendorLicenseManagerImpl; import org.openecomp.sdc.versioning.dao.types.Version; import org.openecomp.sdc.versioning.errors.VersioningErrorCodes; +import org.openecomp.sdc.versioning.types.VersionInfo; import org.testng.Assert; -import org.testng.annotations.BeforeClass; +import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; -import java.util.Collection; -import java.util.Collections; +import java.lang.reflect.Field; +import java.lang.reflect.Modifier; +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; +import java.util.ArrayList; import java.util.HashSet; import java.util.Set; +import static org.mockito.Matchers.anyObject; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.verify; + public class EntitlementPoolTest { - private static final String USER1 = "epTestUser1"; + //JUnit Test Cases using Mockito + private final String USER1 = "epTestUser1"; + private final String EP1_NAME = "EP1 name"; + private final String LT1_NAME = "LT1 name"; + + @Mock + private VendorLicenseFacade vendorLicenseFacade; + + @Mock + private EntitlementPoolDao entitlementPoolDao; + @Mock + private LimitDao limitDao; + + @InjectMocks + @Spy + private VendorLicenseManagerImpl vendorLicenseManagerImpl; + + public EntitlementPoolEntity createEntitlementPool(String vlmId, Version version, + String name, String desc, int threshold, + ThresholdUnit thresholdUnit, + EntitlementMetric entitlementMetricChoice, + String entitlementMetricOther, + String increments, + AggregationFunction aggregationFunctionChoice, + String aggregationFunctionOther, + Set<OperationalScope> operationalScopeChoices, + String operationalScopeOther, + EntitlementTime timeChoice, + String timeOther, String sku) { + EntitlementPoolEntity entitlementPool = new EntitlementPoolEntity(); + //entitlementPool.setVendorLicenseModelId(vlmId); + entitlementPool.setVersion(version); + entitlementPool.setName(name); + entitlementPool.setDescription(desc); + entitlementPool.setThresholdValue(threshold); + entitlementPool.setThresholdUnit(thresholdUnit); + entitlementPool + .setEntitlementMetric(new ChoiceOrOther<>(entitlementMetricChoice, entitlementMetricOther)); + entitlementPool.setIncrements(increments); + entitlementPool.setAggregationFunction( + new ChoiceOrOther<>(aggregationFunctionChoice, aggregationFunctionOther)); + entitlementPool.setOperationalScope( + new MultiChoiceOrOther<>(operationalScopeChoices, operationalScopeOther)); + entitlementPool.setTime(new ChoiceOrOther<>(timeChoice, timeOther)); + entitlementPool.setManufacturerReferenceNumber(sku); + return entitlementPool; + } + + @BeforeMethod + public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); + } + + @Test + public void createTest() { + Set<OperationalScope> opScopeChoices; + opScopeChoices = new HashSet<>(); + opScopeChoices.add(OperationalScope.Core); + opScopeChoices.add(OperationalScope.CPU); + opScopeChoices.add(OperationalScope.Network_Wide); + EntitlementPoolEntity ep2 = + createEntitlementPool("vlm1Id", null, EP1_NAME, "EP2 dec", 70, ThresholdUnit.Absolute, + EntitlementMetric.Other, "exception metric2", "inc2", AggregationFunction.Average, null, + opScopeChoices, null, EntitlementTime.Other, "time2", "sku2"); + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy"); + ep2.setStartDate(LocalDate.now().format(formatter)); + ep2.setExpiryDate(LocalDate.now().plusDays(1L).format(formatter)); + + vendorLicenseManagerImpl.createEntitlementPool(ep2, USER1); + } + + @Test + public void createWithInvalidStartExpiryDateTest() { + try { + + Set<OperationalScope> opScopeChoices; + opScopeChoices = new HashSet<>(); + opScopeChoices.add(OperationalScope.Core); + opScopeChoices.add(OperationalScope.CPU); + opScopeChoices.add(OperationalScope.Network_Wide); + EntitlementPoolEntity ep2 = + createEntitlementPool("vlm2Id", null, EP1_NAME, "EP2 dec", 70, ThresholdUnit.Absolute, + EntitlementMetric.Other, "exception metric2", "inc2", AggregationFunction.Average, null, + opScopeChoices, null, EntitlementTime.Other, "time2", "sku2"); + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy"); + ep2.setStartDate(LocalDate.now().format(formatter)); + ep2.setExpiryDate(LocalDate.now().minusDays(2L).format(formatter)); + vendorLicenseManagerImpl.createEntitlementPool(ep2, USER1).getId(); + Assert.fail(); + } catch (CoreException exception) { + Assert.assertEquals(exception.code().id(), VendorLicenseErrorCodes.DATE_RANGE_INVALID); + } + } + + @Test + public void createWithoutStartDateTest() { + try { + + Set<OperationalScope> opScopeChoices; + opScopeChoices = new HashSet<>(); + opScopeChoices.add(OperationalScope.Core); + opScopeChoices.add(OperationalScope.CPU); + opScopeChoices.add(OperationalScope.Network_Wide); + EntitlementPoolEntity ep2 = + createEntitlementPool("vlm3Id", null, EP1_NAME, "EP2 dec", 70, ThresholdUnit.Absolute, + EntitlementMetric.Other, "exception metric2", "inc2", AggregationFunction.Average, null, + opScopeChoices, null, EntitlementTime.Other, "time2", "sku2"); + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy"); + ep2.setExpiryDate(LocalDate.now().plusDays(2L).format(formatter)); + vendorLicenseManagerImpl.createEntitlementPool(ep2, USER1).getId(); + Assert.fail(); + } catch (CoreException exception) { + Assert.assertEquals(exception.code().id(), VendorLicenseErrorCodes.DATE_RANGE_INVALID); + } + } + + @Test + public void createWithSameStartExpiryDateTest() { + try { + + Set<OperationalScope> opScopeChoices; + opScopeChoices = new HashSet<>(); + opScopeChoices.add(OperationalScope.Core); + opScopeChoices.add(OperationalScope.CPU); + opScopeChoices.add(OperationalScope.Network_Wide); + EntitlementPoolEntity ep2 = + createEntitlementPool("vlm4Id", null, EP1_NAME, "EP2 dec", 70, ThresholdUnit.Absolute, + EntitlementMetric.Other, "exception metric2", "inc2", AggregationFunction.Average, null, + opScopeChoices, null, EntitlementTime.Other, "time2", "sku2"); + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy"); + ep2.setStartDate(LocalDate.now().format(formatter)); + ep2.setExpiryDate(LocalDate.now().format(formatter)); + vendorLicenseManagerImpl.createEntitlementPool(ep2, USER1).getId(); + Assert.fail(); + } catch (CoreException exception) { + Assert.assertEquals(exception.code().id(), VendorLicenseErrorCodes.DATE_RANGE_INVALID); + } + } + + @Test + public void createUpdate() { + Set<OperationalScope> opScopeChoices; + opScopeChoices = new HashSet<>(); + opScopeChoices.add(OperationalScope.Core); + opScopeChoices.add(OperationalScope.CPU); + opScopeChoices.add(OperationalScope.Network_Wide); + EntitlementPoolEntity ep2 = + createEntitlementPool("vlm1Id", null, EP1_NAME, "EP2 dec", 70, ThresholdUnit.Absolute, + EntitlementMetric.Other, "exception metric2", "inc2", AggregationFunction.Average, null, + opScopeChoices, null, EntitlementTime.Other, "time2", "sku2"); + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy"); + ep2.setStartDate(LocalDate.now().minusDays(3L).format(formatter)); + ep2.setExpiryDate(LocalDate.now().minusDays(2L).format(formatter)); + VersionInfo info = new VersionInfo(); + Version version = new Version(); + info.getViewableVersions().add(version); + info.setActiveVersion(version); + doReturn(info).when(vendorLicenseFacade).getVersionInfo(anyObject(),anyObject(),anyObject()); + + vendorLicenseManagerImpl.updateEntitlementPool(ep2, USER1); + } + + @Test + public void updateWithInvalidStartExpiryDateTest() { + try { + + Set<OperationalScope> opScopeChoices; + opScopeChoices = new HashSet<>(); + opScopeChoices.add(OperationalScope.Core); + opScopeChoices.add(OperationalScope.CPU); + opScopeChoices.add(OperationalScope.Network_Wide); + EntitlementPoolEntity ep2 = + createEntitlementPool("vlm2Id", null, EP1_NAME, "EP2 dec", 70, ThresholdUnit.Absolute, + EntitlementMetric.Other, "exception metric2", "inc2", AggregationFunction.Average, null, + opScopeChoices, null, EntitlementTime.Other, "time2", "sku2"); + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy"); + ep2.setStartDate(LocalDate.now().format(formatter)); + ep2.setExpiryDate(LocalDate.now().minusDays(2L).format(formatter)); + vendorLicenseManagerImpl.updateEntitlementPool(ep2, USER1); + Assert.fail(); + } catch (CoreException exception) { + Assert.assertEquals(exception.code().id(), VendorLicenseErrorCodes.DATE_RANGE_INVALID); + } + } + + @Test + public void updateWithoutStartDateTest() { + try { + + Set<OperationalScope> opScopeChoices; + opScopeChoices = new HashSet<>(); + opScopeChoices.add(OperationalScope.Core); + opScopeChoices.add(OperationalScope.CPU); + opScopeChoices.add(OperationalScope.Network_Wide); + EntitlementPoolEntity ep2 = + createEntitlementPool("vlm3Id", null, EP1_NAME, "EP2 dec", 70, ThresholdUnit.Absolute, + EntitlementMetric.Other, "exception metric2", "inc2", AggregationFunction.Average, null, + opScopeChoices, null, EntitlementTime.Other, "time2", "sku2"); + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy"); + ep2.setExpiryDate(LocalDate.now().plusDays(2L).format(formatter)); + vendorLicenseManagerImpl.updateEntitlementPool(ep2, USER1); + Assert.fail(); + } catch (CoreException exception) { + Assert.assertEquals(exception.code().id(), VendorLicenseErrorCodes.DATE_RANGE_INVALID); + } + } + + @Test + public void updateWithSameStartExpiryDateTest() { + try { + + Set<OperationalScope> opScopeChoices; + opScopeChoices = new HashSet<>(); + opScopeChoices.add(OperationalScope.Core); + opScopeChoices.add(OperationalScope.CPU); + opScopeChoices.add(OperationalScope.Network_Wide); + EntitlementPoolEntity ep2 = + createEntitlementPool("vlm4Id", null, EP1_NAME, "EP2 dec", 70, ThresholdUnit.Absolute, + EntitlementMetric.Other, "exception metric2", "inc2", AggregationFunction.Average, null, + opScopeChoices, null, EntitlementTime.Other, "time2", "sku2"); + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy"); + ep2.setStartDate(LocalDate.now().format(formatter)); + ep2.setExpiryDate(LocalDate.now().format(formatter)); + vendorLicenseManagerImpl.updateEntitlementPool(ep2, USER1); + Assert.fail(); + } catch (CoreException exception) { + Assert.assertEquals(exception.code().id(), VendorLicenseErrorCodes.DATE_RANGE_INVALID); + } + } + + @Test + public void deleteEntitlementPoolTest() { + Set<OperationalScope> opScopeChoices; + opScopeChoices = new HashSet<>(); + opScopeChoices.add(OperationalScope.Core); + opScopeChoices.add(OperationalScope.CPU); + opScopeChoices.add(OperationalScope.Network_Wide); + + EntitlementPoolEntity entitlementPool = + createEntitlementPool("vlm1Id", null, EP1_NAME, "EP2 dec", 70, ThresholdUnit.Absolute, + EntitlementMetric.Other, "exception metric2", "inc2", AggregationFunction.Average, null, + opScopeChoices, null, EntitlementTime.Other, "time2", "sku2"); + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy"); + entitlementPool.setStartDate(LocalDate.now().format(formatter)); + entitlementPool.setExpiryDate(LocalDate.now().plusDays(1L).format(formatter)); + + VersionInfo info = new VersionInfo(); + Version version = new Version(); + info.getViewableVersions().add(version); + info.setActiveVersion(version); + doReturn(info).when(vendorLicenseFacade).getVersionInfo(anyObject(),anyObject(),anyObject()); + + LimitEntity limitEntity = LimitTest.createLimitEntity(LT1_NAME,LimitType.Vendor,"string",version, + EntitlementMetric.Core,AggregationFunction.Average,10,EntitlementTime.Hour); + + ArrayList<LimitEntity> limitEntityList = new ArrayList(); + limitEntityList.add(limitEntity); + + doReturn(entitlementPool).when(entitlementPoolDao).get(anyObject()); + doReturn(limitEntityList).when(vendorLicenseFacade).listLimits(anyObject(), anyObject(), anyObject(), anyObject()); + doReturn(true).when(limitDao).isLimitPresent(anyObject()); + doReturn(limitEntity).when(limitDao).get(anyObject()); + try { + Field limitField = VendorLicenseManagerImpl.class.getDeclaredField("limitDao"); + limitField.setAccessible(true); + Field modifiersField = Field.class.getDeclaredField("modifiers"); + modifiersField.setAccessible(true); + modifiersField.setInt(limitField, limitField.getModifiers() & ~Modifier.FINAL); + limitField.set(null, limitDao); + + Field epField = VendorLicenseManagerImpl.class.getDeclaredField("entitlementPoolDao"); + epField.setAccessible(true); + modifiersField = Field.class.getDeclaredField("modifiers"); + modifiersField.setAccessible(true); + modifiersField.setInt(epField, epField.getModifiers() & ~Modifier.FINAL); + epField.set(null, entitlementPoolDao); + } catch(NoSuchFieldException | IllegalAccessException e) + { + Assert.fail(); + } + + vendorLicenseManagerImpl.deleteEntitlementPool(entitlementPool, USER1); + + verify(limitDao).delete(anyObject()); + } + + @Test + public void deleteEntitlementPoolInvalidTest() { + try { + Set<OperationalScope> opScopeChoices; + opScopeChoices = new HashSet<>(); + opScopeChoices.add(OperationalScope.Core); + opScopeChoices.add(OperationalScope.CPU); + opScopeChoices.add(OperationalScope.Network_Wide); + + EntitlementPoolEntity entitlementPool = + createEntitlementPool("vlm1Id", null, EP1_NAME, "EP2 dec", 70, ThresholdUnit.Absolute, + EntitlementMetric.Other, "exception metric2", "inc2", AggregationFunction.Average, null, + opScopeChoices, null, EntitlementTime.Other, "time2", "sku2"); + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy"); + entitlementPool.setStartDate(LocalDate.now().format(formatter)); + entitlementPool.setExpiryDate(LocalDate.now().plusDays(1L).format(formatter)); + + VersionInfo info = new VersionInfo(); + Version version = new Version(); + info.getViewableVersions().add(version); + info.setActiveVersion(version); + doReturn(info).when(vendorLicenseFacade).getVersionInfo(anyObject(),anyObject(),anyObject()); + + LimitEntity limitEntity = LimitTest.createLimitEntity(LT1_NAME,LimitType.Vendor,"string",version, + EntitlementMetric.Core,AggregationFunction.Average,10,EntitlementTime.Hour); + + ArrayList<LimitEntity> limitEntityList = new ArrayList(); + limitEntityList.add(limitEntity); + + doReturn(entitlementPool).when(entitlementPoolDao).get(anyObject()); + doReturn(limitEntityList).when(vendorLicenseFacade).listLimits(anyObject(), anyObject(), anyObject(), anyObject()); + doReturn(false).when(limitDao).isLimitPresent(anyObject()); + + try { + Field limitField = VendorLicenseManagerImpl.class.getDeclaredField("limitDao"); + limitField.setAccessible(true); + Field modifiersField = Field.class.getDeclaredField("modifiers"); + modifiersField.setAccessible(true); + modifiersField.setInt(limitField, limitField.getModifiers() & ~Modifier.FINAL); + limitField.set(null, limitDao); + + Field epField = VendorLicenseManagerImpl.class.getDeclaredField("entitlementPoolDao"); + epField.setAccessible(true); + modifiersField = Field.class.getDeclaredField("modifiers"); + modifiersField.setAccessible(true); + modifiersField.setInt(epField, epField.getModifiers() & ~Modifier.FINAL); + epField.set(null, entitlementPoolDao); + } catch(NoSuchFieldException | IllegalAccessException e) + { + Assert.fail(); + } + + vendorLicenseManagerImpl.deleteEntitlementPool(entitlementPool, USER1); + } catch (CoreException exception) { + Assert.assertEquals(exception.code().id(), VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND); + } + } + + /* private static final String USER1 = "epTestUser1"; private static final String USER2 = "epTestUser2"; private static final String EP1_V01_DESC = "EP1 desc"; private static final Version VERSION01 = new Version(0, 1); @@ -91,7 +440,6 @@ public class EntitlementPoolTest { entitlementPool.setOperationalScope( new MultiChoiceOrOther<>(operationalScopeChoices, operationalScopeOther)); entitlementPool.setTime(new ChoiceOrOther<>(timeChoice, timeOther)); - entitlementPool.setManufacturerReferenceNumber(sku); return entitlementPool; } @@ -109,8 +457,6 @@ public class EntitlementPoolTest { Assert.assertEquals(actual.getAggregationFunction(), expected.getAggregationFunction()); Assert.assertEquals(actual.getOperationalScope(), expected.getOperationalScope()); Assert.assertEquals(actual.getTime(), expected.getTime()); - Assert.assertEquals(actual.getManufacturerReferenceNumber(), - expected.getManufacturerReferenceNumber()); } @BeforeClass @@ -294,6 +640,6 @@ public class EntitlementPoolTest { Assert.assertEquals(exception.code().id(), UniqueValueUtil.UNIQUE_VALUE_VIOLATION); } } - -} */ +} + diff --git a/openecomp-be/backend/openecomp-sdc-vendor-license-manager/src/test/java/org/openecomp/sdc/vendorlicense/FeatureGroupTest.java b/openecomp-be/backend/openecomp-sdc-vendor-license-manager/src/test/java/org/openecomp/sdc/vendorlicense/FeatureGroupTest.java index e41cfa9532..41c4678b01 100644 --- a/openecomp-be/backend/openecomp-sdc-vendor-license-manager/src/test/java/org/openecomp/sdc/vendorlicense/FeatureGroupTest.java +++ b/openecomp-be/backend/openecomp-sdc-vendor-license-manager/src/test/java/org/openecomp/sdc/vendorlicense/FeatureGroupTest.java @@ -17,44 +17,142 @@ * limitations under the License. * ============LICENSE_END========================================================= */ -/* + package org.openecomp.sdc.vendorlicense; -import org.openecomp.core.util.UniqueValueUtil; -import org.openecomp.core.utilities.CommonMethods; -import org.openecomp.sdc.common.errors.CoreException; -import org.openecomp.sdc.vendorlicense.dao.types.AggregationFunction; -import org.openecomp.sdc.vendorlicense.dao.types.EntitlementMetric; -import org.openecomp.sdc.vendorlicense.dao.types.EntitlementPoolEntity; -import org.openecomp.sdc.vendorlicense.dao.types.EntitlementTime; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.mockito.Spy; +import org.openecomp.sdc.vendorlicense.dao.*; import org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupEntity; -import org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupModel; -import org.openecomp.sdc.vendorlicense.dao.types.LicenseKeyGroupEntity; -import org.openecomp.sdc.vendorlicense.dao.types.LicenseKeyType; -import org.openecomp.sdc.vendorlicense.dao.types.MultiChoiceOrOther; -import org.openecomp.sdc.vendorlicense.dao.types.OperationalScope; -import org.openecomp.sdc.vendorlicense.dao.types.ThresholdUnit; import org.openecomp.sdc.vendorlicense.facade.VendorLicenseFacade; -import org.openecomp.sdc.vendorlicense.facade.VendorLicenseFacadeFactory; +import org.openecomp.sdc.vendorlicense.facade.impl.VendorLicenseFacadeImpl; import org.openecomp.sdc.vendorlicense.impl.VendorLicenseManagerImpl; +import org.openecomp.sdc.versioning.VersioningManager; import org.openecomp.sdc.versioning.dao.types.Version; import org.testng.Assert; +import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; import java.util.HashSet; -import java.util.List; import java.util.Set; -*/ +import static org.mockito.Matchers.anyObject; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; + /** * Created by KATYR on 4/10/2016 - *//* + */ public class FeatureGroupTest { + //JUnit Test Cases using Mockito + private static final Version VERSION01 = new Version(0, 1); + private final String FG1_NAME = "FG1 name"; + + @Mock + private VendorLicenseModelDao vendorLicenseModelDao; + + @Mock + private LicenseAgreementDao licenseAgreementDao; + + @Mock + private FeatureGroupDao featureGroupDao; + + @Mock + private EntitlementPoolDao entitlementPoolDao; + + @Mock + private LicenseKeyGroupDao licenseKeyGroupDao; + + @Mock + private VersioningManager versioningManager; + + @InjectMocks + @Spy + private VendorLicenseManagerImpl vendorLicenseManagerImpl; + + public FeatureGroupEntity updateFeatureGroup(String vlmId, Version version, String id, String name, String desc, + String partNumber, String manufacturerReferenceNumber, Set<String> + licenseKeyGroupIds, Set<String> entitlementPoolIds, Set<String> + referencingLicenseAgreements){ + FeatureGroupEntity featureGroup = new FeatureGroupEntity(vlmId, version, id); + featureGroup.setVendorLicenseModelId(vlmId); + featureGroup.setVersion(version); + featureGroup.setId(id); + featureGroup.setName(name); + featureGroup.setDescription(desc); + featureGroup.setPartNumber(partNumber); + //featureGroup.setManufacturerReferenceNumber(manufacturerReferenceNumber); + featureGroup.setLicenseKeyGroupIds(licenseKeyGroupIds); + featureGroup.setEntitlementPoolIds(entitlementPoolIds); + featureGroup.setReferencingLicenseAgreements(referencingLicenseAgreements); + + return featureGroup; + } + + @BeforeMethod + public void setUp() throws Exception{ + MockitoAnnotations.initMocks(this); + } + + @Test + public void testUpdate(){ + Set<String> licenseKeyGroupIds; + licenseKeyGroupIds = new HashSet<>(); + licenseKeyGroupIds.add("lkg1"); + + Set<String> entitlementPoolIds; + entitlementPoolIds = new HashSet<>(); + entitlementPoolIds.add("ep1"); + + Set<String> referencingLicenseAgreements; + referencingLicenseAgreements = new HashSet<>(); + referencingLicenseAgreements.add("la1"); + + FeatureGroupEntity featureGroupEntity = updateFeatureGroup("vlmId", VERSION01, "fgId", FG1_NAME, "fg1 desc", + "partNumber", "MRN", licenseKeyGroupIds, entitlementPoolIds, + referencingLicenseAgreements); + + doReturn(featureGroupEntity).when(featureGroupDao).get(anyObject()); + + /*if(featureGroupEntity.getManufacturerReferenceNumber() != null) + featureGroupDao.update(featureGroupEntity); + verify(featureGroupDao).update(anyObject());*/ + } + + @Test + public void testUpdateWithoutManufacturingReferenceNumber(){ + Set<String> licenseKeyGroupIds; + licenseKeyGroupIds = new HashSet<>(); + licenseKeyGroupIds.add("lkg1"); + + Set<String> entitlementPoolIds; + entitlementPoolIds = new HashSet<>(); + entitlementPoolIds.add("ep1"); + + Set<String> referencingLicenseAgreements; + referencingLicenseAgreements = new HashSet<>(); + referencingLicenseAgreements.add("la1"); + + FeatureGroupEntity featureGroupEntity = updateFeatureGroup("vlmId", VERSION01, "fgId", FG1_NAME, "fg1 desc", + "partNumber", null, licenseKeyGroupIds, entitlementPoolIds, + referencingLicenseAgreements); + + doReturn(featureGroupEntity).when(featureGroupDao).get(anyObject()); + + /*if(featureGroupEntity.getManufacturerReferenceNumber() != null) + featureGroupDao.update(featureGroupEntity); + verify(featureGroupDao, never()).update(anyObject());*/ + } + + +} + +/* protected static final Version VERSION01 = new Version(0, 1); protected static final String USER1 = "FeatureGroupTest_User1"; protected static VendorLicenseManager vendorLicenseManager = new VendorLicenseManagerImpl(); diff --git a/openecomp-be/backend/openecomp-sdc-vendor-license-manager/src/test/java/org/openecomp/sdc/vendorlicense/LicenseKeyGroupTest.java b/openecomp-be/backend/openecomp-sdc-vendor-license-manager/src/test/java/org/openecomp/sdc/vendorlicense/LicenseKeyGroupTest.java index a12d9fcecc..782d93a885 100644 --- a/openecomp-be/backend/openecomp-sdc-vendor-license-manager/src/test/java/org/openecomp/sdc/vendorlicense/LicenseKeyGroupTest.java +++ b/openecomp-be/backend/openecomp-sdc-vendor-license-manager/src/test/java/org/openecomp/sdc/vendorlicense/LicenseKeyGroupTest.java @@ -16,36 +16,177 @@ * See the License for the specific language governing permissions and * limitations under the License. * ============LICENSE_END========================================================= - *//* + */ package org.openecomp.sdc.vendorlicense; -import org.openecomp.core.nosqldb.api.NoSqlDb; -import org.openecomp.core.nosqldb.factory.NoSqlDbFactory; -import org.openecomp.core.util.UniqueValueUtil; -import org.openecomp.core.utilities.CommonMethods; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.mockito.Spy; import org.openecomp.sdc.common.errors.CoreException; import org.openecomp.sdc.vendorlicense.dao.LicenseKeyGroupDao; -import org.openecomp.sdc.vendorlicense.dao.LicenseKeyGroupDaoFactory; -import org.openecomp.sdc.vendorlicense.dao.types.LicenseKeyGroupEntity; -import org.openecomp.sdc.vendorlicense.dao.types.LicenseKeyType; -import org.openecomp.sdc.vendorlicense.dao.types.MultiChoiceOrOther; -import org.openecomp.sdc.vendorlicense.dao.types.OperationalScope; +import org.openecomp.sdc.vendorlicense.dao.LimitDao; +import org.openecomp.sdc.vendorlicense.dao.types.*; +import org.openecomp.sdc.vendorlicense.facade.VendorLicenseFacade; import org.openecomp.sdc.vendorlicense.impl.VendorLicenseManagerImpl; import org.openecomp.sdc.versioning.dao.types.Version; +import org.openecomp.sdc.versioning.errors.VersioningErrorCodes; +import org.openecomp.sdc.versioning.types.VersionInfo; import org.testng.Assert; -import org.testng.annotations.BeforeClass; +import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; -import java.util.Collection; -import java.util.Collections; +import java.lang.reflect.Field; +import java.lang.reflect.Modifier; +import java.util.ArrayList; import java.util.HashSet; import java.util.Set; +import static org.mockito.Matchers.anyObject; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.verify; + public class LicenseKeyGroupTest { - public static final String LKG1_NAME = "LKG1 name"; + //JUnit Test Cases using Mockito + private final String USER = "lkgTestUser"; + private final String LKG_NAME = "LKG name"; + private final String LT_NAME = "LT name"; + + @Mock + private VendorLicenseFacade vendorLicenseFacade; + + @Mock + private LicenseKeyGroupDao licenseKeyGroupDao; + @Mock + private LimitDao limitDao; + + @InjectMocks + @Spy + private VendorLicenseManagerImpl vendorLicenseManagerImpl; + + @BeforeMethod + public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); + } + + private LicenseKeyGroupEntity createLicenseKeyGroup(LicenseKeyType type, Set<OperationalScope> operationalScopeChoices, + String operationalScopeOther) + { + LicenseKeyGroupEntity licenseKeyGroupEntity = new LicenseKeyGroupEntity(); + licenseKeyGroupEntity.setType(type); + licenseKeyGroupEntity.setOperationalScope( + new MultiChoiceOrOther<>(operationalScopeChoices, operationalScopeOther)); + return licenseKeyGroupEntity; + } + + @Test + public void deleteLicenseKeyGroupTest() { + Set<OperationalScope> opScopeChoices; + opScopeChoices = new HashSet<>(); + opScopeChoices.add(OperationalScope.Core); + opScopeChoices.add(OperationalScope.CPU); + opScopeChoices.add(OperationalScope.Network_Wide); + + LicenseKeyGroupEntity licenseKeyGroup = + createLicenseKeyGroup(LicenseKeyType.Unique, opScopeChoices, null); + + VersionInfo info = new VersionInfo(); + Version version = new Version(); + info.getViewableVersions().add(version); + info.setActiveVersion(version); + doReturn(info).when(vendorLicenseFacade).getVersionInfo(anyObject(),anyObject(),anyObject()); + + LimitEntity limitEntity = LimitTest.createLimitEntity(LT_NAME,LimitType.Vendor,"string",version, + EntitlementMetric.Core,AggregationFunction.Average,10,EntitlementTime.Hour); + + ArrayList<LimitEntity> limitEntityList = new ArrayList(); + limitEntityList.add(limitEntity); + + doReturn(licenseKeyGroup).when(licenseKeyGroupDao).get(anyObject()); + doReturn(limitEntityList).when(vendorLicenseFacade).listLimits(anyObject(), anyObject(), anyObject(), anyObject()); + doReturn(true).when(limitDao).isLimitPresent(anyObject()); + doReturn(limitEntity).when(limitDao).get(anyObject()); + try { + Field limitField = VendorLicenseManagerImpl.class.getDeclaredField("limitDao"); + limitField.setAccessible(true); + Field modifiersField = Field.class.getDeclaredField("modifiers"); + modifiersField.setAccessible(true); + modifiersField.setInt(limitField, limitField.getModifiers() & ~Modifier.FINAL); + limitField.set(null, limitDao); + + Field lkgField = VendorLicenseManagerImpl.class.getDeclaredField("licenseKeyGroupDao"); + lkgField.setAccessible(true); + modifiersField = Field.class.getDeclaredField("modifiers"); + modifiersField.setAccessible(true); + modifiersField.setInt(lkgField, lkgField.getModifiers() & ~Modifier.FINAL); + lkgField.set(null, licenseKeyGroupDao); + } catch(NoSuchFieldException | IllegalAccessException e) + { + Assert.fail(); + } + + vendorLicenseManagerImpl.deleteLicenseKeyGroup(licenseKeyGroup, USER); + + verify(limitDao).delete(anyObject()); + } + + @Test + public void deleteLicenseKeyGroupInvalidTest() { + try { + Set<OperationalScope> opScopeChoices; + opScopeChoices = new HashSet<>(); + opScopeChoices.add(OperationalScope.Core); + opScopeChoices.add(OperationalScope.CPU); + opScopeChoices.add(OperationalScope.Network_Wide); + + LicenseKeyGroupEntity licenseKeyGroup = + createLicenseKeyGroup(LicenseKeyType.Unique, opScopeChoices, null); + + VersionInfo info = new VersionInfo(); + Version version = new Version(); + info.getViewableVersions().add(version); + info.setActiveVersion(version); + doReturn(info).when(vendorLicenseFacade).getVersionInfo(anyObject(),anyObject(),anyObject()); + + LimitEntity limitEntity = LimitTest.createLimitEntity(LT_NAME,LimitType.Vendor,"string",version, + EntitlementMetric.Core,AggregationFunction.Average,10,EntitlementTime.Hour); + + ArrayList<LimitEntity> limitEntityList = new ArrayList(); + limitEntityList.add(limitEntity); + + doReturn(licenseKeyGroup).when(licenseKeyGroupDao).get(anyObject()); + doReturn(limitEntityList).when(vendorLicenseFacade).listLimits(anyObject(), anyObject(), anyObject(), anyObject()); + doReturn(false).when(limitDao).isLimitPresent(anyObject()); + + try { + Field limitField = VendorLicenseManagerImpl.class.getDeclaredField("limitDao"); + limitField.setAccessible(true); + Field modifiersField = Field.class.getDeclaredField("modifiers"); + modifiersField.setAccessible(true); + modifiersField.setInt(limitField, limitField.getModifiers() & ~Modifier.FINAL); + limitField.set(null, limitDao); + + Field lkgField = VendorLicenseManagerImpl.class.getDeclaredField("licenseKeyGroupDao"); + lkgField.setAccessible(true); + modifiersField = Field.class.getDeclaredField("modifiers"); + modifiersField.setAccessible(true); + modifiersField.setInt(lkgField, lkgField.getModifiers() & ~Modifier.FINAL); + lkgField.set(null, licenseKeyGroupDao); + } catch(NoSuchFieldException | IllegalAccessException e) + { + Assert.fail(); + } + + vendorLicenseManagerImpl.deleteLicenseKeyGroup(licenseKeyGroup, USER); + } catch (CoreException exception) { + Assert.assertEquals(exception.code().id(), VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND); + } + } + + /*public static final String LKG1_NAME = "LKG1 name"; private static final Version VERSION01 = new Version(0, 1); private static final String USER1 = "user1"; public static String vlm1Id; @@ -179,6 +320,5 @@ public class LicenseKeyGroupTest { public void testCreateWithRemovedName() { testCreate(vlm1Id, LKG1_NAME); } + */ } - -*/ diff --git a/openecomp-be/backend/openecomp-sdc-vendor-license-manager/src/test/java/org/openecomp/sdc/vendorlicense/LimitTest.java b/openecomp-be/backend/openecomp-sdc-vendor-license-manager/src/test/java/org/openecomp/sdc/vendorlicense/LimitTest.java new file mode 100644 index 0000000000..a8d1ed9f65 --- /dev/null +++ b/openecomp-be/backend/openecomp-sdc-vendor-license-manager/src/test/java/org/openecomp/sdc/vendorlicense/LimitTest.java @@ -0,0 +1,332 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 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; + +import static org.mockito.Matchers.anyObject; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.verify; + +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.mockito.Spy; +import org.openecomp.sdc.common.errors.CoreException; +import org.openecomp.sdc.vendorlicense.dao.LimitDao; +import org.openecomp.sdc.vendorlicense.dao.types.AggregationFunction; +import org.openecomp.sdc.vendorlicense.dao.types.EntitlementMetric; +import org.openecomp.sdc.vendorlicense.dao.types.EntitlementTime; +import org.openecomp.sdc.vendorlicense.dao.types.LimitEntity; +import org.openecomp.sdc.vendorlicense.dao.types.LimitType; +import org.openecomp.sdc.vendorlicense.errors.VendorLicenseErrorCodes; +import org.openecomp.sdc.vendorlicense.facade.VendorLicenseFacade; +import org.openecomp.sdc.vendorlicense.impl.VendorLicenseManagerImpl; +import org.openecomp.sdc.versioning.dao.types.Version; +import org.openecomp.sdc.versioning.errors.VersioningErrorCodes; +import org.openecomp.sdc.versioning.types.VersionInfo; +import org.testng.Assert; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; + +import java.lang.reflect.Field; +import java.lang.reflect.Modifier; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.List; + +import static org.mockito.Mockito.when; + +public class LimitTest { + + private final String USER1 = "limitTestUser1"; + private final String LT1_NAME = "LT1 name"; + + private static final String VLM_ID = "VLM_ID"; + private static final Version VERSION = new Version(0, 1); + private static final String EPLKG_ID = "ID"; + private static final String LIMIT1_ID = "limit1"; + private static final String LIMIT2_ID = "limit2"; + + @Mock + private VendorLicenseFacade vendorLicenseFacade; + + @Mock + private LimitDao limitDao; + + @InjectMocks + @Spy + private VendorLicenseManagerImpl vendorLicenseManagerImpl; + + public static LimitEntity createLimitEntity(String name, LimitType type, String description, + Version version, EntitlementMetric metric, + AggregationFunction aggregationFunction, int unit, + EntitlementTime time) { + LimitEntity limitEntity = new LimitEntity(); + limitEntity.setName(name); + limitEntity.setType(type); + limitEntity.setDescription(description); + limitEntity.setVersion(version); + limitEntity.setMetric(metric); + limitEntity.setAggregationFunction(aggregationFunction); + limitEntity.setUnit(unit); + limitEntity.setTime(time); + return limitEntity; + } + + @BeforeMethod + public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); + try { + Field limitField = VendorLicenseManagerImpl.class.getDeclaredField("limitDao"); + limitField.setAccessible(true); + Field modifiersField = Field.class.getDeclaredField("modifiers"); + modifiersField.setAccessible(true); + modifiersField.setInt(limitField, limitField.getModifiers() & ~Modifier.FINAL); + limitField.set(null, limitDao); + } catch(NoSuchFieldException | IllegalAccessException e) + { + Assert.fail(); + } + } + + @Test + public void testUpdateLimit() { + Version version = new Version(); + LimitEntity limitEntity1 = createLimitEntity(LT1_NAME,LimitType.Vendor,"string",version, + EntitlementMetric.Core,AggregationFunction.Average,10,EntitlementTime.Hour); + LimitEntity limitEntity2 = createLimitEntity(LT1_NAME,LimitType.Vendor,"string",version, + EntitlementMetric.Tokens,AggregationFunction.Peak,12,EntitlementTime.Month); + VersionInfo info = new VersionInfo(); + info.getViewableVersions().add(version); + info.setActiveVersion(version); + + doReturn(info).when(vendorLicenseFacade).getVersionInfo(anyObject(),anyObject(),anyObject()); + doReturn(true).when(limitDao).isLimitPresent(anyObject()); + doReturn(limitEntity1).when(limitDao).get(anyObject()); + + List<LimitEntity> limitEntityList = new ArrayList<>(); + limitEntityList.add(limitEntity1); + limitEntityList.add(limitEntity2); + limitEntity1.setId("1234"); + limitEntity2.setId("1234"); + doReturn(limitEntityList).when(vendorLicenseFacade).listLimits(anyObject(),anyObject(), + anyObject(),anyObject()); + + vendorLicenseManagerImpl.updateLimit(limitEntity2,USER1); + + verify(vendorLicenseFacade).updateLimit(anyObject(), anyObject()); + } + + @Test + public void testUpdateLimitErrorWithSameNameType() { + try { + Version version = new Version(); + LimitEntity limitEntity1 = createLimitEntity(LT1_NAME,LimitType.Vendor,"string",version, + EntitlementMetric.Core,AggregationFunction.Average,10,EntitlementTime.Hour); + LimitEntity limitEntity2 = createLimitEntity(LT1_NAME,LimitType.Vendor,"string",version, + EntitlementMetric.Tokens,AggregationFunction.Peak,12,EntitlementTime.Month); + VersionInfo info = new VersionInfo(); + info.getViewableVersions().add(version); + info.setActiveVersion(version); + + doReturn(info).when(vendorLicenseFacade).getVersionInfo(anyObject(),anyObject(),anyObject()); + doReturn(limitEntity1).when(limitDao).get(anyObject()); + + List<LimitEntity> limitEntityList = new ArrayList<>(); + limitEntityList.add(limitEntity1); + limitEntityList.add(limitEntity2); + limitEntity1.setId("1234"); + limitEntity2.setId("9632"); + doReturn(limitEntityList).when(vendorLicenseFacade).listLimits(anyObject(),anyObject(), + anyObject(),anyObject()); + + vendorLicenseManagerImpl.updateLimit(limitEntity2,USER1); + Assert.fail(); + } catch (CoreException exception) { + Assert.assertEquals(exception.code().id(), + VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND); + } + } + + @Test + public void testDeleteLimit() { + Version version = new Version(); + LimitEntity limitEntity = createLimitEntity(LT1_NAME,LimitType.Vendor,"string",version, + EntitlementMetric.Core,AggregationFunction.Average,10,EntitlementTime.Hour); + VersionInfo info = new VersionInfo(); + info.getViewableVersions().add(version); + info.setActiveVersion(version); + + doReturn(info).when(vendorLicenseFacade).getVersionInfo(anyObject(),anyObject(),anyObject()); + doReturn(true).when(limitDao).isLimitPresent(anyObject()); + doReturn(limitEntity).when(limitDao).get(anyObject()); + + List<LimitEntity> limitEntityList = new ArrayList<>(); + limitEntityList.add(limitEntity); + limitEntity.setId("1234"); + + vendorLicenseManagerImpl.deleteLimit(limitEntity,LT1_NAME); + + verify(vendorLicenseManagerImpl).deleteLimit(anyObject(), anyObject()); + } + + @Test + public void testUpdateLimitErrorWithInvalidId() { + try { + Version version = new Version(); + LimitEntity limitEntity1 = createLimitEntity(LT1_NAME,LimitType.Vendor,"string",version, + EntitlementMetric.Core,AggregationFunction.Average,10,EntitlementTime.Hour); + LimitEntity limitEntity2 = createLimitEntity(LT1_NAME,LimitType.Vendor,"string",version, + EntitlementMetric.Tokens,AggregationFunction.Peak,12,EntitlementTime.Month); + VersionInfo info = new VersionInfo(); + info.getViewableVersions().add(version); + info.setActiveVersion(version); + + doReturn(info).when(vendorLicenseFacade).getVersionInfo(anyObject(),anyObject(),anyObject()); + doReturn(null).when(limitDao).get(anyObject()); + + vendorLicenseManagerImpl.updateLimit(limitEntity2,USER1); + Assert.fail(); + } catch (CoreException exception) { + Assert.assertEquals(exception.code().id(), + VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND); + } + } + + @Test + public void testList() { + doReturn(Arrays.asList( + createLimit(VLM_ID, VERSION, EPLKG_ID, LIMIT1_ID), + createLimit(VLM_ID, VERSION, EPLKG_ID, LIMIT2_ID))) + .when(vendorLicenseFacade).listLimits(VLM_ID, VERSION, EPLKG_ID, USER1); + + final Collection<LimitEntity> limits = + vendorLicenseManagerImpl.listLimits(VLM_ID, VERSION, EPLKG_ID, USER1); + Assert.assertEquals(limits.size(), 2); + for (LimitEntity limit : limits) { + Assert.assertEquals(limit.getName(), + LIMIT1_ID.equals(limit.getId()) ? LIMIT1_ID+" name" : LIMIT2_ID+" name" ); + } + } + + @Test + public void testCreateLimit() { + LimitEntity expected = createLimit(VLM_ID, VERSION, EPLKG_ID, LIMIT1_ID); + VersionInfo info = new VersionInfo(); + info.getViewableVersions().add(VERSION); + info.setActiveVersion(VERSION); + + doReturn(info).when(vendorLicenseFacade).getVersionInfo(anyObject(),anyObject(),anyObject()); + + vendorLicenseManagerImpl.createLimit(expected, USER1); + verify(vendorLicenseFacade).createLimit(expected,USER1); + } + + @Test + public void testCreateWithDuplicateName() { + LimitEntity expected = createLimit(VLM_ID, VERSION, EPLKG_ID, LIMIT1_ID); + expected.setType(LimitType.Vendor); + + LimitEntity expectedDiffName = createLimit(VLM_ID, VERSION, EPLKG_ID, LIMIT2_ID); + expectedDiffName.setName(LIMIT1_ID + " name"); + expectedDiffName.setType(LimitType.Vendor); + + List<LimitEntity> vfcImageList = new ArrayList<LimitEntity>(); + vfcImageList.add(expectedDiffName); + doReturn(vfcImageList).when(vendorLicenseFacade).listLimits(anyObject(), anyObject(), anyObject(), + anyObject()); + + VersionInfo info = new VersionInfo(); + info.getViewableVersions().add(VERSION); + info.setActiveVersion(VERSION); + + doReturn(info).when(vendorLicenseFacade).getVersionInfo(anyObject(),anyObject(),anyObject()); + + try { + vendorLicenseManagerImpl.createLimit(expected, USER1); + Assert.fail(); + } + catch (CoreException ex) { + Assert.assertEquals(ex.code().id(), + VendorLicenseErrorCodes.DUPLICATE_LIMIT_NAME_NOT_ALLOWED); + } + } + + @Test + public void testGetNonExistingLimitId_negative() { + LimitEntity limit = createLimit(VLM_ID, VERSION, EPLKG_ID, "non existing limit id"); + VersionInfo info = new VersionInfo(); + info.getViewableVersions().add(VERSION); + info.setActiveVersion(VERSION); + + doReturn(info).when(vendorLicenseFacade).getVersionInfo(anyObject(),anyObject(),anyObject()); + + try { + vendorLicenseManagerImpl.getLimit(limit , USER1); + Assert.fail(); + } catch (CoreException exception) { + Assert.assertEquals(exception.code().id(), VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND); + } + } + + @Test + public void testGet() { + LimitEntity expected = createLimit(VLM_ID, VERSION, EPLKG_ID, LIMIT1_ID); + expected.setType(LimitType.Vendor); + expected.setValue(100); + expected.setUnit(10); + expected.setAggregationFunction(AggregationFunction.Average); + expected.setMetric(EntitlementMetric.CPU); + expected.setTime(EntitlementTime.Day); + + doReturn(true).when(limitDao).isLimitPresent(anyObject()); + doReturn(expected).when(limitDao).get(anyObject()); + VersionInfo info = new VersionInfo(); + info.getViewableVersions().add(VERSION); + info.setActiveVersion(VERSION); + + doReturn(info).when(vendorLicenseFacade).getVersionInfo(anyObject(),anyObject(),anyObject()); + + LimitEntity actual = createLimit(VLM_ID, VERSION, EPLKG_ID, LIMIT1_ID); + vendorLicenseManagerImpl.getLimit(actual, USER1); + Assert.assertEquals(actual.getId(), expected.getId()); + Assert.assertEquals(actual.getName(), expected.getName()); + Assert.assertEquals(actual.getUnit(), expected.getUnit()); + Assert.assertEquals(actual.getValue(), expected.getValue()); + Assert.assertEquals(actual.getAggregationFunction().name(), expected.getAggregationFunction() + .name()); + Assert.assertEquals(actual.getMetric().name(), expected.getMetric().name()); + + } + + static LimitEntity createLimit(String vlmId, Version version, String epLkgId, String limitId) { + LimitEntity limitEntity = new LimitEntity(vlmId, version, epLkgId, limitId); + limitEntity.setName(limitId + " name"); + limitEntity.setDescription(limitId + " desc"); + limitEntity.setVersion(version); + limitEntity.setMetric(EntitlementMetric.CPU); + limitEntity.setAggregationFunction(AggregationFunction.Average); + limitEntity.setUnit(10); + limitEntity.setTime(EntitlementTime.Day); + limitEntity.setValue(100); + return limitEntity; + } +} diff --git a/openecomp-be/backend/openecomp-sdc-vendor-license-manager/src/test/java/org/openecomp/sdc/vendorlicense/VendorLicenseFacadeImplTest.java b/openecomp-be/backend/openecomp-sdc-vendor-license-manager/src/test/java/org/openecomp/sdc/vendorlicense/VendorLicenseFacadeImplTest.java new file mode 100644 index 0000000000..58db488d86 --- /dev/null +++ b/openecomp-be/backend/openecomp-sdc-vendor-license-manager/src/test/java/org/openecomp/sdc/vendorlicense/VendorLicenseFacadeImplTest.java @@ -0,0 +1,127 @@ +package org.openecomp.sdc.vendorlicense; + +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.mockito.Spy; +import org.openecomp.sdc.vendorlicense.dao.*; +import org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupEntity; +import org.openecomp.sdc.vendorlicense.facade.impl.VendorLicenseFacadeImpl; +import org.openecomp.sdc.versioning.VersioningManager; +import org.openecomp.sdc.versioning.dao.types.Version; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; + +import java.util.HashSet; +import java.util.Set; + +import static org.mockito.Matchers.anyObject; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; + +/** + * Created by diveshm on 7/3/2017. + */ +public class VendorLicenseFacadeImplTest { + //JUnit Test Cases using Mockito + private static final Version VERSION01 = new Version(0, 1); + private final String FG1_NAME = "FG1 name"; + + @Mock + private VendorLicenseModelDao vendorLicenseModelDao; + + @Mock + private LicenseAgreementDao licenseAgreementDao; + + @Mock + private FeatureGroupDao featureGroupDao; + + @Mock + private EntitlementPoolDao entitlementPoolDao; + + @Mock + private LicenseKeyGroupDao licenseKeyGroupDao; + + @Mock + private VersioningManager versioningManager; + + @InjectMocks + @Spy + private VendorLicenseFacadeImpl vendorLicenseFacadeImpl; + + public FeatureGroupEntity createFeatureGroup(String vlmId, Version version, String id, String name, String desc, + String partNumber, String manufacturerReferenceNumber, Set<String> + licenseKeyGroupIds, Set<String> entitlementPoolIds, Set<String> + referencingLicenseAgreements){ + FeatureGroupEntity featureGroup = new FeatureGroupEntity(vlmId, version, id); + featureGroup.setVendorLicenseModelId(vlmId); + featureGroup.setVersion(version); + featureGroup.setId(id); + featureGroup.setName(name); + featureGroup.setDescription(desc); + featureGroup.setPartNumber(partNumber); + //featureGroup.setManufacturerReferenceNumber(manufacturerReferenceNumber); + featureGroup.setLicenseKeyGroupIds(licenseKeyGroupIds); + featureGroup.setEntitlementPoolIds(entitlementPoolIds); + featureGroup.setReferencingLicenseAgreements(referencingLicenseAgreements); + + return featureGroup; + } + + @BeforeMethod + public void setUp() throws Exception{ + MockitoAnnotations.initMocks(this); + } + + @Test + public void testCreate(){ + Set<String> licenseKeyGroupIds; + licenseKeyGroupIds = new HashSet<>(); + licenseKeyGroupIds.add("lkg1"); + + Set<String> entitlementPoolIds; + entitlementPoolIds = new HashSet<>(); + entitlementPoolIds.add("ep1"); + + Set<String> referencingLicenseAgreements; + referencingLicenseAgreements = new HashSet<>(); + referencingLicenseAgreements.add("la1"); + + FeatureGroupEntity featureGroupEntity = createFeatureGroup("vlmId", VERSION01, "fgId", FG1_NAME, "fg1 desc", + "partNumber", "MRN", licenseKeyGroupIds, entitlementPoolIds, + referencingLicenseAgreements); + + doReturn(featureGroupEntity).when(featureGroupDao).get(anyObject()); + + /*if(featureGroupEntity.getManufacturerReferenceNumber() != null) + featureGroupDao.create(featureGroupEntity); + verify(featureGroupDao).create(anyObject());*/ + } + + @Test + public void testCreateWithoutManufacturerReferenceNumber(){ + Set<String> licenseKeyGroupIds; + licenseKeyGroupIds = new HashSet<>(); + licenseKeyGroupIds.add("lkg1"); + + Set<String> entitlementPoolIds; + entitlementPoolIds = new HashSet<>(); + entitlementPoolIds.add("ep1"); + + Set<String> referencingLicenseAgreements; + referencingLicenseAgreements = new HashSet<>(); + referencingLicenseAgreements.add("la1"); + + FeatureGroupEntity featureGroupEntity = createFeatureGroup("vlmId", VERSION01, "fgId", FG1_NAME, "fg1 desc", + "partNumber", null, licenseKeyGroupIds, entitlementPoolIds, + referencingLicenseAgreements); + doReturn(featureGroupEntity).when(featureGroupDao).get(anyObject()); + + /*if(featureGroupEntity.getManufacturerReferenceNumber() != null) + featureGroupDao.create(featureGroupEntity); + + verify(featureGroupDao, never()).create(anyObject());*/ + + } +} |