From acc72428047d54ba43f802d457afb4f5d10f9c88 Mon Sep 17 00:00:00 2001 From: Olivier Lamy Date: Wed, 26 Mar 2014 12:16:02 +0000 Subject: [PATCH] add unit test for method getProjectReferences git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@1581807 13f79535-47bb-0310-9956-ffa450edef68 --- .../model/ProjectVersionReference.java | 58 +++++++++++++++++++ .../AbstractMetadataRepositoryTest.java | 56 +++++++++++++++++- .../CassandraMetadataRepository.java | 41 +++++++++++-- .../DefaultCassandraArchivaManager.java | 26 ++------- .../repository/jcr/JcrMetadataRepository.java | 1 + 5 files changed, 155 insertions(+), 27 deletions(-) diff --git a/archiva-modules/metadata/metadata-model/src/main/java/org/apache/archiva/metadata/model/ProjectVersionReference.java b/archiva-modules/metadata/metadata-model/src/main/java/org/apache/archiva/metadata/model/ProjectVersionReference.java index 5dd4b902b..f5a491c78 100644 --- a/archiva-modules/metadata/metadata-model/src/main/java/org/apache/archiva/metadata/model/ProjectVersionReference.java +++ b/archiva-modules/metadata/metadata-model/src/main/java/org/apache/archiva/metadata/model/ProjectVersionReference.java @@ -29,6 +29,20 @@ public class ProjectVersionReference private String projectVersion; + public ProjectVersionReference() + { + // no op + } + + public ProjectVersionReference( ReferenceType referenceType, String projectId, String namespace, + String projectVersion ) + { + this.referenceType = referenceType; + this.projectId = projectId; + this.namespace = namespace; + this.projectVersion = projectVersion; + } + public void setReferenceType( ReferenceType referenceType ) { this.referenceType = referenceType; @@ -74,4 +88,48 @@ public class ProjectVersionReference DEPENDENCY, PARENT } + + @Override + public boolean equals( Object o ) + { + if ( this == o ) + { + return true; + } + if ( o == null || getClass() != o.getClass() ) + { + return false; + } + + ProjectVersionReference that = (ProjectVersionReference) o; + + if ( !namespace.equals( that.namespace ) ) + { + return false; + } + if ( !projectId.equals( that.projectId ) ) + { + return false; + } + if ( !projectVersion.equals( that.projectVersion ) ) + { + return false; + } + if ( referenceType != that.referenceType ) + { + return false; + } + + return true; + } + + @Override + public int hashCode() + { + int result = referenceType.hashCode(); + result = 31 * result + projectId.hashCode(); + result = 31 * result + namespace.hashCode(); + result = 31 * result + projectVersion.hashCode(); + return result; + } } 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 14bf3b7cc..27eef207b 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 @@ -31,6 +31,7 @@ import org.apache.archiva.metadata.model.MetadataFacetFactory; import org.apache.archiva.metadata.model.Organization; import org.apache.archiva.metadata.model.ProjectMetadata; import org.apache.archiva.metadata.model.ProjectVersionMetadata; +import org.apache.archiva.metadata.model.ProjectVersionReference; import org.apache.archiva.metadata.model.Scm; import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner; import org.assertj.core.util.Sets; @@ -56,8 +57,8 @@ 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" }) +@RunWith( ArchivaSpringJUnit4ClassRunner.class ) +@ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" } ) public abstract class AbstractMetadataRepositoryTest extends TestCase { @@ -1368,6 +1369,57 @@ public abstract class AbstractMetadataRepositoryTest } + @Test + public void testgetProjectReferences() + throws Exception + { + ProjectVersionMetadata metadata = new ProjectVersionMetadata(); + metadata.setId( TEST_PROJECT_VERSION ); + + metadata.setName( "project name" ); + metadata.setDescription( "project description" ); + metadata.setUrl( "the url" ); + + Dependency d = new Dependency(); + d.setArtifactId( "artifactId" ); + d.setClassifier( "classifier" ); + d.setGroupId( "groupId" ); + d.setScope( "scope" ); + d.setSystemPath( "system path" ); + d.setType( "type" ); + d.setVersion( "version" ); + d.setOptional( true ); + metadata.addDependency( d ); + + d = new Dependency(); + d.setArtifactId( "artifactId1" ); + d.setClassifier( "classifier" ); + d.setGroupId( "groupId" ); + d.setScope( "scope" ); + d.setSystemPath( "system path" ); + d.setType( "type" ); + d.setVersion( "version1" ); + d.setOptional( true ); + metadata.addDependency( d ); + + repository.updateProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata ); + + repository.save(); + + metadata = repository.getProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION ); + + Collection references = + repository.getProjectReferences( TEST_REPO_ID, d.getGroupId(), d.getArtifactId(), d.getVersion() ); + + log.info( "references: {}", references ); + + assertThat( references ).isNotNull().hasSize( 1 ).contains( + new ProjectVersionReference( ProjectVersionReference.ReferenceType.DEPENDENCY, TEST_PROJECT, TEST_NAMESPACE, + TEST_PROJECT_VERSION ) ); + + } + + private static ProjectMetadata createProject() { return createProject( TEST_NAMESPACE ); diff --git a/archiva-modules/plugins/metadata-store-cassandra/src/main/java/org/apache/archiva/metadata/repository/cassandra/CassandraMetadataRepository.java b/archiva-modules/plugins/metadata-store-cassandra/src/main/java/org/apache/archiva/metadata/repository/cassandra/CassandraMetadataRepository.java index e46675472..1840f1c31 100644 --- a/archiva-modules/plugins/metadata-store-cassandra/src/main/java/org/apache/archiva/metadata/repository/cassandra/CassandraMetadataRepository.java +++ b/archiva-modules/plugins/metadata-store-cassandra/src/main/java/org/apache/archiva/metadata/repository/cassandra/CassandraMetadataRepository.java @@ -875,7 +875,7 @@ public class CassandraMetadataRepository recordLicenses( key, versionMetadata.getLicenses() ); - recordDependencies( key, versionMetadata.getDependencies() ); + recordDependencies( key, versionMetadata.getDependencies(), repositoryId ); MutationResult mutationResult = mutator.execute(); } @@ -942,7 +942,7 @@ public class CassandraMetadataRepository recordLicenses( key, versionMetadata.getLicenses() ); removeDependencies( key ); - recordDependencies( key, versionMetadata.getDependencies() ); + recordDependencies( key, versionMetadata.getDependencies(), repositoryId ); projectVersionMetadataTemplate.update( updater ); @@ -1234,7 +1234,8 @@ public class CassandraMetadataRepository } - protected void recordDependencies( String projectVersionMetadataKey, List dependencies ) + protected void recordDependencies( String projectVersionMetadataKey, List dependencies, + String repositoryId ) { if ( dependencies == null || dependencies.isEmpty() ) @@ -1252,6 +1253,8 @@ public class CassandraMetadataRepository addInsertion( dependencyMutator, keyDependency, cfDependency, "projectVersionMetadataModel.key", projectVersionMetadataKey ); + addInsertion( dependencyMutator, keyDependency, cfDependency, "repositoryName", repositoryId ); + addInsertion( dependencyMutator, keyDependency, cfDependency, "classifier", dependency.getClassifier() ); addInsertion( dependencyMutator, keyDependency, cfDependency, "optional", @@ -1918,8 +1921,36 @@ public class CassandraMetadataRepository String projectVersion ) throws MetadataResolutionException { - // FIXME implement this - return Collections.emptyList(); + QueryResult> result = HFactory // + .createRangeSlicesQuery( keyspace, ss, ss, ss ) // + .setColumnFamily( cassandraArchivaManager.getDependencyFamilyName() ) // + .setColumnNames( "projectVersionMetadataModel.key" ) // + .addEqualsExpression( "repositoryName", repoId ) // + .addEqualsExpression( "groupId", namespace ) // + .addEqualsExpression( "artifactId", projectId ) // + .addEqualsExpression( "version", projectVersion ) // + .execute(); + + List dependenciesIds = new ArrayList( result.get().getCount() ); + + for ( Row row : result.get().getList() ) + { + dependenciesIds.add( getStringValue( row.getColumnSlice(), "projectVersionMetadataModel.key" ) ); + } + + List references = new ArrayList( result.get().getCount() ); + + for ( String key : dependenciesIds ) + { + ColumnFamilyResult columnFamilyResult = + this.projectVersionMetadataTemplate.queryColumns( key ); + references.add( new ProjectVersionReference( ProjectVersionReference.ReferenceType.DEPENDENCY, // + columnFamilyResult.getString( "projectId" ), // + columnFamilyResult.getString( "namespaceId" ), // + columnFamilyResult.getString( "projectVersion" ) ) ); + } + + return references; } @Override diff --git a/archiva-modules/plugins/metadata-store-cassandra/src/main/java/org/apache/archiva/metadata/repository/cassandra/DefaultCassandraArchivaManager.java b/archiva-modules/plugins/metadata-store-cassandra/src/main/java/org/apache/archiva/metadata/repository/cassandra/DefaultCassandraArchivaManager.java index 978ffdb9b..9a40693c4 100644 --- a/archiva-modules/plugins/metadata-store-cassandra/src/main/java/org/apache/archiva/metadata/repository/cassandra/DefaultCassandraArchivaManager.java +++ b/archiva-modules/plugins/metadata-store-cassandra/src/main/java/org/apache/archiva/metadata/repository/cassandra/DefaultCassandraArchivaManager.java @@ -375,13 +375,6 @@ public class DefaultCassandraArchivaManager // creating indexes for cql query - BasicColumnDefinition projectVersionMetadataIdColumn = new BasicColumnDefinition(); - projectVersionMetadataIdColumn.setName( StringSerializer.get().toByteBuffer( "projectVersionMetadataId" ) ); - projectVersionMetadataIdColumn.setIndexName( "projectVersionMetadataId" ); - projectVersionMetadataIdColumn.setIndexType( ColumnIndexType.KEYS ); - projectVersionMetadataIdColumn.setValidationClass( ComparatorType.UTF8TYPE.getClassName() ); - mailingListCf.addColumnDefinition( projectVersionMetadataIdColumn ); - } // license table @@ -394,13 +387,6 @@ public class DefaultCassandraArchivaManager // creating indexes for cql query - BasicColumnDefinition projectVersionMetadataIdColumn = new BasicColumnDefinition(); - projectVersionMetadataIdColumn.setName( StringSerializer.get().toByteBuffer( "projectVersionMetadataId" ) ); - projectVersionMetadataIdColumn.setIndexName( "projectVersionMetadataId" ); - projectVersionMetadataIdColumn.setIndexType( ColumnIndexType.KEYS ); - projectVersionMetadataIdColumn.setValidationClass( ComparatorType.UTF8TYPE.getClassName() ); - licenseCf.addColumnDefinition( projectVersionMetadataIdColumn ); - } // dependency table @@ -413,12 +399,12 @@ public class DefaultCassandraArchivaManager // creating indexes for cql query - BasicColumnDefinition projectVersionMetadataIdColumn = new BasicColumnDefinition(); - projectVersionMetadataIdColumn.setName( StringSerializer.get().toByteBuffer( "projectVersionMetadataId" ) ); - projectVersionMetadataIdColumn.setIndexName( "projectVersionMetadataId" ); - projectVersionMetadataIdColumn.setIndexType( ColumnIndexType.KEYS ); - projectVersionMetadataIdColumn.setValidationClass( ComparatorType.UTF8TYPE.getClassName() ); - dependencyCf.addColumnDefinition( projectVersionMetadataIdColumn ); + BasicColumnDefinition groupIdColumn = new BasicColumnDefinition(); + groupIdColumn.setName( StringSerializer.get().toByteBuffer( "groupId" ) ); + groupIdColumn.setIndexName( "groupIdIdx" ); + groupIdColumn.setIndexType( ColumnIndexType.KEYS ); + groupIdColumn.setValidationClass( ComparatorType.UTF8TYPE.getClassName() ); + dependencyCf.addColumnDefinition( groupIdColumn ); } diff --git a/archiva-modules/plugins/metadata-store-jcr/src/main/java/org/apache/archiva/metadata/repository/jcr/JcrMetadataRepository.java b/archiva-modules/plugins/metadata-store-jcr/src/main/java/org/apache/archiva/metadata/repository/jcr/JcrMetadataRepository.java index 2e51337dc..bd80b15f4 100644 --- a/archiva-modules/plugins/metadata-store-jcr/src/main/java/org/apache/archiva/metadata/repository/jcr/JcrMetadataRepository.java +++ b/archiva-modules/plugins/metadata-store-jcr/src/main/java/org/apache/archiva/metadata/repository/jcr/JcrMetadataRepository.java @@ -987,6 +987,7 @@ public class JcrMetadataRepository String projectId, String projectVersion ) throws MetadataResolutionException { + List references = new ArrayList(); // TODO: bind variables instead -- 2.39.5