You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RemoteRepositoryConnectivityCheckTest.java 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. package org.apache.archiva.web.remotedownload;
  2. /*
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. import org.apache.archiva.admin.model.beans.RemoteRepository;
  20. import org.apache.archiva.rest.api.services.RemoteRepositoriesService;
  21. import org.apache.commons.io.FileUtils;
  22. import org.apache.cxf.jaxrs.client.WebClient;
  23. import org.eclipse.jetty.server.Handler;
  24. import org.eclipse.jetty.server.HttpConnectionFactory;
  25. import org.eclipse.jetty.server.Server;
  26. import org.eclipse.jetty.server.ServerConnector;
  27. import org.eclipse.jetty.server.handler.DefaultHandler;
  28. import org.eclipse.jetty.server.handler.HandlerList;
  29. import org.eclipse.jetty.server.handler.ResourceHandler;
  30. import org.junit.AfterClass;
  31. import org.junit.BeforeClass;
  32. import org.junit.Test;
  33. import java.io.IOException;
  34. import java.nio.file.Files;
  35. import java.nio.file.Path;
  36. import java.util.Locale;
  37. import static org.assertj.core.api.Assertions.assertThat;
  38. /**
  39. * @author Olivier Lamy
  40. */
  41. public class RemoteRepositoryConnectivityCheckTest
  42. extends AbstractDownloadTest
  43. {
  44. private static Path appServerBase;
  45. @BeforeClass
  46. public static void setAppServerBase()
  47. throws IOException
  48. {
  49. previousAppServerBase = System.getProperty( "appserver.base" );
  50. appServerBase = Files.createTempDirectory( "archiva-common-web_appsrv6_" ).toAbsolutePath( );
  51. System.setProperty( "appserver.base", appServerBase.toString( ) );
  52. }
  53. @AfterClass
  54. public static void resetAppServerBase()
  55. {
  56. if (Files.exists(appServerBase)) {
  57. FileUtils.deleteQuietly( appServerBase.toFile() );
  58. }
  59. System.setProperty( "appserver.base", previousAppServerBase );
  60. }
  61. @Override
  62. protected String getSpringConfigLocation()
  63. {
  64. System.out.println( "AppserverBase: " + System.getProperty( "appserver.base" ) );
  65. return "classpath*:META-INF/spring-context.xml classpath*:spring-context-test-common.xml classpath*:spring-context-artifacts-download.xml";
  66. }
  67. @Test
  68. public void checkRemoteConnectivity()
  69. throws Exception
  70. {
  71. String id = Long.toString( System.currentTimeMillis() );
  72. Path srcRep = getProjectDirectory( ).resolve( "src/test/repositories/test-repo" );
  73. Path testRep = getBasedir( ).resolve( "target" ).resolve( "test-repo-" + id ).toAbsolutePath();
  74. FileUtils.copyDirectory( srcRep.toFile( ), testRep.toFile( ) );
  75. createdPaths.add( testRep );
  76. Server repoServer =
  77. buildStaticServer( testRep );
  78. ServerConnector serverConnector = new ServerConnector( repoServer, new HttpConnectionFactory());
  79. repoServer.addConnector( serverConnector );
  80. repoServer.start();
  81. RemoteRepositoriesService service = getRemoteRepositoriesService();
  82. WebClient.client( service ).header( "Authorization", authorizationHeader );
  83. try
  84. {
  85. int repoServerPort = serverConnector.getLocalPort();
  86. RemoteRepository repo = getRemoteRepository();
  87. repo.setUrl( "http://localhost:" + repoServerPort );
  88. service.addRemoteRepository( repo );
  89. assertThat( service.checkRemoteConnectivity( repo.getId() ).isSuccess() ).isTrue();
  90. }
  91. finally
  92. {
  93. service.deleteRemoteRepository( "id-new" );
  94. repoServer.stop();
  95. }
  96. }
  97. @Test
  98. public void checkRemoteConnectivityEmptyRemote()
  99. throws Exception
  100. {
  101. Path tmpDir = Files.createTempDirectory( "test" );
  102. Server repoServer = buildStaticServer( tmpDir );
  103. ServerConnector serverConnector = new ServerConnector( repoServer, new HttpConnectionFactory());
  104. repoServer.addConnector( serverConnector );
  105. repoServer.start();
  106. RemoteRepositoriesService service = getRemoteRepositoriesService();
  107. WebClient.client( service ).header( "Authorization", authorizationHeader );
  108. try
  109. {
  110. int repoServerPort = serverConnector.getLocalPort();
  111. RemoteRepository repo = getRemoteRepository();
  112. repo.setUrl( "http://localhost:" + repoServerPort );
  113. service.addRemoteRepository( repo );
  114. assertThat( service.checkRemoteConnectivity( repo.getId() ).isSuccess() ).isTrue();
  115. }
  116. finally
  117. {
  118. service.deleteRemoteRepository( "id-new" );
  119. org.apache.archiva.common.utils.FileUtils.deleteQuietly( tmpDir );
  120. repoServer.stop();
  121. }
  122. }
  123. @Test
  124. public void checkRemoteConnectivityFail()
  125. throws Exception
  126. {
  127. RemoteRepositoriesService service = getRemoteRepositoriesService();
  128. WebClient.client( service ).header( "Authorization", authorizationHeader );
  129. try
  130. {
  131. RemoteRepository repo = getRemoteRepository();
  132. repo.setUrl( "http://localhost:8956" );
  133. service.addRemoteRepository( repo );
  134. assertThat( service.checkRemoteConnectivity( repo.getId() ).isSuccess() ).isFalse();
  135. }
  136. finally
  137. {
  138. service.deleteRemoteRepository( "id-new" );
  139. }
  140. }
  141. protected Server buildStaticServer( Path path )
  142. {
  143. Server repoServer = new Server( );
  144. ResourceHandler resourceHandler = new ResourceHandler();
  145. resourceHandler.setDirectoriesListed( true );
  146. resourceHandler.setWelcomeFiles( new String[]{ "index.html" } );
  147. resourceHandler.setResourceBase( path.toAbsolutePath().toString() );
  148. HandlerList handlers = new HandlerList();
  149. handlers.setHandlers( new Handler[]{ resourceHandler, new DefaultHandler() } );
  150. repoServer.setHandler( handlers );
  151. return repoServer;
  152. }
  153. RemoteRepository getRemoteRepository()
  154. {
  155. return new RemoteRepository( Locale.getDefault( ), "id-new", "new one", "http://foo.com", "default", "foo", "foopassword", 120,
  156. "cool repo" );
  157. }
  158. }