Skip to content
This repository has been archived by the owner on Mar 17, 2021. It is now read-only.

Commit

Permalink
fixup! Including namespace of Coordinate in SW360Component name Fixes #…
Browse files Browse the repository at this point in the history
…371 Creating the name of the SW360Component with the namespace and name of the Coordinate fact of the artifact using a '/' as the delimiter identical to the PURLs.
  • Loading branch information
Onur Demirci authored and Onur Demirci committed Dec 17, 2019
1 parent cf3fe16 commit 3b5aef2
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import org.eclipse.sw360.antenna.sw360.rest.resource.components.SW360SparseComponent;
import org.eclipse.sw360.antenna.sw360.utils.SW360ComponentAdapterUtils;
import org.eclipse.sw360.antenna.util.ProxySettings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpHeaders;

import java.util.ArrayList;
Expand All @@ -26,6 +28,8 @@
import java.util.stream.Collectors;

public class SW360ComponentClientAdapter {
private static final Logger LOGGER = LoggerFactory.getLogger(SW360ComponentClientAdapter.class);

private final SW360ComponentClient componentClient;

public SW360ComponentClientAdapter(String restUrl, ProxySettings proxySettings) {
Expand All @@ -52,9 +56,13 @@ public Optional<SW360Component> getComponentById(String componentId, HttpHeaders
}

public Optional<SW360Component> getComponentByArtifact(Artifact artifact, HttpHeaders header) {
String componentName = SW360ComponentAdapterUtils.createComponentName(artifact);

return getComponentByName(componentName, header);
try {
String componentName = SW360ComponentAdapterUtils.createComponentName(artifact);
return getComponentByName(componentName, header);
} catch (ExecutionException e) {
LOGGER.debug("No component found for {}. Reason: {}", artifact, e.getMessage());
return Optional.empty();
}
}

public Optional<SW360Component> getComponentByName(String componentName, HttpHeaders header) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
*/
package org.eclipse.sw360.antenna.sw360.utils;

import org.eclipse.sw360.antenna.api.exceptions.ExecutionException;
import org.eclipse.sw360.antenna.model.artifact.Artifact;
import org.eclipse.sw360.antenna.model.artifact.ArtifactCoordinates;
import org.eclipse.sw360.antenna.model.artifact.facts.ArtifactFilename;
Expand All @@ -28,10 +29,10 @@ public static String createComponentName(Artifact artifact) {
.map(ArtifactCoordinates::getMainCoordinate)
.map(o -> Stream.of(o.getNamespace(), o.getName()))
.map(o -> o.collect(Collectors.joining("/")))
.orElse(artifact.askFor(ArtifactFilename.class)
.orElseGet(() -> artifact.askFor(ArtifactFilename.class)
.flatMap(ArtifactFilename::getBestFilenameEntryGuess)
.map(ArtifactFilenameEntry::getFilename)
.orElse(""));
.orElseThrow(() -> new ExecutionException("No suitable component name found.")));
}

public static String createComponentVersion(Artifact artifact) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,16 @@
import org.eclipse.sw360.antenna.sw360.rest.resource.releases.SW360Release;
import org.eclipse.sw360.antenna.sw360.utils.SW360AttachmentAdapterUtils;
import org.eclipse.sw360.antenna.sw360.utils.SW360ReleaseAdapterUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.nio.file.Path;
import java.util.*;
import java.util.stream.Collectors;

public class SW360UpdaterImpl {
private static final Logger LOGGER = LoggerFactory.getLogger(SW360UpdaterImpl.class);

private final String projectName;
private final String projectVersion;
private final SW360MetaDataUpdater sw360MetaDataUpdater;
Expand All @@ -49,19 +53,23 @@ public Map<String, IAttachable> produce(Collection<Artifact> intermediates) {
.map(SW360License::getShortName)
.collect(Collectors.toSet());
}

final SW360Release sw360ReleaseFromArtifact = SW360ReleaseAdapterUtils.convertToRelease(artifact);
sw360ReleaseFromArtifact.setMainLicenseIds(licenseIds);
SW360Release sw360ReleaseFinal = sw360MetaDataUpdater.getOrCreateRelease(sw360ReleaseFromArtifact);
if (sw360MetaDataUpdater.isUploadSources()
&& sw360ReleaseFinal.get_Links().getSelf() != null
&& !sw360ReleaseFinal.get_Links().getSelf().getHref().isEmpty()) {
Map<Path, SW360AttachmentType> attachments = SW360AttachmentAdapterUtils.getAttachmentsFromArtifact(artifact);
if (!attachments.isEmpty()) {
sw360ReleaseFinal = sw360MetaDataUpdater.uploadAttachments(sw360ReleaseFinal, attachments);
try {
final SW360Release sw360ReleaseFromArtifact = SW360ReleaseAdapterUtils.convertToRelease(artifact);
sw360ReleaseFromArtifact.setMainLicenseIds(licenseIds);
SW360Release sw360ReleaseFinal = sw360MetaDataUpdater.getOrCreateRelease(sw360ReleaseFromArtifact);
if (sw360MetaDataUpdater.isUploadSources()
&& sw360ReleaseFinal.get_Links().getSelf() != null
&& !sw360ReleaseFinal.get_Links().getSelf().getHref().isEmpty()) {
Map<Path, SW360AttachmentType> attachments = SW360AttachmentAdapterUtils.getAttachmentsFromArtifact(artifact);
if (!attachments.isEmpty()) {
sw360ReleaseFinal = sw360MetaDataUpdater.uploadAttachments(sw360ReleaseFinal, attachments);
}
}
releases.add(sw360ReleaseFinal);
} catch(Exception e) {
LOGGER.warn("Release for {} will not be created in SW360 due to missing facts. Reason: {}",
artifact, e.getMessage());
}
releases.add(sw360ReleaseFinal);
}

sw360MetaDataUpdater.createProject(projectName, projectVersion, releases);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,17 @@
*/
package org.eclipse.sw360.antenna.sw360.rest.utils;

import org.eclipse.sw360.antenna.api.exceptions.ExecutionException;
import org.eclipse.sw360.antenna.model.artifact.Artifact;
import org.eclipse.sw360.antenna.model.artifact.ArtifactCoordinates;
import org.eclipse.sw360.antenna.model.artifact.facts.ArtifactFilename;
import org.eclipse.sw360.antenna.model.coordinates.Coordinate;
import org.eclipse.sw360.antenna.sw360.rest.resource.components.SW360Component;
import org.eclipse.sw360.antenna.sw360.rest.resource.components.SW360ComponentType;
import org.eclipse.sw360.antenna.sw360.utils.SW360ComponentAdapterUtils;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

Expand All @@ -31,14 +34,20 @@ public class SW360ComponentAdapterUtilsTest {
private Artifact artifact;
private String expectedName;
private SW360ComponentType expectedType;
private Class<? extends Exception> expectedException;
private String expectedExceptionMsg;

public SW360ComponentAdapterUtilsTest(Artifact artifact, String expectedName, SW360ComponentType expectedType) {
public SW360ComponentAdapterUtilsTest(Artifact artifact, String expectedName, SW360ComponentType expectedType,
Class<? extends Exception> expectedException, String expectedExceptionMsg) {
this.artifact = artifact;
this.expectedName = expectedName;
this.expectedType = expectedType;
this.expectedException = expectedException;
this.expectedExceptionMsg = expectedExceptionMsg;

}

@Parameterized.Parameters
@Parameterized.Parameters(name = "{index}: input={0}")
public static Collection<Object[]> data() {
return Arrays.asList(new Object[][] {
{
Expand All @@ -47,37 +56,57 @@ public static Collection<Object[]> data() {
.addFact(new ArtifactCoordinates(
new Coordinate(Coordinate.Types.MAVEN, "org.example", "lib", "1.0"))),
"org.example/lib",
SW360ComponentType.INTERNAL
SW360ComponentType.INTERNAL,
null, null
},
{
new Artifact()
.setProprietary(false)
.addFact(new ArtifactCoordinates(
new Coordinate(Coordinate.Types.NPM, "@organisation", "framework", "1.0"))),
"@organisation/framework",
SW360ComponentType.OSS
SW360ComponentType.OSS,
null, null
},
{
new Artifact()
.setProprietary(false)
.addFact(new ArtifactCoordinates(
new Coordinate(Coordinate.Types.NUGET, "Org.Example", "Library", "1.0"))),
"Org.Example/Library",
SW360ComponentType.OSS
SW360ComponentType.OSS,
null, null
},
{
new Artifact()
.setProprietary(false)
.addFact(new ArtifactFilename("library-filename.ext")),
"library-filename.ext",
SW360ComponentType.OSS
SW360ComponentType.OSS,
null, null
},
{
new Artifact()
.setProprietary(false),
null,
SW360ComponentType.OSS,
ExecutionException.class, ""
}
});
}

@Rule
public ExpectedException thrownException = ExpectedException.none();

@Test
public void testCreateComponentFromArtifact() {
SW360Component component = new SW360Component();

if (expectedException != null) {
thrownException.expect(expectedException);
thrownException.expectMessage(expectedExceptionMsg);
}

SW360ComponentAdapterUtils.prepareComponent(component, artifact);

assertThat(component.getName()).isEqualTo(expectedName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
*/
package org.eclipse.sw360.antenna.sw360.workflow.generator;

import org.eclipse.sw360.antenna.api.exceptions.ExecutionException;
import org.eclipse.sw360.antenna.model.artifact.Artifact;
import org.eclipse.sw360.antenna.model.artifact.facts.*;
import org.eclipse.sw360.antenna.model.coordinates.Coordinate;
Expand All @@ -20,7 +21,9 @@
import org.eclipse.sw360.antenna.sw360.rest.resource.releases.SW360Release;
import org.eclipse.sw360.antenna.sw360.utils.SW360ReleaseAdapterUtils;
import org.eclipse.sw360.antenna.testing.AntennaTestWithMockedContext;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import java.util.Collection;
import java.util.Collections;
Expand All @@ -32,6 +35,9 @@

public class SW360UpdaterTest extends AntennaTestWithMockedContext {

@Rule
public ExpectedException thrownException = ExpectedException.none();

private String sourceUrl = "https://thrift.apache.org/";
private String releaseTagUrl = "https://github.com/apache/thrift/releases/tag/0.10.0";
private String swhID = "swh:1:rel:ae93ff0b4bdbd6749f75c23ad23311b512230894";
Expand Down Expand Up @@ -178,4 +184,14 @@ public void testMainLicenseIsEmptyAndDetectedLicenseNull() {
assertThat(release.getMainLicenseIds().isEmpty()).isTrue();
}

@Test
public void testArtifactWithoutFacts() {
Artifact artifact = new Artifact()
.setProprietary(false);

thrownException.expect(ExecutionException.class);
thrownException.expectMessage("No suitable component name found.");

SW360ReleaseAdapterUtils.convertToRelease(artifact);
}
}

0 comments on commit 3b5aef2

Please sign in to comment.