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.

DownloadMergedIndexNonDefaultPathTest.java 9.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. package org.apache.archiva.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. *
  13. * Unless required by applicable law or agreed to in writing,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. */
  20. import org.apache.archiva.admin.model.beans.ManagedRepository;
  21. import org.apache.archiva.admin.model.beans.ProxyConnector;
  22. import org.apache.archiva.admin.model.beans.RemoteRepository;
  23. import org.apache.archiva.admin.model.beans.RepositoryGroup;
  24. import org.apache.archiva.maven.model.Artifact;
  25. import org.apache.archiva.redback.integration.security.role.RedbackRoleConstants;
  26. import org.apache.archiva.redback.rest.services.BaseSetup;
  27. import org.apache.archiva.rest.api.model.SearchRequest;
  28. import org.apache.archiva.rest.api.services.ManagedRepositoriesService;
  29. import org.apache.archiva.rest.api.services.ProxyConnectorService;
  30. import org.apache.archiva.rest.api.services.RepositoriesService;
  31. import org.apache.archiva.rest.api.services.RepositoryGroupService;
  32. import org.apache.archiva.rest.api.services.SearchService;
  33. import org.apache.archiva.test.utils.ArchivaBlockJUnit4ClassRunner;
  34. import org.apache.commons.io.FileUtils;
  35. import org.junit.After;
  36. import org.junit.AfterClass;
  37. import org.junit.Before;
  38. import org.junit.BeforeClass;
  39. import org.junit.Test;
  40. import org.junit.runner.RunWith;
  41. import java.io.IOException;
  42. import java.nio.file.Files;
  43. import java.nio.file.Path;
  44. import java.util.Arrays;
  45. import java.util.List;
  46. import java.util.Locale;
  47. import static org.assertj.core.api.Assertions.assertThat;
  48. /**
  49. * @author Olivier Lamy
  50. */
  51. @RunWith( ArchivaBlockJUnit4ClassRunner.class )
  52. public class DownloadMergedIndexNonDefaultPathTest
  53. extends AbstractDownloadTest
  54. {
  55. private static Path appServerBase;
  56. private Path indexDir;
  57. @BeforeClass
  58. public static void setAppServerBase()
  59. throws IOException
  60. {
  61. previousAppServerBase = System.getProperty( "appserver.base" );
  62. appServerBase = Files.createTempDirectory( "archiva-common-web_appsrv3_" ).toAbsolutePath();
  63. System.setProperty( "appserver.base", appServerBase.toString( ) );
  64. }
  65. @AfterClass
  66. public static void resetAppServerBase()
  67. {
  68. if (Files.exists(appServerBase)) {
  69. org.apache.commons.io.FileUtils.deleteQuietly( appServerBase.toFile() );
  70. }
  71. System.setProperty( "appserver.base", previousAppServerBase );
  72. }
  73. @Override
  74. protected String getSpringConfigLocation()
  75. {
  76. System.out.println( "AppserverBase: " + System.getProperty( "appserver.base" ) );
  77. return "classpath*:META-INF/spring-context.xml classpath*:spring-context-test-common.xml classpath*:spring-context-merge-index-download.xml";
  78. }
  79. @Before
  80. public void init() throws IOException
  81. {
  82. indexDir = Files.createTempDirectory( "archiva-web-common-index" );
  83. }
  84. @After
  85. public void cleanup()
  86. throws Exception
  87. {
  88. super.tearDown();
  89. if ( Files.exists( indexDir ) )
  90. {
  91. org.apache.commons.io.FileUtils.deleteDirectory( indexDir.toFile() );
  92. }
  93. }
  94. @Test
  95. public void downloadMergedIndexWithNonDefaultPath()
  96. throws Exception
  97. {
  98. Path indexBaseDir = indexDir.resolve("remotedownloadtest");
  99. if (!Files.exists(indexBaseDir)) {
  100. Files.createDirectories( indexBaseDir );
  101. }
  102. String id = Long.toString( System.currentTimeMillis() );
  103. Path srcRep = getProjectDirectory( ).resolve( "src/test/repositories/test-repo" );
  104. Path testRep = getBasedir( ).resolve( "target" ).resolve( "test-repo-" + id ).toAbsolutePath();
  105. FileUtils.copyDirectory( srcRep.toFile( ), testRep.toFile( ) );
  106. createdPaths.add( testRep );
  107. ManagedRepository managedRepository = new ManagedRepository( Locale.getDefault());
  108. managedRepository.setId( id );
  109. managedRepository.setName( "name of " + id );
  110. managedRepository.setLocation( testRep.toString() );
  111. managedRepository.setIndexDirectory( indexBaseDir.resolve( "index-" + id ).toString() );
  112. managedRepository.setPackedIndexDirectory( indexBaseDir.resolve( "indexPacked-" + id ).toString() );
  113. ManagedRepositoriesService managedRepositoriesService = getManagedRepositoriesService();
  114. if ( managedRepositoriesService.getManagedRepository( id ) != null )
  115. {
  116. managedRepositoriesService.deleteManagedRepository( id, false );
  117. }
  118. getManagedRepositoriesService().addManagedRepository( managedRepository );
  119. RepositoriesService repositoriesService = getRepositoriesService();
  120. repositoriesService.scanRepositoryNow( id, true );
  121. // wait a bit to ensure index is finished
  122. int timeout = 20000;
  123. while ( timeout > 0 && repositoriesService.getScanStatus( id ).isAlreadyScanning() )
  124. {
  125. Thread.sleep( 500 );
  126. timeout -= 500;
  127. }
  128. RepositoryGroupService repositoryGroupService = getRepositoryGroupService();
  129. String repoGroupId = "test-group";
  130. if ( repositoryGroupService.getRepositoryGroup( repoGroupId ) != null )
  131. {
  132. repositoryGroupService.deleteRepositoryGroup( repoGroupId );
  133. }
  134. RepositoryGroup repositoryGroup = new RepositoryGroup();
  135. repositoryGroup.setId( repoGroupId );
  136. String path = ".fooooo";
  137. repositoryGroup.setRepositories( Arrays.asList( id ) );
  138. repositoryGroup.setMergedIndexPath( path );
  139. repositoryGroupService.addRepositoryGroup( repositoryGroup );
  140. // create a repo with a remote on the one with index
  141. id = Long.toString( System.currentTimeMillis() );
  142. Path srcRep2 = getProjectDirectory( ).resolve( "src/test/repositories/test-repo" );
  143. Path testRep2 = getBasedir( ).resolve( "target" ).resolve( "test-repo-" + id ).toAbsolutePath();
  144. FileUtils.copyDirectory( srcRep2.toFile( ), testRep2.toFile( ) );
  145. createdPaths.add( testRep2 );
  146. managedRepository = new ManagedRepository(Locale.getDefault());
  147. managedRepository.setId( id );
  148. managedRepository.setName( "name of " + id );
  149. managedRepository.setLocation( testRep2.toString() );
  150. managedRepository.setIndexDirectory( indexBaseDir.resolve( "index-" + id ).toString() );
  151. managedRepository.setPackedIndexDirectory( indexBaseDir.resolve( "indexpacked-" + id ).toString() );
  152. if ( managedRepositoriesService.getManagedRepository( id ) != null )
  153. {
  154. managedRepositoriesService.deleteManagedRepository( id, false );
  155. }
  156. getManagedRepositoriesService().addManagedRepository( managedRepository );
  157. String remoteId = Long.toString( System.currentTimeMillis() );
  158. RemoteRepository remoteRepository = new RemoteRepository(Locale.getDefault());
  159. remoteRepository.setId( remoteId );
  160. remoteRepository.setName( remoteId );
  161. remoteRepository.setDownloadRemoteIndex( true );
  162. remoteRepository.setUrl( "http://localhost:" + port + "/repository/test-group" );
  163. remoteRepository.setRemoteIndexUrl( "http://localhost:" + port + "/repository/test-group/" + path );
  164. remoteRepository.setUserName( RedbackRoleConstants.ADMINISTRATOR_ACCOUNT_NAME );
  165. remoteRepository.setPassword( BaseSetup.getAdminPwd() );
  166. getRemoteRepositoriesService().addRemoteRepository( remoteRepository );
  167. ProxyConnectorService proxyConnectorService = getProxyConnectorService();
  168. ProxyConnector proxyConnector = new ProxyConnector();
  169. proxyConnector.setProxyId( "foo-bar2" );
  170. proxyConnector.setSourceRepoId( id );
  171. proxyConnector.setTargetRepoId( remoteId );
  172. proxyConnectorService.addProxyConnector( proxyConnector );
  173. repositoriesService.scheduleDownloadRemoteIndex( remoteId, true, true );
  174. // wait a bit
  175. /*
  176. timeout = 20000;
  177. while ( timeout > 0 )
  178. {
  179. Thread.sleep( 500 );
  180. timeout -= 500;
  181. }*/
  182. // wait the end
  183. while ( !repositoriesService.getRunningRemoteDownloadIds().getStrings().isEmpty() )
  184. {
  185. Thread.sleep( 500 );
  186. log.debug( "still running remote download" );
  187. }
  188. SearchService searchService = getSearchService();
  189. SearchRequest request = new SearchRequest();
  190. request.setRepositories( Arrays.asList( id ) );
  191. request.setGroupId( "org.apache.felix" );
  192. List<Artifact> artifacts = searchService.searchArtifacts( request );
  193. assertThat( artifacts ).isNotNull().isNotEmpty().hasSize( 1 );
  194. }
  195. }