View Javadoc
1   package org.apache.archiva.scheduler.indexing;
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 junit.framework.TestCase;
22  import org.apache.archiva.admin.model.beans.RemoteRepository;
23  import org.apache.archiva.admin.model.remote.RemoteRepositoryAdmin;
24  import org.apache.archiva.common.plexusbridge.PlexusSisuBridge;
25  import org.apache.archiva.common.utils.FileUtil;
26  import org.apache.lucene.search.BooleanClause;
27  import org.apache.lucene.search.BooleanQuery;
28  import org.apache.maven.index.FlatSearchRequest;
29  import org.apache.maven.index.FlatSearchResponse;
30  import org.apache.maven.index.MAVEN;
31  import org.apache.maven.index.NexusIndexer;
32  import org.apache.maven.index.expr.StringSearchExpression;
33  import org.eclipse.jetty.server.Connector;
34  import org.eclipse.jetty.server.Server;
35  import org.eclipse.jetty.servlet.DefaultServlet;
36  import org.eclipse.jetty.servlet.ServletContextHandler;
37  import org.eclipse.jetty.servlet.ServletHolder;
38  import static org.assertj.core.api.Assertions.assertThat;
39  import org.junit.After;
40  import org.junit.Before;
41  import org.junit.Test;
42  import org.junit.runner.RunWith;
43  import org.slf4j.Logger;
44  import org.slf4j.LoggerFactory;
45  import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
46  import org.springframework.test.context.ContextConfiguration;
47  
48  import javax.inject.Inject;
49  import java.io.File;
50  import java.io.IOException;
51  import java.util.Arrays;
52  import java.util.concurrent.TimeUnit;
53  import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
54  
55  /**
56   * @author Olivier Lamy
57   */
58  @RunWith( ArchivaSpringJUnit4ClassRunner.class )
59  @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" } )
60  public class DownloadRemoteIndexTaskTest
61  {
62  
63      private Server server;
64  
65      private int port;
66  
67      private Logger log = LoggerFactory.getLogger( getClass() );
68  
69      @Inject
70      RemoteRepositoryAdmin remoteRepositoryAdmin;
71  
72      @Inject
73      DefaultDownloadRemoteIndexScheduler downloadRemoteIndexScheduler;
74  
75      @Inject
76      PlexusSisuBridge plexusSisuBridge;
77  
78      NexusIndexer nexusIndexer;
79  
80      @Before
81      public void initialize()
82          throws Exception
83      {
84          server = new Server( 0 );
85          createContext( server, new File( "src/test/" ) );
86  
87          this.server.start();
88          Connector connector = this.server.getConnectors()[0];
89          this.port = connector.getLocalPort();
90          log.info( "start server on port {}", this.port );
91          nexusIndexer = plexusSisuBridge.lookup( NexusIndexer.class );
92      }
93  
94      protected void createContext( Server server, File repositoryDirectory )
95          throws IOException
96      {
97          ServletContextHandler context = new ServletContextHandler();
98          context.setResourceBase( repositoryDirectory.getAbsolutePath() );
99          context.setContextPath( "/" );
100         ServletHolder sh = new ServletHolder( DefaultServlet.class );
101         context.addServlet( sh, "/" );
102         server.setHandler( context );
103 
104     }
105 
106     @After
107     public void tearDown()
108         throws Exception
109     {
110         server.stop();
111     }
112 
113     @Test
114     public void downloadAndMergeRemoteIndexInEmptyIndex()
115         throws Exception
116     {
117         RemoteRepository remoteRepository = getRemoteRepository();
118 
119         remoteRepositoryAdmin.addRemoteRepository( remoteRepository, null );
120 
121         downloadRemoteIndexScheduler.startup();
122 
123         downloadRemoteIndexScheduler.scheduleDownloadRemote( "test-repo", true, true );
124 
125         ( (ThreadPoolTaskScheduler) downloadRemoteIndexScheduler.getTaskScheduler() ).getScheduledExecutor().awaitTermination(
126             10, TimeUnit.SECONDS );
127 
128         remoteRepositoryAdmin.deleteRemoteRepository( "test-repo", null );
129 
130         // search
131         BooleanQuery iQuery = new BooleanQuery();
132         iQuery.add( nexusIndexer.constructQuery( MAVEN.GROUP_ID, new StringSearchExpression( "commons-logging" ) ),
133                     BooleanClause.Occur.SHOULD );
134 
135         FlatSearchRequest rq = new FlatSearchRequest( iQuery );
136         rq.setContexts(
137             Arrays.asList( nexusIndexer.getIndexingContexts().get( "remote-" + getRemoteRepository().getId() ) ) );
138 
139         FlatSearchResponse response = nexusIndexer.searchFlat( rq );
140 
141         log.info( "returned hit count:{}", response.getReturnedHitsCount() );
142         assertThat( response.getReturnedHitsCount() ).isEqualTo( 8 );
143     }
144 
145 
146     protected RemoteRepository getRemoteRepository()
147     {
148         RemoteRepository remoteRepository = new RemoteRepository();
149         File indexDirectory =
150             new File( FileUtil.getBasedir(), "target/index/test-" + Long.toString( System.currentTimeMillis() ) );
151         indexDirectory.mkdirs();
152         indexDirectory.deleteOnExit();
153 
154         remoteRepository.setName( "foo" );
155         remoteRepository.setIndexDirectory( indexDirectory.getAbsolutePath() );
156         remoteRepository.setDownloadRemoteIndex( true );
157         remoteRepository.setId( "test-repo" );
158         remoteRepository.setUrl( "http://localhost:" + port );
159         remoteRepository.setRemoteIndexUrl( "http://localhost:" + port + "/index-updates/" );
160         return remoteRepository;
161     }
162 
163 }