From: Brett Porter Date: Tue, 19 Jan 2010 07:17:02 +0000 (+0000) Subject: [MRM-1296] improve audit logging and testing of upload action X-Git-Tag: archiva-1.4-M1~1017^2~42 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=640d3436f6e4413b669cc40764a340cddba482a4;p=archiva.git [MRM-1296] improve audit logging and testing of upload action git-svn-id: https://svn.apache.org/repos/asf/archiva/branches/MRM-1025@900683 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/archiva-modules/archiva-web/archiva-webapp/src/main/java/org/apache/maven/archiva/web/action/PlexusActionSupport.java b/archiva-modules/archiva-web/archiva-webapp/src/main/java/org/apache/maven/archiva/web/action/PlexusActionSupport.java index 9fc4d979c..682e78808 100644 --- a/archiva-modules/archiva-web/archiva-webapp/src/main/java/org/apache/maven/archiva/web/action/PlexusActionSupport.java +++ b/archiva-modules/archiva-web/archiva-webapp/src/main/java/org/apache/maven/archiva/web/action/PlexusActionSupport.java @@ -22,9 +22,10 @@ package org.apache.maven.archiva.web.action; import java.util.ArrayList; import java.util.List; import java.util.Map; - import javax.servlet.http.HttpServletRequest; +import com.opensymphony.xwork2.ActionContext; +import com.opensymphony.xwork2.ActionSupport; import org.apache.maven.archiva.repository.audit.AuditEvent; import org.apache.maven.archiva.repository.audit.AuditListener; import org.apache.maven.archiva.repository.audit.Auditable; @@ -34,9 +35,6 @@ import org.apache.struts2.interceptor.SessionAware; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.opensymphony.xwork2.ActionContext; -import com.opensymphony.xwork2.ActionSupport; - /** * LogEnabled and SessionAware ActionSupport */ @@ -129,4 +127,9 @@ public abstract class PlexusActionSupport { this.principal = principal; } + + public void setAuditListeners( List auditListeners ) + { + this.auditListeners = auditListeners; + } } diff --git a/archiva-modules/archiva-web/archiva-webapp/src/main/java/org/apache/maven/archiva/web/action/UploadAction.java b/archiva-modules/archiva-web/archiva-webapp/src/main/java/org/apache/maven/archiva/web/action/UploadAction.java index 0919ee8a1..4d5db60d4 100644 --- a/archiva-modules/archiva-web/archiva-webapp/src/main/java/org/apache/maven/archiva/web/action/UploadAction.java +++ b/archiva-modules/archiva-web/archiva-webapp/src/main/java/org/apache/maven/archiva/web/action/UploadAction.java @@ -305,7 +305,8 @@ public class UploadAction int lastIndex = artifactPath.lastIndexOf( '/' ); - File targetPath = new File( repoConfig.getLocation(), artifactPath.substring( 0, lastIndex ) ); + String path = artifactPath.substring( 0, lastIndex ); + File targetPath = new File( repoConfig.getLocation(), path ); Date lastUpdatedTimestamp = Calendar.getInstance().getTime(); int newBuildNumber = -1; @@ -357,6 +358,7 @@ public class UploadAction else { copyFile( artifactFile, targetPath, filename, fixChecksums ); + triggerAuditEvent( repository.getId(), path + "/" + filename, AuditEvent.UPLOAD_FILE ); queueRepositoryTask( repository.getId(), repository.toFile( artifactReference ) ); } } @@ -378,6 +380,7 @@ public class UploadAction try { File generatedPomFile = createPom( targetPath, pomFilename ); + triggerAuditEvent( repoConfig.getId(), path + "/" + pomFilename, AuditEvent.UPLOAD_FILE ); if ( fixChecksums ) { fixChecksums( generatedPomFile ); @@ -396,6 +399,7 @@ public class UploadAction try { copyFile( pomFile, targetPath, pomFilename, fixChecksums ); + triggerAuditEvent( repoConfig.getId(), path + "/" + pomFilename, AuditEvent.UPLOAD_FILE ); queueRepositoryTask( repoConfig.getId(), new File( targetPath, pomFilename ) ); } catch ( IOException ie ) @@ -415,8 +419,6 @@ public class UploadAction String msg = "Artifact \'" + groupId + ":" + artifactId + ":" + version + "\' was successfully deployed to repository \'" + repositoryId + "\'"; - triggerAuditEvent( repositoryId, artifactPath, AuditEvent.UPLOAD_FILE ); - addActionMessage( msg ); reset(); diff --git a/archiva-modules/archiva-web/archiva-webapp/src/test/java/org/apache/maven/archiva/web/action/UploadActionTest.java b/archiva-modules/archiva-web/archiva-webapp/src/test/java/org/apache/maven/archiva/web/action/UploadActionTest.java index 8a98bd2d6..2ac78407b 100644 --- a/archiva-modules/archiva-web/archiva-webapp/src/test/java/org/apache/maven/archiva/web/action/UploadActionTest.java +++ b/archiva-modules/archiva-web/archiva-webapp/src/test/java/org/apache/maven/archiva/web/action/UploadActionTest.java @@ -21,7 +21,13 @@ package org.apache.maven.archiva.web.action; import java.io.File; import java.io.IOException; +import java.text.SimpleDateFormat; import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.Date; +import java.util.List; +import java.util.TimeZone; import com.opensymphony.xwork2.Action; import org.apache.archiva.checksum.ChecksumAlgorithm; @@ -36,9 +42,12 @@ import org.apache.maven.archiva.configuration.RepositoryScanningConfiguration; import org.apache.maven.archiva.repository.ManagedRepositoryContent; import org.apache.maven.archiva.repository.RepositoryContentFactory; import org.apache.maven.archiva.repository.RepositoryNotFoundException; +import org.apache.maven.archiva.repository.audit.AuditEvent; +import org.apache.maven.archiva.repository.audit.AuditListener; import org.apache.maven.archiva.repository.content.ManagedDefaultRepositoryContent; import org.apache.maven.archiva.repository.metadata.MetadataTools; import org.codehaus.plexus.spring.PlexusInSpringTestCase; +import org.easymock.ArgumentsMatcher; import org.easymock.MockControl; import org.easymock.classextension.MockClassControl; @@ -57,10 +66,10 @@ public class UploadActionTest private RepositoryContentFactory repoFactory; private MockControl repoFactoryControl; - + private static final String REPOSITORY_ID = "test-repo"; - private Configuration config; + private Configuration config; public void setUp() throws Exception @@ -75,7 +84,7 @@ public class UploadActionTest repoFactoryControl = MockClassControl.createControl( RepositoryContentFactory.class ); repoFactory = (RepositoryContentFactory) repoFactoryControl.getMock(); - + uploadAction = new UploadAction(); uploadAction.setScheduler( scheduler ); uploadAction.setConfiguration( archivaConfig ); @@ -83,9 +92,9 @@ public class UploadActionTest File testRepo = new File( getBasedir(), "target/test-classes/test-repo" ); testRepo.mkdirs(); - + assertTrue( testRepo.exists() ); - + config = new Configuration(); ManagedRepositoryConfiguration repoConfig = new ManagedRepositoryConfiguration(); repoConfig.setId( REPOSITORY_ID ); @@ -94,7 +103,7 @@ public class UploadActionTest repoConfig.setName( REPOSITORY_ID ); repoConfig.setBlockRedeployments( true ); config.addManagedRepository( repoConfig ); - + RepositoryScanningConfiguration repoScanning = new RepositoryScanningConfiguration(); repoScanning.setKnownContentConsumers( new ArrayList() ); config.setRepositoryScanning( repoScanning ); @@ -128,15 +137,22 @@ public class UploadActionTest private void assertAllArtifactsIncludingSupportArtifactsArePresent( String repoLocation ) { - assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar" ).exists() ); - assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar.sha1" ).exists() ); - assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar.md5" ).exists() ); - - assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom" ).exists() ); - assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom.sha1" ).exists() ); - assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom.md5" ).exists() ); - - assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA ).exists() ); + assertTrue( + new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar" ).exists() ); + assertTrue( + new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar.sha1" ).exists() ); + assertTrue( + new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar.md5" ).exists() ); + + assertTrue( + new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom" ).exists() ); + assertTrue( + new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom.sha1" ).exists() ); + assertTrue( + new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom.md5" ).exists() ); + + assertTrue( + new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA ).exists() ); assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA + ".sha1" ).exists() ); assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA + @@ -147,63 +163,53 @@ public class UploadActionTest throws IOException { // verify checksums of jar file - ChecksummedFile checksum = - new ChecksummedFile( new File( repoLocation, - "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar" ) ); + ChecksummedFile checksum = new ChecksummedFile( + new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar" ) ); String sha1 = checksum.calculateChecksum( ChecksumAlgorithm.SHA1 ); String md5 = checksum.calculateChecksum( ChecksumAlgorithm.MD5 ); - String contents = - FileUtils.readFileToString( new File( repoLocation, - "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar.sha1" ) ); + String contents = FileUtils.readFileToString( + new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar.sha1" ) ); assertTrue( StringUtils.contains( contents, sha1 ) ); - contents = - FileUtils.readFileToString( new File( repoLocation, - "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar.md5" ) ); + contents = FileUtils.readFileToString( + new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar.md5" ) ); assertTrue( StringUtils.contains( contents, md5 ) ); // verify checksums of pom file - checksum = - new ChecksummedFile( new File( repoLocation, - "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom" ) ); + checksum = new ChecksummedFile( + new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom" ) ); sha1 = checksum.calculateChecksum( ChecksumAlgorithm.SHA1 ); md5 = checksum.calculateChecksum( ChecksumAlgorithm.MD5 ); - contents = - FileUtils.readFileToString( new File( repoLocation, - "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom.sha1" ) ); + contents = FileUtils.readFileToString( + new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom.sha1" ) ); assertTrue( StringUtils.contains( contents, sha1 ) ); - contents = - FileUtils.readFileToString( new File( repoLocation, - "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom.md5" ) ); + contents = FileUtils.readFileToString( + new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom.md5" ) ); assertTrue( StringUtils.contains( contents, md5 ) ); // verify checksums of metadata file - checksum = - new ChecksummedFile( new File( repoLocation, "/org/apache/archiva/artifact-upload/" + - MetadataTools.MAVEN_METADATA ) ); + checksum = new ChecksummedFile( + new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA ) ); sha1 = checksum.calculateChecksum( ChecksumAlgorithm.SHA1 ); md5 = checksum.calculateChecksum( ChecksumAlgorithm.MD5 ); - contents = - FileUtils.readFileToString( new File( repoLocation, "/org/apache/archiva/artifact-upload/" + - MetadataTools.MAVEN_METADATA + ".sha1" ) ); + contents = FileUtils.readFileToString( + new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA + ".sha1" ) ); assertTrue( StringUtils.contains( contents, sha1 ) ); - contents = - FileUtils.readFileToString( new File( repoLocation, "/org/apache/archiva/artifact-upload/" + - MetadataTools.MAVEN_METADATA + ".md5" ) ); + contents = FileUtils.readFileToString( + new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA + ".md5" ) ); assertTrue( StringUtils.contains( contents, md5 ) ); } public void testArtifactUploadWithPomSuccessful() throws Exception { - setUploadParameters( "1.0", null, - new File( getBasedir(), - "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ), + setUploadParameters( "1.0", null, new File( getBasedir(), + "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ), new File( getBasedir(), "target/test-classes/upload-artifact-test/pom.xml" ), false ); ManagedRepositoryContent content = new ManagedDefaultRepositoryContent(); @@ -215,11 +221,16 @@ public class UploadActionTest archivaConfigControl.replay(); repoFactoryControl.replay(); + MockControl control = mockAuditLogs( + Arrays.asList( "org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar", + "org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom" ) ); + String returnString = uploadAction.doUpload(); assertEquals( Action.SUCCESS, returnString ); archivaConfigControl.verify(); repoFactoryControl.verify(); + control.verify(); String repoLocation = config.findManagedRepositoryById( REPOSITORY_ID ).getLocation(); assertAllArtifactsIncludingSupportArtifactsArePresent( repoLocation ); @@ -227,13 +238,37 @@ public class UploadActionTest verifyChecksums( repoLocation ); } + private MockControl mockAuditLogs( List resources ) + { + return mockAuditLogs( AuditEvent.UPLOAD_FILE, resources ); + } + + private MockControl mockAuditLogs( String action, List resources ) + { + MockControl control = MockControl.createControl( AuditListener.class ); + AuditListener listener = (AuditListener) control.getMock(); + boolean matcherSet = false; + for ( String resource : resources ) + { + listener.auditEvent( new AuditEvent( REPOSITORY_ID, "guest", resource, action ) ); + if ( !matcherSet ) + { + control.setMatcher( new AuditEventArgumentsMatcher() ); + matcherSet = true; + } + } + control.replay(); + + uploadAction.setAuditListeners( Collections.singletonList( listener ) ); + return control; + } + public void testArtifactUploadWithClassifier() throws Exception { - setUploadParameters( "1.0", "tests", - new File( getBasedir(), - "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ), null, - false ); + setUploadParameters( "1.0", "tests", new File( getBasedir(), + "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ), + new File( getBasedir(), "target/test-classes/upload-artifact-test/pom.xml" ), false ); ManagedRepositoryContent content = new ManagedDefaultRepositoryContent(); content.setRepository( config.findManagedRepositoryById( REPOSITORY_ID ) ); @@ -244,70 +279,88 @@ public class UploadActionTest archivaConfigControl.replay(); repoFactoryControl.replay(); + MockControl control = mockAuditLogs( + Arrays.asList( "org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0-tests.jar", + "org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom" ) ); + String returnString = uploadAction.doUpload(); assertEquals( Action.SUCCESS, returnString ); archivaConfigControl.verify(); repoFactoryControl.verify(); + control.verify(); String repoLocation = config.findManagedRepositoryById( REPOSITORY_ID ).getLocation(); - assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0-tests.jar" ).exists() ); + assertTrue( new File( repoLocation, + "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0-tests.jar" ).exists() ); assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0-tests.jar.sha1" ).exists() ); - assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0-tests.jar.md5" ).exists() ); + assertTrue( new File( repoLocation, + "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0-tests.jar.md5" ).exists() ); - assertFalse( new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom" ).exists() ); - assertFalse( new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom.sha1" ).exists() ); - assertFalse( new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom.md5" ).exists() ); + assertTrue( + new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom" ).exists() ); + assertTrue( + new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom.sha1" ).exists() ); + assertTrue( + new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom.md5" ).exists() ); - assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA ).exists() ); + assertTrue( + new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA ).exists() ); assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA + ".sha1" ).exists() ); assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA + ".md5" ).exists() ); // verify checksums of jar file - ChecksummedFile checksum = - new ChecksummedFile( new File( repoLocation, - "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0-tests.jar" ) ); + ChecksummedFile checksum = new ChecksummedFile( + new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0-tests.jar" ) ); String sha1 = checksum.calculateChecksum( ChecksumAlgorithm.SHA1 ); String md5 = checksum.calculateChecksum( ChecksumAlgorithm.MD5 ); - String contents = - FileUtils.readFileToString( new File( repoLocation, - "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0-tests.jar.sha1" ) ); + String contents = FileUtils.readFileToString( + new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0-tests.jar.sha1" ) ); assertTrue( StringUtils.contains( contents, sha1 ) ); - contents = - FileUtils.readFileToString( new File( repoLocation, - "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0-tests.jar.md5" ) ); + contents = FileUtils.readFileToString( + new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0-tests.jar.md5" ) ); + assertTrue( StringUtils.contains( contents, md5 ) ); + + // verify checksums of jar file + checksum = new ChecksummedFile( + new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom" ) ); + sha1 = checksum.calculateChecksum( ChecksumAlgorithm.SHA1 ); + md5 = checksum.calculateChecksum( ChecksumAlgorithm.MD5 ); + + contents = FileUtils.readFileToString( + new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom.sha1" ) ); + assertTrue( StringUtils.contains( contents, sha1 ) ); + + contents = FileUtils.readFileToString( + new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom.md5" ) ); assertTrue( StringUtils.contains( contents, md5 ) ); // verify checksums of metadata file - checksum = - new ChecksummedFile( new File( repoLocation, "/org/apache/archiva/artifact-upload/" + - MetadataTools.MAVEN_METADATA ) ); + checksum = new ChecksummedFile( + new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA ) ); sha1 = checksum.calculateChecksum( ChecksumAlgorithm.SHA1 ); md5 = checksum.calculateChecksum( ChecksumAlgorithm.MD5 ); - contents = - FileUtils.readFileToString( new File( repoLocation, "/org/apache/archiva/artifact-upload/" + - MetadataTools.MAVEN_METADATA + ".sha1" ) ); + contents = FileUtils.readFileToString( + new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA + ".sha1" ) ); assertTrue( StringUtils.contains( contents, sha1 ) ); - contents = - FileUtils.readFileToString( new File( repoLocation, "/org/apache/archiva/artifact-upload/" + - MetadataTools.MAVEN_METADATA + ".md5" ) ); + contents = FileUtils.readFileToString( + new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA + ".md5" ) ); assertTrue( StringUtils.contains( contents, md5 ) ); } public void testArtifactUploadGeneratePomSuccessful() throws Exception { - setUploadParameters( "1.0", null, - new File( getBasedir(), - "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ), null, - true ); + setUploadParameters( "1.0", null, new File( getBasedir(), + "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ), + null, true ); ManagedRepositoryContent content = new ManagedDefaultRepositoryContent(); content.setRepository( config.findManagedRepositoryById( REPOSITORY_ID ) ); @@ -318,11 +371,16 @@ public class UploadActionTest archivaConfigControl.replay(); repoFactoryControl.replay(); + MockControl control = mockAuditLogs( + Arrays.asList( "org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar", + "org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom" ) ); + String returnString = uploadAction.doUpload(); assertEquals( Action.SUCCESS, returnString ); archivaConfigControl.verify(); repoFactoryControl.verify(); + control.verify(); String repoLocation = config.findManagedRepositoryById( REPOSITORY_ID ).getLocation(); assertAllArtifactsIncludingSupportArtifactsArePresent( repoLocation ); @@ -330,13 +388,89 @@ public class UploadActionTest verifyChecksums( repoLocation ); } + public void testArtifactUploadNoPomSuccessful() + throws Exception + { + setUploadParameters( "1.0", null, new File( getBasedir(), + "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ), + null, false ); + + ManagedRepositoryContent content = new ManagedDefaultRepositoryContent(); + content.setRepository( config.findManagedRepositoryById( REPOSITORY_ID ) ); + + archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config ); + repoFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( REPOSITORY_ID ), content ); + + archivaConfigControl.replay(); + repoFactoryControl.replay(); + + MockControl control = + mockAuditLogs( Arrays.asList( "org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar" ) ); + + String returnString = uploadAction.doUpload(); + assertEquals( Action.SUCCESS, returnString ); + + archivaConfigControl.verify(); + repoFactoryControl.verify(); + control.verify(); + + String repoLocation = config.findManagedRepositoryById( REPOSITORY_ID ).getLocation(); + assertTrue( + new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar" ).exists() ); + assertTrue( + new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar.sha1" ).exists() ); + assertTrue( + new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar.md5" ).exists() ); + + assertFalse( + new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom" ).exists() ); + assertFalse( + new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom.sha1" ).exists() ); + assertFalse( + new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom.md5" ).exists() ); + + assertTrue( + new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA ).exists() ); + assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA + + ".sha1" ).exists() ); + assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA + + ".md5" ).exists() ); + + // verify checksums of jar file + ChecksummedFile checksum = new ChecksummedFile( + new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar" ) ); + String sha1 = checksum.calculateChecksum( ChecksumAlgorithm.SHA1 ); + String md5 = checksum.calculateChecksum( ChecksumAlgorithm.MD5 ); + + String contents = FileUtils.readFileToString( + new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar.sha1" ) ); + assertTrue( StringUtils.contains( contents, sha1 ) ); + + contents = FileUtils.readFileToString( + new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar.md5" ) ); + assertTrue( StringUtils.contains( contents, md5 ) ); + + // verify checksums of metadata file + checksum = new ChecksummedFile( + new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA ) ); + sha1 = checksum.calculateChecksum( ChecksumAlgorithm.SHA1 ); + md5 = checksum.calculateChecksum( ChecksumAlgorithm.MD5 ); + + contents = FileUtils.readFileToString( + new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA + ".sha1" ) ); + assertTrue( StringUtils.contains( contents, sha1 ) ); + + contents = FileUtils.readFileToString( + new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA + ".md5" ) ); + assertTrue( StringUtils.contains( contents, md5 ) ); + } + public void testArtifactUploadFailedRepositoryNotFound() throws Exception - { - setUploadParameters( "1.0", null, - new File( getBasedir(), - "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ), null, - false ); + { + setUploadParameters( "1.0", null, new File( getBasedir(), + "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ), + null, false ); archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config ); repoFactoryControl.expectAndThrow( repoFactory.getManagedRepositoryContent( REPOSITORY_ID ), @@ -352,20 +486,22 @@ public class UploadActionTest repoFactoryControl.verify(); String repoLocation = config.findManagedRepositoryById( REPOSITORY_ID ).getLocation(); - assertFalse( new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar" ).exists() ); + assertFalse( + new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar" ).exists() ); - assertFalse( new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom" ).exists() ); + assertFalse( + new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom" ).exists() ); - assertFalse( new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA ).exists() ); + assertFalse( + new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA ).exists() ); } public void testArtifactUploadSnapshots() throws Exception { - setUploadParameters( "1.0-SNAPSHOT", null, - new File( getBasedir(), - "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ), null, - true ); + setUploadParameters( "1.0-SNAPSHOT", null, new File( getBasedir(), + "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ), + null, true ); ManagedRepositoryContent content = new ManagedDefaultRepositoryContent(); content.setRepository( config.findManagedRepositoryById( REPOSITORY_ID ) ); @@ -376,16 +512,25 @@ public class UploadActionTest archivaConfigControl.replay(); repoFactoryControl.replay(); + SimpleDateFormat fmt = new SimpleDateFormat( "yyyyMMdd.HHmmss" ); + fmt.setTimeZone( TimeZone.getTimeZone( "UTC" ) ); + String timestamp = fmt.format( new Date() ); + MockControl control = mockAuditLogs( Arrays.asList( + "org/apache/archiva/artifact-upload/1.0-SNAPSHOT/artifact-upload-1.0-" + timestamp + "-1.jar", + "org/apache/archiva/artifact-upload/1.0-SNAPSHOT/artifact-upload-1.0-" + timestamp + "-1.pom" ) ); + String returnString = uploadAction.doUpload(); assertEquals( Action.SUCCESS, returnString ); archivaConfigControl.verify(); repoFactoryControl.verify(); + control.verify(); String repoLocation = config.findManagedRepositoryById( REPOSITORY_ID ).getLocation(); assertEquals( 6, new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0-SNAPSHOT/" ).list().length ); - assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA ).exists() ); + assertTrue( + new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA ).exists() ); assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA + ".sha1" ).exists() ); assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA + @@ -395,16 +540,15 @@ public class UploadActionTest public void testChecksumIsCorrectWhenArtifactIsReUploaded() throws Exception { - setUploadParameters( "1.0", null, - new File( getBasedir(), - "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ), null, - true ); + setUploadParameters( "1.0", null, new File( getBasedir(), + "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ), + null, true ); ManagedRepositoryContent content = new ManagedDefaultRepositoryContent(); ManagedRepositoryConfiguration repoConfig = config.findManagedRepositoryById( REPOSITORY_ID ); - repoConfig.setBlockRedeployments( false ); - content.setRepository( repoConfig ); - + repoConfig.setBlockRedeployments( false ); + content.setRepository( repoConfig ); + archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config ); repoFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( REPOSITORY_ID ), content ); @@ -426,9 +570,8 @@ public class UploadActionTest verifyChecksums( repoLocation ); // RE-upload artifact - setUploadParameters( "1.0", null, - new File( getBasedir(), - "target/test-classes/upload-artifact-test/artifact-to-be-reuploaded.jar" ), + setUploadParameters( "1.0", null, new File( getBasedir(), + "target/test-classes/upload-artifact-test/artifact-to-be-reuploaded.jar" ), null, true ); archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config ); @@ -437,25 +580,31 @@ public class UploadActionTest archivaConfigControl.replay(); repoFactoryControl.replay(); + // TODO: track modifications? +// MockControl control = mockAuditLogs( AuditEvent.MODIFY_FILE, Arrays.asList( + MockControl control = mockAuditLogs( + Arrays.asList( "org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar", + "org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom" ) ); + returnString = uploadAction.doUpload(); assertEquals( Action.SUCCESS, returnString ); archivaConfigControl.verify(); repoFactoryControl.verify(); + control.verify(); repoLocation = config.findManagedRepositoryById( REPOSITORY_ID ).getLocation(); assertAllArtifactsIncludingSupportArtifactsArePresent( repoLocation ); verifyChecksums( repoLocation ); } - + public void testUploadArtifactAlreadyExistingRedeploymentsBlocked() throws Exception - { - setUploadParameters( "1.0", null, - new File( getBasedir(), - "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ), null, - true ); + { + setUploadParameters( "1.0", null, new File( getBasedir(), + "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ), + null, true ); ManagedRepositoryContent content = new ManagedDefaultRepositoryContent(); content.setRepository( config.findManagedRepositoryById( REPOSITORY_ID ) ); @@ -468,37 +617,38 @@ public class UploadActionTest String returnString = uploadAction.doUpload(); assertEquals( Action.SUCCESS, returnString ); - - setUploadParameters( "1.0", null, - new File( getBasedir(), - "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ), null, - true ); - + + setUploadParameters( "1.0", null, new File( getBasedir(), + "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ), + null, true ); + + MockControl control = mockAuditLogs( Collections.emptyList() ); + returnString = uploadAction.doUpload(); assertEquals( Action.ERROR, returnString ); archivaConfigControl.verify(); repoFactoryControl.verify(); + control.verify(); String repoLocation = config.findManagedRepositoryById( REPOSITORY_ID ).getLocation(); assertAllArtifactsIncludingSupportArtifactsArePresent( repoLocation ); verifyChecksums( repoLocation ); } - + public void testUploadArtifactAlreadyExistingRedeploymentsAllowed() throws Exception - { - setUploadParameters( "1.0", null, - new File( getBasedir(), - "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ), null, - true ); - + { + setUploadParameters( "1.0", null, new File( getBasedir(), + "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ), + null, true ); + ManagedRepositoryContent content = new ManagedDefaultRepositoryContent(); ManagedRepositoryConfiguration repoConfig = config.findManagedRepositoryById( REPOSITORY_ID ); - repoConfig.setBlockRedeployments( false ); - content.setRepository( repoConfig ); - + repoConfig.setBlockRedeployments( false ); + content.setRepository( repoConfig ); + archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config, 2 ); repoFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( REPOSITORY_ID ), content, 2 ); @@ -507,21 +657,51 @@ public class UploadActionTest String returnString = uploadAction.doUpload(); assertEquals( Action.SUCCESS, returnString ); - - setUploadParameters( "1.0", null, - new File( getBasedir(), - "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ), null, - true ); - + + setUploadParameters( "1.0", null, new File( getBasedir(), + "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ), + null, true ); + + // TODO: track modifications? +// MockControl control = mockAuditLogs( AuditEvent.MODIFY_FILE, Arrays.asList( + MockControl control = mockAuditLogs( + Arrays.asList( "org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar", + "org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom" ) ); + returnString = uploadAction.doUpload(); assertEquals( Action.SUCCESS, returnString ); - + archivaConfigControl.verify(); repoFactoryControl.verify(); - + control.verify(); + String repoLocation = config.findManagedRepositoryById( REPOSITORY_ID ).getLocation(); assertAllArtifactsIncludingSupportArtifactsArePresent( repoLocation ); - + verifyChecksums( repoLocation ); } + + private static class AuditEventArgumentsMatcher + implements ArgumentsMatcher + { + public boolean matches( Object[] objects, Object[] objects1 ) + { + if ( objects.length != 1 || objects1.length != 1 ) + { + return false; + } + else + { + AuditEvent o1 = (AuditEvent) objects[0]; + AuditEvent o2 = (AuditEvent) objects1[0]; + o2.setTimestamp( o1.getTimestamp() ); // effectively ignore the timestamp + return o1.equals( o2 ); + } + } + + public String toString( Object[] objects ) + { + return Arrays.asList( objects ).toString(); + } + } }