From: Martin Stockhammer Date: Sun, 8 Mar 2020 20:18:50 +0000 (+0100) Subject: Adding version list methods and tests X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=f266538fc535beb140e6cd88c839e20c4c51d24e;p=archiva.git Adding version list methods and tests --- diff --git a/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/repository/ManagedRepositoryContent.java b/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/repository/ManagedRepositoryContent.java index 09e7621e6..9a25aa6b1 100644 --- a/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/repository/ManagedRepositoryContent.java +++ b/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/repository/ManagedRepositoryContent.java @@ -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 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 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 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 getVersions( ItemSelector selector ) throws ContentAccessException, IllegalArgumentException; + + /** * Return all the artifacts of a given content item (namespace, project, version) * diff --git a/archiva-modules/archiva-base/archiva-repository-layer/src/test/java/org/apache/archiva/repository/mock/ManagedRepositoryContentMock.java b/archiva-modules/archiva-base/archiva-repository-layer/src/test/java/org/apache/archiva/repository/mock/ManagedRepositoryContentMock.java index ac1444543..d58769bf4 100644 --- a/archiva-modules/archiva-base/archiva-repository-layer/src/test/java/org/apache/archiva/repository/mock/ManagedRepositoryContentMock.java +++ b/archiva-modules/archiva-base/archiva-repository-layer/src/test/java/org/apache/archiva/repository/mock/ManagedRepositoryContentMock.java @@ -144,12 +144,24 @@ public class ManagedRepositoryContentMock implements ManagedRepositoryContent return null; } + @Override + public List getProjects( ItemSelector selector ) throws ContentAccessException, IllegalArgumentException + { + return null; + } + @Override public List getVersions( Project project ) throws ContentAccessException { return null; } + @Override + public List getVersions( ItemSelector selector ) throws ContentAccessException, IllegalArgumentException + { + return null; + } + @Override public List getArtifacts( ContentItem item ) throws ContentAccessException { diff --git a/archiva-modules/archiva-base/archiva-repository-scanner/src/test/java/org/apache/archiva/repository/scanner/mock/ManagedRepositoryContentMock.java b/archiva-modules/archiva-base/archiva-repository-scanner/src/test/java/org/apache/archiva/repository/scanner/mock/ManagedRepositoryContentMock.java index d1ce70567..d0f8b2c44 100644 --- a/archiva-modules/archiva-base/archiva-repository-scanner/src/test/java/org/apache/archiva/repository/scanner/mock/ManagedRepositoryContentMock.java +++ b/archiva-modules/archiva-base/archiva-repository-scanner/src/test/java/org/apache/archiva/repository/scanner/mock/ManagedRepositoryContentMock.java @@ -145,12 +145,24 @@ public class ManagedRepositoryContentMock implements ManagedRepositoryContent return null; } + @Override + public List getProjects( ItemSelector selector ) throws ContentAccessException, IllegalArgumentException + { + return null; + } + @Override public List getVersions( Project project ) throws ContentAccessException { return null; } + @Override + public List getVersions( ItemSelector selector ) throws ContentAccessException, IllegalArgumentException + { + return null; + } + @Override public List getArtifacts( ContentItem item ) throws ContentAccessException { diff --git a/archiva-modules/archiva-maven/archiva-maven-proxy/src/test/java/org/apache/archiva/repository/mock/ManagedRepositoryContentMock.java b/archiva-modules/archiva-maven/archiva-maven-proxy/src/test/java/org/apache/archiva/repository/mock/ManagedRepositoryContentMock.java index 87db3d806..b1b990860 100644 --- a/archiva-modules/archiva-maven/archiva-maven-proxy/src/test/java/org/apache/archiva/repository/mock/ManagedRepositoryContentMock.java +++ b/archiva-modules/archiva-maven/archiva-maven-proxy/src/test/java/org/apache/archiva/repository/mock/ManagedRepositoryContentMock.java @@ -149,12 +149,24 @@ public class ManagedRepositoryContentMock implements ManagedRepositoryContent return null; } + @Override + public List getProjects( ItemSelector selector ) throws ContentAccessException, IllegalArgumentException + { + return null; + } + @Override public List getVersions( Project project ) throws ContentAccessException { return null; } + @Override + public List getVersions( ItemSelector selector ) throws ContentAccessException, IllegalArgumentException + { + return null; + } + @Override public List getArtifacts( ContentItem item ) throws ContentAccessException { diff --git a/archiva-modules/archiva-maven/archiva-maven-repository/src/main/java/org/apache/archiva/repository/maven/content/ManagedDefaultRepositoryContent.java b/archiva-modules/archiva-maven/archiva-maven-repository/src/main/java/org/apache/archiva/repository/maven/content/ManagedDefaultRepositoryContent.java index 6970d0eb5..e8f49c019 100644 --- a/archiva-modules/archiva-maven/archiva-maven-repository/src/main/java/org/apache/archiva/repository/maven/content/ManagedDefaultRepositoryContent.java +++ b/archiva-modules/archiva-maven/archiva-maven-repository/src/main/java/org/apache/archiva/repository/maven/content/ManagedDefaultRepositoryContent.java @@ -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 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 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 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 getArtifacts( ContentItem item ) { return null; diff --git a/archiva-modules/archiva-maven/archiva-maven-repository/src/test/java/org/apache/archiva/repository/maven/content/ManagedDefaultRepositoryContentTest.java b/archiva-modules/archiva-maven/archiva-maven-repository/src/test/java/org/apache/archiva/repository/maven/content/ManagedDefaultRepositoryContentTest.java index 588bb4726..c3fe7085f 100644 --- a/archiva-modules/archiva-maven/archiva-maven-repository/src/test/java/org/apache/archiva/repository/maven/content/ManagedDefaultRepositoryContentTest.java +++ b/archiva-modules/archiva-maven/archiva-maven-repository/src/test/java/org/apache/archiva/repository/maven/content/ManagedDefaultRepositoryContentTest.java @@ -252,9 +252,10 @@ public class ManagedDefaultRepositoryContentTest Set testedVersionSet = repoContent.getVersions( reference ); // Sort the list (for asserts later) + final VersionComparator comparator = new VersionComparator( ); List 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 versions = repoContent.getVersions( selector ).stream() + .map(v -> v.getVersion()).sorted( comparator ).collect( Collectors.toList()); + assertArrayEquals( expectedVersions, versions.toArray( ) ); + + }