diff options
author | Avi Ziv <avi.ziv@amdocs.com> | 2017-07-18 19:45:38 +0300 |
---|---|---|
committer | Avi Ziv <avi.ziv@amdocs.com> | 2017-07-18 19:45:38 +0300 |
commit | b8e2faf476202b6ffe61bc3a9a37df1304881d40 (patch) | |
tree | f78b8c0517d8e16c5ae610bf8b49f68ea8a312a1 /openecomp-be/backend/openecomp-sdc-vendor-license-manager/src | |
parent | 75aacbbe1acf78fa53378f07f0a8c7769449a17e (diff) |
[SDC] Onboarding 1710 rebase.
Change-Id: If3b6b81d221fde13908f1e8160db6f7d9433c535
Signed-off-by: Avi Ziv <avi.ziv@amdocs.com>
Diffstat (limited to 'openecomp-be/backend/openecomp-sdc-vendor-license-manager/src')
9 files changed, 1420 insertions, 65 deletions
diff --git a/openecomp-be/backend/openecomp-sdc-vendor-license-manager/src/main/java/org/openecomp/sdc/vendorlicense/VendorLicenseManager.java b/openecomp-be/backend/openecomp-sdc-vendor-license-manager/src/main/java/org/openecomp/sdc/vendorlicense/VendorLicenseManager.java index aa9fc0a8cd..a76b9b865d 100644 --- a/openecomp-be/backend/openecomp-sdc-vendor-license-manager/src/main/java/org/openecomp/sdc/vendorlicense/VendorLicenseManager.java +++ b/openecomp-be/backend/openecomp-sdc-vendor-license-manager/src/main/java/org/openecomp/sdc/vendorlicense/VendorLicenseManager.java @@ -26,7 +26,9 @@ import org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupModel; import org.openecomp.sdc.vendorlicense.dao.types.LicenseAgreementEntity; import org.openecomp.sdc.vendorlicense.dao.types.LicenseAgreementModel; import org.openecomp.sdc.vendorlicense.dao.types.LicenseKeyGroupEntity; +import org.openecomp.sdc.vendorlicense.dao.types.LimitEntity; import org.openecomp.sdc.vendorlicense.dao.types.VendorLicenseModelEntity; +import org.openecomp.sdc.vendorlicense.errors.LimitErrorBuilder; import org.openecomp.sdc.vendorlicense.types.VersionedVendorLicenseModel; import org.openecomp.sdc.versioning.dao.types.Version; @@ -109,4 +111,14 @@ public interface VendorLicenseManager { void deleteLicenseKeyGroup(LicenseKeyGroupEntity licenseKeyGroup, String user); + LimitEntity createLimit(LimitEntity limitEntity, String user); + + Collection<LimitEntity> listLimits(String vlmId, Version version, String epLkgId, String user); + + void deleteLimit(LimitEntity limitEntity, String user); + + void updateLimit(LimitEntity limitEntity, String user); + + LimitEntity getLimit(LimitEntity entitlementPool, String user); + } diff --git a/openecomp-be/backend/openecomp-sdc-vendor-license-manager/src/main/java/org/openecomp/sdc/vendorlicense/errors/LimitErrorBuilder.java b/openecomp-be/backend/openecomp-sdc-vendor-license-manager/src/main/java/org/openecomp/sdc/vendorlicense/errors/LimitErrorBuilder.java new file mode 100644 index 0000000000..4889b5a04a --- /dev/null +++ b/openecomp-be/backend/openecomp-sdc-vendor-license-manager/src/main/java/org/openecomp/sdc/vendorlicense/errors/LimitErrorBuilder.java @@ -0,0 +1,30 @@ +package org.openecomp.sdc.vendorlicense.errors; + + +import org.openecomp.sdc.common.errors.ErrorCategory; +import org.openecomp.sdc.common.errors.ErrorCode; + +public class LimitErrorBuilder { + + private static final String LIMIT_INVALID_ATTR_VALUE_MSG = "The %s value doesn't meet the " + + "expected attribute value."; + + private static final String DUPLICATE_LIMIT_NAME_NOT_ALLOWED_MSG = + "Invalid request, Limit with name %s already exists for type %s."; + + public static ErrorCode getInvalidValueErrorBuilder(String attribute, String errorCode) { + ErrorCode.ErrorCodeBuilder builder = new ErrorCode.ErrorCodeBuilder(); + builder.withId(errorCode); + builder.withCategory(ErrorCategory.APPLICATION); + builder.withMessage(String.format(LIMIT_INVALID_ATTR_VALUE_MSG, attribute)); + return builder.build(); + } + + public static ErrorCode getDuplicateNameErrorbuilder(String name, String type) { + ErrorCode.ErrorCodeBuilder builder = new ErrorCode.ErrorCodeBuilder(); + builder.withId(VendorLicenseErrorCodes.DUPLICATE_LIMIT_NAME_NOT_ALLOWED); + builder.withCategory(ErrorCategory.APPLICATION); + builder.withMessage(String.format(DUPLICATE_LIMIT_NAME_NOT_ALLOWED_MSG, name, type )); + return builder.build(); + } +} diff --git a/openecomp-be/backend/openecomp-sdc-vendor-license-manager/src/main/java/org/openecomp/sdc/vendorlicense/impl/VendorLicenseManagerImpl.java b/openecomp-be/backend/openecomp-sdc-vendor-license-manager/src/main/java/org/openecomp/sdc/vendorlicense/impl/VendorLicenseManagerImpl.java index eb559f2e62..622ff02501 100644 --- a/openecomp-be/backend/openecomp-sdc-vendor-license-manager/src/main/java/org/openecomp/sdc/vendorlicense/impl/VendorLicenseManagerImpl.java +++ b/openecomp-be/backend/openecomp-sdc-vendor-license-manager/src/main/java/org/openecomp/sdc/vendorlicense/impl/VendorLicenseManagerImpl.java @@ -24,6 +24,8 @@ import org.openecomp.core.util.UniqueValueUtil; import org.openecomp.sdc.activityLog.ActivityLogManager; import org.openecomp.sdc.activityLog.ActivityLogManagerFactory; import org.openecomp.sdc.activitylog.dao.type.ActivityLogEntity; +import org.openecomp.sdc.common.errors.CoreException; +import org.openecomp.sdc.common.errors.ErrorCode; import org.openecomp.sdc.datatypes.error.ErrorLevel; import org.openecomp.sdc.logging.api.Logger; import org.openecomp.sdc.logging.api.LoggerFactory; @@ -32,6 +34,7 @@ import org.openecomp.sdc.logging.context.impl.MdcDataErrorMessage; import org.openecomp.sdc.logging.types.LoggerConstants; import org.openecomp.sdc.logging.types.LoggerErrorCode; import org.openecomp.sdc.logging.types.LoggerErrorDescription; +import org.openecomp.sdc.logging.types.LoggerServiceName; import org.openecomp.sdc.logging.types.LoggerTragetServiceName; import org.openecomp.sdc.vendorlicense.VendorLicenseConstants; import org.openecomp.sdc.vendorlicense.VendorLicenseManager; @@ -43,6 +46,8 @@ import org.openecomp.sdc.vendorlicense.dao.LicenseAgreementDao; import org.openecomp.sdc.vendorlicense.dao.LicenseAgreementDaoFactory; import org.openecomp.sdc.vendorlicense.dao.LicenseKeyGroupDao; import org.openecomp.sdc.vendorlicense.dao.LicenseKeyGroupDaoFactory; +import org.openecomp.sdc.vendorlicense.dao.LimitDao; +import org.openecomp.sdc.vendorlicense.dao.LimitDaoFactory; import org.openecomp.sdc.vendorlicense.dao.VendorLicenseModelDao; import org.openecomp.sdc.vendorlicense.dao.VendorLicenseModelDaoFactory; import org.openecomp.sdc.vendorlicense.dao.types.EntitlementPoolEntity; @@ -51,7 +56,11 @@ import org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupModel; import org.openecomp.sdc.vendorlicense.dao.types.LicenseAgreementEntity; import org.openecomp.sdc.vendorlicense.dao.types.LicenseAgreementModel; import org.openecomp.sdc.vendorlicense.dao.types.LicenseKeyGroupEntity; +import org.openecomp.sdc.vendorlicense.dao.types.LimitEntity; +import org.openecomp.sdc.vendorlicense.dao.types.LimitType; import org.openecomp.sdc.vendorlicense.dao.types.VendorLicenseModelEntity; +import org.openecomp.sdc.vendorlicense.errors.InvalidDateErrorBuilder; +import org.openecomp.sdc.vendorlicense.errors.LimitErrorBuilder; import org.openecomp.sdc.vendorlicense.facade.VendorLicenseFacade; import org.openecomp.sdc.vendorlicense.facade.VendorLicenseFacadeFactory; import org.openecomp.sdc.vendorlicense.types.VersionedVendorLicenseModel; @@ -64,10 +73,14 @@ import org.openecomp.sdc.versioning.types.VersionInfo; import org.openecomp.sdc.versioning.types.VersionableEntityAction; import org.openecomp.sdcrests.activitylog.types.ActivityType; +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; import java.util.ArrayList; import java.util.Collection; +import java.util.Iterator; import java.util.List; import java.util.Map; +import java.util.Optional; import java.util.Set; import static org.openecomp.sdc.vendorlicense.VendorLicenseConstants.VENDOR_LICENSE_MODEL_VERSIONABLE_TYPE; @@ -75,7 +88,7 @@ import static org.openecomp.sdc.vendorlicense.VendorLicenseConstants.VENDOR_LICE public class VendorLicenseManagerImpl implements VendorLicenseManager { private static final VersioningManager versioningManager = VersioningManagerFactory.getInstance().createInterface(); - private static final VendorLicenseFacade vendorLicenseFacade = + private VendorLicenseFacade vendorLicenseFacade = VendorLicenseFacadeFactory.getInstance().createInterface(); private static final VendorLicenseModelDao vendorLicenseModelDao = VendorLicenseModelDaoFactory.getInstance().createInterface(); @@ -87,6 +100,9 @@ public class VendorLicenseManagerImpl implements VendorLicenseManager { EntitlementPoolDaoFactory.getInstance().createInterface(); private static final LicenseKeyGroupDao licenseKeyGroupDao = LicenseKeyGroupDaoFactory.getInstance().createInterface(); + private static final LimitDao limitDao = + LimitDaoFactory.getInstance().createInterface(); + private ActivityLogManager activityLogManager = ActivityLogManagerFactory.getInstance().createInterface(); private static MdcDataDebugMessage mdcDataDebugMessage = new MdcDataDebugMessage(); private static final Logger logger = @@ -471,14 +487,110 @@ public class VendorLicenseManagerImpl implements VendorLicenseManager { .debugEntryMessage("VLM id", entitlementPool.getVendorLicenseModelId()); mdcDataDebugMessage .debugExitMessage("VLM id", entitlementPool.getVendorLicenseModelId()); + validateCreateDate(entitlementPool); return vendorLicenseFacade.createEntitlementPool(entitlementPool, user); } + private void validateCreateDate(EntitlementPoolEntity entitlementPool){ + mdcDataDebugMessage.debugEntryMessage("Start date and end date", entitlementPool.getStartDate + ()+" "+entitlementPool.getExpiryDate()); + + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy'T'HH:mm:ss'Z'"); + + entitlementPool.setStartDate(entitlementPool.getStartDate() != null ? (entitlementPool + .getStartDate().trim().length() != 0 ? entitlementPool.getStartDate()+"T00:00:00Z" + : null) : null); + entitlementPool.setExpiryDate(entitlementPool.getExpiryDate() != null ? (entitlementPool + .getExpiryDate().trim().length() != 0 ? entitlementPool.getExpiryDate()+"T23:59:59Z" + : null) : null); + + if(entitlementPool.getStartDate() != null && entitlementPool.getExpiryDate() != null) { + if (LocalDate.parse(entitlementPool.getStartDate(), formatter).atStartOfDay().isBefore + (LocalDate.now().atStartOfDay()) || + LocalDate.parse(entitlementPool.getExpiryDate(), formatter).atStartOfDay() + .isEqual(LocalDate.now().atStartOfDay()) || + LocalDate.parse(entitlementPool.getExpiryDate(), formatter) + .isBefore(LocalDate.parse(entitlementPool.getStartDate(), formatter))) { + MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB, + LoggerTragetServiceName.VALIDATE_DATE_RANGE,ErrorLevel.ERROR.name(), + LoggerErrorCode.DATA_ERROR.getErrorCode(), LoggerErrorDescription.INVALID_VALUE); + throw new CoreException( + new InvalidDateErrorBuilder(entitlementPool.getVendorLicenseModelId()) + .build()); + } + } + + if(entitlementPool.getStartDate() != null && entitlementPool.getExpiryDate() == null) { + if (LocalDate.parse(entitlementPool.getStartDate(), formatter).atStartOfDay().isBefore + (LocalDate.now().atStartOfDay())) { + MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB, + LoggerTragetServiceName.VALIDATE_DATE_RANGE,ErrorLevel.ERROR.name(), + LoggerErrorCode.DATA_ERROR.getErrorCode(), LoggerErrorDescription.INVALID_VALUE); + throw new CoreException( + new InvalidDateErrorBuilder(entitlementPool.getVendorLicenseModelId()) + .build()); + } + } + + if(entitlementPool.getStartDate() == null && entitlementPool.getExpiryDate() != null) { + MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB, + LoggerTragetServiceName.VALIDATE_DATE_RANGE,ErrorLevel.ERROR.name(), + LoggerErrorCode.DATA_ERROR.getErrorCode(), LoggerErrorDescription.INVALID_VALUE); + throw new CoreException( + new InvalidDateErrorBuilder(entitlementPool.getVendorLicenseModelId()) + .build()); + + } + + mdcDataDebugMessage.debugExitMessage(null,null); + } + + private void validateUpdateDate(EntitlementPoolEntity entitlementPool){ + mdcDataDebugMessage.debugEntryMessage("Start date and end date", entitlementPool.getStartDate + ()+" "+entitlementPool.getExpiryDate()); + + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy'T'HH:mm:ss'Z'"); + + entitlementPool.setStartDate(entitlementPool.getStartDate() != null ? (entitlementPool + .getStartDate().trim().length() != 0 ? entitlementPool.getStartDate()+"T00:00:00Z" + : null) : null); + entitlementPool.setExpiryDate(entitlementPool.getExpiryDate() != null ? (entitlementPool + .getExpiryDate().trim().length() != 0 ? entitlementPool.getExpiryDate()+"T23:59:59Z" + : null) : null); + + if(entitlementPool.getStartDate() != null && entitlementPool.getExpiryDate() != null) { + if (LocalDate.parse(entitlementPool.getExpiryDate(), formatter).atStartOfDay() + .isEqual(LocalDate.parse(entitlementPool.getStartDate(), formatter).atStartOfDay()) || + LocalDate.parse(entitlementPool.getExpiryDate(), formatter) + .isBefore(LocalDate.parse(entitlementPool.getStartDate(), formatter))) { + MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB, + LoggerTragetServiceName.VALIDATE_DATE_RANGE,ErrorLevel.ERROR.name(), + LoggerErrorCode.DATA_ERROR.getErrorCode(), LoggerErrorDescription.INVALID_VALUE); + throw new CoreException( + new InvalidDateErrorBuilder(entitlementPool.getVendorLicenseModelId()) + .build()); + } + } + + if(entitlementPool.getStartDate() == null && entitlementPool.getExpiryDate() != null) { + MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB, + LoggerTragetServiceName.VALIDATE_DATE_RANGE,ErrorLevel.ERROR.name(), + LoggerErrorCode.DATA_ERROR.getErrorCode(), LoggerErrorDescription.INVALID_VALUE); + throw new CoreException( + new InvalidDateErrorBuilder(entitlementPool.getVendorLicenseModelId()) + .build()); + + } + + mdcDataDebugMessage.debugExitMessage(null,null); + } + @Override public void updateEntitlementPool(EntitlementPoolEntity entitlementPool, String user) { mdcDataDebugMessage.debugEntryMessage("VLM id, EP id", entitlementPool .getVendorLicenseModelId(), entitlementPool.getId()); + validateUpdateDate(entitlementPool); Version version = VersioningUtil.resolveVersion(entitlementPool.getVersion(), getVersionInfo(entitlementPool.getVendorLicenseModelId(), VersionableEntityAction.Write, user), user); @@ -504,6 +616,18 @@ public class VendorLicenseManagerImpl implements VendorLicenseManager { VersioningUtil .validateEntityExistence(retrieved, entitlementPool, VendorLicenseModelEntity.ENTITY_TYPE); + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy'T'HH:mm:ss'Z'"); + DateTimeFormatter targetFormatter = DateTimeFormatter.ofPattern("MM/dd/yyyy"); + if(retrieved.getStartDate() != null){ + retrieved.setStartDate(LocalDate.parse(retrieved.getStartDate(),formatter).format + (targetFormatter)); + } + + if(retrieved.getExpiryDate() != null){ + retrieved.setExpiryDate(LocalDate.parse(retrieved.getExpiryDate(),formatter).format + (targetFormatter)); + } + mdcDataDebugMessage.debugExitMessage("VLM id, EP id", entitlementPool .getVendorLicenseModelId(), entitlementPool.getId()); return retrieved; @@ -529,6 +653,8 @@ public class VendorLicenseManagerImpl implements VendorLicenseManager { referencingFeatureGroupId), entitlementPool.getId()); } + deleteChildLimits(entitlementPool.getVendorLicenseModelId(), entitlementPool.getVersion(), entitlementPool.getId(), user); + entitlementPoolDao.delete(entitlementPool); UniqueValueUtil.deleteUniqueValue(VendorLicenseConstants.UniqueValues.ENTITLEMENT_POOL_NAME, @@ -542,6 +668,14 @@ public class VendorLicenseManagerImpl implements VendorLicenseManager { .getVendorLicenseModelId(), entitlementPool.getId()); } + private void deleteChildLimits(String vlmId, Version version, String epLkgId, String user) { + Optional<Collection<LimitEntity>> limitEntities = Optional.ofNullable( + listLimits(vlmId, version, epLkgId, user)); + limitEntities.ifPresent(entities-> + entities.forEach(entity-> + deleteLimit(entity, user))); + } + @Override public Collection<LicenseKeyGroupEntity> listLicenseKeyGroups(String vlmId, Version version, String user) { @@ -617,6 +751,8 @@ public class VendorLicenseManagerImpl implements VendorLicenseManager { referencingFeatureGroupId), licenseKeyGroup.getId()); } + deleteChildLimits(licenseKeyGroup.getVendorLicenseModelId(), licenseKeyGroup.getVersion(), licenseKeyGroup.getId(), user); + licenseKeyGroupDao.delete(licenseKeyGroup); UniqueValueUtil.deleteUniqueValue(VendorLicenseConstants.UniqueValues.LICENSE_KEY_GROUP_NAME, @@ -630,6 +766,124 @@ public class VendorLicenseManagerImpl implements VendorLicenseManager { .getVendorLicenseModelId(), licenseKeyGroup.getId()); } + @Override + public LimitEntity createLimit(LimitEntity limit, String user) { + mdcDataDebugMessage + .debugEntryMessage("VLM id", limit.getVendorLicenseModelId(), "EP/LKGId", limit + .getEpLkgId()); + mdcDataDebugMessage + .debugExitMessage("VLM id", limit.getVendorLicenseModelId(), "EP/LKGId", limit + .getEpLkgId()); + validateLimit(limit, user); + return vendorLicenseFacade.createLimit(limit, user); + } + + private void validateLimit(LimitEntity limit, String user) { + Version version = VersioningUtil.resolveVersion(limit.getVersion(), + getVersionInfo(limit.getVendorLicenseModelId(), VersionableEntityAction.Write, + user), user); + Collection<LimitEntity> limitList = listLimits(limit.getVendorLicenseModelId(),version + ,limit.getEpLkgId(), user); + + if (!isLimitNameUnique(limitList,limit.getName(), limit.getType(), limit.getId())) { + final ErrorCode duplicateLimitNameErrorBuilder = + LimitErrorBuilder.getDuplicateNameErrorbuilder(limit.getName(), limit.getType().name()); + MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB, + LoggerServiceName.Create_LIMIT.toString(), ErrorLevel.ERROR.name(), + LoggerErrorCode.DATA_ERROR.getErrorCode(), + duplicateLimitNameErrorBuilder.message()); + throw new CoreException(duplicateLimitNameErrorBuilder); + } + } + + private boolean isLimitNameUnique(Collection<LimitEntity> limitList, String name, LimitType + type, String id) { + for (LimitEntity limit : limitList) { + if(limit.getName().equalsIgnoreCase(name) && + limit.getType().name().equalsIgnoreCase(type.name())) { + if(id != null && limit.getId().equals(id)){ + continue; + } + return false; + } + } + return true; + } + + @Override + public Collection<LimitEntity> listLimits(String vlmId, Version version, String epLkgId, + String user) { + mdcDataDebugMessage.debugEntryMessage("VLM id", vlmId, "EP/LKGId", epLkgId); + mdcDataDebugMessage.debugExitMessage("VLM id", vlmId, "EP/LKGId", epLkgId); + return vendorLicenseFacade.listLimits(vlmId, version, epLkgId, user); + } + + @Override + public void deleteLimit(LimitEntity limitEntity, String user) { + mdcDataDebugMessage.debugEntryMessage("VLM id, EP id, Limit Id", limitEntity + .getVendorLicenseModelId(), limitEntity.getEpLkgId(), limitEntity.getId()); + + Version version = VersioningUtil.resolveVersion(limitEntity.getVersion(), + getVersionInfo(limitEntity.getVendorLicenseModelId(), VersionableEntityAction.Write, + user), user); + limitEntity.setVersion(version); + + if(!isLimitPresent(limitEntity)){ + VersioningUtil + .validateEntityExistence(null, limitEntity, VendorLicenseModelEntity.ENTITY_TYPE); + } + LimitEntity retrieved = limitDao.get(limitEntity); + VersioningUtil + .validateEntityExistence(retrieved, limitEntity, VendorLicenseModelEntity.ENTITY_TYPE); + + limitDao.delete(limitEntity); + + vendorLicenseFacade.updateVlmLastModificationTime(limitEntity.getVendorLicenseModelId(), + limitEntity.getVersion()); + + mdcDataDebugMessage.debugExitMessage("VLM id, EP id, Limit Id", limitEntity + .getVendorLicenseModelId(), limitEntity.getEpLkgId(), limitEntity.getId()); + } + + @Override + public void updateLimit(LimitEntity limit, String user) { + mdcDataDebugMessage + .debugEntryMessage("VLM id", limit.getVendorLicenseModelId(), "EP/LKGId", limit + .getEpLkgId()); + getLimit(limit,user); + validateLimit(limit, user); + vendorLicenseFacade.updateLimit(limit, user); + mdcDataDebugMessage + .debugExitMessage("VLM id", limit.getVendorLicenseModelId(), "EP/LKGId", limit + .getEpLkgId()); + } + + private boolean isLimitPresent(LimitEntity limit) { + return limitDao.isLimitPresent(limit); + } + + @Override + public LimitEntity getLimit(LimitEntity limitEntity, + String user) { + mdcDataDebugMessage.debugEntryMessage("VLM id", limitEntity.getVendorLicenseModelId(), + "EP/LKGId", limitEntity.getEpLkgId()); + + limitEntity.setVersion(VersioningUtil.resolveVersion(limitEntity.getVersion(), + getVersionInfo(limitEntity.getVendorLicenseModelId(), VersionableEntityAction.Read, + user), user)); + if(!isLimitPresent(limitEntity)){ + VersioningUtil + .validateEntityExistence(null, limitEntity, VendorLicenseModelEntity.ENTITY_TYPE); + } + LimitEntity retrieved = limitDao.get(limitEntity); + VersioningUtil + .validateEntityExistence(retrieved, limitEntity, VendorLicenseModelEntity.ENTITY_TYPE); + + mdcDataDebugMessage.debugExitMessage("VLM id", limitEntity.getVendorLicenseModelId(), + "EP/LKGId", limitEntity.getEpLkgId()); + return retrieved; + } + private void addFeatureGroupsToLicenseAgreementRef(Set<String> featureGroupIds, LicenseAgreementEntity licenseAgreement) { if (featureGroupIds != null) { 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());*/ + + } +} |