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 org.apache.archiva.indexer.search.RepositorySearch;
24 import org.apache.archiva.indexer.search.SearchResultHit;
25 import org.apache.archiva.indexer.search.SearchResultLimits;
26 import org.apache.archiva.indexer.search.SearchResults;
27 import org.apache.archiva.indexer.util.SearchUtil;
28 import org.apache.archiva.metadata.model.ArtifactMetadata;
29 import org.apache.archiva.metadata.model.ProjectVersionMetadata;
30 import org.apache.archiva.metadata.model.ProjectVersionReference;
31 import org.apache.archiva.metadata.repository.MetadataRepository;
32 import org.apache.archiva.metadata.repository.MetadataResolver;
33 import org.apache.archiva.metadata.repository.storage.maven2.MavenArtifactFacet;
34 import org.apache.archiva.metadata.repository.storage.maven2.MavenProjectFacet;
35 import org.apache.archiva.web.xmlrpc.api.SearchService;
36 import org.apache.archiva.web.xmlrpc.api.beans.Artifact;
37 import org.apache.archiva.web.xmlrpc.api.beans.Dependency;
38 import org.apache.archiva.web.xmlrpc.security.XmlRpcUserRepositories;
39 import org.codehaus.plexus.spring.PlexusInSpringTestCase;
40 import org.easymock.MockControl;
41 import org.easymock.classextension.MockClassControl;
43 import java.util.ArrayList;
44 import java.util.Arrays;
45 import java.util.Collections;
46 import java.util.Date;
47 import java.util.List;
50 * SearchServiceImplTest
52 * @version $Id: SearchServiceImplTest.java
54 public class SearchServiceImplTest
55 extends PlexusInSpringTestCase
57 private SearchService searchService;
59 private MockControl userReposControl;
61 private XmlRpcUserRepositories userRepos;
63 private MockControl searchControl;
65 private RepositorySearch search;
67 private static final String ARCHIVA_TEST_ARTIFACT_ID = "archiva-xmlrpc-test";
69 private static final String ARCHIVA_TEST_GROUP_ID = "org.apache.archiva";
71 private MockControl metadataResolverControl;
73 private MetadataResolver metadataResolver;
75 private MockControl metadataRepositoryControl;
77 private MetadataRepository metadataRepository;
79 private static final String CHECKSUM = "a1b2c3aksjhdasfkdasasd";
81 private static final String TEST_REPO = "test-repo";
87 userReposControl = MockClassControl.createControl( XmlRpcUserRepositories.class );
88 userRepos = (XmlRpcUserRepositories) userReposControl.getMock();
90 searchControl = MockControl.createControl( RepositorySearch.class );
91 searchControl.setDefaultMatcher( MockControl.ALWAYS_MATCHER );
92 search = (RepositorySearch) searchControl.getMock();
94 metadataResolverControl = MockControl.createControl( MetadataResolver.class );
95 metadataResolver = (MetadataResolver) metadataResolverControl.getMock();
97 metadataRepositoryControl = MockControl.createControl( MetadataRepository.class );
98 metadataRepository = (MetadataRepository) metadataRepositoryControl.getMock();
100 searchService = new SearchServiceImpl( userRepos, metadataResolver, metadataRepository, search );
105 public void testQuickSearchModelPackagingIsUsed()
108 List<String> observableRepoIds = new ArrayList<String>();
109 observableRepoIds.add( "repo1.mirror" );
110 observableRepoIds.add( "public.releases" );
112 userReposControl.expectAndReturn( userRepos.getObservableRepositories(), observableRepoIds );
114 SearchResults results = new SearchResults();
115 List<String> versions = new ArrayList<String>();
116 versions.add( "1.0" );
118 SearchResultHit resultHit = new SearchResultHit();
119 resultHit.setGroupId( ARCHIVA_TEST_GROUP_ID );
120 resultHit.setArtifactId( "archiva-webapp" );
121 resultHit.setVersions( versions );
122 resultHit.setRepositoryId( null );
124 results.addHit( SearchUtil.getHitId( ARCHIVA_TEST_GROUP_ID, "archiva-webapp" ), resultHit );
126 SearchResultLimits limits = new SearchResultLimits( SearchResultLimits.ALL_PAGES );
128 searchControl.expectAndDefaultReturn( search.search( "", observableRepoIds, "archiva", limits, null ),
131 ProjectVersionMetadata model = new ProjectVersionMetadata();
132 model.setId( "1.0" );
133 MavenProjectFacet facet = new MavenProjectFacet();
134 facet.setPackaging( "war" );
135 model.addFacet( facet );
137 metadataResolverControl.expectAndReturn( metadataResolver.resolveProjectVersion( "repo1.mirror",
138 ARCHIVA_TEST_GROUP_ID,
139 "archiva-webapp", "1.0" ),
142 userReposControl.replay();
143 searchControl.replay();
144 metadataResolverControl.replay();
145 metadataRepositoryControl.replay();
147 List<Artifact> artifacts = searchService.quickSearch( "archiva" );
149 userReposControl.verify();
150 searchControl.verify();
151 metadataResolverControl.verify();
152 metadataRepositoryControl.verify();
154 assertNotNull( artifacts );
155 assertEquals( 1, artifacts.size() );
157 Artifact artifact = artifacts.get( 0 );
158 assertEquals( ARCHIVA_TEST_GROUP_ID, artifact.getGroupId() );
159 assertEquals( "archiva-webapp", artifact.getArtifactId() );
160 assertEquals( "1.0", artifact.getVersion() );
161 assertEquals( "war", artifact.getType() );
162 assertNotNull( "Repository should not be null!", artifact.getRepositoryId() );
163 assertEquals( "repo1.mirror", artifact.getRepositoryId() );
166 public void testQuickSearchDefaultPackagingIsUsed()
169 List<String> observableRepoIds = new ArrayList<String>();
170 observableRepoIds.add( "repo1.mirror" );
171 observableRepoIds.add( "public.releases" );
173 userReposControl.expectAndReturn( userRepos.getObservableRepositories(), observableRepoIds );
175 SearchResults results = new SearchResults();
176 List<String> versions = new ArrayList<String>();
177 versions.add( "1.0" );
179 SearchResultHit resultHit = new SearchResultHit();
180 resultHit.setRepositoryId( null );
181 resultHit.setGroupId( ARCHIVA_TEST_GROUP_ID );
182 resultHit.setArtifactId( ARCHIVA_TEST_ARTIFACT_ID );
183 resultHit.setVersions( versions );
185 results.addHit( SearchUtil.getHitId( ARCHIVA_TEST_GROUP_ID, ARCHIVA_TEST_ARTIFACT_ID ), resultHit );
187 SearchResultLimits limits = new SearchResultLimits( SearchResultLimits.ALL_PAGES );
189 searchControl.expectAndDefaultReturn( search.search( "", observableRepoIds, "archiva", limits, null ),
192 metadataResolverControl.expectAndReturn( metadataResolver.resolveProjectVersion( "repo1.mirror",
193 ARCHIVA_TEST_GROUP_ID,
194 ARCHIVA_TEST_ARTIFACT_ID,
197 ProjectVersionMetadata model = new ProjectVersionMetadata();
198 model.setId( "1.0" );
199 metadataResolverControl.expectAndReturn( metadataResolver.resolveProjectVersion( "public.releases",
200 ARCHIVA_TEST_GROUP_ID,
201 ARCHIVA_TEST_ARTIFACT_ID,
204 userReposControl.replay();
205 searchControl.replay();
206 metadataResolverControl.replay();
207 metadataRepositoryControl.replay();
209 List<Artifact> artifacts = searchService.quickSearch( "archiva" );
211 userReposControl.verify();
212 searchControl.verify();
213 metadataResolverControl.verify();
214 metadataRepositoryControl.verify();
216 assertNotNull( artifacts );
217 assertEquals( 1, artifacts.size() );
219 Artifact artifact = artifacts.get( 0 );
220 assertEquals( ARCHIVA_TEST_GROUP_ID, artifact.getGroupId() );
221 assertEquals( ARCHIVA_TEST_ARTIFACT_ID, artifact.getArtifactId() );
222 assertEquals( "1.0", artifact.getVersion() );
223 assertEquals( "jar", artifact.getType() );
224 assertEquals( "public.releases", artifact.getRepositoryId() );
227 public void testQuickSearchArtifactRegularSearch()
230 List<String> observableRepoIds = new ArrayList<String>();
231 observableRepoIds.add( "repo1.mirror" );
232 observableRepoIds.add( "public.releases" );
234 userReposControl.expectAndReturn( userRepos.getObservableRepositories(), observableRepoIds );
236 SearchResults results = new SearchResults();
237 List<String> versions = new ArrayList<String>();
238 versions.add( "1.0" );
240 SearchResultHit resultHit = new SearchResultHit();
241 resultHit.setGroupId( ARCHIVA_TEST_GROUP_ID );
242 resultHit.setArtifactId( ARCHIVA_TEST_ARTIFACT_ID );
243 resultHit.setVersions( versions );
244 resultHit.setRepositoryId( null );
246 results.addHit( SearchUtil.getHitId( resultHit.getGroupId(), resultHit.getArtifactId() ), resultHit );
248 SearchResultLimits limits = new SearchResultLimits( SearchResultLimits.ALL_PAGES );
250 searchControl.expectAndDefaultReturn( search.search( "", observableRepoIds, "archiva", limits, null ),
253 ProjectVersionMetadata model = new ProjectVersionMetadata();
254 model.setId( "1.0" );
255 MavenProjectFacet facet = new MavenProjectFacet();
256 facet.setPackaging( "jar" );
257 model.addFacet( facet );
259 metadataResolverControl.expectAndReturn( metadataResolver.resolveProjectVersion( "repo1.mirror",
260 ARCHIVA_TEST_GROUP_ID,
261 ARCHIVA_TEST_ARTIFACT_ID,
264 userReposControl.replay();
265 searchControl.replay();
266 metadataRepositoryControl.replay();
267 metadataResolverControl.replay();
269 List<Artifact> artifacts = searchService.quickSearch( "archiva" );
271 userReposControl.verify();
272 searchControl.verify();
273 metadataRepositoryControl.verify();
274 metadataResolverControl.verify();
276 assertNotNull( artifacts );
277 assertEquals( 1, artifacts.size() );
279 Artifact artifact = artifacts.get( 0 );
280 assertEquals( ARCHIVA_TEST_GROUP_ID, artifact.getGroupId() );
281 assertEquals( ARCHIVA_TEST_ARTIFACT_ID, artifact.getArtifactId() );
282 assertEquals( "1.0", artifact.getVersion() );
283 assertEquals( "jar", artifact.getType() );
284 assertNotNull( "Repository should not be null!", artifact.getRepositoryId() );
285 assertEquals( "repo1.mirror", artifact.getRepositoryId() );
288 public void testQuickSearchNoResults()
291 List<String> observableRepoIds = new ArrayList<String>();
292 observableRepoIds.add( "repo1.mirror" );
293 observableRepoIds.add( "public.releases" );
295 userReposControl.expectAndReturn( userRepos.getObservableRepositories(), observableRepoIds );
297 SearchResults results = new SearchResults();
298 SearchResultLimits limits = new SearchResultLimits( SearchResultLimits.ALL_PAGES );
300 searchControl.expectAndDefaultReturn( search.search( "", observableRepoIds, "non-existent", limits, null ),
302 userReposControl.replay();
303 searchControl.replay();
305 List<Artifact> artifacts = searchService.quickSearch( "test" );
307 userReposControl.verify();
308 searchControl.verify();
310 assertNotNull( artifacts );
311 assertEquals( 0, artifacts.size() );
314 public void testGetArtifactByChecksum()
317 userReposControl.expectAndReturn( userRepos.getObservableRepositories(), Collections.singletonList(
320 Date whenGathered = new Date();
322 ArtifactMetadata artifact = createArtifact( whenGathered );
323 metadataRepositoryControl.expectAndReturn( metadataRepository.getArtifactsByChecksum( TEST_REPO, CHECKSUM ),
324 Collections.singletonList( artifact ) );
326 metadataRepositoryControl.replay();
327 userReposControl.replay();
329 List<Artifact> results = searchService.getArtifactByChecksum( CHECKSUM );
331 metadataRepositoryControl.verify();
332 userReposControl.verify();
334 assertNotNull( results );
335 assertEquals( 1, results.size() );
336 Artifact result = results.get( 0 );
337 assertEquals( ARCHIVA_TEST_GROUP_ID, result.getGroupId() );
338 assertEquals( ARCHIVA_TEST_ARTIFACT_ID, result.getArtifactId() );
339 assertEquals( "1.0", result.getVersion() );
340 assertEquals( "jar", result.getType() );
341 assertEquals( TEST_REPO, result.getRepositoryId() );
344 public void testGetArtifactVersionsArtifactExists()
347 List<String> observableRepoIds = new ArrayList<String>();
348 observableRepoIds.add( "repo1.mirror" );
349 observableRepoIds.add( "public.releases" );
351 userReposControl.expectAndReturn( userRepos.getObservableRepositories(), observableRepoIds );
352 metadataResolverControl.expectAndReturn( metadataResolver.resolveProjectVersions( "repo1.mirror",
353 ARCHIVA_TEST_GROUP_ID,
354 ARCHIVA_TEST_ARTIFACT_ID ),
355 Arrays.asList( "1.0", "1.1-beta-2", "1.2" ) );
356 metadataResolverControl.expectAndReturn( metadataResolver.resolveProjectVersions( "public.releases",
357 ARCHIVA_TEST_GROUP_ID,
358 ARCHIVA_TEST_ARTIFACT_ID ),
359 Arrays.asList( "1.1-beta-1", "1.1", "1.2.1-SNAPSHOT" ) );
361 userReposControl.replay();
362 metadataResolverControl.replay();
364 List<Artifact> artifacts = searchService.getArtifactVersions( ARCHIVA_TEST_GROUP_ID, ARCHIVA_TEST_ARTIFACT_ID );
366 userReposControl.verify();
367 metadataResolverControl.verify();
369 assertNotNull( artifacts );
370 assertEquals( 6, artifacts.size() );
371 assertEquals( new Artifact( "repo1.mirror", ARCHIVA_TEST_GROUP_ID, ARCHIVA_TEST_ARTIFACT_ID, "1.0", "pom" ),
372 artifacts.get( 0 ) );
373 assertEquals( new Artifact( "public.releases", ARCHIVA_TEST_GROUP_ID, ARCHIVA_TEST_ARTIFACT_ID, "1.1-beta-1",
374 "pom" ), artifacts.get( 3 ) );
375 assertEquals( new Artifact( "repo1.mirror", ARCHIVA_TEST_GROUP_ID, ARCHIVA_TEST_ARTIFACT_ID, "1.1-beta-2",
376 "pom" ), artifacts.get( 1 ) );
377 assertEquals( new Artifact( "public.releases", ARCHIVA_TEST_GROUP_ID, ARCHIVA_TEST_ARTIFACT_ID, "1.1", "pom" ),
378 artifacts.get( 4 ) );
379 assertEquals( new Artifact( "repo1.mirror", ARCHIVA_TEST_GROUP_ID, ARCHIVA_TEST_ARTIFACT_ID, "1.2", "pom" ),
380 artifacts.get( 2 ) );
381 assertEquals( new Artifact( "public.releases", ARCHIVA_TEST_GROUP_ID, ARCHIVA_TEST_ARTIFACT_ID,
382 "1.2.1-SNAPSHOT", "pom" ), artifacts.get( 5 ) );
385 public void testGetArtifactVersionsByDateArtifactExists()
391 public void testGetArtifactVersionsByDateArtifactDoesNotExist()
397 public void testGetDependenciesArtifactExists()
400 String repoId = "repo1.mirror";
402 ProjectVersionMetadata model = new ProjectVersionMetadata();
403 model.setId( "1.0" );
405 org.apache.archiva.metadata.model.Dependency dependency = new org.apache.archiva.metadata.model.Dependency();
406 dependency.setGroupId( "org.apache.commons" );
407 dependency.setArtifactId( "commons-logging" );
408 dependency.setVersion( "2.0" );
410 model.addDependency( dependency );
412 dependency = new org.apache.archiva.metadata.model.Dependency();
413 dependency.setGroupId( "junit" );
414 dependency.setArtifactId( "junit" );
415 dependency.setVersion( "2.4" );
416 dependency.setScope( "test" );
418 model.addDependency( dependency );
420 userReposControl.expectAndReturn( userRepos.getObservableRepositories(), Collections.singletonList( repoId ) );
421 metadataResolverControl.expectAndReturn( metadataResolver.resolveProjectVersion( repoId, ARCHIVA_TEST_GROUP_ID,
422 ARCHIVA_TEST_ARTIFACT_ID,
425 metadataResolverControl.replay();
426 userReposControl.replay();
428 List<Dependency> dependencies = searchService.getDependencies( ARCHIVA_TEST_GROUP_ID, ARCHIVA_TEST_ARTIFACT_ID,
431 metadataResolverControl.verify();
432 userReposControl.verify();
434 assertNotNull( dependencies );
435 assertEquals( 2, dependencies.size() );
436 assertEquals( new Dependency( "org.apache.commons", "commons-logging", "2.0", null, null, null ),
437 dependencies.get( 0 ) );
438 assertEquals( new Dependency( "junit", "junit", "2.4", null, null, "test" ), dependencies.get( 1 ) );
441 public void testGetDependenciesArtifactDoesNotExist()
444 String repoId = "repo1.mirror";
446 userReposControl.expectAndReturn( userRepos.getObservableRepositories(), Collections.singletonList( repoId ) );
447 metadataResolverControl.expectAndReturn( metadataResolver.resolveProjectVersion( repoId, ARCHIVA_TEST_GROUP_ID,
448 ARCHIVA_TEST_ARTIFACT_ID,
451 userReposControl.replay();
452 metadataResolverControl.replay();
456 searchService.getDependencies( ARCHIVA_TEST_GROUP_ID, ARCHIVA_TEST_ARTIFACT_ID, "1.0" );
457 fail( "An exception should have been thrown." );
459 catch ( Exception e )
461 assertEquals( "Artifact does not exist.", e.getMessage() );
464 userReposControl.verify();
465 metadataResolverControl.verify();
468 public void testGetDependencyTreeArtifactExists()
474 public void testGetDependencyTreeArtifactDoesNotExist()
480 public void testGetDependees()
483 List<String> observableRepoIds = new ArrayList<String>();
484 String repoId = "repo1.mirror";
485 observableRepoIds.add( repoId );
487 List<ProjectVersionReference> dependeeModels = new ArrayList<ProjectVersionReference>();
488 ProjectVersionReference dependeeModel = new ProjectVersionReference();
489 dependeeModel.setNamespace( ARCHIVA_TEST_GROUP_ID );
490 dependeeModel.setProjectId( "archiva-dependee-one" );
491 dependeeModel.setProjectVersion( "1.0" );
492 dependeeModels.add( dependeeModel );
494 dependeeModel = new ProjectVersionReference();
495 dependeeModel.setNamespace( ARCHIVA_TEST_GROUP_ID );
496 dependeeModel.setProjectId( "archiva-dependee-two" );
497 dependeeModel.setProjectVersion( "1.0" );
498 dependeeModels.add( dependeeModel );
500 userReposControl.expectAndReturn( userRepos.getObservableRepositories(), observableRepoIds );
501 metadataResolverControl.expectAndReturn( metadataResolver.resolveProjectReferences( repoId,
502 ARCHIVA_TEST_GROUP_ID,
503 ARCHIVA_TEST_ARTIFACT_ID,
504 "1.0" ), dependeeModels );
506 metadataResolverControl.replay();
507 userReposControl.replay();
509 List<Artifact> dependees = searchService.getDependees( ARCHIVA_TEST_GROUP_ID, ARCHIVA_TEST_ARTIFACT_ID, "1.0" );
511 metadataResolverControl.verify();
512 userReposControl.verify();
514 assertNotNull( dependees );
515 assertEquals( 2, dependees.size() );
516 assertEquals( new Artifact( repoId, ARCHIVA_TEST_GROUP_ID, "archiva-dependee-one", "1.0", "" ), dependees.get(
518 assertEquals( new Artifact( repoId, ARCHIVA_TEST_GROUP_ID, "archiva-dependee-two", "1.0", "" ), dependees.get(
522 public void testGetDependeesArtifactDoesNotExist()
525 List<String> observableRepoIds = new ArrayList<String>();
526 observableRepoIds.add( "repo1.mirror" );
527 observableRepoIds.add( "public.releases" );
529 // no longer differentiating between a project not being present and a project that is present but with
530 // no references. If it is later determined to be needed, we will need to modify the metadata content repository
531 userReposControl.expectAndReturn( userRepos.getObservableRepositories(), observableRepoIds );
532 metadataResolverControl.expectAndReturn( metadataResolver.resolveProjectReferences( "repo1.mirror",
533 ARCHIVA_TEST_GROUP_ID,
534 ARCHIVA_TEST_ARTIFACT_ID,
536 Collections.<ProjectVersionReference>emptyList() );
537 metadataResolverControl.expectAndReturn( metadataResolver.resolveProjectReferences( "public.releases",
538 ARCHIVA_TEST_GROUP_ID,
539 ARCHIVA_TEST_ARTIFACT_ID,
541 Collections.<ProjectVersionReference>emptyList() );
543 userReposControl.replay();
544 metadataResolverControl.replay();
546 assertTrue( searchService.getDependees( ARCHIVA_TEST_GROUP_ID, ARCHIVA_TEST_ARTIFACT_ID, "1.0" ).isEmpty() );
549 private ArtifactMetadata createArtifact( Date whenGathered )
551 String version = "1.0";
552 ArtifactMetadata artifactMetadata = new ArtifactMetadata();
553 artifactMetadata.setVersion( version );
554 artifactMetadata.setProjectVersion( version );
555 artifactMetadata.setId( ARCHIVA_TEST_ARTIFACT_ID + "-" + version + ".jar" );
556 artifactMetadata.setProject( ARCHIVA_TEST_ARTIFACT_ID );
557 artifactMetadata.setNamespace( ARCHIVA_TEST_GROUP_ID );
558 artifactMetadata.setMd5( CHECKSUM );
559 artifactMetadata.setWhenGathered( whenGathered );
560 artifactMetadata.setRepositoryId( TEST_REPO );
562 MavenArtifactFacet facet = new MavenArtifactFacet();
563 facet.setType( "jar" );
564 artifactMetadata.addFacet( facet );
566 return artifactMetadata;