]> source.dussan.org Git - archiva.git/commitdiff
[MRM-1750] Add remote repository health check
authorOlivier Lamy <olamy@apache.org>
Sun, 22 Dec 2013 12:25:43 +0000 (12:25 +0000)
committerOlivier Lamy <olamy@apache.org>
Sun, 22 Dec 2013 12:25:43 +0000 (12:25 +0000)
add unit test

git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@1552982 13f79535-47bb-0310-9956-ffa450edef68

archiva-modules/archiva-web/archiva-rest/archiva-rest-services/pom.xml
archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultRemoteRepositoriesService.java
archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/ManagedRepositoriesServiceTest.java
archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/RemoteRepositoriesServiceTest.java
archiva-modules/archiva-web/archiva-web-common/src/test/java/org/apache/archiva/DownloadSnapshotTest.java [deleted file]
archiva-modules/archiva-web/archiva-web-common/src/test/java/org/apache/archiva/remotedownload/DownloadArtifactsTest.java
archiva-modules/archiva-web/archiva-web-common/src/test/java/org/apache/archiva/remotedownload/DownloadSnapshotTest.java [new file with mode: 0644]
archiva-modules/archiva-web/archiva-web-common/src/test/java/org/apache/archiva/remotedownload/RemoteRepositoryConnectivityCheckTest.java [new file with mode: 0644]

index e58c40a3658daa954b70f95b23193014c7aac73f..99e37c8eef56c4e40766c9742a8ef741078229c5 100644 (file)
       <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>
