From 3dcb07c94087e266c0b0d474603a5b8e6b5ed8f5 Mon Sep 17 00:00:00 2001 From: Oliver Heger <60134199+oheger-bosch@users.noreply.github.com> Date: Tue, 5 May 2020 11:39:26 +0200 Subject: [PATCH] SW360Updater now catches and logs exceptions during processing. Issue: eclipse#502. If an exception during processing of an artifact occurs, the update process should not be stopped, but continue with the next artifact. Signed-off-by: Oliver Heger <60134199+oheger-bosch@users.noreply.github.com> --- .../sw360/updater/SW360Updater.java | 28 +++++++++++++------ .../sw360/updater/SW360UpdaterTest.java | 18 ++++++------ 2 files changed, 29 insertions(+), 17 deletions(-) diff --git a/assembly/compliance-tool/src/main/java/org/eclipse/sw360/antenna/frontend/compliancetool/sw360/updater/SW360Updater.java b/assembly/compliance-tool/src/main/java/org/eclipse/sw360/antenna/frontend/compliancetool/sw360/updater/SW360Updater.java index f535e5f3b..37f61bf88 100644 --- a/assembly/compliance-tool/src/main/java/org/eclipse/sw360/antenna/frontend/compliancetool/sw360/updater/SW360Updater.java +++ b/assembly/compliance-tool/src/main/java/org/eclipse/sw360/antenna/frontend/compliancetool/sw360/updater/SW360Updater.java @@ -17,7 +17,10 @@ import org.eclipse.sw360.antenna.sw360.client.adapter.SW360ReleaseClientAdapter; import org.eclipse.sw360.antenna.sw360.client.rest.resource.attachments.SW360AttachmentType; import org.eclipse.sw360.antenna.sw360.client.rest.resource.releases.SW360Release; +import org.eclipse.sw360.antenna.sw360.client.utils.SW360ClientException; import org.eclipse.sw360.antenna.sw360.workflow.generators.SW360UpdaterImpl; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.nio.file.Path; import java.util.Collection; @@ -28,6 +31,8 @@ import static org.eclipse.sw360.antenna.frontend.compliancetool.sw360.ComplianceFeatureUtils.getArtifactsFromCsvFile; public class SW360Updater { + private static final Logger LOG = LoggerFactory.getLogger(SW360Updater.class); + private final SW360UpdaterImpl updater; private final SW360Configuration configuration; private final ClearingReportGenerator generator; @@ -46,16 +51,21 @@ public void execute() { } private void uploadReleaseWithClearingDocumentFromArtifact(Artifact artifact) { - SW360Release release = updater.artifactToReleaseInSW360(artifact); - SW360ReleaseClientAdapter releaseClientAdapter = configuration.getConnection().getReleaseAdapter(); + LOG.info("Processing {}.", artifact); + try { + SW360Release release = updater.artifactToReleaseInSW360(artifact); + SW360ReleaseClientAdapter releaseClientAdapter = configuration.getConnection().getReleaseAdapter(); - if (release.getClearingState() != null && - !release.getClearingState().isEmpty() && - ArtifactClearingState.ClearingState.valueOf(release.getClearingState()) != ArtifactClearingState.ClearingState.INITIAL) { - Map attachmentPathMap = - Collections.singletonMap(getOrGenerateClearingDocument(release, artifact), - SW360AttachmentType.CLEARING_REPORT); - releaseClientAdapter.uploadAttachments(release, attachmentPathMap); + if (release.getClearingState() != null && + !release.getClearingState().isEmpty() && + ArtifactClearingState.ClearingState.valueOf(release.getClearingState()) != ArtifactClearingState.ClearingState.INITIAL) { + Map attachmentPathMap = + Collections.singletonMap(getOrGenerateClearingDocument(release, artifact), + SW360AttachmentType.CLEARING_REPORT); + releaseClientAdapter.uploadAttachments(release, attachmentPathMap); + } + } catch (SW360ClientException e) { + LOG.error("Failed to process artifact {}.", artifact, e); } } diff --git a/assembly/compliance-tool/src/test/java/org/eclipse/sw360/antenna/frontend/compliancetool/sw360/updater/SW360UpdaterTest.java b/assembly/compliance-tool/src/test/java/org/eclipse/sw360/antenna/frontend/compliancetool/sw360/updater/SW360UpdaterTest.java index 8f11725aa..113211308 100644 --- a/assembly/compliance-tool/src/test/java/org/eclipse/sw360/antenna/frontend/compliancetool/sw360/updater/SW360UpdaterTest.java +++ b/assembly/compliance-tool/src/test/java/org/eclipse/sw360/antenna/frontend/compliancetool/sw360/updater/SW360UpdaterTest.java @@ -12,10 +12,11 @@ import org.eclipse.sw360.antenna.frontend.compliancetool.sw360.ComplianceFeatureUtils; import org.eclipse.sw360.antenna.frontend.compliancetool.sw360.SW360Configuration; -import org.eclipse.sw360.antenna.sw360.client.adapter.SW360ReleaseClientAdapter; import org.eclipse.sw360.antenna.sw360.client.adapter.SW360Connection; +import org.eclipse.sw360.antenna.sw360.client.adapter.SW360ReleaseClientAdapter; import org.eclipse.sw360.antenna.sw360.client.rest.resource.attachments.SW360AttachmentType; import org.eclipse.sw360.antenna.sw360.client.rest.resource.releases.SW360Release; +import org.eclipse.sw360.antenna.sw360.client.utils.SW360ClientException; import org.eclipse.sw360.antenna.sw360.workflow.generators.SW360UpdaterImpl; import org.jetbrains.annotations.NotNull; import org.junit.Rule; @@ -50,8 +51,8 @@ public class SW360UpdaterTest { @Rule public TemporaryFolder folder = new TemporaryFolder(); - private SW360Configuration configurationMock = mock(SW360Configuration.class); - private SW360ReleaseClientAdapter releaseClientAdapter = mock(SW360ReleaseClientAdapter.class); + private final SW360Configuration configurationMock = mock(SW360Configuration.class); + private final SW360ReleaseClientAdapter releaseClientAdapter = mock(SW360ReleaseClientAdapter.class); private final String clearingState; private final boolean clearingDocAvailable; @@ -133,12 +134,11 @@ public void testExecute() throws IOException { initConnectionConfiguration(); SW360UpdaterImpl updater = mock(SW360UpdaterImpl.class); - when(updater.artifactToReleaseInSW360(any())) - .thenReturn(new SW360Release()); SW360Release release = new SW360Release(); release.setClearingState(clearingState); when(updater.artifactToReleaseInSW360(any())) - .thenReturn(release); + .thenReturn(release) + .thenThrow(new SW360ClientException("Boo")); ClearingReportGenerator generator = mock(ClearingReportGenerator.class); when(generator.createClearingDocument(any(), any())) @@ -153,7 +153,7 @@ public void testExecute() throws IOException { if (expectUpload && !clearingDocAvailable) { verify(generator).createClearingDocument(release, getTargetDir()); } - verify(updater).artifactToReleaseInSW360(any()); + verify(updater, times(2)).artifactToReleaseInSW360(any()); verify(releaseClientAdapter, times(expectUpload ? 1 : 0)).uploadAttachments(any(), eq(testAttachmentMap)); } @@ -177,7 +177,9 @@ private Path writeCsvFile() throws IOException { clearingDocPath = clearingDoc.getPath(); } String csvContent = String.format("Artifact Id,Group Id,Version,Coordinate Type,Clearing State,Clearing Document%s" + - "test,test,x.x.x,mvn,%s,%s%s", System.lineSeparator(), clearingState, clearingDocPath, System.lineSeparator()); + "test,test,x.x.x,mvn,%s,%s%s" + + "error,error,y.y.y,mvn,%s,%s%s", System.lineSeparator(), clearingState, clearingDocPath, + System.lineSeparator(), clearingState, clearingDocPath, System.lineSeparator()); Files.write(tempCsvFile.toPath(), csvContent.getBytes(StandardCharsets.UTF_8)); return tempCsvFile.toPath(); }