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.ProjectVersionMetadata;
35 import org.apache.archiva.metadata.model.ProjectVersionReference;
36 import org.apache.archiva.metadata.repository.MetadataResolver;
37 import org.apache.archiva.metadata.repository.storage.maven2.MavenProjectFacet;
38 import org.apache.archiva.web.xmlrpc.api.SearchService;
39 import org.apache.archiva.web.xmlrpc.api.beans.Artifact;
40 import org.apache.archiva.web.xmlrpc.api.beans.Dependency;
41 import org.apache.archiva.web.xmlrpc.security.XmlRpcUserRepositories;
42 import org.apache.maven.archiva.database.ArchivaDAO;
43 import org.apache.maven.archiva.database.ArtifactDAO;
44 import org.apache.maven.archiva.database.constraints.ArtifactsByChecksumConstraint;
45 import org.apache.maven.archiva.database.constraints.UniqueVersionConstraint;
46 import org.apache.maven.archiva.model.ArchivaArtifact;
47 import org.apache.maven.archiva.model.ArchivaProjectModel;
48 import org.codehaus.plexus.spring.PlexusInSpringTestCase;
49 import org.easymock.MockControl;
50 import org.easymock.classextension.MockClassControl;
53 * SearchServiceImplTest
55 * @version $Id: SearchServiceImplTest.java
57 public class SearchServiceImplTest
58 extends PlexusInSpringTestCase
60 private SearchService searchService;
62 private MockControl userReposControl;
64 private XmlRpcUserRepositories userRepos;
66 private MockControl searchControl;
68 private RepositorySearch search;
70 private MockControl archivaDAOControl;
72 private ArchivaDAO archivaDAO;
74 private MockControl artifactDAOControl;
76 private ArtifactDAO artifactDAO;
78 private static final String ARCHIVA_TEST_ARTIFACT_ID = "archiva-xmlrpc-test";
80 private static final String ARCHIVA_TEST_GROUP_ID = "org.apache.archiva";
82 private MockControl metadataResolverControl;
84 private MetadataResolver metadataResolver;
90 userReposControl = MockClassControl.createControl( XmlRpcUserRepositories.class );
91 userRepos = (XmlRpcUserRepositories) userReposControl.getMock();
93 archivaDAOControl = MockControl.createControl( ArchivaDAO.class );
94 archivaDAOControl.setDefaultMatcher( MockControl.ALWAYS_MATCHER );
95 archivaDAO = (ArchivaDAO) archivaDAOControl.getMock();
97 searchControl = MockControl.createControl( RepositorySearch.class );
98 searchControl.setDefaultMatcher( MockControl.ALWAYS_MATCHER );
99 search = (RepositorySearch) searchControl.getMock();
101 metadataResolverControl = MockControl.createControl( MetadataResolver.class );
102 metadataResolver = (MetadataResolver) metadataResolverControl.getMock();
104 artifactDAOControl = MockControl.createControl( ArtifactDAO.class );
105 artifactDAO = (ArtifactDAO) artifactDAOControl.getMock();
107 searchService = new SearchServiceImpl( userRepos, archivaDAO, metadataResolver, search );
111 public void testQuickSearchModelPackagingIsUsed()
114 List<String> observableRepoIds = new ArrayList<String>();
115 observableRepoIds.add( "repo1.mirror" );
116 observableRepoIds.add( "public.releases" );
118 userReposControl.expectAndReturn( userRepos.getObservableRepositories(), observableRepoIds );
120 SearchResults results = new SearchResults();
121 List<String> versions = new ArrayList<String>();
122 versions.add( "1.0" );
124 SearchResultHit resultHit = new SearchResultHit();
125 resultHit.setGroupId( ARCHIVA_TEST_GROUP_ID );
126 resultHit.setArtifactId( "archiva-webapp" );
127 resultHit.setVersions( versions );
128 resultHit.setRepositoryId( null );
130 results.addHit( SearchUtil.getHitId( ARCHIVA_TEST_GROUP_ID, "archiva-webapp" ), resultHit );
132 SearchResultLimits limits = new SearchResultLimits( SearchResultLimits.ALL_PAGES );
134 searchControl.expectAndDefaultReturn( search.search( "", observableRepoIds, "archiva", limits, null ),
137 archivaDAOControl.expectAndReturn( archivaDAO.query(
138 new UniqueVersionConstraint( observableRepoIds, resultHit.getGroupId(), resultHit.getArtifactId() ) ),
141 ProjectVersionMetadata model = new ProjectVersionMetadata();
142 model.setId( "1.0" );
143 MavenProjectFacet facet = new MavenProjectFacet();
144 facet.setPackaging( "war" );
145 model.addFacet( facet );
147 metadataResolverControl.expectAndReturn(
148 metadataResolver.getProjectVersion( "repo1.mirror", ARCHIVA_TEST_GROUP_ID, "archiva-webapp", "1.0" ),
151 userReposControl.replay();
152 searchControl.replay();
153 metadataResolverControl.replay();
154 archivaDAOControl.replay();
156 List<Artifact> artifacts = searchService.quickSearch( "archiva" );
158 userReposControl.verify();
159 searchControl.verify();
160 metadataResolverControl.verify();
161 archivaDAOControl.verify();
163 assertNotNull( artifacts );
164 assertEquals( 1, artifacts.size() );
166 Artifact artifact = artifacts.get( 0 );
167 assertEquals( ARCHIVA_TEST_GROUP_ID, artifact.getGroupId() );
168 assertEquals( "archiva-webapp", artifact.getArtifactId() );
169 assertEquals( "1.0", artifact.getVersion() );
170 assertEquals( "war", artifact.getType() );
171 assertNotNull( "Repository should not be null!", artifact.getRepositoryId() );
172 assertEquals( "repo1.mirror", artifact.getRepositoryId() );
175 public void testQuickSearchDefaultPackagingIsUsed()
178 List<String> observableRepoIds = new ArrayList<String>();
179 observableRepoIds.add( "repo1.mirror" );
180 observableRepoIds.add( "public.releases" );
182 userReposControl.expectAndReturn( userRepos.getObservableRepositories(), observableRepoIds );
184 SearchResults results = new SearchResults();
185 List<String> versions = new ArrayList<String>();
186 versions.add( "1.0" );
188 SearchResultHit resultHit = new SearchResultHit();
189 resultHit.setRepositoryId( null );
190 resultHit.setGroupId( ARCHIVA_TEST_GROUP_ID );
191 resultHit.setArtifactId( ARCHIVA_TEST_ARTIFACT_ID );
192 resultHit.setVersions( versions );
194 results.addHit( SearchUtil.getHitId( ARCHIVA_TEST_GROUP_ID, ARCHIVA_TEST_ARTIFACT_ID ), resultHit );
196 SearchResultLimits limits = new SearchResultLimits( SearchResultLimits.ALL_PAGES );
198 searchControl.expectAndDefaultReturn( search.search( "", observableRepoIds, "archiva", limits, null ),
201 archivaDAOControl.expectAndReturn( archivaDAO.query(
202 new UniqueVersionConstraint( observableRepoIds, resultHit.getGroupId(), resultHit.getArtifactId() ) ),
205 metadataResolverControl.expectAndReturn(
206 metadataResolver.getProjectVersion( "repo1.mirror", ARCHIVA_TEST_GROUP_ID, ARCHIVA_TEST_ARTIFACT_ID,
209 ProjectVersionMetadata model = new ProjectVersionMetadata();
210 model.setId( "1.0" );
211 metadataResolverControl.expectAndReturn(
212 metadataResolver.getProjectVersion( "public.releases", ARCHIVA_TEST_GROUP_ID, ARCHIVA_TEST_ARTIFACT_ID,
215 userReposControl.replay();
216 searchControl.replay();
217 metadataResolverControl.replay();
218 archivaDAOControl.replay();
220 List<Artifact> artifacts = searchService.quickSearch( "archiva" );
222 userReposControl.verify();
223 searchControl.verify();
224 metadataResolverControl.verify();
225 archivaDAOControl.verify();
227 assertNotNull( artifacts );
228 assertEquals( 1, artifacts.size() );
230 Artifact artifact = artifacts.get( 0 );
231 assertEquals( ARCHIVA_TEST_GROUP_ID, artifact.getGroupId() );
232 assertEquals( ARCHIVA_TEST_ARTIFACT_ID, artifact.getArtifactId() );
233 assertEquals( "1.0", artifact.getVersion() );
234 assertEquals( "jar", artifact.getType() );
235 assertEquals( "public.releases", artifact.getRepositoryId() );
238 public void testQuickSearchArtifactRegularSearch()
241 List<String> observableRepoIds = new ArrayList<String>();
242 observableRepoIds.add( "repo1.mirror" );
243 observableRepoIds.add( "public.releases" );
245 userReposControl.expectAndReturn( userRepos.getObservableRepositories(), observableRepoIds );
247 SearchResults results = new SearchResults();
248 List<String> versions = new ArrayList<String>();
249 versions.add( "1.0" );
251 SearchResultHit resultHit = new SearchResultHit();
252 resultHit.setGroupId( ARCHIVA_TEST_GROUP_ID );
253 resultHit.setArtifactId( ARCHIVA_TEST_ARTIFACT_ID );
254 resultHit.setVersions( versions );
255 resultHit.setRepositoryId( null );
257 results.addHit( SearchUtil.getHitId( resultHit.getGroupId(), resultHit.getArtifactId() ), resultHit );
259 archivaDAOControl.expectAndReturn( archivaDAO.query(
260 new UniqueVersionConstraint( observableRepoIds, resultHit.getGroupId(), resultHit.getArtifactId() ) ),
263 SearchResultLimits limits = new SearchResultLimits( SearchResultLimits.ALL_PAGES );
265 searchControl.expectAndDefaultReturn( search.search( "", observableRepoIds, "archiva", limits, null ),
268 ProjectVersionMetadata model = new ProjectVersionMetadata();
269 model.setId( "1.0" );
270 MavenProjectFacet facet = new MavenProjectFacet();
271 facet.setPackaging( "jar" );
272 model.addFacet( facet );
274 metadataResolverControl.expectAndReturn(
275 metadataResolver.getProjectVersion( "repo1.mirror", ARCHIVA_TEST_GROUP_ID, ARCHIVA_TEST_ARTIFACT_ID,
278 userReposControl.replay();
279 searchControl.replay();
280 archivaDAOControl.replay();
281 metadataResolverControl.replay();
283 List<Artifact> artifacts = searchService.quickSearch( "archiva" );
285 userReposControl.verify();
286 searchControl.verify();
287 archivaDAOControl.verify();
288 metadataResolverControl.verify();
290 assertNotNull( artifacts );
291 assertEquals( 1, artifacts.size() );
293 Artifact artifact = artifacts.get( 0 );
294 assertEquals( ARCHIVA_TEST_GROUP_ID, artifact.getGroupId() );
295 assertEquals( ARCHIVA_TEST_ARTIFACT_ID, artifact.getArtifactId() );
296 assertEquals( "1.0", artifact.getVersion() );
297 assertEquals( "jar", artifact.getType() );
298 assertNotNull( "Repository should not be null!", artifact.getRepositoryId() );
299 assertEquals( "repo1.mirror", artifact.getRepositoryId() );
302 public void testQuickSearchNoResults()
305 List<String> observableRepoIds = new ArrayList<String>();
306 observableRepoIds.add( "repo1.mirror" );
307 observableRepoIds.add( "public.releases" );
309 userReposControl.expectAndReturn( userRepos.getObservableRepositories(), observableRepoIds );
311 SearchResults results = new SearchResults();
312 SearchResultLimits limits = new SearchResultLimits( SearchResultLimits.ALL_PAGES );
314 searchControl.expectAndDefaultReturn( search.search( "", observableRepoIds, "non-existent", limits, null ),
316 userReposControl.replay();
317 searchControl.replay();
319 List<Artifact> artifacts = searchService.quickSearch( "test" );
321 userReposControl.verify();
322 searchControl.verify();
324 assertNotNull( artifacts );
325 assertEquals( 0, artifacts.size() );
328 public void testGetArtifactByChecksum()
331 Date whenGathered = new Date();
333 ArtifactsByChecksumConstraint constraint = new ArtifactsByChecksumConstraint( "a1b2c3aksjhdasfkdasasd" );
334 List<ArchivaArtifact> artifacts = new ArrayList<ArchivaArtifact>();
335 ArchivaArtifact artifact =
336 new ArchivaArtifact( ARCHIVA_TEST_GROUP_ID, ARCHIVA_TEST_ARTIFACT_ID, "1.0", "", "jar", "test-repo" );
337 artifact.getModel().setWhenGathered( whenGathered );
338 artifacts.add( artifact );
340 archivaDAOControl.expectAndReturn( archivaDAO.getArtifactDAO(), artifactDAO );
341 artifactDAO.queryArtifacts( constraint );
342 artifactDAOControl.setMatcher( MockControl.ALWAYS_MATCHER );
343 artifactDAOControl.setReturnValue( artifacts );
345 archivaDAOControl.replay();
346 artifactDAOControl.replay();
348 List<Artifact> results = searchService.getArtifactByChecksum( "a1b2c3aksjhdasfkdasasd" );
350 archivaDAOControl.verify();
351 artifactDAOControl.verify();
353 assertNotNull( results );
354 assertEquals( 1, results.size() );
357 public void testGetArtifactVersionsArtifactExists()
360 List<String> observableRepoIds = new ArrayList<String>();
361 observableRepoIds.add( "repo1.mirror" );
362 observableRepoIds.add( "public.releases" );
364 userReposControl.expectAndReturn( userRepos.getObservableRepositories(), observableRepoIds );
365 metadataResolverControl.expectAndReturn(
366 metadataResolver.getProjectVersions( "repo1.mirror", ARCHIVA_TEST_GROUP_ID, ARCHIVA_TEST_ARTIFACT_ID ),
367 Arrays.asList( "1.0", "1.1-beta-2", "1.2" ) );
368 metadataResolverControl.expectAndReturn(
369 metadataResolver.getProjectVersions( "public.releases", ARCHIVA_TEST_GROUP_ID, ARCHIVA_TEST_ARTIFACT_ID ),
370 Arrays.asList( "1.1-beta-1", "1.1", "1.2.1-SNAPSHOT" ) );
372 userReposControl.replay();
373 metadataResolverControl.replay();
375 List<Artifact> artifacts = searchService.getArtifactVersions( ARCHIVA_TEST_GROUP_ID, ARCHIVA_TEST_ARTIFACT_ID );
377 userReposControl.verify();
378 metadataResolverControl.verify();
380 assertNotNull( artifacts );
381 assertEquals( 6, artifacts.size() );
382 assertEquals( new Artifact( "repo1.mirror", ARCHIVA_TEST_GROUP_ID, ARCHIVA_TEST_ARTIFACT_ID, "1.0", "pom" ),
383 artifacts.get( 0 ) );
385 new Artifact( "public.releases", ARCHIVA_TEST_GROUP_ID, ARCHIVA_TEST_ARTIFACT_ID, "1.1-beta-1", "pom" ),
386 artifacts.get( 3 ) );
388 new Artifact( "repo1.mirror", ARCHIVA_TEST_GROUP_ID, ARCHIVA_TEST_ARTIFACT_ID, "1.1-beta-2", "pom" ),
389 artifacts.get( 1 ) );
390 assertEquals( new Artifact( "public.releases", ARCHIVA_TEST_GROUP_ID, ARCHIVA_TEST_ARTIFACT_ID, "1.1", "pom" ),
391 artifacts.get( 4 ) );
392 assertEquals( new Artifact( "repo1.mirror", ARCHIVA_TEST_GROUP_ID, ARCHIVA_TEST_ARTIFACT_ID, "1.2", "pom" ),
393 artifacts.get( 2 ) );
395 new Artifact( "public.releases", ARCHIVA_TEST_GROUP_ID, ARCHIVA_TEST_ARTIFACT_ID, "1.2.1-SNAPSHOT", "pom" ),
396 artifacts.get( 5 ) );
399 public void testGetArtifactVersionsByDateArtifactExists()
405 public void testGetArtifactVersionsByDateArtifactDoesNotExist()
411 public void testGetDependenciesArtifactExists()
414 String repoId = "repo1.mirror";
416 ProjectVersionMetadata model = new ProjectVersionMetadata();
417 model.setId( "1.0" );
419 org.apache.archiva.metadata.model.Dependency dependency = new org.apache.archiva.metadata.model.Dependency();
420 dependency.setGroupId( "org.apache.commons" );
421 dependency.setArtifactId( "commons-logging" );
422 dependency.setVersion( "2.0" );
424 model.addDependency( dependency );
426 dependency = new org.apache.archiva.metadata.model.Dependency();
427 dependency.setGroupId( "junit" );
428 dependency.setArtifactId( "junit" );
429 dependency.setVersion( "2.4" );
430 dependency.setScope( "test" );
432 model.addDependency( dependency );
434 userReposControl.expectAndReturn( userRepos.getObservableRepositories(), Collections.singletonList( repoId ) );
435 metadataResolverControl.expectAndReturn(
436 metadataResolver.getProjectVersion( repoId, ARCHIVA_TEST_GROUP_ID, ARCHIVA_TEST_ARTIFACT_ID, "1.0" ),
439 metadataResolverControl.replay();
440 userReposControl.replay();
442 List<Dependency> dependencies =
443 searchService.getDependencies( ARCHIVA_TEST_GROUP_ID, ARCHIVA_TEST_ARTIFACT_ID, "1.0" );
445 metadataResolverControl.verify();
446 userReposControl.verify();
448 assertNotNull( dependencies );
449 assertEquals( 2, dependencies.size() );
450 assertEquals( new Dependency( "org.apache.commons", "commons-logging", "2.0", null, null, null ),
451 dependencies.get( 0 ) );
452 assertEquals( new Dependency( "junit", "junit", "2.4", null, null, "test" ), dependencies.get( 1 ) );
455 public void testGetDependenciesArtifactDoesNotExist()
458 String repoId = "repo1.mirror";
460 userReposControl.expectAndReturn( userRepos.getObservableRepositories(), Collections.singletonList( repoId ) );
461 metadataResolverControl.expectAndReturn(
462 metadataResolver.getProjectVersion( repoId, ARCHIVA_TEST_GROUP_ID, ARCHIVA_TEST_ARTIFACT_ID, "1.0" ),
465 userReposControl.replay();
466 metadataResolverControl.replay();
470 searchService.getDependencies( ARCHIVA_TEST_GROUP_ID, ARCHIVA_TEST_ARTIFACT_ID, "1.0" );
471 fail( "An exception should have been thrown." );
473 catch ( Exception e )
475 assertEquals( "Artifact does not exist.", e.getMessage() );
478 userReposControl.verify();
479 metadataResolverControl.verify();
482 public void testGetDependencyTreeArtifactExists()
488 public void testGetDependencyTreeArtifactDoesNotExist()
494 public void testGetDependees()
497 List<String> observableRepoIds = new ArrayList<String>();
498 String repoId = "repo1.mirror";
499 observableRepoIds.add( repoId );
501 List<ProjectVersionReference> dependeeModels = new ArrayList<ProjectVersionReference>();
502 ProjectVersionReference dependeeModel = new ProjectVersionReference();
503 dependeeModel.setNamespace( ARCHIVA_TEST_GROUP_ID );
504 dependeeModel.setProjectId( "archiva-dependee-one" );
505 dependeeModel.setProjectVersion( "1.0" );
506 dependeeModels.add( dependeeModel );
508 dependeeModel = new ProjectVersionReference();
509 dependeeModel.setNamespace( ARCHIVA_TEST_GROUP_ID );
510 dependeeModel.setProjectId( "archiva-dependee-two" );
511 dependeeModel.setProjectVersion( "1.0" );
512 dependeeModels.add( dependeeModel );
514 userReposControl.expectAndReturn( userRepos.getObservableRepositories(), observableRepoIds );
515 metadataResolverControl.expectAndReturn(
516 metadataResolver.getProjectReferences( repoId, ARCHIVA_TEST_GROUP_ID, ARCHIVA_TEST_ARTIFACT_ID, "1.0" ),
519 metadataResolverControl.replay();
520 userReposControl.replay();
522 List<Artifact> dependees = searchService.getDependees( ARCHIVA_TEST_GROUP_ID, ARCHIVA_TEST_ARTIFACT_ID, "1.0" );
524 metadataResolverControl.verify();
525 userReposControl.verify();
527 assertNotNull( dependees );
528 assertEquals( 2, dependees.size() );
529 assertEquals( new Artifact( repoId, ARCHIVA_TEST_GROUP_ID, "archiva-dependee-one", "1.0", "" ),
530 dependees.get( 0 ) );
531 assertEquals( new Artifact( repoId, ARCHIVA_TEST_GROUP_ID, "archiva-dependee-two", "1.0", "" ),
532 dependees.get( 1 ) );
535 public void testGetDependeesArtifactDoesNotExist()
538 List<String> observableRepoIds = new ArrayList<String>();
539 observableRepoIds.add( "repo1.mirror" );
540 observableRepoIds.add( "public.releases" );
542 // no longer differentiating between a project not being present and a project that is present but with
543 // no references. If it is later determined to be needed, we will need to modify the metadata content repository
544 userReposControl.expectAndReturn( userRepos.getObservableRepositories(), observableRepoIds );
545 metadataResolverControl.expectAndReturn(
546 metadataResolver.getProjectReferences( "repo1.mirror", ARCHIVA_TEST_GROUP_ID, ARCHIVA_TEST_ARTIFACT_ID,
547 "1.0" ), Collections.<ProjectVersionReference>emptyList() );
548 metadataResolverControl.expectAndReturn(
549 metadataResolver.getProjectReferences( "public.releases", ARCHIVA_TEST_GROUP_ID, ARCHIVA_TEST_ARTIFACT_ID,
550 "1.0" ), Collections.<ProjectVersionReference>emptyList() );
552 userReposControl.replay();
553 metadataResolverControl.replay();
555 assertTrue( searchService.getDependees( ARCHIVA_TEST_GROUP_ID, ARCHIVA_TEST_ARTIFACT_ID, "1.0" ).isEmpty() );