Adding version list methods and tests

This commit is contained in:
Martin Stockhammer 2020-03-08 21:18:50 +01:00
parent c2f785a209
commit f266538fc5
6 changed files with 123 additions and 6 deletions

View File

@ -125,6 +125,7 @@ public interface ManagedRepositoryContent extends RepositoryContent
*/
Version getVersion(ItemSelector versionCoordinates) throws ContentAccessException, IllegalArgumentException;
/**
* Returns the artifact object for the given coordinates.
*
@ -195,14 +196,38 @@ public interface ManagedRepositoryContent extends RepositoryContent
*/
List<? extends Project> getProjects( Namespace namespace) throws ContentAccessException;
/**
* Returns the list of projects that match the given selector. The selector must at least specify a
* a namespace.
*
* @param selector the selector
* @return the list of projects that match the selector. A empty list of not project matches.
* @throws ContentAccessException if the access to the storage backend failed
* @throws IllegalArgumentException if the selector does not contain sufficient data for selecting projects
*/
List<? extends Project> getProjects( ItemSelector selector ) throws ContentAccessException, IllegalArgumentException;
/**
* Return the existing versions of the given project.
*
* @param project the project
* @return a list of versions or a empty list, if not versions are available for the specified project
* @throws ContentAccessException if the access to the underlying storage failed
*/
List<? extends Version> getVersions( Project project) throws ContentAccessException;
/**
* Return the versions that match the given selector. The selector must at least specify a namespace and a projectId.
*
* @param selector the item selector. At least namespace and projectId must be set.
* @return the list of version or a empty list, if no version matches the selector
* @throws ContentAccessException if the access to the backend failed
* @throws IllegalArgumentException if the selector does not contain enough information for selecting versions
*/
List<? extends Version> getVersions( ItemSelector selector ) throws ContentAccessException, IllegalArgumentException;
/**
* Return all the artifacts of a given content item (namespace, project, version)
*

View File

@ -144,12 +144,24 @@ public class ManagedRepositoryContentMock implements ManagedRepositoryContent
return null;
}
@Override
public List<? extends Project> getProjects( ItemSelector selector ) throws ContentAccessException, IllegalArgumentException
{
return null;
}
@Override
public List<? extends Version> getVersions( Project project ) throws ContentAccessException
{
return null;
}
@Override
public List<? extends Version> getVersions( ItemSelector selector ) throws ContentAccessException, IllegalArgumentException
{
return null;
}
@Override
public List<? extends Artifact> getArtifacts( ContentItem item ) throws ContentAccessException
{

View File

@ -145,12 +145,24 @@ public class ManagedRepositoryContentMock implements ManagedRepositoryContent
return null;
}
@Override
public List<? extends Project> getProjects( ItemSelector selector ) throws ContentAccessException, IllegalArgumentException
{
return null;
}
@Override
public List<? extends Version> getVersions( Project project ) throws ContentAccessException
{
return null;
}
@Override
public List<? extends Version> getVersions( ItemSelector selector ) throws ContentAccessException, IllegalArgumentException
{
return null;
}
@Override
public List<? extends Artifact> getArtifacts( ContentItem item ) throws ContentAccessException
{

View File

@ -149,12 +149,24 @@ public class ManagedRepositoryContentMock implements ManagedRepositoryContent
return null;
}
@Override
public List<? extends Project> getProjects( ItemSelector selector ) throws ContentAccessException, IllegalArgumentException
{
return null;
}
@Override
public List<? extends Version> getVersions( Project project ) throws ContentAccessException
{
return null;
}
@Override
public List<? extends Version> getVersions( ItemSelector selector ) throws ContentAccessException, IllegalArgumentException
{
return null;
}
@Override
public List<? extends Artifact> getArtifacts( ContentItem item ) throws ContentAccessException
{

View File

@ -79,7 +79,9 @@ public class ManagedDefaultRepositoryContent
implements ManagedRepositoryContent
{
public static final String METADATA_FILENAME = "maven-metadata.xml";
// attribute flag that marks version objects that point to a snapshot artifact version
public static final String SNAPSHOT_ARTIFACT_VERSION = "maven.snav";
private FileTypes filetypes;
public void setFileTypes(FileTypes fileTypes) {
@ -391,10 +393,12 @@ public class ManagedDefaultRepositoryContent
private String type;
private String classifier;
private String contentType;
private StorageAsset asset;
}
private ArtifactInfo getArtifactInfoFromPath(String genericVersion, StorageAsset path) {
final ArtifactInfo info = new ArtifactInfo( );
info.asset = path;
info.id = path.getParent( ).getParent( ).getName( );
final String fileName = path.getName( );
if ( genericVersion.endsWith( "-" + SNAPSHOT ) )
@ -564,8 +568,16 @@ public class ManagedDefaultRepositoryContent
return null;
}
/*
TBD
@Override
public List<? extends Project> getProjects( ItemSelector selector ) throws ContentAccessException, IllegalArgumentException
{
return null;
}
/**
* Returns a version object for each directory that is a direct child of the project directory.
* @param project the project for which the versions should be returned
* @return the list of versions or a empty list, if not version was found
*/
@Override
public List<? extends Version> getVersions( final Project project )
@ -578,10 +590,41 @@ public class ManagedDefaultRepositoryContent
.collect( Collectors.toList( ) );
}
/*
TBD
/**
* If the selector specifies a version, all artifact versions are returned, which means for snapshot
* versions the artifact versions are returned too.
*
* @param selector the item selector. At least namespace and projectId must be set.
* @return the list of version objects or a empty list, if the selector does not match a version
* @throws ContentAccessException if the access to the underlying backend failed
* @throws IllegalArgumentException if the selector has no projectId specified
*/
@Override
public List<? extends Version> getVersions( final ItemSelector selector ) throws ContentAccessException, IllegalArgumentException
{
if (StringUtils.isEmpty( selector.getProjectId() )) {
log.error( "Bad item selector for version list: {}", selector );
throw new IllegalArgumentException( "Project ID not set, while retrieving versions." );
}
final Project project = getProject( selector );
if (selector.hasVersion()) {
final StorageAsset asset = getAsset( selector.getNamespace( ), selector.getProjectId( ), selector.getVersion( ) );
return asset.list( ).stream( ).map( a -> getArtifactInfoFromPath( selector.getVersion( ), a ) )
.filter( ai -> StringUtils.isNotEmpty( ai.version ) )
.map( v -> ArchivaVersion.withAsset( v.asset.getParent() )
.withProject( project ).withVersion( v.version )
.withAttribute(SNAPSHOT_ARTIFACT_VERSION,"true").build() )
.distinct()
.collect( Collectors.toList( ) );
} else {
return getVersions( project );
}
}
/*
TBD
*/
@Override
public List<? extends Artifact> getArtifacts( ContentItem item )
{
return null;

View File

@ -252,9 +252,10 @@ public class ManagedDefaultRepositoryContentTest
Set<String> testedVersionSet = repoContent.getVersions( reference );
// Sort the list (for asserts later)
final VersionComparator comparator = new VersionComparator( );
List<String> testedVersions = new ArrayList<>();
testedVersions.addAll( testedVersionSet );
Collections.sort( testedVersions, new VersionComparator() );
Collections.sort( testedVersions, comparator );
// Test the expected array of versions, to the actual tested versions
assertEquals( "Assert Versions: length/size", expectedVersions.length, testedVersions.size() );
@ -264,6 +265,18 @@ public class ManagedDefaultRepositoryContentTest
String actualVersion = testedVersions.get( i );
assertEquals( "Versions[" + i + "]", expectedVersions[i], actualVersion );
}
ItemSelector selector = ArchivaItemSelector.builder( )
.withNamespace( "org.apache.archiva.metadata.tests" )
.withProjectId( artifactId )
.withVersion( version )
.build( );
List<String> versions = repoContent.getVersions( selector ).stream()
.map(v -> v.getVersion()).sorted( comparator ).collect( Collectors.toList());
assertArrayEquals( expectedVersions, versions.toArray( ) );
}