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

Commit

Permalink
Including namespace of Coordinate in SW360Component name
Browse files Browse the repository at this point in the history
Fixes #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.

The SW360ComponentAdapterUtils::createComponentName method tries to
extract the component name from the ArtifactCoordinate fact first. If
it doesn't exist, it extracts it from the ArtifactFilename fact.
Otherwise it returns an empty string.

Signed-off-by: Onur Demirci <[email protected]>
  • Loading branch information
Onur Demirci authored and Onur Demirci committed Dec 18, 2019
1 parent b18aeba commit 52388d1
Show file tree
Hide file tree
Showing 5 changed files with 177 additions and 16 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,14 @@ 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.prettyPrint(), e.getMessage());
LOGGER.debug("Error: ", e);
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,19 +10,31 @@
*/
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;
import org.eclipse.sw360.antenna.model.artifact.facts.ArtifactFilename.ArtifactFilenameEntry;
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.rest.resource.releases.SW360Release;

import java.util.stream.Collectors;
import java.util.stream.Stream;

public class SW360ComponentAdapterUtils {
public static String createComponentName(Artifact artifact) {
return artifact.askFor(ArtifactCoordinates.class)
.map(ArtifactCoordinates::getMainCoordinate)
.map(Coordinate::getName)
.orElse(artifact.toString()); // TODO: ugly hack
.map(o -> Stream.of(o.getNamespace(), o.getName()))
.map(o -> o.collect(Collectors.joining("/")))
.orElseGet(() -> artifact.askFor(ArtifactFilename.class)
.flatMap(ArtifactFilename::getBestFilenameEntryGuess)
.map(ArtifactFilenameEntry::getFilename)
.orElseThrow(() -> new ExecutionException("Artifact " + artifact.prettyPrint() + " does not have " +
"enough facts to create a component name. Please provide more information " +
"by an analyzer (" + artifact.getAnalysisSource() + ")")));
}

public static String createComponentVersion(Artifact artifact) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
package org.eclipse.sw360.antenna.sw360.workflow.generators;

import org.eclipse.sw360.antenna.api.IAttachable;
import org.eclipse.sw360.antenna.api.exceptions.ExecutionException;
import org.eclipse.sw360.antenna.model.artifact.Artifact;
import org.eclipse.sw360.antenna.model.util.ArtifactLicenseUtils;
import org.eclipse.sw360.antenna.model.xml.generated.License;
Expand All @@ -20,12 +21,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 +54,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(ExecutionException e) {
LOGGER.error("Release will not be created in SW360. Reason: {}", e.getMessage());
LOGGER.debug("Error: ", e);
}
releases.add(sw360ReleaseFinal);
}

sw360MetaDataUpdater.createProject(projectName, projectVersion, releases);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/*
* Copyright (c) Bosch Software Innovations GmbH 2019.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v20.html
*
* SPDX-License-Identifier: EPL-2.0
*/
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;

import java.util.Arrays;
import java.util.Collection;

import static org.assertj.core.api.Assertions.assertThat;

@RunWith(Parameterized.class)
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,
Class<? extends Exception> expectedException, String expectedExceptionMsg) {
this.artifact = artifact;
this.expectedName = expectedName;
this.expectedType = expectedType;
this.expectedException = expectedException;
this.expectedExceptionMsg = expectedExceptionMsg;

}

@Parameterized.Parameters(name = "{index}: input={0}")
public static Collection<Object[]> data() {
return Arrays.asList(new Object[][] {
{
new Artifact()
.setProprietary(true)
.addFact(new ArtifactCoordinates(
new Coordinate(Coordinate.Types.MAVEN, "org.example", "lib", "1.0"))),
"org.example/lib",
SW360ComponentType.INTERNAL,
null, null
},
{
new Artifact()
.setProprietary(false)
.addFact(new ArtifactCoordinates(
new Coordinate(Coordinate.Types.NPM, "@organisation", "framework", "1.0"))),
"@organisation/framework",
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,
null, null
},
{
new Artifact()
.setProprietary(false)
.addFact(new ArtifactFilename("library-filename.ext")),
"library-filename.ext",
SW360ComponentType.OSS,
null, null
},
{
new Artifact()
.setProprietary(false),
null,
SW360ComponentType.OSS,
ExecutionException.class, "does not have enough facts to create a component name."
}
});
}

@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);
assertThat(component.getComponentType()).isEqualTo(expectedType);
}
}
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 52388d1

Please sign in to comment.