PortletUtils { public static Set<Attachment> updateAttachmentsFromRequest(PortletRequest request, Set<Attachment> documentAttachments) { Set<Attachment> attachments = Sets.newHashSet(); User user = UserCacheHolder.getUserFromRequest(request); String[] ids = request .getParameterValues(Release._Fields.ATTACHMENTS.toString() + Attachment._Fields.ATTACHMENT_CONTENT_ID.toString()); String[] fileNames = request.getParameterValues(Release._Fields.ATTACHMENTS.toString() + Attachment._Fields.FILENAME.toString()); String[] types = request .getParameterValues(Release._Fields.ATTACHMENTS.toString() + Attachment._Fields.ATTACHMENT_TYPE.toString()); String[] createdComments = request.getParameterValues(Release._Fields.ATTACHMENTS.toString() + Attachment._Fields.CREATED_COMMENT.toString()); String[] checkStatuses = request .getParameterValues(Release._Fields.ATTACHMENTS.toString() + Attachment._Fields.CHECK_STATUS.toString()); String[] checkedComments = request.getParameterValues(Release._Fields.ATTACHMENTS.toString() + Attachment._Fields.CHECKED_COMMENT.toString()); if (ids == null || ids.length == 0) { LOGGER.info("No ids transmitted. All attachments will be deleted."); return attachments; } else if (CommonUtils.oneIsNull(fileNames, types, createdComments, checkStatuses, checkedComments)) { LOGGER.error("Invalid request content. One of the attachment parameters is null. No attachments will be saved or deleted."); return documentAttachments; } else if (!CommonUtils.allHaveSameLength(ids, fileNames, types, createdComments, checkStatuses, checkedComments)) { LOGGER.error("Not all of the attachment parameter arrays have the same length! No attachments will be saved or deleted."); return documentAttachments; } Map<String, Attachment> documentAttachmentMap = CommonUtils.nullToEmptySet(documentAttachments).stream() .collect(Collectors.toMap(Attachment::getAttachmentContentId, Function.identity())); for (int i = 0; i < ids.length; ++i) { String id = ids[i]; Attachment attachment = documentAttachmentMap.get(id); if (attachment == null) { attachment = CommonUtils.getNewAttachment(user, id, fileNames[i]); documentAttachmentMap.put(attachment.getAttachmentContentId(), attachment); } attachment.setAttachmentType(getAttachmentTypefromString(types[i])); attachment.setCreatedComment(createdComments[i]); if (getCheckStatusfromString(checkStatuses[i]) != CheckStatus.NOTCHECKED) { if (attachment.checkStatus != getCheckStatusfromString(checkStatuses[i]) || !checkedComments[i].equals(attachment.checkedComment)) { attachment.setCheckedOn(SW360Utils.getCreatedOn()); attachment.setCheckedBy(UserCacheHolder.getUserFromRequest(request).getEmail()); attachment.setCheckedTeam(UserCacheHolder.getUserFromRequest(request).getDepartment()); attachment.setCheckedComment(checkedComments[i]); } } else { attachment.setCheckedOn(null); attachment.setCheckedBy(null); attachment.setCheckedTeam(null); attachment.setCheckedComment(""); } attachment.setCheckStatus(getCheckStatusfromString(checkStatuses[i])); attachments.add(attachment); } return attachments; } private  PortletUtils(); static ComponentType getComponentTypefromString(String enumNumber); static ClearingState getClearingStatefromString(String enumNumber); static RepositoryType getRepositoryTypefromString(String enumNumber); static MainlineState getMainlineStatefromString(String enumNumber); static ModerationState getModerationStatusfromString(String enumNumber); static AttachmentType getAttachmentTypefromString(String enumNumber); static CheckStatus getCheckStatusfromString(String enumNumber); static ProjectState getProjectStateFromString(String enumNumber); static ProjectClearingState getProjectClearingStateFromString(String enumNumber); static ProjectType getProjectTypeFromString(String enumNumber); static Visibility getVisibilityFromString(String enumNumber); static UserGroup getUserGroupFromString(String enumNumber); static ECCStatus getEccStatusFromString(String enumNumber); static void setFieldValue(PortletRequest request, T instance, U field, FieldMetaData fieldMetaData, String prefix); static Set<Attachment> updateAttachmentsFromRequest(PortletRequest request, Set<Attachment> documentAttachments); static Release cloneRelease(String emailFromRequest, Release release); static Project cloneProject(String emailFromRequest, String department, Project project); static JSONObject importStatusToJSON(VulnerabilityUpdateStatus updateStatus); static VerificationState getVerificationState(VulnerabilityDTO vul); static Map<String,Set<String>> getCustomMapFromRequest(PortletRequest request); static Map<String,Set<String>> getCustomMapFromRequest(PortletRequest request, String mapKey, String mapValue); static Map<String,String> getExternalIdMapFromRequest(PortletRequest request);  }
@Test public void testUpdateAttachmentsFromRequestIdsAreNull() { PortletRequest request = Mockito.mock(PortletRequest.class); new TestUserCacheHolder().enable(); Set<Attachment> documentAttachments = ImmutableSet.of(); assertThat(PortletUtils.updateAttachmentsFromRequest(request, documentAttachments), is(empty())); }
@Test public void testUpdateAttachmentsFromRequestIdsAreEmpty() { PortletRequest request = Mockito.mock(PortletRequest.class); new TestUserCacheHolder().enable(); Set<Attachment> documentAttachments = ImmutableSet.of(); setAttachmentIds(request); assertThat(PortletUtils.updateAttachmentsFromRequest(request, documentAttachments), is(empty())); }
@Test public void testUpdateAttachmentsFromRequestOneArrayIsNull() { PortletRequest request = Mockito.mock(PortletRequest.class); new TestUserCacheHolder().enable(); Set<Attachment> documentAttachments = ImmutableSet.of(); setAttachmentIds(request, "1", "2", "3"); Set<Attachment> updatedAttachments = PortletUtils.updateAttachmentsFromRequest(request, documentAttachments); assertTrue(updatedAttachments.isEmpty()); assertThat(updatedAttachments, is(sameInstance(documentAttachments))); }
@Test public void testUpdateAttachmentsFromRequestOneArrayHasDifferentLength() { PortletRequest request = Mockito.mock(PortletRequest.class); new TestUserCacheHolder().enable(); Set<Attachment> documentAttachments = ImmutableSet.of(); setAttachmentIds(request, "1", "2", "3"); setAttachmentFilenames(request, "A", "B", "C"); setAttachmentTypes(request, AttachmentType.DOCUMENT, AttachmentType.BINARY, AttachmentType.DECISION_REPORT); setAttachmenCreatedComments(request, "CC1", "CC2", "CC3"); setAttachmentCheckStatus(request, CheckStatus.NOTCHECKED, CheckStatus.ACCEPTED, CheckStatus.REJECTED); setAttachmentCheckComment(request, "CCK1", "CCK2"); Set<Attachment> updatedAttachments = PortletUtils.updateAttachmentsFromRequest(request, documentAttachments); assertTrue(updatedAttachments.isEmpty()); assertThat(updatedAttachments, is(sameInstance(documentAttachments))); }
@Test public void testUpdateAttachmentsFromRequestOnlyAddToEmpty() { PortletRequest request = Mockito.mock(PortletRequest.class); new TestUserCacheHolder().enable(); Set<Attachment> documentAttachments = ImmutableSet.of(); setAttachmentIds(request, "1", "2", "3"); setAttachmentFilenames(request, "A", "B", "C"); setAttachmentTypes(request, AttachmentType.DOCUMENT, AttachmentType.BINARY, AttachmentType.DECISION_REPORT); setAttachmenCreatedComments(request, "CC1", "CC2", "CC3"); setAttachmentCheckStatus(request, CheckStatus.NOTCHECKED, CheckStatus.ACCEPTED, CheckStatus.REJECTED); setAttachmentCheckComment(request, "CCK1", "CCK2", "CCK3"); Set<Attachment> updatedAttachments = PortletUtils.updateAttachmentsFromRequest(request, documentAttachments); assertThat(updatedAttachments.size(), is(3)); assertThat(updatedAttachments.stream().map(Attachment::getAttachmentContentId).collect(Collectors.toList()), containsInAnyOrder("1", "2", "3")); }
@Test public void testUpdateAttachmentsFromRequestAddAndUpdate() { PortletRequest request = Mockito.mock(PortletRequest.class); new TestUserCacheHolder().enable(); Set<Attachment> documentAttachments = createAttachments("9", "8"); setAttachmentIds(request, "1", "2", "8", "9"); setAttachmentFilenames(request, "A", "B", "C", "D"); setAttachmentTypes(request, AttachmentType.DOCUMENT, AttachmentType.BINARY, AttachmentType.DECISION_REPORT, AttachmentType.OTHER); setAttachmenCreatedComments(request, "CC1", "CC2", "CC3", "CC4"); setAttachmentCheckStatus(request, CheckStatus.NOTCHECKED, CheckStatus.ACCEPTED, CheckStatus.REJECTED, CheckStatus.NOTCHECKED); setAttachmentCheckComment(request, "CCK1", "CCK2", "CCK3", "CCK4;"); Set<Attachment> updatedAttachments = PortletUtils.updateAttachmentsFromRequest(request, documentAttachments); assertThat(updatedAttachments.size(), is(4)); assertThat(updatedAttachments.stream().map(Attachment::getAttachmentContentId).collect(Collectors.toList()), containsInAnyOrder("1", "2", "8", "9")); assertThat(updatedAttachments.stream().map(Attachment::getFilename).collect(Collectors.toList()), containsInAnyOrder("A", "B", "file8", "file9")); }
@Test public void testUpdateAttachmentsFromRequestUpdateOnly() { PortletRequest request = Mockito.mock(PortletRequest.class); User user = createUser("tid", "test@example.org", "tpd"); new TestUserCacheHolder().enable(user); Set<Attachment> documentAttachments = createAttachments(user, "1", "2", "3"); new TestUserCacheHolder().enable(createUser("cid", "check@example.org", "cpd")); setAttachmentIds(request, "1", "2", "3"); setAttachmentFilenames(request, "A", "B", "C"); setAttachmentTypes(request, AttachmentType.DOCUMENT, AttachmentType.SOURCE, AttachmentType.DESIGN); setAttachmenCreatedComments(request, "CC1", "CC2", "CC3"); setAttachmentCheckStatus(request, CheckStatus.NOTCHECKED, CheckStatus.ACCEPTED, CheckStatus.ACCEPTED); setAttachmentCheckComment(request, "", "CCK2", "CCK3"); Set<Attachment> updatedAttachments = PortletUtils.updateAttachmentsFromRequest(request, documentAttachments); assertThat(updatedAttachments.size(), is(3)); assertThat(updatedAttachments.stream().map(Attachment::getAttachmentContentId).collect(Collectors.toList()), containsInAnyOrder("1", "2", "3")); assertThat(updatedAttachments.stream().map(Attachment::getFilename).collect(Collectors.toList()), containsInAnyOrder("file1", "file2", "file3")); assertThat(updatedAttachments.stream().map(Attachment::getAttachmentType).collect(Collectors.toList()), containsInAnyOrder(AttachmentType.DOCUMENT, AttachmentType.SOURCE, AttachmentType.DESIGN)); assertThat(updatedAttachments.stream().map(Attachment::getCreatedBy).collect(Collectors.toList()), containsInAnyOrder("test@example.org", "test@example.org", "test@example.org")); assertThat(updatedAttachments.stream().map(Attachment::getCreatedTeam).collect(Collectors.toList()), containsInAnyOrder("tpd", "tpd", "tpd")); assertThat(updatedAttachments.stream().map(Attachment::getCreatedComment).collect(Collectors.toList()), containsInAnyOrder("CC1", "CC2", "CC3")); assertThat(updatedAttachments.stream().map(Attachment::getCheckStatus).collect(Collectors.toList()), containsInAnyOrder(CheckStatus.NOTCHECKED, CheckStatus.ACCEPTED, CheckStatus.ACCEPTED)); assertThat(updatedAttachments.stream().map(Attachment::getCheckedBy).collect(Collectors.toList()), containsInAnyOrder(null, "check@example.org", "check@example.org")); assertThat(updatedAttachments.stream().map(Attachment::getCheckedTeam).collect(Collectors.toList()), containsInAnyOrder(null, "cpd", "cpd")); assertThat(updatedAttachments.stream().map(Attachment::getCheckedComment).collect(Collectors.toList()), containsInAnyOrder("", "CCK2", "CCK3")); }
@Test public void testUpdateAttachmentsUncheckRemovesCheckedFields() { PortletRequest request = Mockito.mock(PortletRequest.class); User user = createUser("tid", "test@example.org", "tpd"); Attachment attachment = createAttachments(user, "1").iterator().next(); attachment.setCheckStatus(CheckStatus.ACCEPTED); attachment.setCheckedBy("check@example.org"); attachment.setCheckedComment("check-comment"); attachment.setCheckedOn("2017-01-31"); attachment.setCheckedTeam("check-team"); setAttachmentIds(request, "1"); setAttachmentFilenames(request, "A"); setAttachmentTypes(request, AttachmentType.SOURCE); setAttachmenCreatedComments(request, "CC1"); setAttachmentCheckStatus(request, CheckStatus.NOTCHECKED); setAttachmentCheckComment(request, ""); Set<Attachment> updatedAttachments = PortletUtils.updateAttachmentsFromRequest(request, Sets.newHashSet(attachment)); assertThat(updatedAttachments.size(), is(1)); Attachment updatedAttachment = updatedAttachments.iterator().next(); assertThat(updatedAttachment.getAttachmentContentId(), is("1")); assertThat(updatedAttachment.getCheckStatus(), is(CheckStatus.NOTCHECKED)); assertNull(updatedAttachment.getCheckedBy()); assertNull(updatedAttachment.getCheckedTeam()); assertThat(updatedAttachment.getCheckedComment(), is("")); }
@Test public void testUpdateAttachmentsRemoveAndUpdate() { PortletRequest request = Mockito.mock(PortletRequest.class); new TestUserCacheHolder().enable(); Set<Attachment> documentAttachments = createAttachments("1", "2", "3"); new TestUserCacheHolder().enable(createUser("tid", "test@example.org", "tpd")); setAttachmentIds(request, "2"); setAttachmentFilenames(request, "B"); setAttachmentTypes(request, AttachmentType.SCREENSHOT); setAttachmenCreatedComments(request, "CC2"); setAttachmentCheckStatus(request, CheckStatus.ACCEPTED); setAttachmentCheckComment(request, "CCK2"); Set<Attachment> updatedAttachments = PortletUtils.updateAttachmentsFromRequest(request, documentAttachments); assertThat(updatedAttachments.size(), is(1)); Attachment updatedAttachment = updatedAttachments.iterator().next(); assertThat(updatedAttachment.getAttachmentContentId(), is("2")); assertThat(updatedAttachment.getCheckStatus(), is(CheckStatus.ACCEPTED)); assertThat(updatedAttachment.getCheckedBy(), is("test@example.org")); assertThat(updatedAttachment.getCheckedTeam(), is("tpd")); assertThat(updatedAttachment.getCheckedComment(), is("CCK2")); }
@Test public void testUpdateAttachmentsRemoveAll() { PortletRequest request = Mockito.mock(PortletRequest.class); new TestUserCacheHolder().enable(); Set<Attachment> documentAttachments = createAttachments("1", "2", "3"); setAttachmentIds(request); setAttachmentFilenames(request); setAttachmentTypes(request); setAttachmenCreatedComments(request); setAttachmentCheckStatus(request); setAttachmentCheckComment(request); Set<Attachment> updatedAttachments = PortletUtils.updateAttachmentsFromRequest(request, documentAttachments); assertThat(documentAttachments.size(), is(3)); assertTrue(updatedAttachments.isEmpty()); }