1 package org.apache.archiva.web.xmlrpc.services;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
23 import java.util.ArrayList;
24 import java.util.Arrays;
25 import java.util.Collections;
26 import java.util.Date;
27 import java.util.List;
29 import org.apache.archiva.indexer.search.RepositorySearch;
30 import org.apache.archiva.indexer.search.SearchResultHit;
31 import org.apache.archiva.indexer.search.SearchResultLimits;
32 import org.apache.archiva.indexer.search.SearchResults;
33 import org.apache.archiva.indexer.util.SearchUtil;
34 import org.apache.archiva.metadata.model.ArtifactMetadata;
35 import org.apache.archiva.metadata.model.ProjectVersionMetadata;
36 import org.apache.archiva.metadata.model.ProjectVersionReference;
37 import org.apache.archiva.metadata.repository.MetadataRepository;
38 import org.apache.archiva.metadata.repository.MetadataResolver;
39 import org.apache.archiva.metadata.repository.storage.maven2.MavenProjectFacet;
40 import org.apache.archiva.web.xmlrpc.api.SearchService;
41 import org.apache.archiva.web.xmlrpc.api.beans.Artifact;
42 import org.apache.archiva.web.xmlrpc.api.beans.Dependency;
43 import org.apache.archiva.web.xmlrpc.security.XmlRpcUserRepositories;
44 import org.codehaus.plexus.spring.PlexusInSpringTestCase;
45 import org.easymock.MockControl;
46 import org.easymock.classextension.MockClassControl;
49 * SearchServiceImplTest
51 * @version $Id: SearchServiceImplTest.java
53 public class SearchServiceImplTest
54 extends PlexusInSpringTestCase
56 private SearchService searchService;
58 private MockControl userReposControl;
60 private XmlRpcUserRepositories userRepos;
62 private MockControl searchControl;
64 private RepositorySearch search;
66 private static final String ARCHIVA_TEST_ARTIFACT_ID = "archiva-xmlrpc-test";
68 private static final String ARCHIVA_TEST_GROUP_ID = "org.apache.archiva";
70 private MockControl metadataResolverControl;
72 private MetadataResolver metadataResolver;
74 private MockControl metadataRepositoryControl;
76 private MetadataRepository metadataRepository;
78 private static final String CHECKSUM = "a1b2c3aksjhdasfkdasasd";
80 private static final String TEST_REPO = "test-repo";
86 userReposControl = MockClassControl.createControl( XmlRpcUserRepositories.class );
87 userRepos = (XmlRpcUserRepositories) userReposControl.getMock();
89 searchControl = MockControl.createControl( RepositorySearch.class );
90 searchControl.setDefaultMatcher( MockControl.ALWAYS_MATCHER );
91 search = (RepositorySearch) searchControl.getMock();
93 metadataResolverControl = MockControl.createControl( MetadataResolver.class );
94 metadataResolver = (MetadataResolver) metadataResolverControl.getMock();
96 metadataRepositoryControl = MockControl.createControl( MetadataRepository.class );
97 metadataRepository = (MetadataRepository) metadataRepositoryControl.getMock();
99 searchService = new SearchServiceImpl( userRepos, metadataResolver, metadataRepository, search );
104 public void testQuickSearchModelPackagingIsUsed()
107 List<String> observableRepoIds = new ArrayList<String>();
108 observableRepoIds.add( "repo1.mirror" );
109 observableRepoIds.add( "public.releases" );
111 userReposControl.expectAndReturn( userRepos.getObservableRepositories(), observableRepoIds );
113 SearchResults results = new SearchResults();
114 List<String> versions = new ArrayList<String>();
115 versions.add( "1.0" );
117 SearchResultHit resultHit = new SearchResultHit();
118 resultHit.setGroupId( ARCHIVA_TEST_GROUP_ID );
119 resultHit.setArtifactId( "archiva-webapp" );
120 resultHit.setVersions( versions );
121 resultHit.setRepositoryId( null );
123 results.addHit( SearchUtil.getHitId( ARCHIVA_TEST_GROUP_ID, "archiva-webapp" ), resultHit );
125 SearchResultLimits limits = new SearchResultLimits( SearchResultLimits.ALL_PAGES );
127 searchControl.expectAndDefaultReturn( search.search( "", observableRepoIds, "archiva", limits, null ),
130 ProjectVersionMetadata model = new ProjectVersionMetadata();
131 model.setId( "1.0" );
132 MavenProjectFacet facet = new MavenProjectFacet();
133 facet.setPackaging( "war" );
134 model.addFacet( facet );
136 metadataResolverControl.expectAndReturn(
137 metadataResolver.getProjectVersion( "repo1.mirror", ARCHIVA_TEST_GROUP_ID, "archiva-webapp", "1.0" ),
140 userReposControl.replay();
141 searchControl.replay();
142 metadataResolverControl.replay();
143 metadataRepositoryControl.replay();
145 List<Artifact> artifacts = searchService.quickSearch( "archiva" );
147 userReposControl.verify();
148 searchControl.verify();
149 metadataResolverControl.verify();
150 metadataRepositoryControl.verify();
152 assertNotNull( artifacts );
153 assertEquals( 1, artifacts.size() );
155 Artifact artifact = artifacts.get( 0 );
156 assertEquals( ARCHIVA_TEST_GROUP_ID, artifact.getGroupId() );
157 assertEquals( "archiva-webapp", artifact.getArtifactId() );
158 assertEquals( "1.0", artifact.getVersion() );
159 assertEquals( "war", artifact.getType() );
160 assertNotNull( "Repository should not be null!", artifact.getRepositoryId() );
161 assertEquals( "repo1.mirror", artifact.getRepositoryId() );
164 public void testQuickSearchDefaultPackagingIsUsed()
167 List<String> observableRepoIds = new ArrayList<String>();
168 observableRepoIds.add( "repo1.mirror" );
169 observableRepoIds.add( "public.releases" );
171 userReposControl.expectAndReturn( userRepos.getObservableRepositories(), observableRepoIds );
173 SearchResults results = new SearchResults();
174 List<String> versions = new ArrayList<String>();
175 versions.add( "1.0" );
177 SearchResultHit resultHit = new SearchResultHit();
178 resultHit.setRepositoryId( null );
179 resultHit.setGroupId( ARCHIVA_TEST_GROUP_ID );
180 resultHit.setArtifactId( ARCHIVA_TEST_ARTIFACT_ID );
181 resultHit.setVersions( versions );
183 results.addHit( SearchUtil.getHitId( ARCHIVA_TEST_GROUP_ID, ARCHIVA_TEST_ARTIFACT_ID ), resultHit );
185 SearchResultLimits limits = new SearchResultLimits( SearchResultLimits.ALL_PAGES );
187 searchControl.expectAndDefaultReturn( search.search( "", observableRepoIds, "archiva", limits, null ),
190 metadataResolverControl.expectAndReturn(
191 metadataResolver.getProjectVersion( "repo1.mirror", ARCHIVA_TEST_GROUP_ID, ARCHIVA_TEST_ARTIFACT_ID,
194 ProjectVersionMetadata model = new ProjectVersionMetadata();
195 model.setId( "1.0" );
196 metadataResolverControl.expectAndReturn(
197 metadataResolver.getProjectVersion( "public.releases", ARCHIVA_TEST_GROUP_ID, ARCHIVA_TEST_ARTIFACT_ID,
200 userReposControl.replay();
201 searchControl.replay();
202 metadataResolverControl.replay();
203 metadataRepositoryControl.replay();
205 List<Artifact> artifacts = searchService.quickSearch( "archiva" );
207 userReposControl.verify();
208 searchControl.verify();
209 metadataResolverControl.verify();
210 metadataRepositoryControl.verify();
212 assertNotNull( artifacts );
213 assertEquals( 1, artifacts.size() );
215 Artifact artifact = artifacts.get( 0 );
216 assertEquals( ARCHIVA_TEST_GROUP_ID, artifact.getGroupId() );
217 assertEquals( ARCHIVA_TEST_ARTIFACT_ID, artifact.getArtifactId() );
218 assertEquals( "1.0", artifact.getVersion() );
219 assertEquals( "jar", artifact.getType() );
220 assertEquals( "public.releases", artifact.getRepositoryId() );
223 public void testQuickSearchArtifactRegularSearch()
226 List<String> observableRepoIds = new ArrayList<String>();
227 observableRepoIds.add( "repo1.mirror" );
228 observableRepoIds.add( "public.releases" );
230 userReposControl.expectAndReturn( userRepos.getObservableRepositories(), observableRepoIds );
232 SearchResults results = new SearchResults();
233 List<String> versions = new ArrayList<String>();
234 versions.add( "1.0" );
236 SearchResultHit resultHit = new SearchResultHit();
237 resultHit.setGroupId( ARCHIVA_TEST_GROUP_ID );
238 resultHit.setArtifactId( ARCHIVA_TEST_ARTIFACT_ID );
239 resultHit.setVersions( versions );
240 resultHit.setRepositoryId( null );
242 results.addHit( SearchUtil.getHitId( resultHit.getGroupId(), resultHit.getArtifactId() ), resultHit );
244 SearchResultLimits limits = new SearchResultLimits( SearchResultLimits.ALL_PAGES );
246 searchControl.expectAndDefaultReturn( search.search( "", observableRepoIds, "archiva", limits, null ),
249 ProjectVersionMetadata model = new ProjectVersionMetadata();
250 model.setId( "1.0" );
251 MavenProjectFacet facet = new MavenProjectFacet();
252 facet.setPackaging( "jar" );
253 model.addFacet( facet );
255 metadataResolverControl.expectAndReturn(
256 metadataResolver.getProjectVersion( "repo1.mirror", ARCHIVA_TEST_GROUP_ID, ARCHIVA_TEST_ARTIFACT_ID,
259 userReposControl.replay();
260 searchControl.replay();
261 metadataRepositoryControl.replay();
262 metadataResolverControl.replay();
264 List<Artifact> artifacts = searchService.quickSearch( "archiva" );
266 userReposControl.verify();
267 searchControl.verify();
268 metadataRepositoryControl.verify();
269 metadataResolverControl.verify();
271 assertNotNull( artifacts );
272 assertEquals( 1, artifacts.size() );
274 Artifact artifact = artifacts.get( 0 );
275 assertEquals( ARCHIVA_TEST_GROUP_ID, artifact.getGroupId() );
276 assertEquals( ARCHIVA_TEST_ARTIFACT_ID, artifact.getArtifactId() );
277 assertEquals( "1.0", artifact.getVersion() );
278 assertEquals( "jar", artifact.getType() );
279 assertNotNull( "Repository should not be null!", artifact.getRepositoryId() );
280 assertEquals( "repo1.mirror", artifact.getRepositoryId() );
283 public void testQuickSearchNoResults()
286 List<String> observableRepoIds = new ArrayList<String>();
287 observableRepoIds.add( "repo1.mirror" );
288 observableRepoIds.add( "public.releases" );
290 userReposControl.expectAndReturn( userRepos.getObservableRepositories(), observableRepoIds );
292 SearchResults results = new SearchResults();
293 SearchResultLimits limits = new SearchResultLimits( SearchResultLimits.ALL_PAGES );
295 searchControl.expectAndDefaultReturn( search.search( "", observableRepoIds, "non-existent", limits, null ),
297 userReposControl.replay();
298 searchControl.replay();
300 List<Artifact> artifacts = searchService.quickSearch( "test" );
302 userReposControl.verify();
303 searchControl.verify();
305 assertNotNull( artifacts );
306 assertEquals( 0, artifacts.size() );
309 public void testGetArtifactByChecksum()
312 userReposControl.expectAndReturn( userRepos.getObservableRepositories(),
313 Collections.singletonList( TEST_REPO ) );
315 Date whenGathered = new Date();
317 ArtifactMetadata artifact = createArtifact( whenGathered );
318 metadataRepositoryControl.expectAndReturn( metadataRepository.getArtifactsByChecksum( TEST_REPO, CHECKSUM ),
319 Collections.singletonList( artifact ) );
321 metadataRepositoryControl.replay();
322 userReposControl.replay();
324 List<Artifact> results = searchService.getArtifactByChecksum( CHECKSUM );
326 metadataRepositoryControl.verify();
327 userReposControl.verify();
329 assertNotNull( results );
330 assertEquals( 1, results.size() );
331 Artifact result = results.get( 0 );
332 assertEquals( ARCHIVA_TEST_GROUP_ID, result.getGroupId() );
333 assertEquals( ARCHIVA_TEST_ARTIFACT_ID, result.getArtifactId() );
334 assertEquals( "1.0", result.getVersion() );
335 assertEquals( "jar", result.getType() );
336 assertEquals( TEST_REPO, result.getRepositoryId() );
339 public void testGetArtifactVersionsArtifactExists()
342 List<String> observableRepoIds = new ArrayList<String>();
343 observableRepoIds.add( "repo1.mirror" );
344 observableRepoIds.add( "public.releases" );
346 userReposControl.expectAndReturn( userRepos.getObservableRepositories(), observableRepoIds );
347 metadataResolverControl.expectAndReturn(
348 metadataResolver.getProjectVersions( "repo1.mirror", ARCHIVA_TEST_GROUP_ID, ARCHIVA_TEST_ARTIFACT_ID ),
349 Arrays.asList( "1.0", "1.1-beta-2", "1.2" ) );
350 metadataResolverControl.expectAndReturn(
351 metadataResolver.getProjectVersions( "public.releases", ARCHIVA_TEST_GROUP_ID, ARCHIVA_TEST_ARTIFACT_ID ),
352 Arrays.asList( "1.1-beta-1", "1.1", "1.2.1-SNAPSHOT" ) );
354 userReposControl.replay();
355 metadataResolverControl.replay();
357 List<Artifact> artifacts = searchService.getArtifactVersions( ARCHIVA_TEST_GROUP_ID, ARCHIVA_TEST_ARTIFACT_ID );
359 userReposControl.verify();
360 metadataResolverControl.verify();
362 assertNotNull( artifacts );
363 assertEquals( 6, artifacts.size() );
364 assertEquals( new Artifact( "repo1.mirror", ARCHIVA_TEST_GROUP_ID, ARCHIVA_TEST_ARTIFACT_ID, "1.0", "pom" ),
365 artifacts.get( 0 ) );
367 new Artifact( "public.releases", ARCHIVA_TEST_GROUP_ID, ARCHIVA_TEST_ARTIFACT_ID, "1.1-beta-1", "pom" ),
368 artifacts.get( 3 ) );
370 new Artifact( "repo1.mirror", ARCHIVA_TEST_GROUP_ID, ARCHIVA_TEST_ARTIFACT_ID, "1.1-beta-2", "pom" ),
371 artifacts.get( 1 ) );
372 assertEquals( new Artifact( "public.releases", ARCHIVA_TEST_GROUP_ID, ARCHIVA_TEST_ARTIFACT_ID, "1.1", "pom" ),
373 artifacts.get( 4 ) );
374 assertEquals( new Artifact( "repo1.mirror", ARCHIVA_TEST_GROUP_ID, ARCHIVA_TEST_ARTIFACT_ID, "1.2", "pom" ),
375 artifacts.get( 2 ) );
377 new Artifact( "public.releases", ARCHIVA_TEST_GROUP_ID, ARCHIVA_TEST_ARTIFACT_ID, "1.2.1-SNAPSHOT", "pom" ),
378 artifacts.get( 5 ) );
381 public void testGetArtifactVersionsByDateArtifactExists()
387 public void testGetArtifactVersionsByDateArtifactDoesNotExist()
393 public void testGetDependenciesArtifactExists()
396 String repoId = "repo1.mirror";
398 ProjectVersionMetadata model = new ProjectVersionMetadata();
399 model.setId( "1.0" );
401 org.apache.archiva.metadata.model.Dependency dependency = new org.apache.archiva.metadata.model.Dependency();
402 dependency.setGroupId( "org.apache.commons" );
403 dependency.setArtifactId( "commons-logging" );
404 dependency.setVersion( "2.0" );
406 model.addDependency( dependency );
408 dependency = new org.apache.archiva.metadata.model.Dependency();
409 dependency.setGroupId( "junit" );
410 dependency.setArtifactId( "junit" );
411 dependency.setVersion( "2.4" );
412 dependency.setScope( "test" );
414 model.addDependency( dependency );
416 userReposControl.expectAndReturn( userRepos.getObservableRepositories(), Collections.singletonList( repoId ) );
417 metadataResolverControl.expectAndReturn(
418 metadataResolver.getProjectVersion( repoId, ARCHIVA_TEST_GROUP_ID, ARCHIVA_TEST_ARTIFACT_ID, "1.0" ),
421 metadataResolverControl.replay();
422 userReposControl.replay();
424 List<Dependency> dependencies =
425 searchService.getDependencies( ARCHIVA_TEST_GROUP_ID, ARCHIVA_TEST_ARTIFACT_ID, "1.0" );
427 metadataResolverControl.verify();
428 userReposControl.verify();
430 assertNotNull( dependencies );
431 assertEquals( 2, dependencies.size() );
432 assertEquals( new Dependency( "org.apache.commons", "commons-logging", "2.0", null, null, null ),
433 dependencies.get( 0 ) );
434 assertEquals( new Dependency( "junit", "junit", "2.4", null, null, "test" ), dependencies.get( 1 ) );
437 public void testGetDependenciesArtifactDoesNotExist()
440 String repoId = "repo1.mirror";
442 userReposControl.expectAndReturn( userRepos.getObservableRepositories(), Collections.singletonList( repoId ) );
443 metadataResolverControl.expectAndReturn(
444 metadataResolver.getProjectVersion( repoId, ARCHIVA_TEST_GROUP_ID, ARCHIVA_TEST_ARTIFACT_ID, "1.0" ),
447 userReposControl.replay();
448 metadataResolverControl.replay();
452 searchService.getDependencies( ARCHIVA_TEST_GROUP_ID, ARCHIVA_TEST_ARTIFACT_ID, "1.0" );
453 fail( "An exception should have been thrown." );
455 catch ( Exception e )
457 assertEquals( "Artifact does not exist.", e.getMessage() );
460 userReposControl.verify();
461 metadataResolverControl.verify();
464 public void testGetDependencyTreeArtifactExists()
470 public void testGetDependencyTreeArtifactDoesNotExist()
476 public void testGetDependees()
479 List<String> observableRepoIds = new ArrayList<String>();
480 String repoId = "repo1.mirror";
481 observableRepoIds.add( repoId );
483 List<ProjectVersionReference> dependeeModels = new ArrayList<ProjectVersionReference>();
484 ProjectVersionReference dependeeModel = new ProjectVersionReference();
485 dependeeModel.setNamespace( ARCHIVA_TEST_GROUP_ID );
486 dependeeModel.setProjectId( "archiva-dependee-one" );
487 dependeeModel.setProjectVersion( "1.0" );
488 dependeeModels.add( dependeeModel );
490 dependeeModel = new ProjectVersionReference();
491 dependeeModel.setNamespace( ARCHIVA_TEST_GROUP_ID );
492 dependeeModel.setProjectId( "archiva-dependee-two" );
493 dependeeModel.setProjectVersion( "1.0" );
494 dependeeModels.add( dependeeModel );
496 userReposControl.expectAndReturn( userRepos.getObservableRepositories(), observableRepoIds );
497 metadataResolverControl.expectAndReturn(
498 metadataResolver.getProjectReferences( repoId, ARCHIVA_TEST_GROUP_ID, ARCHIVA_TEST_ARTIFACT_ID, "1.0" ),
501 metadataResolverControl.replay();
502 userReposControl.replay();
504 List<Artifact> dependees = searchService.getDependees( ARCHIVA_TEST_GROUP_ID, ARCHIVA_TEST_ARTIFACT_ID, "1.0" );
506 metadataResolverControl.verify();
507 userReposControl.verify();
509 assertNotNull( dependees );
510 assertEquals( 2, dependees.size() );
511 assertEquals( new Artifact( repoId, ARCHIVA_TEST_GROUP_ID, "archiva-dependee-one", "1.0", "" ),
512 dependees.get( 0 ) );
513 assertEquals( new Artifact( repoId, ARCHIVA_TEST_GROUP_ID, "archiva-dependee-two", "1.0", "" ),
514 dependees.get( 1 ) );
517 public void testGetDependeesArtifactDoesNotExist()
520 List<String> observableRepoIds = new ArrayList<String>();
521 observableRepoIds.add( "repo1.mirror" );
522 observableRepoIds.add( "public.releases" );
524 // no longer differentiating between a project not being present and a project that is present but with
525 // no references. If it is later determined to be needed, we will need to modify the metadata content repository
526 userReposControl.expectAndReturn( userRepos.getObservableRepositories(), observableRepoIds );
527 metadataResolverControl.expectAndReturn(
528 metadataResolver.getProjectReferences( "repo1.mirror", ARCHIVA_TEST_GROUP_ID, ARCHIVA_TEST_ARTIFACT_ID,
529 "1.0" ), Collections.<ProjectVersionReference>emptyList() );
530 metadataResolverControl.expectAndReturn(
531 metadataResolver.getProjectReferences( "public.releases", ARCHIVA_TEST_GROUP_ID, ARCHIVA_TEST_ARTIFACT_ID,
532 "1.0" ), Collections.<ProjectVersionReference>emptyList() );
534 userReposControl.replay();
535 metadataResolverControl.replay();
537 assertTrue( searchService.getDependees( ARCHIVA_TEST_GROUP_ID, ARCHIVA_TEST_ARTIFACT_ID, "1.0" ).isEmpty() );
540 private ArtifactMetadata createArtifact( Date whenGathered )
542 String version = "1.0";
543 ArtifactMetadata artifactMetadata = new ArtifactMetadata();
544 artifactMetadata.setVersion( version );
545 artifactMetadata.setId( ARCHIVA_TEST_ARTIFACT_ID + "-" + version + ".jar" );
546 artifactMetadata.setProject( ARCHIVA_TEST_ARTIFACT_ID );
547 artifactMetadata.setNamespace( ARCHIVA_TEST_GROUP_ID );
548 artifactMetadata.setMd5( CHECKSUM );
549 artifactMetadata.setWhenGathered( whenGathered );
550 artifactMetadata.setRepositoryId( TEST_REPO );
551 return artifactMetadata;