diff options
author | talig <talig@amdocs.com> | 2018-01-01 13:07:47 +0200 |
---|---|---|
committer | talig <talig@amdocs.com> | 2018-01-01 13:07:47 +0200 |
commit | 664c11581c7ea5d36e36af1fe7b26e14271e77fe (patch) | |
tree | e0e519697d5105c8ef57406c548f1444e9bf476c /openecomp-be/api | |
parent | 16f5d02fee065f1bbafe056e22627d996dcf2e6a (diff) |
Fix sonar issues + tests in versioning lib
Change-Id: Ib49288fe49be1e848b9615e548689cdbe0df4fee
Issue-ID: SDC-779
Signed-off-by: talig <talig@amdocs.com>
Diffstat (limited to 'openecomp-be/api')
3 files changed, 122 insertions, 102 deletions
diff --git a/openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/main/java/org/openecomp/sdcrests/item/rest/services/VersionsImpl.java b/openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/main/java/org/openecomp/sdcrests/item/rest/services/VersionsImpl.java index c2f6c4708d..23ffc2549b 100644 --- a/openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/main/java/org/openecomp/sdcrests/item/rest/services/VersionsImpl.java +++ b/openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/main/java/org/openecomp/sdcrests/item/rest/services/VersionsImpl.java @@ -145,21 +145,11 @@ public class VersionsImpl implements Versions { @Override public Response listRevisions(String itemId, String versionId, String user) { - GenericCollectionWrapper<RevisionDto> results = new GenericCollectionWrapper<>(); - MapRevisionToDto mapper = new MapRevisionToDto(); - List<Revision> revisions = versioningManager.listRevisions(itemId, new Version(versionId)); - /* When creating a new item an initial version is created with invalid data. - This revision is not an applicable revision. The logic of identifying this revision is: - 1- only the first version of item has this issue - 2- only in the first item version there are 2 revisions created - 3- the second revision is in format "Initial <vlm/vsp>: <name of the vlm/vsp>" - 4- only if a revision in this format exists we remove the first revision. */ - if (revisions.size() > 1 && - revisions.get(revisions.size() - 2).getMessage().matches("Initial .*:.*")) { - revisions.remove(revisions.size() - 1); - } + filterRevisions(revisions); + GenericCollectionWrapper<RevisionDto> results = new GenericCollectionWrapper<>(); + MapRevisionToDto mapper = new MapRevisionToDto(); revisions.forEach(revision -> results.add(mapper.applyMapping(revision, RevisionDto.class))); return Response.ok(results).build(); } @@ -189,13 +179,15 @@ public class VersionsImpl implements Versions { return Response.ok().build(); } + private Version getVersion(String itemId, Version version) { + Version retrievedVersion = versioningManager.get(itemId, version); - private void revert(RevisionRequestDto request, String itemId, String versionId) { - if (request.getRevisionId() == null) { - throw new CoreException(new RevisionIdNotFoundErrorBuilder().build()); + if (retrievedVersion.getState().getSynchronizationState() != SynchronizationState.Merging && + // looks for sdc applicative conflicts + conflictsManager.isConflicted(itemId, retrievedVersion)) { + retrievedVersion.getState().setSynchronizationState(SynchronizationState.Merging); } - - versioningManager.revert(itemId, new Version(versionId), request.getRevisionId()); + return retrievedVersion; } private void sync(String itemId, Version version) { @@ -214,6 +206,29 @@ public class VersionsImpl implements Versions { ActivityType.Commit, user, true, "", message)); } + private void revert(RevisionRequestDto request, String itemId, String versionId) { + if (request.getRevisionId() == null) { + throw new CoreException(new RevisionIdNotFoundErrorBuilder().build()); + } + + versioningManager.revert(itemId, new Version(versionId), request.getRevisionId()); + } + + private void filterRevisions(List<Revision> revisions) { + /* When creating a new item an initial version is created with invalid data. + This revision is not an applicable revision. The logic of identifying this revision is: + 1- only the first version of item has this issue + 2- only in the first item version there are 2 revisions created + 3- the second revision is in format "Initial <vlm/vsp>: <name of the vlm/vsp>" + 4- only if a revision in this format exists we remove the first revision. */ + int numOfRevisions = revisions.size(); + if (numOfRevisions > 1 && + + revisions.get(numOfRevisions - 2).getMessage().matches("Initial .*:.*")) { + revisions.remove(numOfRevisions - 1); + } + } + private void notifyUsers(String itemId, Version version, String message, String userName, NotificationEventTypes eventType) { Map<String, Object> eventProperties = new HashMap<>(); @@ -236,10 +251,10 @@ public class VersionsImpl implements Versions { } private class SyncEvent implements Event { - private String eventType; private String originatorId; private Map<String, Object> attributes; + private String entityId; public SyncEvent(String eventType, String originatorId, @@ -269,15 +284,6 @@ public class VersionsImpl implements Versions { public String getEntityId() { return entityId; } - } - private Version getVersion(String itemId, Version version) { - version = versioningManager.get(itemId, version); - - if (version.getState().getSynchronizationState() != SynchronizationState.Merging && - conflictsManager.isConflicted(itemId, version)) { // looks for sdc applicative conflicts - version.getState().setSynchronizationState(SynchronizationState.Merging); - } - return version; } } diff --git a/openecomp-be/api/openecomp-sdc-rest-webapp/vendor-license-rest/vendor-license-rest-services/src/main/java/org/openecomp/sdcrests/vendorlicense/rest/services/VendorLicenseModelsImpl.java b/openecomp-be/api/openecomp-sdc-rest-webapp/vendor-license-rest/vendor-license-rest-services/src/main/java/org/openecomp/sdcrests/vendorlicense/rest/services/VendorLicenseModelsImpl.java index 77152c6fbb..e1a60386e4 100644 --- a/openecomp-be/api/openecomp-sdc-rest-webapp/vendor-license-rest/vendor-license-rest-services/src/main/java/org/openecomp/sdcrests/vendorlicense/rest/services/VendorLicenseModelsImpl.java +++ b/openecomp-be/api/openecomp-sdc-rest-webapp/vendor-license-rest/vendor-license-rest-services/src/main/java/org/openecomp/sdcrests/vendorlicense/rest/services/VendorLicenseModelsImpl.java @@ -81,6 +81,7 @@ import static org.openecomp.sdc.versioning.VersioningNotificationConstansts.ITEM import static org.openecomp.sdc.versioning.VersioningNotificationConstansts.SUBMIT_DESCRIPTION; import static org.openecomp.sdc.versioning.VersioningNotificationConstansts.VERSION_ID; import static org.openecomp.sdc.versioning.VersioningNotificationConstansts.VERSION_NAME; +import static org.openecomp.sdcrests.vendorlicense.types.VendorLicenseModelActionRequestDto.VendorLicenseModelAction.Submit; @Named @Service("vendorLicenseModels") @@ -88,9 +89,12 @@ import static org.openecomp.sdc.versioning.VersioningNotificationConstansts.VERS @Validated public class VendorLicenseModelsImpl implements VendorLicenseModels { - private static MdcDataDebugMessage mdcDataDebugMessage = new MdcDataDebugMessage(); - private static final Logger logger = LoggerFactory.getLogger(VendorLicenseModelsImpl.class); - public static final String SUBMIT_ITEM = "Submit_Item"; + private static final String VLM_ID = "VLM id"; + private static final String SUBMIT_ITEM_ACTION = "Submit_Item"; + private static final String SUBMIT_HEALED_VERSION_ERROR = + "VLM Id %s: Error while submitting version %s created based on Certified version %s for healing purpose."; + private static final Logger LOGGER = LoggerFactory.getLogger(VendorLicenseModelsImpl.class); + private static final MdcDataDebugMessage MDC_DATA_DEBUG_MESSAGE = new MdcDataDebugMessage(); private ItemPermissionsManager permissionsManager = ItemPermissionsManagerFactory.getInstance() .createInterface(); @@ -107,7 +111,7 @@ public class VendorLicenseModelsImpl implements VendorLicenseModels { @Override public Response listLicenseModels(String versionStatus, String user) { - mdcDataDebugMessage.debugEntryMessage(null); + MDC_DATA_DEBUG_MESSAGE.debugEntryMessage(null); MdcUtil.initMdc(LoggerServiceName.List_VLM.toString()); Predicate<Item> itemPredicate; @@ -130,14 +134,14 @@ public class VendorLicenseModelsImpl implements VendorLicenseModels { .sorted((o1, o2) -> o2.getModificationTime().compareTo(o1.getModificationTime())) .forEach(vspItem -> results.add(mapper.applyMapping(vspItem, ItemDto.class))); - mdcDataDebugMessage.debugExitMessage(null); + MDC_DATA_DEBUG_MESSAGE.debugExitMessage(null); return Response.ok(results).build(); } @Override public Response createLicenseModel(VendorLicenseModelRequestDto request, String user) { - mdcDataDebugMessage.debugEntryMessage(null); - logger.audit(AuditMessages.AUDIT_MSG + AuditMessages.CREATE_VLM + request.getVendorName()); + MDC_DATA_DEBUG_MESSAGE.debugEntryMessage(null); + LOGGER.audit(AuditMessages.AUDIT_MSG + AuditMessages.CREATE_VLM + request.getVendorName()); MdcUtil.initMdc(LoggerServiceName.Create_VLM.toString()); Item item = new Item(); @@ -169,14 +173,14 @@ public class VendorLicenseModelsImpl implements VendorLicenseModels { activityLogManager.logActivity(new ActivityLogEntity(vlm.getId(), version, ActivityType.Create, user, true, "", "")); - mdcDataDebugMessage.debugExitMessage(null); + MDC_DATA_DEBUG_MESSAGE.debugExitMessage(null); return Response.ok(itemCreationDto).build(); } @Override public Response updateLicenseModel(VendorLicenseModelRequestDto request, String vlmId, String versionId, String user) { - mdcDataDebugMessage.debugEntryMessage("VLM id", vlmId); + MDC_DATA_DEBUG_MESSAGE.debugEntryMessage(VLM_ID, vlmId); MdcUtil.initMdc(LoggerServiceName.Update_VLM.toString()); VendorLicenseModelEntity vlm = @@ -187,13 +191,13 @@ public class VendorLicenseModelsImpl implements VendorLicenseModels { vendorLicenseManager.updateVendorLicenseModel(vlm); - mdcDataDebugMessage.debugExitMessage("VLM id", vlmId); + MDC_DATA_DEBUG_MESSAGE.debugExitMessage(VLM_ID, vlmId); return Response.ok().build(); } @Override public Response getLicenseModel(String vlmId, String versionId, String user) { - mdcDataDebugMessage.debugEntryMessage("VLM id", vlmId); + MDC_DATA_DEBUG_MESSAGE.debugEntryMessage(VLM_ID, vlmId); MdcUtil.initMdc(LoggerServiceName.Get_VLM.toString()); Version version = versioningManager.get(vlmId, new Version(versionId)); @@ -203,37 +207,34 @@ public class VendorLicenseModelsImpl implements VendorLicenseModels { try { Optional<Version> healedVersion = HealingManagerFactory.getInstance().createInterface() .healItemVersion(vlmId, version, ItemType.vlm, false); - healedVersion.ifPresent(vlm::setVersion); - - if (healedVersion.isPresent() && version.getStatus() == VersionStatus.Certified) { - try { - submit(vlmId, healedVersion.get(), "Submit after heal", user); - } catch (Exception ex) { - logger.error("VLM Id {}: Error while submitting version {} " + - "created based on Certified version {} for healing purpose.", - vlmId, healedVersion.get().getId(), versionId, ex.getMessage()); + + if (healedVersion.isPresent()) { + vlm.setVersion(healedVersion.get()); + if (version.getStatus() == VersionStatus.Certified) { + submitHealedVersion(vlmId, healedVersion.get(), versionId, user); } } } catch (Exception e) { - logger.error(String.format("Error while auto healing VLM with Id %s and version %s: %s", - vlmId, versionId, e.getMessage())); + LOGGER.error( + String.format("Error while auto healing VLM with Id %s and version %s", vlmId, versionId), + e); } VendorLicenseModelEntityDto vlmDto = new MapVendorLicenseModelEntityToDto().applyMapping(vlm, VendorLicenseModelEntityDto.class); - mdcDataDebugMessage.debugExitMessage("VLM id", vlmId); + MDC_DATA_DEBUG_MESSAGE.debugExitMessage(VLM_ID, vlmId); return Response.ok(vlmDto).build(); } @Override public Response deleteLicenseModel(String vlmId, String versionId, String user) { - mdcDataDebugMessage.debugEntryMessage("VLM id", vlmId); + MDC_DATA_DEBUG_MESSAGE.debugEntryMessage(VLM_ID, vlmId); MdcUtil.initMdc(LoggerServiceName.Delete_VLM.toString()); vendorLicenseManager.deleteVendorLicenseModel(vlmId, new Version(versionId)); - mdcDataDebugMessage.debugExitMessage("VLM id", vlmId); + MDC_DATA_DEBUG_MESSAGE.debugExitMessage(VLM_ID, vlmId); return Response.ok().build(); } @@ -243,27 +244,24 @@ public class VendorLicenseModelsImpl implements VendorLicenseModels { String versionId, String user) { Version version = new Version(versionId); - switch (request.getAction()) { - case Submit: - if (!permissionsManager.isAllowed(vlmId, user, SUBMIT_ITEM)) { - return Response.status(Response.Status.FORBIDDEN).entity - (new Exception(Messages.PERMISSIONS_ERROR.getErrorMessage())).build(); - } - String message = - request.getSubmitRequest() == null ? "" : request.getSubmitRequest().getMessage(); - submit(vlmId, version, message, user); + if (request.getAction() == Submit) { + if (!permissionsManager.isAllowed(vlmId, user, SUBMIT_ITEM_ACTION)) { + return Response.status(Response.Status.FORBIDDEN) + .entity(new Exception(Messages.PERMISSIONS_ERROR.getErrorMessage())).build(); + } + String message = + request.getSubmitRequest() == null ? "Submit" : request.getSubmitRequest().getMessage(); + submit(vlmId, version, message, user); - notifyUsers(vlmId, version, message, user, NotificationEventTypes.SUBMIT); - break; - default: - } + notifyUsers(vlmId, version, message, user, NotificationEventTypes.SUBMIT); + } return Response.ok().build(); } private void submit(String vlmId, Version version, String message, String user) { MdcUtil.initMdc(LoggerServiceName.Submit_VLM.toString()); - logger.audit(AuditMessages.AUDIT_MSG + AuditMessages.SUBMIT_VLM + vlmId); + LOGGER.audit(AuditMessages.AUDIT_MSG + AuditMessages.SUBMIT_VLM + vlmId); vendorLicenseManager.validate(vlmId, version); versioningManager.submit(vlmId, version, message); @@ -272,6 +270,17 @@ public class VendorLicenseModelsImpl implements VendorLicenseModels { new ActivityLogEntity(vlmId, version, ActivityType.Submit, user, true, "", message)); } + private void submitHealedVersion(String vlmId, Version healedVersion, String baseVersionId, + String user) { + try { + submit(vlmId, healedVersion, "Submit after heal", user); + } catch (Exception ex) { + LOGGER.error( + String.format(SUBMIT_HEALED_VERSION_ERROR, vlmId, healedVersion.getId(), baseVersionId), + ex); + } + } + private void notifyUsers(String itemId, Version version, String message, String userName, NotificationEventTypes eventType) { Map<String, Object> eventProperties = new HashMap<>(); @@ -289,7 +298,7 @@ public class VendorLicenseModelsImpl implements VendorLicenseModels { try { notifier.notifySubscribers(syncEvent, userName); } catch (Exception e) { - logger.error("Failed to send sync notification to users subscribed o item '" + itemId); + LOGGER.error("Failed to send sync notification to users subscribed o item '" + itemId); } } diff --git a/openecomp-be/api/openecomp-sdc-rest-webapp/vendor-software-products-rest/vendor-software-products-rest-services/src/main/java/org/openecomp/sdcrests/vsp/rest/services/VendorSoftwareProductsImpl.java b/openecomp-be/api/openecomp-sdc-rest-webapp/vendor-software-products-rest/vendor-software-products-rest-services/src/main/java/org/openecomp/sdcrests/vsp/rest/services/VendorSoftwareProductsImpl.java index b92a431714..3eab416306 100644 --- a/openecomp-be/api/openecomp-sdc-rest-webapp/vendor-software-products-rest/vendor-software-products-rest-services/src/main/java/org/openecomp/sdcrests/vsp/rest/services/VendorSoftwareProductsImpl.java +++ b/openecomp-be/api/openecomp-sdc-rest-webapp/vendor-software-products-rest/vendor-software-products-rest-services/src/main/java/org/openecomp/sdcrests/vsp/rest/services/VendorSoftwareProductsImpl.java @@ -107,6 +107,7 @@ import java.util.Map; import java.util.Optional; import java.util.function.Predicate; +import static javax.ws.rs.core.HttpHeaders.CONTENT_DISPOSITION; import static org.openecomp.sdc.itempermissions.notifications.NotificationConstants.PERMISSION_USER; import static org.openecomp.sdc.logging.messages.AuditMessages.SUBMIT_VSP_ERROR; import static org.openecomp.sdc.vendorsoftwareproduct.VendorSoftwareProductConstants.UniqueValues.VENDOR_SOFTWARE_PRODUCT_NAME; @@ -122,8 +123,11 @@ import static org.openecomp.sdc.versioning.VersioningNotificationConstansts.VERS @Service("vendorSoftwareProducts") @Scope(value = "prototype") public class VendorSoftwareProductsImpl implements VendorSoftwareProducts { - private static final String SUBMIT_ITEM = "Submit_Item"; + private static final String SUBMIT_ITEM_ACTION = "Submit_Item"; + private static final String ATTACHMENT_FILENAME = "attachment; filename="; + private static final String SUBMIT_HEALED_VERSION_ERROR = + "VSP Id %s: Error while submitting version %s created based on Certified version %s for healing purpose."; private static final Logger LOGGER = LoggerFactory.getLogger(VendorSoftwareProductsImpl.class); private static ItemCreationDto validationVsp; @@ -237,20 +241,17 @@ public class VendorSoftwareProductsImpl implements VendorSoftwareProducts { try { Optional<Version> healedVersion = HealingManagerFactory.getInstance().createInterface() .healItemVersion(vspId, version, ItemType.vsp, false); - healedVersion.ifPresent(vspDetails::setVersion); - - if (healedVersion.isPresent() && version.getStatus() == VersionStatus.Certified) { - try { - submitHealedVsp(vspId, healedVersion.get(), user); - } catch (Exception ex) { - LOGGER.error("VSP Id {}: Error while submitting version {} " + - "created based on Certified version {} for healing purpose.", - vspId, healedVersion.get().getId(), versionId, ex.getMessage()); + + if (healedVersion.isPresent()) { + vspDetails.setVersion(healedVersion.get()); + if (version.getStatus() == VersionStatus.Certified) { + submitHealedVersion(vspId, healedVersion.get(), versionId, user); } } } catch (Exception e) { - LOGGER.error(String.format("Error while auto healing VSP with Id %s and version %s: %s", - vspId, versionId, e.getMessage())); + LOGGER.error( + String.format("Error while auto healing VSP with Id %s and version %s", vspId, versionId), + e); } VspDetailsDto vspDetailsDto = @@ -260,16 +261,22 @@ public class VendorSoftwareProductsImpl implements VendorSoftwareProducts { return Response.ok(vspDetailsDto).build(); } - private void submitHealedVsp(String vspId, Version healedVersion, String user) - throws IOException { - Optional<ValidationResponse> - validationResponse = submit(vspId, healedVersion, "Submit healed Vsp", user); - if (validationResponse.isPresent()) { - // TODO: 8/9/2017 before collaboration checkout was done at this scenario (equivalent - // to new version in collaboration). need to decide what should be done now. - throw new IllegalStateException("Certified vsp after healing failed on validation"); + private void submitHealedVersion(String vspId, Version healedVersion, String baseVersionId, + String user) { + try { + Optional<ValidationResponse> + validationResponse = submit(vspId, healedVersion, "Submit healed Vsp", user); + if (validationResponse.isPresent()) { + // TODO: 8/9/2017 before collaboration checkout was done at this scenario (equivalent + // to new version in collaboration). need to decide what should be done now. + throw new IllegalStateException("Certified vsp after healing failed on validation"); + } + vendorSoftwareProductManager.createPackage(vspId, healedVersion); + } catch (Exception ex) { + LOGGER.error( + String.format(SUBMIT_HEALED_VERSION_ERROR, vspId, healedVersion.getId(), baseVersionId), + ex); } - vendorSoftwareProductManager.createPackage(vspId, healedVersion); } @Override @@ -302,12 +309,12 @@ public class VendorSoftwareProductsImpl implements VendorSoftwareProducts { switch (request.getAction()) { case Submit: - if (!permissionsManager.isAllowed(vspId, user, SUBMIT_ITEM)) { + if (!permissionsManager.isAllowed(vspId, user, SUBMIT_ITEM_ACTION)) { return Response.status(Response.Status.FORBIDDEN) .entity(new Exception(Messages.PERMISSIONS_ERROR.getErrorMessage())).build(); } String message = - request.getSubmitRequest() == null ? "" : request.getSubmitRequest().getMessage(); + request.getSubmitRequest() == null ? "Submit" : request.getSubmitRequest().getMessage(); Optional<ValidationResponse> validationResponse = submit(vspId, version, message, user); if (validationResponse.isPresent()) { @@ -368,7 +375,7 @@ public class VendorSoftwareProductsImpl implements VendorSoftwareProducts { return Response.status(Response.Status.NOT_FOUND).build(); } Response.ResponseBuilder response = Response.ok(orchestrationTemplateFile); - response.header("Content-Disposition", "attachment; filename=LatestHeatPackage.zip"); + response.header(CONTENT_DISPOSITION, ATTACHMENT_FILENAME + "LatestHeatPackage.zip"); return response.build(); } @@ -428,7 +435,7 @@ public class VendorSoftwareProductsImpl implements VendorSoftwareProducts { LOGGER.audit(AuditMessages.AUDIT_MSG + AuditMessages.IMPORT_FAIL + vspId); return Response.status(Response.Status.NOT_FOUND).build(); } - response.header("Content-Disposition", "attachment; filename=" + zipFile.getName()); + response.header(CONTENT_DISPOSITION, ATTACHMENT_FILENAME + zipFile.getName()); LOGGER.audit(AuditMessages.AUDIT_MSG + AuditMessages.IMPORT_SUCCESS + vspId); return response.build(); @@ -478,7 +485,7 @@ public class VendorSoftwareProductsImpl implements VendorSoftwareProducts { return Response.status(Response.Status.NOT_FOUND).build(); } response - .header("Content-Disposition", "attachment; filename=" + textInformationArtifact.getName()); + .header(CONTENT_DISPOSITION, ATTACHMENT_FILENAME + textInformationArtifact.getName()); return response.build(); } @@ -621,16 +628,14 @@ public class VendorSoftwareProductsImpl implements VendorSoftwareProducts { .getInfo(vspId, version); //todo - remove after fix missing candidate element - if(candidateInfo == null){ + if (candidateInfo == null) { candidateInfo = new OrchestrationTemplateCandidateData(); candidateInfo.setFileSuffix("zip"); } - vspDetailsDto - .setCandidateOnboardingOrigin( candidateInfo.getFileSuffix() - == null - ? OnboardingTypesEnum.NONE.toString() - : candidateInfo.getFileSuffix()); + vspDetailsDto.setCandidateOnboardingOrigin(candidateInfo.getFileSuffix() == null + ? OnboardingTypesEnum.NONE.toString() + : candidateInfo.getFileSuffix()); } private boolean userHasPermission(String itemId, String userId) { @@ -642,8 +647,8 @@ public class VendorSoftwareProductsImpl implements VendorSoftwareProducts { private void printAuditForErrors(List<ErrorMessage> errorList, String vspId, String auditType) { errorList.forEach(errorMessage -> { if (errorMessage.getLevel().equals(ErrorLevel.ERROR)) { - LOGGER.audit(AuditMessages.AUDIT_MSG + String.format(auditType, errorMessage.getMessage(), - vspId)); + LOGGER.audit( + AuditMessages.AUDIT_MSG + String.format(auditType, errorMessage.getMessage(), vspId)); } }); } |