diff options
author | Olivier Lamy <olamy@apache.org> | 2013-12-22 12:25:43 +0000 |
---|---|---|
committer | Olivier Lamy <olamy@apache.org> | 2013-12-22 12:25:43 +0000 |
commit | 0c9fab3b3bc6d439e1660a8e1ef06b85de8889b9 (patch) | |
tree | 6642e09a08b4a1d6850888705b577f815cb37b5f | |
parent | 84bcc48e6b6f96c62c6826807794cd747afa41f6 (diff) | |
download | archiva-0c9fab3b3bc6d439e1660a8e1ef06b85de8889b9.tar.gz archiva-0c9fab3b3bc6d439e1660a8e1ef06b85de8889b9.zip |
[MRM-1750] Add remote repository health check
add unit test
git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@1552982 13f79535-47bb-0310-9956-ffa450edef68
7 files changed, 159 insertions, 32 deletions
diff --git a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/pom.xml b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/pom.xml index e58c40a36..99e37c8ee 100644 --- a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/pom.xml +++ b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/pom.xml @@ -402,18 +402,26 @@ <version>${tomcat7Version}</version> <scope>test</scope> </dependency> + <!-- <dependency> <groupId>org.apache.tomcat</groupId> <artifactId>tomcat-servlet-api</artifactId> <version>${tomcat7Version}</version> <scope>provided</scope> </dependency> + --> <dependency> <groupId>org.apache.archiva</groupId> <artifactId>archiva-metadata-consumer</artifactId> <scope>test</scope> </dependency> + <dependency> + <groupId>javax.servlet</groupId> + <artifactId>javax.servlet-api</artifactId> + <scope>provided</scope> + </dependency> + </dependencies> <build> diff --git a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultRemoteRepositoriesService.java b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultRemoteRepositoriesService.java index b48bccfb2..38afc6758 100644 --- a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultRemoteRepositoriesService.java +++ b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultRemoteRepositoriesService.java @@ -37,6 +37,8 @@ import org.apache.maven.wagon.authorization.AuthorizationException; import org.apache.maven.wagon.providers.http.AbstractHttpClientWagon; import org.apache.maven.wagon.providers.http.HttpConfiguration; import org.apache.maven.wagon.providers.http.HttpMethodConfiguration; +import org.apache.maven.wagon.providers.http.HttpWagon; +import org.apache.maven.wagon.repository.Repository; import org.springframework.stereotype.Service; import javax.inject.Inject; @@ -170,7 +172,7 @@ public class DefaultRemoteRepositoriesService wagon.setReadTimeout( remoteRepository.getRemoteDownloadTimeout() * 1000 ); wagon.setTimeout( remoteRepository.getTimeout() * 1000 ); - + HttpWagon foo; if ( wagon instanceof AbstractHttpClientWagon ) { HttpConfiguration httpConfiguration = new HttpConfiguration(); @@ -181,37 +183,14 @@ public class DefaultRemoteRepositoriesService AbstractHttpClientWagon.class.cast( wagon ).setHttpConfiguration( httpConfiguration ); } + wagon.connect( new Repository( remoteRepository.getId(), remoteRepository.getUrl() ) ); + // we only check connectivity as remote repo can be empty wagon.getFileList( "/" ); return Boolean.TRUE; } - catch ( RepositoryAdminException e ) - { - throw new ArchivaRestServiceException( e.getMessage(), - Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e ); - } - catch ( MalformedURLException e ) - { - throw new ArchivaRestServiceException( e.getMessage(), - Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e ); - } - catch ( WagonFactoryException e ) - { - throw new ArchivaRestServiceException( e.getMessage(), - Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e ); - } - catch ( TransferFailedException e ) - { - throw new ArchivaRestServiceException( e.getMessage(), - Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e ); - } - catch ( ResourceDoesNotExistException e ) - { - throw new ArchivaRestServiceException( e.getMessage(), - Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e ); - } - catch ( AuthorizationException e ) + catch ( Exception e ) { throw new ArchivaRestServiceException( e.getMessage(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e ); diff --git a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/ManagedRepositoriesServiceTest.java b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/ManagedRepositoriesServiceTest.java index 38e12302b..a49995457 100644 --- a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/ManagedRepositoriesServiceTest.java +++ b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/ManagedRepositoriesServiceTest.java @@ -167,4 +167,6 @@ public class ManagedRepositoriesServiceTest deleteTestRepo( testRepoId ); } + + } diff --git a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/RemoteRepositoriesServiceTest.java b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/RemoteRepositoriesServiceTest.java index c2d0a0d77..4d7d78c21 100644 --- a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/RemoteRepositoriesServiceTest.java +++ b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/RemoteRepositoriesServiceTest.java @@ -152,5 +152,4 @@ public class RemoteRepositoriesServiceTest "cool repo" ); } - } diff --git a/archiva-modules/archiva-web/archiva-web-common/src/test/java/org/apache/archiva/remotedownload/DownloadArtifactsTest.java b/archiva-modules/archiva-web/archiva-web-common/src/test/java/org/apache/archiva/remotedownload/DownloadArtifactsTest.java index 105c6d035..4775a88d5 100644 --- a/archiva-modules/archiva-web/archiva-web-common/src/test/java/org/apache/archiva/remotedownload/DownloadArtifactsTest.java +++ b/archiva-modules/archiva-web/archiva-web-common/src/test/java/org/apache/archiva/remotedownload/DownloadArtifactsTest.java @@ -124,8 +124,6 @@ public class DownloadArtifactsTest this.redirectPort = redirectServer.getConnectors()[0].getLocalPort(); log.info( "redirect server port {}", redirectPort ); - - } @After diff --git a/archiva-modules/archiva-web/archiva-web-common/src/test/java/org/apache/archiva/DownloadSnapshotTest.java b/archiva-modules/archiva-web/archiva-web-common/src/test/java/org/apache/archiva/remotedownload/DownloadSnapshotTest.java index 4da67a6e5..9b969ede9 100644 --- a/archiva-modules/archiva-web/archiva-web-common/src/test/java/org/apache/archiva/DownloadSnapshotTest.java +++ b/archiva-modules/archiva-web/archiva-web-common/src/test/java/org/apache/archiva/remotedownload/DownloadSnapshotTest.java @@ -1,4 +1,4 @@ -package org.apache.archiva; +package org.apache.archiva.remotedownload; /* * Licensed to the Apache Software Foundation (ASF) under one @@ -21,7 +21,6 @@ package org.apache.archiva; import org.apache.archiva.admin.model.beans.ManagedRepository; import org.apache.archiva.redback.rest.api.services.RoleManagementService; -import org.apache.archiva.remotedownload.AbstractDownloadTest; import org.apache.archiva.rest.api.services.ManagedRepositoriesService; import org.apache.archiva.security.common.ArchivaRoleConstants; import org.apache.archiva.test.utils.ArchivaBlockJUnit4ClassRunner; diff --git a/archiva-modules/archiva-web/archiva-web-common/src/test/java/org/apache/archiva/remotedownload/RemoteRepositoryConnectivityCheckTest.java b/archiva-modules/archiva-web/archiva-web-common/src/test/java/org/apache/archiva/remotedownload/RemoteRepositoryConnectivityCheckTest.java new file mode 100644 index 000000000..c882d8816 --- /dev/null +++ b/archiva-modules/archiva-web/archiva-web-common/src/test/java/org/apache/archiva/remotedownload/RemoteRepositoryConnectivityCheckTest.java @@ -0,0 +1,142 @@ +package org.apache.archiva; + +import com.google.common.io.Files; +import org.apache.archiva.admin.model.beans.RemoteRepository; +import org.apache.archiva.remotedownload.AbstractDownloadTest; +import org.apache.archiva.rest.api.services.RemoteRepositoriesService; +import org.apache.catalina.startup.Tomcat; +import org.apache.commons.io.FileUtils; +import org.apache.cxf.jaxrs.client.WebClient; +import org.eclipse.jetty.server.Handler; +import org.eclipse.jetty.server.Server; +import org.eclipse.jetty.server.handler.ContextHandler; +import org.eclipse.jetty.server.handler.ContextHandlerCollection; +import org.eclipse.jetty.server.handler.DefaultHandler; +import org.eclipse.jetty.server.handler.HandlerList; +import org.eclipse.jetty.server.handler.ResourceHandler; +import org.eclipse.jetty.servlet.DefaultServlet; +import org.eclipse.jetty.servlet.ServletContextHandler; +import org.eclipse.jetty.servlet.ServletHolder; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; + +import java.io.File; + +/** + * @author Olivier Lamy + */ +public class RemoteRepositoryConnectivityCheckTest + extends AbstractDownloadTest +{ + + @BeforeClass + public static void setAppServerBase() + { + previousAppServerBase = System.getProperty( "appserver.base" ); + System.setProperty( "appserver.base", "target/" + RemoteRepositoryConnectivityCheckTest.class.getName() ); + } + + + @AfterClass + public static void resetAppServerBase() + { + System.setProperty( "appserver.base", previousAppServerBase ); + } + + protected String getSpringConfigLocation() + { + return "classpath*:META-INF/spring-context.xml classpath*:spring-context-test-common.xml classpath*:spring-context-artifacts-download.xml"; + } + + @Test + public void checkRemoteConnectivity() + throws Exception + { + + Server repoServer = + buildStaticServer( new File( System.getProperty( "basedir" ) + "/src/test/repositories/test-repo" ) ); + repoServer.start(); + + RemoteRepositoriesService service = getRemoteRepositoriesService(); + + WebClient.client( service ).header( "Authorization", authorizationHeader ); + + try + { + + int repoServerPort = repoServer.getConnectors()[0].getLocalPort(); + + RemoteRepository repo = getRemoteRepository(); + + repo.setUrl( "http://localhost:" + repoServerPort ); + + service.addRemoteRepository( repo ); + + service.checkRemoteConnectivity( repo.getId() ); + } + finally + { + service.deleteRemoteRepository( "id-new" ); + repoServer.stop(); + } + } + + @Test + public void checkRemoteConnectivityEmptyRemote() + throws Exception + { + + File tmpDir = Files.createTempDir(); + Server repoServer = buildStaticServer( tmpDir ); + repoServer.start(); + + RemoteRepositoriesService service = getRemoteRepositoriesService(); + + WebClient.client( service ).header( "Authorization", authorizationHeader ); + + try + { + + int repoServerPort = repoServer.getConnectors()[0].getLocalPort(); + + RemoteRepository repo = getRemoteRepository(); + + repo.setUrl( "http://localhost:" + repoServerPort ); + + service.addRemoteRepository( repo ); + + service.checkRemoteConnectivity( repo.getId() ); + } + finally + { + service.deleteRemoteRepository( "id-new" ); + FileUtils.deleteQuietly( tmpDir ); + repoServer.stop(); + } + } + + protected Server buildStaticServer( File path ) + { + Server repoServer = new Server( 0 ); + + ResourceHandler resourceHandler = new ResourceHandler(); + resourceHandler.setDirectoriesListed( true ); + resourceHandler.setWelcomeFiles( new String[]{ "index.html" } ); + resourceHandler.setResourceBase( path.getAbsolutePath() ); + + HandlerList handlers = new HandlerList(); + handlers.setHandlers( new Handler[]{ resourceHandler, new DefaultHandler() } ); + repoServer.setHandler( handlers ); + + return repoServer; + } + + + RemoteRepository getRemoteRepository() + { + return new RemoteRepository( "id-new", "new one", "http://foo.com", "default", "foo", "foopassword", 120, + "cool repo" ); + } + +} |