diff options
author | Carlos Sanchez <csanchez@maestrodev.com> | 2014-11-05 20:11:08 +0100 |
---|---|---|
committer | Carlos Sanchez <carlos@apache.org> | 2014-12-01 16:48:13 +0100 |
commit | e4da1fa6f5a56bbf63bae1de7d1b7dd0ae0375aa (patch) | |
tree | 6477dd5905f983cc3f4cd83580795ab8a7be9ef8 /archiva-modules/metadata | |
parent | d9473241c956359325aa10bdbff4e39be284c559 (diff) | |
download | archiva-e4da1fa6f5a56bbf63bae1de7d1b7dd0ae0375aa.tar.gz archiva-e4da1fa6f5a56bbf63bae1de7d1b7dd0ae0375aa.zip |
[MRM-1390] Add REST methods to search JCR store for generic metadata and properties
Added REST methods:
artifactsByProjectVersionMetadata/{key}/{value}
artifactsByMetadata/{key}/{value}
artifactsByProperty/{key}/{value}
searchArtifacts/{text}
searchArtifacts/{key}/{text}
In JCR implementation When searching into any property (key = nil) we can't do exact searchs
Diffstat (limited to 'archiva-modules/metadata')
4 files changed, 342 insertions, 23 deletions
diff --git a/archiva-modules/metadata/metadata-repository-api/pom.xml b/archiva-modules/metadata/metadata-repository-api/pom.xml index 8cfd25d68..face82553 100644 --- a/archiva-modules/metadata/metadata-repository-api/pom.xml +++ b/archiva-modules/metadata/metadata-repository-api/pom.xml @@ -67,6 +67,11 @@ <artifactId>assertj-core</artifactId> <scope>test</scope> </dependency> + <dependency> + <groupId>org.apache.archiva</groupId> + <artifactId>generic-metadata-support</artifactId> + <scope>test</scope> + </dependency> </dependencies> <build> <plugins> diff --git a/archiva-modules/metadata/metadata-repository-api/src/main/java/org/apache/archiva/metadata/repository/AbstractMetadataRepository.java b/archiva-modules/metadata/metadata-repository-api/src/main/java/org/apache/archiva/metadata/repository/AbstractMetadataRepository.java index 7372229bd..8bec50167 100644 --- a/archiva-modules/metadata/metadata-repository-api/src/main/java/org/apache/archiva/metadata/repository/AbstractMetadataRepository.java +++ b/archiva-modules/metadata/metadata-repository-api/src/main/java/org/apache/archiva/metadata/repository/AbstractMetadataRepository.java @@ -125,6 +125,27 @@ public abstract class AbstractMetadataRepository } @Override + public List<ArtifactMetadata> getArtifactsByProjectVersionMetadata( String key , String value , String repositoryId ) + throws MetadataRepositoryException + { + throw new UnsupportedOperationException(); + } + + @Override + public List<ArtifactMetadata> getArtifactsByMetadata( String key , String value , String repositoryId ) + throws MetadataRepositoryException + { + throw new UnsupportedOperationException(); + } + + @Override + public List<ArtifactMetadata> getArtifactsByProperty( String key, String value, String repositoryId ) + throws MetadataRepositoryException + { + throw new UnsupportedOperationException(); + } + + @Override public void removeArtifact( String repositoryId, String namespace, String project, String version, String id ) throws MetadataRepositoryException { @@ -277,4 +298,18 @@ public abstract class AbstractMetadataRepository throw new UnsupportedOperationException(); } + @Override + public List<ArtifactMetadata> searchArtifacts( String text, String repositoryId, boolean exact ) + throws MetadataRepositoryException + { + throw new UnsupportedOperationException(); + } + + @Override + public List<ArtifactMetadata> searchArtifacts( String key, String text, String repositoryId, boolean exact ) + throws MetadataRepositoryException + { + throw new UnsupportedOperationException(); + } + } diff --git a/archiva-modules/metadata/metadata-repository-api/src/main/java/org/apache/archiva/metadata/repository/MetadataRepository.java b/archiva-modules/metadata/metadata-repository-api/src/main/java/org/apache/archiva/metadata/repository/MetadataRepository.java index eb7648bdf..421eccd91 100644 --- a/archiva-modules/metadata/metadata-repository-api/src/main/java/org/apache/archiva/metadata/repository/MetadataRepository.java +++ b/archiva-modules/metadata/metadata-repository-api/src/main/java/org/apache/archiva/metadata/repository/MetadataRepository.java @@ -102,6 +102,43 @@ public interface MetadataRepository Collection<ArtifactMetadata> getArtifactsByChecksum( String repositoryId, String checksum ) throws MetadataRepositoryException; + /** + * Get artifacts with a project version metadata key that matches the passed value. + * + * @param key + * @param value + * @param repositoryId can be null, meaning search in all repositories + * @return a list of artifacts + * @throws MetadataRepositoryException + */ + List<ArtifactMetadata> getArtifactsByProjectVersionMetadata( String key, String value, String repositoryId ) + throws MetadataRepositoryException; + + /** + * Get artifacts with an artifact metadata key that matches the passed value. + * + * @param key + * @param value + * @param repositoryId can be null, meaning search in all repositories + * @return a list of artifacts + * @throws MetadataRepositoryException + */ + List<ArtifactMetadata> getArtifactsByMetadata( String key, String value, String repositoryId ) + throws MetadataRepositoryException; + + /** + * Get artifacts with a property key that matches the passed value. + * Possible keys are 'scm.url', 'org.name', 'url', 'mailingList.0.name', 'license.0.name',... + * + * @param key + * @param value + * @param repositoryId can be null, meaning search in all repositories + * @return a list of artifacts + * @throws MetadataRepositoryException + */ + List<ArtifactMetadata> getArtifactsByProperty( String key, String value, String repositoryId ) + throws MetadataRepositoryException; + void removeArtifact( String repositoryId, String namespace, String project, String version, String id ) throws MetadataRepositoryException; @@ -270,4 +307,30 @@ public interface MetadataRepository <T> T obtainAccess( Class<T> aClass ) throws MetadataRepositoryException; + + /** + * Full text artifacts search. + * + * @param text + * @param repositoryId can be null to search in all repositories + * @param exact running an exact search, the value must exactly match the text. + * @return a list of artifacts + * @throws MetadataRepositoryException + */ + List<ArtifactMetadata> searchArtifacts( String text, String repositoryId, boolean exact ) + throws MetadataRepositoryException; + + /** + * Full text artifacts search inside the specified key. + * + * @param key search only inside this key + * @param text + * @param repositoryId can be null to search in all repositories + * @param exact running an exact search, the value must exactly match the text. + * @return a list of artifacts + * @throws MetadataRepositoryException + */ + List<ArtifactMetadata> searchArtifacts( String key, String text, String repositoryId, boolean exact ) + throws MetadataRepositoryException; + } diff --git a/archiva-modules/metadata/metadata-repository-api/src/test/java/org/apache/archiva/metadata/repository/AbstractMetadataRepositoryTest.java b/archiva-modules/metadata/metadata-repository-api/src/test/java/org/apache/archiva/metadata/repository/AbstractMetadataRepositoryTest.java index 221eae279..75c961fbc 100644 --- a/archiva-modules/metadata/metadata-repository-api/src/test/java/org/apache/archiva/metadata/repository/AbstractMetadataRepositoryTest.java +++ b/archiva-modules/metadata/metadata-repository-api/src/test/java/org/apache/archiva/metadata/repository/AbstractMetadataRepositoryTest.java @@ -19,7 +19,26 @@ package org.apache.archiva.metadata.repository; * under the License. */ +import static org.assertj.core.api.Assertions.assertThat; + +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Calendar; +import java.util.Collection; +import java.util.Collections; +import java.util.Comparator; +import java.util.Date; +import java.util.HashMap; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Map; +import java.util.TimeZone; + import junit.framework.TestCase; + +import org.apache.archiva.metadata.generic.GenericMetadataFacet; +import org.apache.archiva.metadata.generic.GenericMetadataFacetFactory; import org.apache.archiva.metadata.model.ArtifactMetadata; import org.apache.archiva.metadata.model.CiManagement; import org.apache.archiva.metadata.model.Dependency; @@ -41,22 +60,6 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.test.context.ContextConfiguration; -import java.text.SimpleDateFormat; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Calendar; -import java.util.Collection; -import java.util.Collections; -import java.util.Comparator; -import java.util.Date; -import java.util.HashMap; -import java.util.LinkedHashSet; -import java.util.List; -import java.util.Map; -import java.util.TimeZone; - -import static org.assertj.core.api.Assertions.assertThat; - @RunWith( ArchivaSpringJUnit4ClassRunner.class ) @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" } ) public abstract class AbstractMetadataRepositoryTest @@ -76,6 +79,10 @@ public abstract class AbstractMetadataRepositoryTest private static final String TEST_PROJECT_VERSION_2_0 = "2.0"; + private static final String TEST_URL = "http://archiva.apache.org"; + + private static final Organization TEST_ORGANIZATION = new Organization( "Apache", "http://apache.org" ); + private static final String TEST_FACET_ID = "test-facet-id"; private static final String TEST_NAME = "test/name"; @@ -88,7 +95,9 @@ public abstract class AbstractMetadataRepositoryTest private static final String TEST_SHA1 = "2e5daf0201ddeb068a62d5e08da18657ab2c6be9"; - private static final String TEST_METADATA_VALUE = "test-metadata"; + private static final String TEST_METADATA_KEY = "testkey"; + + private static final String TEST_METADATA_VALUE = "testmetadata"; protected Logger log = LoggerFactory.getLogger( getClass() ); @@ -125,6 +134,10 @@ public abstract class AbstractMetadataRepositoryTest return new TestMetadataFacet( "", TEST_VALUE ); } } ); + + // for the getArtifactsByProjectVersionMetadata tests + factories.put( GenericMetadataFacet.FACET_ID, new GenericMetadataFacetFactory() ); + return factories; } @@ -301,10 +314,7 @@ public abstract class AbstractMetadataRepositoryTest tracker.setUrl( "issue tracker url" ); metadata.setIssueManagement( tracker ); - Organization org = new Organization(); - org.setName( "org name" ); - org.setUrl( "url" ); - metadata.setOrganization( org ); + metadata.setOrganization( TEST_ORGANIZATION ); License l = new License(); l.setName( "license name" ); @@ -341,8 +351,8 @@ public abstract class AbstractMetadataRepositoryTest assertEquals( "system", metadata.getIssueManagement().getSystem() ); assertEquals( "issue tracker url", metadata.getIssueManagement().getUrl() ); - assertEquals( "org name", metadata.getOrganization().getName() ); - assertEquals( "url", metadata.getOrganization().getUrl() ); + assertEquals( TEST_ORGANIZATION.getName(), metadata.getOrganization().getName() ); + assertEquals( TEST_ORGANIZATION.getUrl(), metadata.getOrganization().getUrl() ); assertEquals( 1, metadata.getMailingLists().size() ); MailingList retrievedMailingList = metadata.getMailingLists().get( 0 ); @@ -1192,6 +1202,72 @@ public abstract class AbstractMetadataRepositoryTest assertThat( artifactsByChecksum ).isNotNull().isEmpty(); } + @Test + public void testGetArtifactsByProjectVersionMetadata() + throws Exception + { + createArtifactWithGenericMetadataFacet( 10 ); + Collection<ArtifactMetadata> artifactsByMetadata = + repository.getArtifactsByProjectVersionMetadata( TEST_METADATA_KEY, TEST_METADATA_VALUE, TEST_REPO_ID ); + assertThat( artifactsByMetadata ).hasSize( 1 ); + ArtifactMetadata artifactMetadata = artifactsByMetadata.iterator().next(); + assertThat( artifactMetadata.getId() ).isEqualTo( "projectId-1.0.jar" ); + assertThat( artifactMetadata.getSha1() ).isEqualTo( TEST_SHA1 ); + assertThat( artifactMetadata.getRepositoryId() ).isEqualTo( TEST_REPO_ID ); + } + + @Test + public void testGetArtifactsByProjectVersionMetadataNoRepository() + throws Exception + { + createArtifactWithGenericMetadataFacet(); + Collection<ArtifactMetadata> artifactsByMetadata = + repository.getArtifactsByProjectVersionMetadata( TEST_METADATA_KEY, TEST_METADATA_VALUE, null ); + assertThat( artifactsByMetadata ).hasSize( 1 ); + assertThat( artifactsByMetadata.iterator().next().getRepositoryId() ).isNotNull().isNotEmpty(); + } + + @Test + public void testGetArtifactsByProjectVersionMetadataAllRepositories() + throws Exception + { + createArtifactWithGenericMetadataFacet(); + Collection<ArtifactMetadata> artifactsByMetadata = + repository.getArtifactsByProjectVersionMetadata( TEST_METADATA_KEY, TEST_METADATA_VALUE, null ); + assertThat( artifactsByMetadata ).hasSize( 1 ); + } + + @Test + public void testGetArtifactsByMetadataAllRepositories() + throws Exception + { + createArtifactWithMavenArtifactFacet(); + Collection<ArtifactMetadata> artifactsByMetadata = + repository.getArtifactsByMetadata( "foo", TEST_METADATA_VALUE, null ); + assertThat( artifactsByMetadata ).hasSize( 1 ); + ArtifactMetadata artifactMetadata = artifactsByMetadata.iterator().next(); + assertThat( artifactMetadata.getId() ).isEqualTo( "projectId-1.0.jar" ); + assertThat( artifactMetadata.getSha1() ).isEqualTo( TEST_SHA1 ); + assertThat( artifactMetadata.getRepositoryId() ).isEqualTo( TEST_REPO_ID ); + MetadataFacet facet = artifactMetadata.getFacet( TEST_FACET_ID ); + assertThat( facet ).isNotNull(); + assertThat( facet.toProperties() ).isEqualTo( Collections.singletonMap( "foo", TEST_METADATA_VALUE ) ); + } + + @Test + public void testGetArtifactsByPropertySingleResult() + throws Exception + { + createArtifactWithData(); + // only works on JCR implementation + // Collection<ArtifactMetadata> artifactsByProperty = repository.getArtifactsByProperty( "org.name", TEST_ORGANIZATION.getName(), TEST_REPO_ID ); + Collection<ArtifactMetadata> artifactsByProperty = repository.getArtifactsByProperty( "url", TEST_URL, TEST_REPO_ID ); + assertThat( artifactsByProperty ).hasSize( 1 ); + ArtifactMetadata artifactMetadata = artifactsByProperty.iterator().next(); + assertThat( artifactMetadata.getId() ).isEqualTo( "projectId-1.0.jar" ); + assertThat( artifactMetadata.getSha1() ).isEqualTo( TEST_SHA1 ); + assertThat( artifactMetadata.getRepositoryId() ).isEqualTo( TEST_REPO_ID ); + } @Test public void testDeleteRepository() @@ -1424,6 +1500,78 @@ public abstract class AbstractMetadataRepositoryTest } + @Test + public void testSearchArtifactsByKey() + throws Exception + { + createArtifactWithData(); + Collection<ArtifactMetadata> artifactsByProperty = repository.searchArtifacts( "url", TEST_URL, TEST_REPO_ID, false ); + assertThat( artifactsByProperty ).isNotNull().isNotEmpty(); + } + + @Test + public void testSearchArtifactsByKeyExact() + throws Exception + { + createArtifactWithData(); + Collection<ArtifactMetadata> artifactsByProperty = repository.searchArtifacts( "url", TEST_URL, TEST_REPO_ID, true ); + assertThat( artifactsByProperty ).isNotNull().isNotEmpty(); + artifactsByProperty = repository.searchArtifacts( "org.name", "pache", TEST_REPO_ID, true ); + assertThat( artifactsByProperty ).isNotNull().isEmpty(); + } + + @Test + public void testSearchArtifactsByFacetKey() + throws Exception + { + createArtifactWithGenericMetadataFacet(); + Collection<ArtifactMetadata> artifactsByProperty = repository.searchArtifacts( TEST_METADATA_KEY, TEST_METADATA_VALUE, TEST_REPO_ID, false ); + assertThat( artifactsByProperty ).isNotNull().isNotEmpty(); + } + + @Test + public void testSearchArtifactsByFacetKeyAllRepos() + throws Exception + { + createArtifactWithGenericMetadataFacet(); + Collection<ArtifactMetadata> artifactsByProperty = repository.searchArtifacts( TEST_METADATA_KEY, TEST_METADATA_VALUE, null, false ); + assertThat( artifactsByProperty ).isNotNull().isNotEmpty(); + } + + @Test + public void testSearchArtifactsFullText() + throws Exception + { + createArtifactWithGenericMetadataFacet(); + // only works in JCR + // Collection<ArtifactMetadata> artifactsByProperty = repository.searchArtifacts( TEST_URL, TEST_REPO_ID, false ); + Collection<ArtifactMetadata> artifactsByProperty = + repository.searchArtifacts( TEST_METADATA_VALUE, TEST_REPO_ID, false ); + assertThat( artifactsByProperty ).isNotNull().isNotEmpty(); + } + + @Test + public void testSearchArtifactsFullTextExact() + throws Exception + { + createArtifactWithGenericMetadataFacet(); + // only works in JCR + // Collection<ArtifactMetadata> artifactsByProperty = repository.searchArtifacts( TEST_URL, TEST_REPO_ID, true ); + Collection<ArtifactMetadata> artifactsByProperty = + repository.searchArtifacts( TEST_METADATA_VALUE, TEST_REPO_ID, true ); + assertThat( artifactsByProperty ).isNotNull().isNotEmpty(); + artifactsByProperty = repository.searchArtifacts( TEST_METADATA_VALUE.substring( 2 ), TEST_REPO_ID, true ); + assertThat( artifactsByProperty ).isNotNull().isEmpty(); + } + + @Test + public void testSearchArtifactsFullTextByFacet() + throws Exception + { + createArtifactWithGenericMetadataFacet(); + Collection<ArtifactMetadata> artifactsByProperty = repository.searchArtifacts( TEST_METADATA_VALUE, TEST_REPO_ID, false ); + assertThat( artifactsByProperty ).isNotNull().isNotEmpty(); + } private static ProjectMetadata createProject() { @@ -1438,6 +1586,74 @@ public abstract class AbstractMetadataRepositoryTest return project; } + private void createArtifactWithGenericMetadataFacet() + throws MetadataRepositoryException, MetadataResolutionException + { + createArtifactWithGenericMetadataFacet( 1 ); + } + + private void createArtifactWithGenericMetadataFacet( int artifacts ) + throws MetadataRepositoryException, MetadataResolutionException + { + MetadataFacet metadataFacet = new GenericMetadataFacet(); + Map<String, String> properties = new HashMap<>(); + properties.put( TEST_METADATA_KEY, TEST_METADATA_VALUE ); + metadataFacet.fromProperties( properties ); + createArtifactWithFacet( artifacts, null, metadataFacet ); + } + + private void createArtifactWithMavenArtifactFacet() + throws MetadataRepositoryException, MetadataResolutionException + { + createArtifactWithMavenArtifactFacet( 1 ); + } + + private void createArtifactWithMavenArtifactFacet( int artifacts ) + throws MetadataRepositoryException, MetadataResolutionException + { + TestMetadataFacet facet = new TestMetadataFacet( TEST_METADATA_VALUE ); + createArtifactWithFacet( artifacts, facet, null ); + } + + private void createArtifactWithFacet( int artifacts, MetadataFacet artifactFacet, + MetadataFacet projectVersionMetadataFacet ) + throws MetadataRepositoryException, MetadataResolutionException + { + for ( int i = 0; i < artifacts; i++ ) + { + ArtifactMetadata artifact = createArtifact(); + if ( artifactFacet != null ) + { + artifact.addFacet( artifactFacet ); + } + repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact ); + } + if ( projectVersionMetadataFacet != null ) + { + ProjectVersionMetadata metadata = + repository.getProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION ); + metadata.addFacet( projectVersionMetadataFacet ); + metadata.setOrganization( TEST_ORGANIZATION ); + metadata.setUrl( TEST_URL ); + repository.updateProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata ); + } + repository.save(); + } + + private void createArtifactWithData() + throws MetadataRepositoryException, MetadataResolutionException + { + ArtifactMetadata artifact = createArtifact(); + repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact ); + ProjectVersionMetadata metadata = + repository.getProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION ); + metadata.setOrganization( TEST_ORGANIZATION ); + metadata.setUrl( TEST_URL ); + + repository.updateProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata ); + repository.save(); + } + private static ArtifactMetadata createArtifact() { return createArtifact( "jar" ); |