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

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