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 16, 2019
1 parent 6ed09be commit c3d7f5f
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,26 @@

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("/")))
.orElse(artifact.askFor(ArtifactFilename.class)
.flatMap(ArtifactFilename::getBestFilenameEntryGuess)
.map(ArtifactFilenameEntry::getFilename)
.orElse(""));
}

public static String createComponentVersion(Artifact artifact) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* 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.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.Test;
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;

public SW360ComponentAdapterUtilsTest(Artifact artifact, String expectedName, SW360ComponentType expectedType) {
this.artifact = artifact;
this.expectedName = expectedName;
this.expectedType = expectedType;
}

@Parameterized.Parameters
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
},
{
new Artifact()
.setProprietary(false)
.addFact(new ArtifactCoordinates(
new Coordinate(Coordinate.Types.NPM, "@organisation", "framework", "1.0"))),
"@organisation/framework",
SW360ComponentType.OSS
},
{
new Artifact()
.setProprietary(false)
.addFact(new ArtifactCoordinates(
new Coordinate(Coordinate.Types.NUGET, "Org.Example", "Library", "1.0"))),
"Org.Example/Library",
SW360ComponentType.OSS
},
{
new Artifact()
.setProprietary(false)
.addFact(new ArtifactFilename("library-filename.ext")),
"library-filename.ext",
SW360ComponentType.OSS
}
});
}

@Test
public void testCreateComponentFromArtifact() {
SW360Component component = new SW360Component();
SW360ComponentAdapterUtils.prepareComponent(component, artifact);

assertThat(component.getName()).isEqualTo(expectedName);
assertThat(component.getComponentType()).isEqualTo(expectedType);
}
}

0 comments on commit c3d7f5f

Please sign in to comment.