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.

DownloadMergedIndexTest.java 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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 DownloadMergedIndexTest
  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_appsrv4_" ).toAbsolutePath();
  63. System.setProperty( "appserver.base", appServerBase.toString( ) );
  64. }
  65. @AfterClass
  66. public static void resetAppServerBase()
  67. {
  68. if (Files.exists(appServerBase)) {
  69. 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. FileUtils.deleteDirectory( indexDir.toFile() );
  92. }
  93. }
  94. @Test
  95. public void downloadMergedIndex()
  96. throws Exception
  97. {
  98. String id = Long.toString( System.currentTimeMillis() );
  99. Path srcRep = getProjectDirectory( ).resolve( "src/test/repositories/test-repo" );
  100. Path testRep = getBasedir( ).resolve( "target" ).resolve( "test-repo-" + id ).toAbsolutePath();
  101. FileUtils.copyDirectory( srcRep.toFile( ), testRep.toFile( ) );
  102. createdPaths.add( testRep );
  103. ManagedRepository managedRepository = new ManagedRepository( Locale.getDefault());
  104. managedRepository.setId( id );
  105. managedRepository.setName( "name of " + id );
  106. managedRepository.setLocation( testRep.toString() );
  107. managedRepository.setIndexDirectory( indexDir.resolve( "index-" + id ).toString() );
  108. ManagedRepositoriesService managedRepositoriesService = getManagedRepositoriesService();
  109. if ( managedRepositoriesService.getManagedRepository( id ) != null )
  110. {
  111. managedRepositoriesService.deleteManagedRepository( id, false );
  112. }
  113. getManagedRepositoriesService().addManagedRepository( managedRepository );
  114. RepositoriesService repositoriesService = getRepositoriesService();
  115. repositoriesService.scanRepositoryNow( id, true );
  116. // wait a bit to ensure index is finished
  117. int timeout = 20000;
  118. while ( timeout > 0 && repositoriesService.getScanStatus( id ).isAlreadyScanning() )
  119. {
  120. Thread.sleep( 500 );
  121. timeout -= 500;
  122. }
  123. RepositoryGroupService repositoryGroupService = getRepositoryGroupService();
  124. String repoGroupId = "test-group";
  125. if ( repositoryGroupService.getRepositoryGroup( repoGroupId ) != null )
  126. {
  127. repositoryGroupService.deleteRepositoryGroup( repoGroupId );
  128. }
  129. RepositoryGroup repositoryGroup = new RepositoryGroup();
  130. repositoryGroup.setId( repoGroupId );
  131. repositoryGroup.setRepositories( Arrays.asList( id ) );
  132. repositoryGroupService.addRepositoryGroup( repositoryGroup );
  133. // create a repo with a remote on the one with index
  134. id = Long.toString( System.currentTimeMillis() );
  135. Path srcRep2 = getProjectDirectory( ).resolve( "src/test/repositories/test-repo" );
  136. Path testRep2 = getBasedir( ).resolve( "target" ).resolve( "test-repo-" + id ).toAbsolutePath();
  137. FileUtils.copyDirectory( srcRep2.toFile( ), testRep2.toFile( ) );
  138. createdPaths.add( testRep2 );
  139. managedRepository = new ManagedRepository(Locale.getDefault());
  140. managedRepository.setId( id );
  141. managedRepository.setName( "name of " + id );
  142. managedRepository.setLocation( testRep2.toString() );
  143. managedRepository.setIndexDirectory( indexDir.resolve( "index-" + id ).toString() );
  144. if ( managedRepositoriesService.getManagedRepository( id ) != null )
  145. {
  146. managedRepositoriesService.deleteManagedRepository( id, false );
  147. }
  148. getManagedRepositoriesService().addManagedRepository( managedRepository );
  149. RemoteRepository remoteRepository = new RemoteRepository(Locale.getDefault());
  150. remoteRepository.setId( "all-merged" );
  151. remoteRepository.setName( "all-merged" );
  152. remoteRepository.setDownloadRemoteIndex( true );
  153. remoteRepository.setUrl( "http://localhost:" + port + "/repository/test-group" );
  154. remoteRepository.setRemoteIndexUrl( "http://localhost:" + port + "/repository/test-group/.index" );
  155. remoteRepository.setUserName( RedbackRoleConstants.ADMINISTRATOR_ACCOUNT_NAME );
  156. remoteRepository.setPassword( BaseSetup.getAdminPwd() );
  157. if ( getRemoteRepositoriesService().getRemoteRepository( remoteRepository.getId() ) != null )
  158. {
  159. getRemoteRepositoriesService().deleteRemoteRepository( remoteRepository.getId() );
  160. }
  161. getRemoteRepositoriesService().addRemoteRepository( remoteRepository );
  162. ProxyConnectorService proxyConnectorService = getProxyConnectorService();
  163. ProxyConnector proxyConnector = new ProxyConnector();
  164. proxyConnector.setProxyId( "foo-bar1" );
  165. proxyConnector.setSourceRepoId( id );
  166. proxyConnector.setTargetRepoId( "all-merged" );
  167. proxyConnectorService.addProxyConnector( proxyConnector );
  168. repositoriesService.scheduleDownloadRemoteIndex( "all-merged", true, true );
  169. // wait a bit
  170. timeout = 20000;
  171. while ( timeout > 0 )
  172. {
  173. Thread.sleep( 500 );
  174. timeout -= 500;
  175. }
  176. SearchService searchService = getSearchService();
  177. SearchRequest request = new SearchRequest();
  178. request.setRepositories( Arrays.asList( id ) );
  179. request.setGroupId( "org.apache.felix" );
  180. List<Artifact> artifacts = searchService.searchArtifacts( request );
  181. assertThat( artifacts ).isNotNull().isNotEmpty().hasSize( 1 );
  182. }
  183. }