ProjectPortletUtils { public static Map<String, Set<LicenseNameWithText>> getExcludedLicensesPerAttachmentIdFromRequest(Set<String> attachmentContentIds, ResourceRequest request) { Map<String, Set<LicenseNameWithText>> excludedLicenses = Maps.newHashMap(); for (String attachmentContentId : attachmentContentIds) { String[] checkboxes = request.getParameterValues(attachmentContentId); String[] keys = request.getParameterValues(attachmentContentId + "_key"); if (checkboxes == null) { continue; } @SuppressWarnings("unchecked") Map<String, LicenseNameWithText> licenseStore = (Map<String, LicenseNameWithText>) request.getPortletSession() .getAttribute(ProjectPortlet.LICENSE_STORE_KEY_PREFIX + attachmentContentId); if (licenseStore == null) { throw new IllegalStateException("No license store found for attachment content id " + attachmentContentId); } Set<Integer> includedIds = Arrays.stream(checkboxes).map(s -> Integer.valueOf(s)).collect(Collectors.toSet()); Set<LicenseNameWithText> licenseNameWithTexts = Sets.newHashSet(); for (int index = 0; index < keys.length; index++) { if (includedIds.contains(index)) { continue; } LicenseNameWithText licenseNameWithText = licenseStore.get(keys[index]); if (licenseNameWithText == null) { throw new IllegalStateException("No license found for key " + keys[index]); } licenseNameWithTexts.add(licenseNameWithText); } excludedLicenses.put(attachmentContentId, licenseNameWithTexts); } return excludedLicenses; } private  ProjectPortletUtils(); static void updateProjectFromRequest(PortletRequest request, Project project); static ProjectVulnerabilityRating updateProjectVulnerabilityRatingFromRequest(Optional<ProjectVulnerabilityRating> projectVulnerabilityRatings, ResourceRequest request); static Map<String, Set<String>> getSelectedReleaseAndAttachmentIdsFromRequest(ResourceRequest request); static Map<String, Set<LicenseNameWithText>> getExcludedLicensesPerAttachmentIdFromRequest(Set<String> attachmentContentIds,
                                                                                                      ResourceRequest request); static List<AttachmentUsage> makeAttachmentUsages(Project project, Map<String, Set<String>> selectedReleaseAndAttachmentIds,
            Map<String, Set<LicenseNameWithText>> excludedLicensesPerAttachmentId); static Map<Source, Set<String>> extractContainedAttachments(Collection<ProjectLink> projectLinks);  }
@Test public void testGetExcludedLicensesPerAttachmantIdFromRequest() { Mockito.when(request.getParameterValues("a1")).thenReturn(new String[] { "1", "2" }); Mockito.when(request.getParameterValues("a2")).thenReturn(new String[] {}); Mockito.when(request.getParameterValues("a3")).thenReturn(new String[] { "0", "1", "2", "3", "4" }); Mockito.when(request.getParameterValues("a1_key")).thenReturn(new String[] { "a1l1", "a1l2", "a1l3", "a1l4" }); Mockito.when(request.getParameterValues("a2_key")).thenReturn(new String[] { "a2l1", "a2l2", "a2l3" }); Mockito.when(request.getParameterValues("a3_key")).thenReturn(new String[] { "a3l1", "a3l2", "a3l3", "a3l4", "a3l5" }); Mockito.when(request.getPortletSession()).thenReturn(portletSession); Mockito.when(portletSession.getAttribute("license-store-a1")).thenReturn(ImmutableMap.of( "a1l1", createLicense("a1l1", "a1l1_t"), "a1l2", createLicense("a1l2", "a1l2_t"), "a1l3", createLicense("a1l3", "a1l3_t"), "a1l4", createLicense("a1l4", "a1l4_t") )); Mockito.when(portletSession.getAttribute("license-store-a2")).thenReturn(ImmutableMap.of( "a2l1", createLicense("a2l1", "a2l1_t"), "a2l2", createLicense("a2l2", "a2l2_t"), "a2l3", createLicense("a2l3", "a2l3_t") )); Mockito.when(portletSession.getAttribute("license-store-a3")).thenReturn(ImmutableMap.of( "a3l1", createLicense("a3l1", "a3l1_t"), "a3l2", createLicense("a3l2", "a3l2_t"), "a3l3", createLicense("a3l3", "a3l3_t"), "a3l4", createLicense("a3l4", "a3l4_t"), "a3l5", createLicense("a3l5", "a3l5_t") )); Map<String, Set<LicenseNameWithText>> excludedLicenses = ProjectPortletUtils .getExcludedLicensesPerAttachmentIdFromRequest(ImmutableSet.of("a1", "a2", "a3"), request); Assert.assertThat(excludedLicenses.keySet(), Matchers.containsInAnyOrder("a1", "a2", "a3")); Assert.assertThat(excludedLicenses.get("a1"), Matchers.containsInAnyOrder(createLicense("a1l1", "a1l1_t"), createLicense("a1l4", "a1l4_t"))); Assert.assertThat(excludedLicenses.get("a2"), Matchers.containsInAnyOrder(createLicense("a2l1", "a2l1_t"), createLicense("a2l2", "a2l2_t"), createLicense("a2l3", "a2l3_t"))); Assert.assertTrue(excludedLicenses.get("a3").isEmpty()); }