]> source.dussan.org Git - archiva.git/blob
2210ef8e7353f33493576a427b8af292aa085bad
[archiva.git] /
1 package org.apache.archiva;
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
21 import org.apache.archiva.admin.model.beans.ManagedRepository;
22 import org.apache.archiva.admin.model.beans.ProxyConnector;
23 import org.apache.archiva.admin.model.beans.RemoteRepository;
24 import org.apache.archiva.admin.model.beans.RepositoryGroup;
25 import org.apache.archiva.rest.api.model.Artifact;
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.codehaus.redback.integration.security.role.RedbackRoleConstants;
33 import org.codehaus.redback.rest.services.FakeCreateAdminService;
34 import org.junit.AfterClass;
35 import org.junit.BeforeClass;
36 import org.junit.Test;
37 import org.junit.runner.RunWith;
38 import org.junit.runners.JUnit4;
39
40 import java.util.Arrays;
41 import java.util.List;
42
43 /**
44  * @author Olivier Lamy
45  */
46 @RunWith( JUnit4.class )
47 public class DownloadMergedIndexTest
48     extends AbstractDownloadTest
49 {
50
51     @BeforeClass
52     public static void setAppServerBase()
53     {
54         previousAppServerBase = System.getProperty( "appserver.base" );
55         System.setProperty( "appserver.base", "target/" + DownloadMergedIndexTest.class.getName() );
56     }
57
58     @AfterClass
59     public static void resetAppServerBase()
60     {
61         System.setProperty( "appserver.base", previousAppServerBase );
62     }
63
64     protected String getSpringConfigLocation()
65     {
66         return "classpath*:META-INF/spring-context.xml classpath*:spring-context-merge-index-download.xml";
67     }
68
69     @Test
70     public void downloadMergedIndex()
71         throws Exception
72     {
73         String id = Long.toString( System.currentTimeMillis() );
74         ManagedRepository managedRepository = new ManagedRepository();
75         managedRepository.setId( id );
76         managedRepository.setName( "name of " + id );
77         managedRepository.setLocation( "src/test/repositories/test-repo" );
78         managedRepository.setIndexDirectory( System.getProperty( "java.io.tmpdir" ) + "/target/tmpIndex/" + id );
79
80         ManagedRepositoriesService managedRepositoriesService = getManagedRepositoriesService();
81
82         if ( managedRepositoriesService.getManagedRepository( id ) != null )
83         {
84             managedRepositoriesService.deleteManagedRepository( id, false );
85         }
86
87         getManagedRepositoriesService().addManagedRepository( managedRepository );
88
89         RepositoriesService repositoriesService = getRepositoriesService();
90
91         repositoriesService.scanRepositoryNow( id, true );
92
93         // wait a bit to ensure index is finished
94         int timeout = 20000;
95         while ( timeout > 0 && repositoriesService.alreadyScanning( id ) )
96         {
97             Thread.sleep( 500 );
98             timeout -= 500;
99         }
100
101         RepositoryGroupService repositoryGroupService = getRepositoryGroupService();
102
103         RepositoryGroup repositoryGroup = new RepositoryGroup();
104         repositoryGroup.setId( "test-group" );
105         repositoryGroup.setRepositories( Arrays.asList( id ) );
106
107         repositoryGroupService.addRepositoryGroup( repositoryGroup );
108
109         // create a repo with a remote on the one with index
110         id = Long.toString( System.currentTimeMillis() );
111         managedRepository = new ManagedRepository();
112         managedRepository.setId( id );
113         managedRepository.setName( "name of " + id );
114         managedRepository.setLocation( "src/test/repositories/test-repo" );
115         managedRepository.setIndexDirectory( System.getProperty( "java.io.tmpdir" ) + "/target/tmpIndex/" + id );
116
117         if ( managedRepositoriesService.getManagedRepository( id ) != null )
118         {
119             managedRepositoriesService.deleteManagedRepository( id, false );
120         }
121
122         getManagedRepositoriesService().addManagedRepository( managedRepository );
123
124         RemoteRepository remoteRepository = new RemoteRepository();
125         remoteRepository.setId( "all-merged" );
126         remoteRepository.setName( "all-merged" );
127         remoteRepository.setDownloadRemoteIndex( true );
128         remoteRepository.setUrl( "http://localhost:" + port + "/repository/test-group" );
129         remoteRepository.setRemoteIndexUrl( "http://localhost:" + port + "/repository/test-group/.indexer" );
130         remoteRepository.setUserName( RedbackRoleConstants.ADMINISTRATOR_ACCOUNT_NAME );
131         remoteRepository.setPassword( FakeCreateAdminService.ADMIN_TEST_PWD );
132
133         getRemoteRepositoriesService().addRemoteRepository( remoteRepository );
134
135         ProxyConnectorService proxyConnectorService = getProxyConnectorService();
136         ProxyConnector proxyConnector = new ProxyConnector();
137         proxyConnector.setProxyId( "foo-bar" );
138         proxyConnector.setSourceRepoId( id );
139         proxyConnector.setTargetRepoId( "all-merged" );
140         proxyConnectorService.addProxyConnector( proxyConnector );
141
142         repositoriesService.scheduleDownloadRemoteIndex( "all-merged", true, true );
143
144         // wait a bit
145         timeout = 20000;
146         while ( timeout > 0 )
147         {
148             Thread.sleep( 500 );
149             timeout -= 500;
150         }
151
152         SearchService searchService = getSearchService();
153
154         SearchRequest request = new SearchRequest();
155         request.setRepositories( Arrays.asList( id ) );
156         request.setGroupId( "org.apache.felix" );
157
158         List<Artifact> artifacts = searchService.searchArtifacts( request );
159         assertFalse( artifacts.isEmpty() );
160         assertEquals( 1, artifacts.size() );
161     }
162 }