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(
138 metadataResolver.getProjectVersion( "repo1.mirror", ARCHIVA_TEST_GROUP_ID, "archiva-webapp", "1.0" ),
141 userReposControl.replay();
142 searchControl.replay();
143 metadataResolverControl.replay();
144 metadataRepositoryControl.replay();
146 List<Artifact> artifacts = searchService.quickSearch( "archiva" );
148 userReposControl.verify();
149 searchControl.verify();
150 metadataResolverControl.verify();
151 metadataRepositoryControl.verify();
153 assertNotNull( artifacts );
154 assertEquals( 1, artifacts.size() );
156 Artifact artifact = artifacts.get( 0 );
157 assertEquals( ARCHIVA_TEST_GROUP_ID, artifact.getGroupId() );
158 assertEquals( "archiva-webapp", artifact.getArtifactId() );
159 assertEquals( "1.0", artifact.getVersion() );
160 assertEquals( "war", artifact.getType() );
161 assertNotNull( "Repository should not be null!", artifact.getRepositoryId() );
162 assertEquals( "repo1.mirror", artifact.getRepositoryId() );
165 public void testQuickSearchDefaultPackagingIsUsed()
168 List<String> observableRepoIds = new ArrayList<String>();
169 observableRepoIds.add( "repo1.mirror" );
170 observableRepoIds.add( "public.releases" );
172 userReposControl.expectAndReturn( userRepos.getObservableRepositories(), observableRepoIds );
174 SearchResults results = new SearchResults();
175 List<String> versions = new ArrayList<String>();
176 versions.add( "1.0" );
178 SearchResultHit resultHit = new SearchResultHit();
179 resultHit.setRepositoryId( null );
180 resultHit.setGroupId( ARCHIVA_TEST_GROUP_ID );
181 resultHit.setArtifactId( ARCHIVA_TEST_ARTIFACT_ID );
182 resultHit.setVersions( versions );
184 results.addHit( SearchUtil.getHitId( ARCHIVA_TEST_GROUP_ID, ARCHIVA_TEST_ARTIFACT_ID ), resultHit );
186 SearchResultLimits limits = new SearchResultLimits( SearchResultLimits.ALL_PAGES );
188 searchControl.expectAndDefaultReturn( search.search( "", observableRepoIds, "archiva", limits, null ),
191 metadataResolverControl.expectAndReturn(
192 metadataResolver.getProjectVersion( "repo1.mirror", ARCHIVA_TEST_GROUP_ID, ARCHIVA_TEST_ARTIFACT_ID,
195 ProjectVersionMetadata model = new ProjectVersionMetadata();
196 model.setId( "1.0" );
197 metadataResolverControl.expectAndReturn(
198 metadataResolver.getProjectVersion( "public.releases", ARCHIVA_TEST_GROUP_ID, ARCHIVA_TEST_ARTIFACT_ID,
201 userReposControl.replay();
202 searchControl.replay();
203 metadataResolverControl.replay();
204 metadataRepositoryControl.replay();
206 List<Artifact> artifacts = searchService.quickSearch( "archiva" );
208 userReposControl.verify();
209 searchControl.verify();
210 metadataResolverControl.verify();
211 metadataRepositoryControl.verify();
213 assertNotNull( artifacts );
214 assertEquals( 1, artifacts.size() );
216 Artifact artifact = artifacts.get( 0 );
217 assertEquals( ARCHIVA_TEST_GROUP_ID, artifact.getGroupId() );
218 assertEquals( ARCHIVA_TEST_ARTIFACT_ID, artifact.getArtifactId() );
219 assertEquals( "1.0", artifact.getVersion() );
220 assertEquals( "jar", artifact.getType() );
221 assertEquals( "public.releases", artifact.getRepositoryId() );
224 public void testQuickSearchArtifactRegularSearch()
227 List<String> observableRepoIds = new ArrayList<String>();
228 observableRepoIds.add( "repo1.mirror" );
229 observableRepoIds.add( "public.releases" );
231 userReposControl.expectAndReturn( userRepos.getObservableRepositories(), observableRepoIds );
233 SearchResults results = new SearchResults();
234 List<String> versions = new ArrayList<String>();
235 versions.add( "1.0" );
237 SearchResultHit resultHit = new SearchResultHit();
238 resultHit.setGroupId( ARCHIVA_TEST_GROUP_ID );
239 resultHit.setArtifactId( ARCHIVA_TEST_ARTIFACT_ID );
240 resultHit.setVersions( versions );
241 resultHit.setRepositoryId( null );
243 results.addHit( SearchUtil.getHitId( resultHit.getGroupId(), resultHit.getArtifactId() ), resultHit );
245 SearchResultLimits limits = new SearchResultLimits( SearchResultLimits.ALL_PAGES );
247 searchControl.expectAndDefaultReturn( search.search( "", observableRepoIds, "archiva", limits, null ),
250 ProjectVersionMetadata model = new ProjectVersionMetadata();
251 model.setId( "1.0" );
252 MavenProjectFacet facet = new MavenProjectFacet();
253 facet.setPackaging( "jar" );
254 model.addFacet( facet );
256 metadataResolverControl.expectAndReturn(
257 metadataResolver.getProjectVersion( "repo1.mirror", ARCHIVA_TEST_GROUP_ID, ARCHIVA_TEST_ARTIFACT_ID,
260 userReposControl.replay();
261 searchControl.replay();
262 metadataRepositoryControl.replay();
263 metadataResolverControl.replay();
265 List<Artifact> artifacts = searchService.quickSearch( "archiva" );
267 userReposControl.verify();
268 searchControl.verify();
269 metadataRepositoryControl.verify();
270 metadataResolverControl.verify();
272 assertNotNull( artifacts );
273 assertEquals( 1, artifacts.size() );
275 Artifact artifact = artifacts.get( 0 );
276 assertEquals( ARCHIVA_TEST_GROUP_ID, artifact.getGroupId() );
277 assertEquals( ARCHIVA_TEST_ARTIFACT_ID, artifact.getArtifactId() );
278 assertEquals( "1.0", artifact.getVersion() );
279 assertEquals( "jar", artifact.getType() );
280 assertNotNull( "Repository should not be null!", artifact.getRepositoryId() );
281 assertEquals( "repo1.mirror", artifact.getRepositoryId() );
284 public void testQuickSearchNoResults()
287 List<String> observableRepoIds = new ArrayList<String>();
288 observableRepoIds.add( "repo1.mirror" );
289 observableRepoIds.add( "public.releases" );
291 userReposControl.expectAndReturn( userRepos.getObservableRepositories(), observableRepoIds );
293 SearchResults results = new SearchResults();
294 SearchResultLimits limits = new SearchResultLimits( SearchResultLimits.ALL_PAGES );
296 searchControl.expectAndDefaultReturn( search.search( "", observableRepoIds, "non-existent", limits, null ),
298 userReposControl.replay();
299 searchControl.replay();
301 List<Artifact> artifacts = searchService.quickSearch( "test" );
303 userReposControl.verify();
304 searchControl.verify();
306 assertNotNull( artifacts );
307 assertEquals( 0, artifacts.size() );
310 public void testGetArtifactByChecksum()
313 userReposControl.expectAndReturn( userRepos.getObservableRepositories(),
314 Collections.singletonList( TEST_REPO ) );
316 Date whenGathered = new Date();
318 ArtifactMetadata artifact = createArtifact( whenGathered );
319 metadataRepositoryControl.expectAndReturn( metadataRepository.getArtifactsByChecksum( TEST_REPO, CHECKSUM ),
320 Collections.singletonList( artifact ) );
322 metadataRepositoryControl.replay();
323 userReposControl.replay();
325 List<Artifact> results = searchService.getArtifactByChecksum( CHECKSUM );
327 metadataRepositoryControl.verify();
328 userReposControl.verify();
330 assertNotNull( results );
331 assertEquals( 1, results.size() );
332 Artifact result = results.get( 0 );
333 assertEquals( ARCHIVA_TEST_GROUP_ID, result.getGroupId() );
334 assertEquals( ARCHIVA_TEST_ARTIFACT_ID, result.getArtifactId() );
335 assertEquals( "1.0", result.getVersion() );
336 assertEquals( "jar", result.getType() );
337 assertEquals( TEST_REPO, result.getRepositoryId() );
340 public void testGetArtifactVersionsArtifactExists()
343 List<String> observableRepoIds = new ArrayList<String>();
344 observableRepoIds.add( "repo1.mirror" );
345 observableRepoIds.add( "public.releases" );
347 userReposControl.expectAndReturn( userRepos.getObservableRepositories(), observableRepoIds );
348 metadataResolverControl.expectAndReturn(
349 metadataResolver.getProjectVersions( "repo1.mirror", ARCHIVA_TEST_GROUP_ID, ARCHIVA_TEST_ARTIFACT_ID ),
350 Arrays.asList( "1.0", "1.1-beta-2", "1.2" ) );
351 metadataResolverControl.expectAndReturn(
352 metadataResolver.getProjectVersions( "public.releases", ARCHIVA_TEST_GROUP_ID, ARCHIVA_TEST_ARTIFACT_ID ),
353 Arrays.asList( "1.1-beta-1", "1.1", "1.2.1-SNAPSHOT" ) );
355 userReposControl.replay();
356 metadataResolverControl.replay();
358 List<Artifact> artifacts = searchService.getArtifactVersions( ARCHIVA_TEST_GROUP_ID, ARCHIVA_TEST_ARTIFACT_ID );
360 userReposControl.verify();
361 metadataResolverControl.verify();
363 assertNotNull( artifacts );
364 assertEquals( 6, artifacts.size() );
365 assertEquals( new Artifact( "repo1.mirror", ARCHIVA_TEST_GROUP_ID, ARCHIVA_TEST_ARTIFACT_ID, "1.0", "pom" ),
366 artifacts.get( 0 ) );
368 new Artifact( "public.releases", ARCHIVA_TEST_GROUP_ID, ARCHIVA_TEST_ARTIFACT_ID, "1.1-beta-1", "pom" ),
369 artifacts.get( 3 ) );
371 new Artifact( "repo1.mirror", ARCHIVA_TEST_GROUP_ID, ARCHIVA_TEST_ARTIFACT_ID, "1.1-beta-2", "pom" ),
372 artifacts.get( 1 ) );
373 assertEquals( new Artifact( "public.releases", ARCHIVA_TEST_GROUP_ID, ARCHIVA_TEST_ARTIFACT_ID, "1.1", "pom" ),
374 artifacts.get( 4 ) );
375 assertEquals( new Artifact( "repo1.mirror", ARCHIVA_TEST_GROUP_ID, ARCHIVA_TEST_ARTIFACT_ID, "1.2", "pom" ),
376 artifacts.get( 2 ) );
378 new Artifact( "public.releases", ARCHIVA_TEST_GROUP_ID, ARCHIVA_TEST_ARTIFACT_ID, "1.2.1-SNAPSHOT", "pom" ),
379 artifacts.get( 5 ) );
382 public void testGetArtifactVersionsByDateArtifactExists()
388 public void testGetArtifactVersionsByDateArtifactDoesNotExist()
394 public void testGetDependenciesArtifactExists()
397 String repoId = "repo1.mirror";
399 ProjectVersionMetadata model = new ProjectVersionMetadata();
400 model.setId( "1.0" );
402 org.apache.archiva.metadata.model.Dependency dependency = new org.apache.archiva.metadata.model.Dependency();
403 dependency.setGroupId( "org.apache.commons" );
404 dependency.setArtifactId( "commons-logging" );
405 dependency.setVersion( "2.0" );
407 model.addDependency( dependency );
409 dependency = new org.apache.archiva.metadata.model.Dependency();
410 dependency.setGroupId( "junit" );
411 dependency.setArtifactId( "junit" );
412 dependency.setVersion( "2.4" );
413 dependency.setScope( "test" );
415 model.addDependency( dependency );
417 userReposControl.expectAndReturn( userRepos.getObservableRepositories(), Collections.singletonList( repoId ) );
418 metadataResolverControl.expectAndReturn(
419 metadataResolver.getProjectVersion( repoId, ARCHIVA_TEST_GROUP_ID, ARCHIVA_TEST_ARTIFACT_ID, "1.0" ),
422 metadataResolverControl.replay();
423 userReposControl.replay();
425 List<Dependency> dependencies =
426 searchService.getDependencies( ARCHIVA_TEST_GROUP_ID, ARCHIVA_TEST_ARTIFACT_ID, "1.0" );
428 metadataResolverControl.verify();
429 userReposControl.verify();
431 assertNotNull( dependencies );
432 assertEquals( 2, dependencies.size() );
433 assertEquals( new Dependency( "org.apache.commons", "commons-logging", "2.0", null, null, null ),
434 dependencies.get( 0 ) );
435 assertEquals( new Dependency( "junit", "junit", "2.4", null, null, "test" ), dependencies.get( 1 ) );
438 public void testGetDependenciesArtifactDoesNotExist()
441 String repoId = "repo1.mirror";
443 userReposControl.expectAndReturn( userRepos.getObservableRepositories(), Collections.singletonList( repoId ) );
444 metadataResolverControl.expectAndReturn(
445 metadataResolver.getProjectVersion( repoId, ARCHIVA_TEST_GROUP_ID, ARCHIVA_TEST_ARTIFACT_ID, "1.0" ),
448 userReposControl.replay();
449 metadataResolverControl.replay();
453 searchService.getDependencies( ARCHIVA_TEST_GROUP_ID, ARCHIVA_TEST_ARTIFACT_ID, "1.0" );
454 fail( "An exception should have been thrown." );
456 catch ( Exception e )
458 assertEquals( "Artifact does not exist.", e.getMessage() );
461 userReposControl.verify();
462 metadataResolverControl.verify();
465 public void testGetDependencyTreeArtifactExists()
471 public void testGetDependencyTreeArtifactDoesNotExist()
477 public void testGetDependees()
480 List<String> observableRepoIds = new ArrayList<String>();
481 String repoId = "repo1.mirror";
482 observableRepoIds.add( repoId );
484 List<ProjectVersionReference> dependeeModels = new ArrayList<ProjectVersionReference>();
485 ProjectVersionReference dependeeModel = new ProjectVersionReference();
486 dependeeModel.setNamespace( ARCHIVA_TEST_GROUP_ID );
487 dependeeModel.setProjectId( "archiva-dependee-one" );
488 dependeeModel.setProjectVersion( "1.0" );
489 dependeeModels.add( dependeeModel );
491 dependeeModel = new ProjectVersionReference();
492 dependeeModel.setNamespace( ARCHIVA_TEST_GROUP_ID );
493 dependeeModel.setProjectId( "archiva-dependee-two" );
494 dependeeModel.setProjectVersion( "1.0" );
495 dependeeModels.add( dependeeModel );
497 userReposControl.expectAndReturn( userRepos.getObservableRepositories(), observableRepoIds );
498 metadataResolverControl.expectAndReturn(
499 metadataResolver.getProjectReferences( repoId, ARCHIVA_TEST_GROUP_ID, ARCHIVA_TEST_ARTIFACT_ID, "1.0" ),
502 metadataResolverControl.replay();
503 userReposControl.replay();
505 List<Artifact> dependees = searchService.getDependees( ARCHIVA_TEST_GROUP_ID, ARCHIVA_TEST_ARTIFACT_ID, "1.0" );
507 metadataResolverControl.verify();
508 userReposControl.verify();
510 assertNotNull( dependees );
511 assertEquals( 2, dependees.size() );
512 assertEquals( new Artifact( repoId, ARCHIVA_TEST_GROUP_ID, "archiva-dependee-one", "1.0", "" ),
513 dependees.get( 0 ) );
514 assertEquals( new Artifact( repoId, ARCHIVA_TEST_GROUP_ID, "archiva-dependee-two", "1.0", "" ),
515 dependees.get( 1 ) );
518 public void testGetDependeesArtifactDoesNotExist()
521 List<String> observableRepoIds = new ArrayList<String>();
522 observableRepoIds.add( "repo1.mirror" );
523 observableRepoIds.add( "public.releases" );
525 // no longer differentiating between a project not being present and a project that is present but with
526 // no references. If it is later determined to be needed, we will need to modify the metadata content repository
527 userReposControl.expectAndReturn( userRepos.getObservableRepositories(), observableRepoIds );
528 metadataResolverControl.expectAndReturn(
529 metadataResolver.getProjectReferences( "repo1.mirror", ARCHIVA_TEST_GROUP_ID, ARCHIVA_TEST_ARTIFACT_ID,
530 "1.0" ), Collections.<ProjectVersionReference>emptyList() );
531 metadataResolverControl.expectAndReturn(
532 metadataResolver.getProjectReferences( "public.releases", ARCHIVA_TEST_GROUP_ID, ARCHIVA_TEST_ARTIFACT_ID,
533 "1.0" ), Collections.<ProjectVersionReference>emptyList() );
535 userReposControl.replay();
536 metadataResolverControl.replay();
538 assertTrue( searchService.getDependees( ARCHIVA_TEST_GROUP_ID, ARCHIVA_TEST_ARTIFACT_ID, "1.0" ).isEmpty() );
541 private ArtifactMetadata createArtifact( Date whenGathered )
543 String version = "1.0";
544 ArtifactMetadata artifactMetadata = new ArtifactMetadata();
545 artifactMetadata.setVersion( version );
546 artifactMetadata.setId( ARCHIVA_TEST_ARTIFACT_ID + "-" + version + ".jar" );
547 artifactMetadata.setProject( ARCHIVA_TEST_ARTIFACT_ID );
548 artifactMetadata.setNamespace( ARCHIVA_TEST_GROUP_ID );
549 artifactMetadata.setMd5( CHECKSUM );
550 artifactMetadata.setWhenGathered( whenGathered );
551 artifactMetadata.setRepositoryId( TEST_REPO );
553 MavenArtifactFacet facet = new MavenArtifactFacet();
554 facet.setType( "jar" );
555 artifactMetadata.addFacet( facet );
557 return artifactMetadata;