]> source.dussan.org Git - archiva.git/blob
01921e45de77c1bbcb73bf5f00c49f75f62e1a6e
[archiva.git] /
1 package org.apache.archiva.scheduler.indexing.maven;
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.RemoteRepository;
22 import org.apache.archiva.admin.model.remote.RemoteRepositoryAdmin;
23 import org.apache.archiva.common.utils.FileUtils;
24 import org.apache.archiva.repository.RepositoryRegistry;
25 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
26 import org.apache.maven.index.FlatSearchRequest;
27 import org.apache.maven.index.FlatSearchResponse;
28 import org.apache.maven.index.Indexer;
29 import org.apache.maven.index.MAVEN;
30 import org.apache.maven.index.context.IndexingContext;
31 import org.apache.maven.index.expr.StringSearchExpression;
32 import org.apache.maven.index_shaded.lucene.search.BooleanClause;
33 import org.apache.maven.index_shaded.lucene.search.BooleanQuery;
34 import org.assertj.core.api.Assertions;
35 import org.eclipse.jetty.server.HttpConnectionFactory;
36 import org.eclipse.jetty.server.Server;
37 import org.eclipse.jetty.server.ServerConnector;
38 import org.eclipse.jetty.servlet.DefaultServlet;
39 import org.eclipse.jetty.servlet.ServletContextHandler;
40 import org.eclipse.jetty.servlet.ServletHolder;
41 import org.junit.After;
42 import org.junit.Before;
43 import org.junit.Test;
44 import org.junit.runner.RunWith;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47 import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
48 import org.springframework.test.context.ContextConfiguration;
49
50 import javax.inject.Inject;
51 import java.io.IOException;
52 import java.nio.file.Files;
53 import java.nio.file.Path;
54 import java.nio.file.Paths;
55 import java.util.Arrays;
56 import java.util.Locale;
57 import java.util.concurrent.TimeUnit;
58
59 import static org.assertj.core.api.Assertions.assertThat;
60
61 /**
62  * @author Olivier Lamy
63  */
64 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
65 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" } )
66 public class DownloadRemoteIndexTaskTest
67 {
68
69     private Server server;
70     private ServerConnector serverConnector;
71
72     private int port;
73
74     private Logger log = LoggerFactory.getLogger( getClass() );
75
76     @Inject
77     RemoteRepositoryAdmin remoteRepositoryAdmin;
78
79     @Inject
80     DefaultDownloadRemoteIndexScheduler downloadRemoteIndexScheduler;
81
82     @Inject
83     Indexer indexer;
84
85     @Inject
86     RepositoryRegistry repositoryRegistry;
87
88     @Before
89     public void initialize()
90         throws Exception
91     {
92         Path cfgFile = Paths.get("target/appserver-base/conf/archiva.xml");
93         if (Files.exists(cfgFile)) {
94             Files.delete(cfgFile);
95         }
96         try {
97             remoteRepositoryAdmin.deleteRemoteRepository("test-repo-re", null);
98         } catch (Exception e) {
99             // Ignore
100         }
101         server = new Server( );
102         serverConnector = new ServerConnector( server, new HttpConnectionFactory());
103         server.addConnector( serverConnector );
104         createContext( server, Paths.get( "src/test/" ) );
105         this.server.start();
106         this.port = serverConnector.getLocalPort();
107         log.info( "start server on port {}", this.port );
108     }
109
110     protected void createContext( Server server, Path repositoryDirectory )
111         throws IOException
112     {
113         ServletContextHandler context = new ServletContextHandler();
114         context.setResourceBase( repositoryDirectory.toAbsolutePath().toString() );
115         context.setContextPath( "/" );
116         ServletHolder sh = new ServletHolder( DefaultServlet.class );
117         context.addServlet( sh, "/" );
118         server.setHandler( context );
119
120     }
121
122     @After
123     public void tearDown()
124         throws Exception
125     {
126         if (server!=null) {
127             server.stop();
128         }
129         Path cfgFile = Paths.get("target/appserver-base/conf/archiva.xml");
130         if (Files.exists(cfgFile)) {
131             Files.delete(cfgFile);
132         }
133     }
134
135     @Test
136     public void downloadAndMergeRemoteIndexInEmptyIndex()
137         throws Exception
138     {
139         RemoteRepository remoteRepository = getRemoteRepository();
140
141         remoteRepositoryAdmin.addRemoteRepository( remoteRepository, null );
142
143         downloadRemoteIndexScheduler.startup();
144
145         downloadRemoteIndexScheduler.scheduleDownloadRemote( "test-repo-re", true, true );
146
147         ( (ThreadPoolTaskScheduler) downloadRemoteIndexScheduler.getTaskScheduler() ).getScheduledExecutor().awaitTermination(
148             10, TimeUnit.SECONDS );
149
150         remoteRepositoryAdmin.deleteRemoteRepository( "test-repo-re", null );
151
152         // search
153         BooleanQuery.Builder iQuery = new BooleanQuery.Builder();
154         iQuery.add( indexer.constructQuery( MAVEN.GROUP_ID, new StringSearchExpression( "commons-logging" ) ),
155                     BooleanClause.Occur.SHOULD );
156
157         remoteRepositoryAdmin.addRemoteRepository(remoteRepository,  null);
158         FlatSearchRequest rq = new FlatSearchRequest( iQuery.build() );
159         rq.setContexts(
160             Arrays.asList( repositoryRegistry.getRemoteRepository(remoteRepository.getId()).getIndexingContext().getBaseContext(IndexingContext.class) ) );
161
162         FlatSearchResponse response = indexer.searchFlat(rq);
163
164         log.info( "returned hit count:{}", response.getReturnedHitsCount() );
165         Assertions.assertThat( response.getReturnedHitsCount() ).isEqualTo( 8 );
166     }
167
168
169     protected RemoteRepository getRemoteRepository() throws IOException
170     {
171         RemoteRepository remoteRepository = new RemoteRepository( Locale.getDefault());
172         Path indexDirectory =
173             Paths.get( FileUtils.getBasedir(), "target/index/test-" + Long.toString( System.currentTimeMillis() ) );
174         Files.createDirectories( indexDirectory );
175         indexDirectory.toFile().deleteOnExit();
176
177         remoteRepository.setName( "foo" );
178         remoteRepository.setIndexDirectory( indexDirectory.toAbsolutePath().toString() );
179         remoteRepository.setDownloadRemoteIndex( true );
180         remoteRepository.setId( "test-repo-re" );
181         remoteRepository.setUrl( "http://localhost:" + port );
182         remoteRepository.setRemoteIndexUrl( "http://localhost:" + port + "/index-updates/" );
183
184         return remoteRepository;
185     }
186
187 }