ComponentImportUtils { public static RequestSummary writeToDatabase(Iterable<ComponentCSVRecord> compCSVRecords, ComponentService.Iface componentClient, VendorService.Iface vendorClient, AttachmentService.Iface attachmentClient, User user) throws TException { Map<String, String> vendorNameToVendorId = getVendorNameToId(compCSVRecords, vendorClient); log.debug(format("Read vendors: (%d) %s ", vendorNameToVendorId.size(), vendorNameToVendorId)); final RequestSummary componentRequestSummary = updateComponents(compCSVRecords, componentClient, user); Map<String, String> componentNameToId = new HashMap<>(); final ArrayList<Release> releases = new ArrayList<>(); for (Component component : componentClient.getComponentDetailedSummaryForExport()) { componentNameToId.put(component.getName(), component.getId()); final List<Release> componentReleases = component.getReleases(); if (componentReleases != null && componentReleases.size() > 0) releases.addAll(componentReleases); } Set<String> knownReleaseIdentifiers = Sets.newHashSet(getReleaseIdentifiers(releases)); List<ComponentCSVRecord> relevantCSVRecords = new ArrayList<>(); final HashMap<String, List<String>> releaseIdentifierToDownloadURL = new HashMap<>(); List<AttachmentContent> attachmentContentsToUpdate = new ArrayList<>(); filterRelevantCSVRecordsAndGetAttachmentContents(compCSVRecords, componentNameToId, knownReleaseIdentifiers, relevantCSVRecords, releaseIdentifierToDownloadURL, attachmentContentsToUpdate); attachmentContentsToUpdate = attachmentClient.makeAttachmentContents(attachmentContentsToUpdate); final ImmutableMap<String, AttachmentContent> URLtoAttachment = Maps.uniqueIndex(attachmentContentsToUpdate, new Function<AttachmentContent, String>() { @Override public String apply(AttachmentContent input) { return input.getRemoteUrl(); } }); Set<Release> releasesToUpdate = new HashSet<>(); for (ComponentCSVRecord componentCSVRecord : relevantCSVRecords) { String releaseIdentifier = componentCSVRecord.getReleaseIdentifier(); String vendorName = componentCSVRecord.getVendorName(); String vendorId = vendorNameToVendorId.get(vendorName); String componentId = componentNameToId.get(componentCSVRecord.getComponentName()); List<AttachmentContent> attachmentContents = getAttachmentContents(releaseIdentifierToDownloadURL, URLtoAttachment, releaseIdentifier); Release releaseToAdd = componentCSVRecord.getRelease(vendorId, componentId, attachmentContents); knownReleaseIdentifiers.add(releaseIdentifier); if (releaseToAdd != null) { releasesToUpdate.add(releaseToAdd); } } final RequestSummary releaseRequestSummary = componentClient.updateReleases(releasesToUpdate, user); return CommonUtils.addRequestSummaries(componentRequestSummary, "component", releaseRequestSummary, "release"); } private  ComponentImportUtils(); static URL getURL(String filename); static RequestSummary writeReleaseLinksToDatabase(FluentIterable<ReleaseLinkCSVRecord> csvRecords, ComponentService.Iface componentClient, User user); static RequestSummary writeAttachmentsToDatabase(FluentIterable<ComponentAttachmentCSVRecord> compCSVRecords,
                                                            User user, ComponentService.Iface componentClient, AttachmentService.Iface attachmentClient); @NotNull static Map<String, Release> getReleasesById(List<Component> componentDetailedSummaryForExport); @NotNull static Map<String, Release> getReleasesByIdentifier(List<Component> componentDetailedSummaryForExport); static RequestSummary writeToDatabase(Iterable<ComponentCSVRecord> compCSVRecords,
                                                 ComponentService.Iface componentClient, VendorService.Iface vendorClient,
                                                 AttachmentService.Iface attachmentClient, User user); static FluentIterable<ComponentCSVRecord> convertCSVRecordsToCompCSVRecords(List<CSVRecord> in); static FluentIterable<ComponentAttachmentCSVRecord> convertCSVRecordsToComponentAttachmentCSVRecords(List<CSVRecord> in); static FluentIterable<ReleaseLinkCSVRecord> convertCSVRecordsToReleaseLinkCSVRecords(List<CSVRecord> in); @NotNull static Map<String, String> getVendorNameToId(Iterable<ComponentCSVRecord> compCSVRecords,
                                                        VendorService.Iface vendorClient); @NotNull static List<String> getReleaseIdentifiers(List<Release> releaseSummary); @NotNull static List<Iterable<String>> getFlattenedView(List<Component> componentDetailedSummaryForExport); static boolean isNotProcessed(Set<String> exportedComponentIdentifiers, Component component); static boolean isNotProcessed(Set<String> exportedReleaseIdentifiers, Release release);  }
@Test public void testImportTwiceWithOnlyAPart() throws Exception { FluentIterable<ComponentCSVRecord> compCSVRecords = getCompCSVRecordsFromTestFile(fileName); ComponentImportUtils.writeToDatabase(compCSVRecords.limit(1), componentClient, vendorClient, attachmentClient, user); assertThat(componentClient.getComponentSummary(user), hasSize(1)); List<Release> releaseSummary = componentClient.getReleaseSummary(user); assertThat(releaseSummary, hasSize(1)); assertThat(releaseSummary.get(0).getName(), is("7-Zip")); ComponentImportUtils.writeToDatabase(compCSVRecords, componentClient, vendorClient, attachmentClient, user); assertExpectedComponentsInDb(); }
@Test public void testImportTwiceIsANoOp() throws Exception { FluentIterable<ComponentCSVRecord> compCSVRecords = getCompCSVRecordsFromTestFile(fileName); assertThat(componentClient.getComponentSummary(user), hasSize(0)); assertThat(componentClient.getReleaseSummary(user), hasSize(0)); assertThat(attachmentRepository.getAll(), Matchers.hasSize(0) ); ComponentImportUtils.writeToDatabase(compCSVRecords, componentClient, vendorClient, attachmentClient, user); assertThat(attachmentRepository.getAll(), Matchers.hasSize(1) ); List<Component> componentSummaryAfterFirst = componentClient.getComponentSummary(user); List<Release> releaseSummaryAfterFirst = componentClient.getReleaseSummary(user); assertExpectedComponentsInDb(); ComponentImportUtils.writeToDatabase(compCSVRecords, componentClient, vendorClient, attachmentClient, user); assertExpectedComponentsInDb(); assertThat(attachmentRepository.getAll(), Matchers.hasSize(1) ); assertThat(componentClient.getComponentSummary(user), is(componentSummaryAfterFirst)); assertThat(componentClient.getReleaseSummary(user), is(releaseSummaryAfterFirst)); }