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.codehaus.plexus.spring.PlexusInSpringTestCase;
48 import org.easymock.MockControl;
49 import org.easymock.classextension.MockClassControl;
52 * SearchServiceImplTest
54 * @version $Id: SearchServiceImplTest.java
56 public class SearchServiceImplTest
57 extends PlexusInSpringTestCase
59 private SearchService searchService;
61 private MockControl userReposControl;
63 private XmlRpcUserRepositories userRepos;
65 private MockControl searchControl;
67 private RepositorySearch search;
69 private MockControl archivaDAOControl;
71 private ArchivaDAO archivaDAO;
73 private MockControl artifactDAOControl;
75 private ArtifactDAO artifactDAO;
77 private static final String ARCHIVA_TEST_ARTIFACT_ID = "archiva-xmlrpc-test";
79 private static final String ARCHIVA_TEST_GROUP_ID = "org.apache.archiva";
81 private MockControl metadataResolverControl;
83 private MetadataResolver metadataResolver;
89 userReposControl = MockClassControl.createControl( XmlRpcUserRepositories.class );
90 userRepos = (XmlRpcUserRepositories) userReposControl.getMock();
92 archivaDAOControl = MockControl.createControl( ArchivaDAO.class );
93 archivaDAOControl.setDefaultMatcher( MockControl.ALWAYS_MATCHER );
94 archivaDAO = (ArchivaDAO) archivaDAOControl.getMock();
96 searchControl = MockControl.createControl( RepositorySearch.class );
97 searchControl.setDefaultMatcher( MockControl.ALWAYS_MATCHER );
98 search = (RepositorySearch) searchControl.getMock();
100 metadataResolverControl = MockControl.createControl( MetadataResolver.class );
101 metadataResolver = (MetadataResolver) metadataResolverControl.getMock();
103 artifactDAOControl = MockControl.createControl( ArtifactDAO.class );
104 artifactDAO = (ArtifactDAO) artifactDAOControl.getMock();
106 searchService = new SearchServiceImpl( userRepos, archivaDAO, metadataResolver, search );
110 public void testQuickSearchModelPackagingIsUsed()
113 List<String> observableRepoIds = new ArrayList<String>();
114 observableRepoIds.add( "repo1.mirror" );
115 observableRepoIds.add( "public.releases" );
117 userReposControl.expectAndReturn( userRepos.getObservableRepositories(), observableRepoIds );
119 SearchResults results = new SearchResults();
120 List<String> versions = new ArrayList<String>();
121 versions.add( "1.0" );
123 SearchResultHit resultHit = new SearchResultHit();
124 resultHit.setGroupId( ARCHIVA_TEST_GROUP_ID );
125 resultHit.setArtifactId( "archiva-webapp" );
126 resultHit.setVersions( versions );
127 resultHit.setRepositoryId( null );
129 results.addHit( SearchUtil.getHitId( ARCHIVA_TEST_GROUP_ID, "archiva-webapp" ), resultHit );
131 SearchResultLimits limits = new SearchResultLimits( SearchResultLimits.ALL_PAGES );
133 searchControl.expectAndDefaultReturn( search.search( "", observableRepoIds, "archiva", limits, null ),
136 archivaDAOControl.expectAndReturn( archivaDAO.query(
137 new UniqueVersionConstraint( observableRepoIds, resultHit.getGroupId(), resultHit.getArtifactId() ) ),
140 ProjectVersionMetadata model = new ProjectVersionMetadata();
141 model.setId( "1.0" );
142 MavenProjectFacet facet = new MavenProjectFacet();
143 facet.setPackaging( "war" );
144 model.addFacet( facet );
146 metadataResolverControl.expectAndReturn(
147 metadataResolver.getProjectVersion( "repo1.mirror", ARCHIVA_TEST_GROUP_ID, "archiva-webapp", "1.0" ),
150 userReposControl.replay();
151 searchControl.replay();
152 metadataResolverControl.replay();
153 archivaDAOControl.replay();
155 List<Artifact> artifacts = searchService.quickSearch( "archiva" );
157 userReposControl.verify();
158 searchControl.verify();
159 metadataResolverControl.verify();
160 archivaDAOControl.verify();
162 assertNotNull( artifacts );
163 assertEquals( 1, artifacts.size() );
165 Artifact artifact = artifacts.get( 0 );
166 assertEquals( ARCHIVA_TEST_GROUP_ID, artifact.getGroupId() );
167 assertEquals( "archiva-webapp", artifact.getArtifactId() );
168 assertEquals( "1.0", artifact.getVersion() );
169 assertEquals( "war", artifact.getType() );
170 assertNotNull( "Repository should not be null!", artifact.getRepositoryId() );
171 assertEquals( "repo1.mirror", artifact.getRepositoryId() );
174 public void testQuickSearchDefaultPackagingIsUsed()
177 List<String> observableRepoIds = new ArrayList<String>();
178 observableRepoIds.add( "repo1.mirror" );
179 observableRepoIds.add( "public.releases" );
181 userReposControl.expectAndReturn( userRepos.getObservableRepositories(), observableRepoIds );
183 SearchResults results = new SearchResults();
184 List<String> versions = new ArrayList<String>();
185 versions.add( "1.0" );
187 SearchResultHit resultHit = new SearchResultHit();
188 resultHit.setRepositoryId( null );
189 resultHit.setGroupId( ARCHIVA_TEST_GROUP_ID );
190 resultHit.setArtifactId( ARCHIVA_TEST_ARTIFACT_ID );
191 resultHit.setVersions( versions );
193 results.addHit( SearchUtil.getHitId( ARCHIVA_TEST_GROUP_ID, ARCHIVA_TEST_ARTIFACT_ID ), resultHit );
195 SearchResultLimits limits = new SearchResultLimits( SearchResultLimits.ALL_PAGES );
197 searchControl.expectAndDefaultReturn( search.search( "", observableRepoIds, "archiva", limits, null ),
200 archivaDAOControl.expectAndReturn( archivaDAO.query(
201 new UniqueVersionConstraint( observableRepoIds, resultHit.getGroupId(), resultHit.getArtifactId() ) ),
204 metadataResolverControl.expectAndReturn(
205 metadataResolver.getProjectVersion( "repo1.mirror", ARCHIVA_TEST_GROUP_ID, ARCHIVA_TEST_ARTIFACT_ID,
208 ProjectVersionMetadata model = new ProjectVersionMetadata();
209 model.setId( "1.0" );
210 metadataResolverControl.expectAndReturn(
211 metadataResolver.getProjectVersion( "public.releases", ARCHIVA_TEST_GROUP_ID, ARCHIVA_TEST_ARTIFACT_ID,
214 userReposControl.replay();
215 searchControl.replay();
216 metadataResolverControl.replay();
217 archivaDAOControl.replay();
219 List<Artifact> artifacts = searchService.quickSearch( "archiva" );
221 userReposControl.verify();
222 searchControl.verify();
223 metadataResolverControl.verify();
224 archivaDAOControl.verify();
226 assertNotNull( artifacts );
227 assertEquals( 1, artifacts.size() );
229 Artifact artifact = artifacts.get( 0 );
230 assertEquals( ARCHIVA_TEST_GROUP_ID, artifact.getGroupId() );
231 assertEquals( ARCHIVA_TEST_ARTIFACT_ID, artifact.getArtifactId() );
232 assertEquals( "1.0", artifact.getVersion() );
233 assertEquals( "jar", artifact.getType() );
234 assertEquals( "public.releases", artifact.getRepositoryId() );
237 public void testQuickSearchArtifactRegularSearch()
240 List<String> observableRepoIds = new ArrayList<String>();
241 observableRepoIds.add( "repo1.mirror" );
242 observableRepoIds.add( "public.releases" );
244 userReposControl.expectAndReturn( userRepos.getObservableRepositories(), observableRepoIds );
246 SearchResults results = new SearchResults();
247 List<String> versions = new ArrayList<String>();
248 versions.add( "1.0" );
250 SearchResultHit resultHit = new SearchResultHit();
251 resultHit.setGroupId( ARCHIVA_TEST_GROUP_ID );
252 resultHit.setArtifactId( ARCHIVA_TEST_ARTIFACT_ID );
253 resultHit.setVersions( versions );
254 resultHit.setRepositoryId( null );
256 results.addHit( SearchUtil.getHitId( resultHit.getGroupId(), resultHit.getArtifactId() ), resultHit );
258 archivaDAOControl.expectAndReturn( archivaDAO.query(
259 new UniqueVersionConstraint( observableRepoIds, resultHit.getGroupId(), resultHit.getArtifactId() ) ),
262 SearchResultLimits limits = new SearchResultLimits( SearchResultLimits.ALL_PAGES );
264 searchControl.expectAndDefaultReturn( search.search( "", observableRepoIds, "archiva", limits, null ),
267 ProjectVersionMetadata model = new ProjectVersionMetadata();
268 model.setId( "1.0" );
269 MavenProjectFacet facet = new MavenProjectFacet();
270 facet.setPackaging( "jar" );
271 model.addFacet( facet );
273 metadataResolverControl.expectAndReturn(
274 metadataResolver.getProjectVersion( "repo1.mirror", ARCHIVA_TEST_GROUP_ID, ARCHIVA_TEST_ARTIFACT_ID,
277 userReposControl.replay();
278 searchControl.replay();
279 archivaDAOControl.replay();
280 metadataResolverControl.replay();
282 List<Artifact> artifacts = searchService.quickSearch( "archiva" );
284 userReposControl.verify();
285 searchControl.verify();
286 archivaDAOControl.verify();
287 metadataResolverControl.verify();
289 assertNotNull( artifacts );
290 assertEquals( 1, artifacts.size() );
292 Artifact artifact = artifacts.get( 0 );
293 assertEquals( ARCHIVA_TEST_GROUP_ID, artifact.getGroupId() );
294 assertEquals( ARCHIVA_TEST_ARTIFACT_ID, artifact.getArtifactId() );
295 assertEquals( "1.0", artifact.getVersion() );
296 assertEquals( "jar", artifact.getType() );
297 assertNotNull( "Repository should not be null!", artifact.getRepositoryId() );
298 assertEquals( "repo1.mirror", artifact.getRepositoryId() );
301 public void testQuickSearchNoResults()
304 List<String> observableRepoIds = new ArrayList<String>();
305 observableRepoIds.add( "repo1.mirror" );
306 observableRepoIds.add( "public.releases" );
308 userReposControl.expectAndReturn( userRepos.getObservableRepositories(), observableRepoIds );
310 SearchResults results = new SearchResults();
311 SearchResultLimits limits = new SearchResultLimits( SearchResultLimits.ALL_PAGES );
313 searchControl.expectAndDefaultReturn( search.search( "", observableRepoIds, "non-existent", limits, null ),
315 userReposControl.replay();
316 searchControl.replay();
318 List<Artifact> artifacts = searchService.quickSearch( "test" );
320 userReposControl.verify();
321 searchControl.verify();
323 assertNotNull( artifacts );
324 assertEquals( 0, artifacts.size() );
327 public void testGetArtifactByChecksum()
330 Date whenGathered = new Date();
332 ArtifactsByChecksumConstraint constraint = new ArtifactsByChecksumConstraint( "a1b2c3aksjhdasfkdasasd" );
333 List<ArchivaArtifact> artifacts = new ArrayList<ArchivaArtifact>();
334 ArchivaArtifact artifact =
335 new ArchivaArtifact( ARCHIVA_TEST_GROUP_ID, ARCHIVA_TEST_ARTIFACT_ID, "1.0", "", "jar", "test-repo" );
336 artifact.getModel().setWhenGathered( whenGathered );
337 artifacts.add( artifact );
339 archivaDAOControl.expectAndReturn( archivaDAO.getArtifactDAO(), artifactDAO );
340 artifactDAO.queryArtifacts( constraint );
341 artifactDAOControl.setMatcher( MockControl.ALWAYS_MATCHER );
342 artifactDAOControl.setReturnValue( artifacts );
344 archivaDAOControl.replay();
345 artifactDAOControl.replay();
347 List<Artifact> results = searchService.getArtifactByChecksum( "a1b2c3aksjhdasfkdasasd" );
349 archivaDAOControl.verify();
350 artifactDAOControl.verify();
352 assertNotNull( results );
353 assertEquals( 1, results.size() );
356 public void testGetArtifactVersionsArtifactExists()
359 List<String> observableRepoIds = new ArrayList<String>();
360 observableRepoIds.add( "repo1.mirror" );
361 observableRepoIds.add( "public.releases" );
363 userReposControl.expectAndReturn( userRepos.getObservableRepositories(), observableRepoIds );
364 metadataResolverControl.expectAndReturn(
365 metadataResolver.getProjectVersions( "repo1.mirror", ARCHIVA_TEST_GROUP_ID, ARCHIVA_TEST_ARTIFACT_ID ),
366 Arrays.asList( "1.0", "1.1-beta-2", "1.2" ) );
367 metadataResolverControl.expectAndReturn(
368 metadataResolver.getProjectVersions( "public.releases", ARCHIVA_TEST_GROUP_ID, ARCHIVA_TEST_ARTIFACT_ID ),
369 Arrays.asList( "1.1-beta-1", "1.1", "1.2.1-SNAPSHOT" ) );
371 userReposControl.replay();
372 metadataResolverControl.replay();
374 List<Artifact> artifacts = searchService.getArtifactVersions( ARCHIVA_TEST_GROUP_ID, ARCHIVA_TEST_ARTIFACT_ID );
376 userReposControl.verify();
377 metadataResolverControl.verify();
379 assertNotNull( artifacts );
380 assertEquals( 6, artifacts.size() );
381 assertEquals( new Artifact( "repo1.mirror", ARCHIVA_TEST_GROUP_ID, ARCHIVA_TEST_ARTIFACT_ID, "1.0", "pom" ),
382 artifacts.get( 0 ) );
384 new Artifact( "public.releases", ARCHIVA_TEST_GROUP_ID, ARCHIVA_TEST_ARTIFACT_ID, "1.1-beta-1", "pom" ),
385 artifacts.get( 3 ) );
387 new Artifact( "repo1.mirror", ARCHIVA_TEST_GROUP_ID, ARCHIVA_TEST_ARTIFACT_ID, "1.1-beta-2", "pom" ),
388 artifacts.get( 1 ) );
389 assertEquals( new Artifact( "public.releases", ARCHIVA_TEST_GROUP_ID, ARCHIVA_TEST_ARTIFACT_ID, "1.1", "pom" ),
390 artifacts.get( 4 ) );
391 assertEquals( new Artifact( "repo1.mirror", ARCHIVA_TEST_GROUP_ID, ARCHIVA_TEST_ARTIFACT_ID, "1.2", "pom" ),
392 artifacts.get( 2 ) );
394 new Artifact( "public.releases", ARCHIVA_TEST_GROUP_ID, ARCHIVA_TEST_ARTIFACT_ID, "1.2.1-SNAPSHOT", "pom" ),
395 artifacts.get( 5 ) );
398 public void testGetArtifactVersionsByDateArtifactExists()
404 public void testGetArtifactVersionsByDateArtifactDoesNotExist()
410 public void testGetDependenciesArtifactExists()
413 String repoId = "repo1.mirror";
415 ProjectVersionMetadata model = new ProjectVersionMetadata();
416 model.setId( "1.0" );
418 org.apache.archiva.metadata.model.Dependency dependency = new org.apache.archiva.metadata.model.Dependency();
419 dependency.setGroupId( "org.apache.commons" );
420 dependency.setArtifactId( "commons-logging" );
421 dependency.setVersion( "2.0" );
423 model.addDependency( dependency );
425 dependency = new org.apache.archiva.metadata.model.Dependency();
426 dependency.setGroupId( "junit" );
427 dependency.setArtifactId( "junit" );
428 dependency.setVersion( "2.4" );
429 dependency.setScope( "test" );
431 model.addDependency( dependency );
433 userReposControl.expectAndReturn( userRepos.getObservableRepositories(), Collections.singletonList( repoId ) );
434 metadataResolverControl.expectAndReturn(
435 metadataResolver.getProjectVersion( repoId, ARCHIVA_TEST_GROUP_ID, ARCHIVA_TEST_ARTIFACT_ID, "1.0" ),
438 metadataResolverControl.replay();
439 userReposControl.replay();
441 List<Dependency> dependencies =
442 searchService.getDependencies( ARCHIVA_TEST_GROUP_ID, ARCHIVA_TEST_ARTIFACT_ID, "1.0" );
444 metadataResolverControl.verify();
445 userReposControl.verify();
447 assertNotNull( dependencies );
448 assertEquals( 2, dependencies.size() );
449 assertEquals( new Dependency( "org.apache.commons", "commons-logging", "2.0", null, null, null ),
450 dependencies.get( 0 ) );
451 assertEquals( new Dependency( "junit", "junit", "2.4", null, null, "test" ), dependencies.get( 1 ) );
454 public void testGetDependenciesArtifactDoesNotExist()
457 String repoId = "repo1.mirror";
459 userReposControl.expectAndReturn( userRepos.getObservableRepositories(), Collections.singletonList( repoId ) );
460 metadataResolverControl.expectAndReturn(
461 metadataResolver.getProjectVersion( repoId, ARCHIVA_TEST_GROUP_ID, ARCHIVA_TEST_ARTIFACT_ID, "1.0" ),
464 userReposControl.replay();
465 metadataResolverControl.replay();
469 searchService.getDependencies( ARCHIVA_TEST_GROUP_ID, ARCHIVA_TEST_ARTIFACT_ID, "1.0" );
470 fail( "An exception should have been thrown." );
472 catch ( Exception e )
474 assertEquals( "Artifact does not exist.", e.getMessage() );
477 userReposControl.verify();
478 metadataResolverControl.verify();
481 public void testGetDependencyTreeArtifactExists()
487 public void testGetDependencyTreeArtifactDoesNotExist()
493 public void testGetDependees()
496 List<String> observableRepoIds = new ArrayList<String>();
497 String repoId = "repo1.mirror";
498 observableRepoIds.add( repoId );
500 List<ProjectVersionReference> dependeeModels = new ArrayList<ProjectVersionReference>();
501 ProjectVersionReference dependeeModel = new ProjectVersionReference();
502 dependeeModel.setNamespace( ARCHIVA_TEST_GROUP_ID );
503 dependeeModel.setProjectId( "archiva-dependee-one" );
504 dependeeModel.setProjectVersion( "1.0" );
505 dependeeModels.add( dependeeModel );
507 dependeeModel = new ProjectVersionReference();
508 dependeeModel.setNamespace( ARCHIVA_TEST_GROUP_ID );
509 dependeeModel.setProjectId( "archiva-dependee-two" );
510 dependeeModel.setProjectVersion( "1.0" );
511 dependeeModels.add( dependeeModel );
513 userReposControl.expectAndReturn( userRepos.getObservableRepositories(), observableRepoIds );
514 metadataResolverControl.expectAndReturn(
515 metadataResolver.getProjectReferences( repoId, ARCHIVA_TEST_GROUP_ID, ARCHIVA_TEST_ARTIFACT_ID, "1.0" ),
518 metadataResolverControl.replay();
519 userReposControl.replay();
521 List<Artifact> dependees = searchService.getDependees( ARCHIVA_TEST_GROUP_ID, ARCHIVA_TEST_ARTIFACT_ID, "1.0" );
523 metadataResolverControl.verify();
524 userReposControl.verify();
526 assertNotNull( dependees );
527 assertEquals( 2, dependees.size() );
528 assertEquals( new Artifact( repoId, ARCHIVA_TEST_GROUP_ID, "archiva-dependee-one", "1.0", "" ),
529 dependees.get( 0 ) );
530 assertEquals( new Artifact( repoId, ARCHIVA_TEST_GROUP_ID, "archiva-dependee-two", "1.0", "" ),
531 dependees.get( 1 ) );
534 public void testGetDependeesArtifactDoesNotExist()
537 List<String> observableRepoIds = new ArrayList<String>();
538 observableRepoIds.add( "repo1.mirror" );
539 observableRepoIds.add( "public.releases" );
541 // no longer differentiating between a project not being present and a project that is present but with
542 // no references. If it is later determined to be needed, we will need to modify the metadata content repository
543 userReposControl.expectAndReturn( userRepos.getObservableRepositories(), observableRepoIds );
544 metadataResolverControl.expectAndReturn(
545 metadataResolver.getProjectReferences( "repo1.mirror", ARCHIVA_TEST_GROUP_ID, ARCHIVA_TEST_ARTIFACT_ID,
546 "1.0" ), Collections.<ProjectVersionReference>emptyList() );
547 metadataResolverControl.expectAndReturn(
548 metadataResolver.getProjectReferences( "public.releases", ARCHIVA_TEST_GROUP_ID, ARCHIVA_TEST_ARTIFACT_ID,
549 "1.0" ), Collections.<ProjectVersionReference>emptyList() );
551 userReposControl.replay();
552 metadataResolverControl.replay();
554 assertTrue( searchService.getDependees( ARCHIVA_TEST_GROUP_ID, ARCHIVA_TEST_ARTIFACT_ID, "1.0" ).isEmpty() );