index b48bccfb2f05df0c7931b40ddaddfa7f1ed29efa..38afc67587682cdf47fdf2f4c2a89d0a0575560d 100644 (file)
@@ -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-web-common/src/test/java/org/apache/archiva/DownloadSnapshotTest.java b/archiva-modules/archiva-web/archiva-web-common/src/test/java/org/apache/archiva/DownloadSnapshotTest.java
deleted file mode 100644 (file)
index 4da67a6..0000000
+++ /dev/null
@@ -1,137 +0,0 @@
-package org.apache.archiva;
-
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-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;
-import org.apache.commons.io.FileUtils;
-import org.apache.maven.wagon.providers.http.HttpWagon;
-import org.apache.maven.wagon.repository.Repository;
-import org.junit.AfterClass;
-import org.junit.BeforeClass;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import java.io.File;
-import java.util.List;
-import java.util.zip.ZipEntry;
-import java.util.zip.ZipFile;
-
-/**
- * @author Olivier Lamy
- */
-@RunWith( ArchivaBlockJUnit4ClassRunner.class )
-public class DownloadSnapshotTest
-    extends AbstractDownloadTest
-{
-    protected Logger log = LoggerFactory.getLogger( getClass() );
-
-    @BeforeClass
-    public static void setAppServerBase()
-    {
-        previousAppServerBase = System.getProperty( "appserver.base" );
-        System.setProperty( "appserver.base", "target/" + DownloadSnapshotTest.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 downloadSNAPSHOT()
-        throws Exception
-    {
-
-        File tmpIndexDir = new File( System.getProperty( "java.io.tmpdir" ) + "/tmpIndex" );
-        if ( tmpIndexDir.exists() )
-        {
-            FileUtils.deleteDirectory( tmpIndexDir );
-        }
-        String id = Long.toString( System.currentTimeMillis() );
-        ManagedRepository managedRepository = new ManagedRepository();
-        managedRepository.setId( id );
-        managedRepository.setName( "name of " + id );
-        managedRepository.setLocation( System.getProperty( "basedir" ) + "/src/test/repositories/snapshot-repo" );
-        managedRepository.setIndexDirectory( System.getProperty( "java.io.tmpdir" ) + "/tmpIndex/" + id );
-
-        ManagedRepositoriesService managedRepositoriesService = getManagedRepositoriesService();
-
-        if ( managedRepositoriesService.getManagedRepository( id ) != null )
-        {
-            managedRepositoriesService.deleteManagedRepository( id, false );
-        }
-
-        getManagedRepositoriesService().addManagedRepository( managedRepository );
-
-        RoleManagementService roleManagementService = getRoleManagementService( authorizationHeader );
-
-        if ( !roleManagementService.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER,
-                                                         id ) )
-        {
-            roleManagementService.createTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, id );
-        }
-
-        getUserService( authorizationHeader ).createGuestUser();
-        roleManagementService.assignRole( ArchivaRoleConstants.TEMPLATE_GUEST, "guest" );
-
-        roleManagementService.assignTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, id,
-                                                   "guest" );
-
-        getUserService( authorizationHeader ).removeFromCache( "guest" );
-
-        File file = new File( "target/archiva-model-1.4-M4-SNAPSHOT.jar" );
-        if ( file.exists() )
-        {
-            file.delete();
-        }
-
-        HttpWagon httpWagon = new HttpWagon();
-        httpWagon.connect( new Repository( "foo", "http://localhost:" + port ) );
-
-        httpWagon.get( "/repository/"+ id +"/org/apache/archiva/archiva-model/1.4-M4-SNAPSHOT/archiva-model-1.4-M4-SNAPSHOT.jar", file );
-
-        ZipFile zipFile = new ZipFile( file );
-        List<String> entries = getZipEntriesNames( zipFile );
-        ZipEntry zipEntry = zipFile.getEntry( "org/apache/archiva/model/ArchivaArtifact.class" );
-        assertNotNull( "cannot find zipEntry org/apache/archiva/model/ArchivaArtifact.class, entries: " + entries + ", content is: "
-                           + FileUtils.readFileToString( file ), zipEntry );
-        zipFile.close();
-        file.deleteOnExit();
-
-
-
-    }
-
-}
index 105c6d035f4b098412ff082d749f8c130bc66479..4775a88d5acc6ad6f7e82e29d2188c5254f1655d 100644 (file)
@@ -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/remotedownload/DownloadSnapshotTest.java b/archiva-modules/archiva-web/archiva-web-common/src/test/java/org/apache/archiva/remotedownload/DownloadSnapshotTest.java
new file mode 100644 (file)
index 0000000..9b969ed
--- /dev/null
@@ -0,0 +1,136 @@
+package org.apache.archiva.remotedownload;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import org.apache.archiva.admin.model.beans.ManagedRepository;
+import org.apache.archiva.redback.rest.api.services.RoleManagementService;
+import org.apache.archiva.rest.api.services.ManagedRepositoriesService;
+import org.apache.archiva.security.common.ArchivaRoleConstants;
+import org.apache.archiva.test.utils.ArchivaBlockJUnit4ClassRunner;
+import org.apache.commons.io.FileUtils;
+import org.apache.maven.wagon.providers.http.HttpWagon;
+import org.apache.maven.wagon.repository.Repository;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.File;
+import java.util.List;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipFile;
+
+/**
+ * @author Olivier Lamy
+ */
+@RunWith( ArchivaBlockJUnit4ClassRunner.class )
+public class DownloadSnapshotTest
+    extends AbstractDownloadTest
+{
+    protected Logger log = LoggerFactory.getLogger( getClass() );
+
+    @BeforeClass
+    public static void setAppServerBase()
+    {
+        previousAppServerBase = System.getProperty( "appserver.base" );
+        System.setProperty( "appserver.base", "target/" + DownloadSnapshotTest.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 downloadSNAPSHOT()
+        throws Exception
+    {
+
+        File tmpIndexDir = new File( System.getProperty( "java.io.tmpdir" ) + "/tmpIndex" );
+        if ( tmpIndexDir.exists() )
+        {
+            FileUtils.deleteDirectory( tmpIndexDir );
+        }
+        String id = Long.toString( System.currentTimeMillis() );
+        ManagedRepository managedRepository = new ManagedRepository();
+        managedRepository.setId( id );
+        managedRepository.setName( "name of " + id );
+        managedRepository.setLocation( System.getProperty( "basedir" ) + "/src/test/repositories/snapshot-repo" );
+        managedRepository.setIndexDirectory( System.getProperty( "java.io.tmpdir" ) + "/tmpIndex/" + id );
+
+        ManagedRepositoriesService managedRepositoriesService = getManagedRepositoriesService();
+
+        if ( managedRepositoriesService.getManagedRepository( id ) != null )
+        {
+            managedRepositoriesService.deleteManagedRepository( id, false );
+        }
+
+        getManagedRepositoriesService().addManagedRepository( managedRepository );
+
+        RoleManagementService roleManagementService = getRoleManagementService( authorizationHeader );
+
+        if ( !roleManagementService.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER,
+                                                         id ) )
+        {
+            roleManagementService.createTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, id );
+        }
+
+        getUserService( authorizationHeader ).createGuestUser();
+        roleManagementService.assignRole( ArchivaRoleConstants.TEMPLATE_GUEST, "guest" );
+
+        roleManagementService.assignTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, id,
+                                                   "guest" );
+
+        getUserService( authorizationHeader ).removeFromCache( "guest" );
+
+        File file = new File( "target/archiva-model-1.4-M4-SNAPSHOT.jar" );
+        if ( file.exists() )
+        {
+            file.delete();
+        }
+
+        HttpWagon httpWagon = new HttpWagon();
+        httpWagon.connect( new Repository( "foo", "http://localhost:" + port ) );
+
+        httpWagon.get( "/repository/"+ id +"/org/apache/archiva/archiva-model/1.4-M4-SNAPSHOT/archiva-model-1.4-M4-SNAPSHOT.jar", file );
+
+        ZipFile zipFile = new ZipFile( file );
+        List<String> entries = getZipEntriesNames( zipFile );
+        ZipEntry zipEntry = zipFile.getEntry( "org/apache/archiva/model/ArchivaArtifact.class" );
+        assertNotNull( "cannot find zipEntry org/apache/archiva/model/ArchivaArtifact.class, entries: " + entries + ", content is: "
+                           + FileUtils.readFileToString( file ), zipEntry );
+        zipFile.close();
+        file.deleteOnExit();
+
+
+
+    }
+
+}
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 (file)
index 0000000..c882d88
--- /dev/null
@@ -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" );
+    }
+
+}