1 package org.apache.archiva.rest.services;
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
21 import org.apache.archiva.admin.model.beans.UiConfiguration;
22 import org.apache.archiva.maven2.model.Artifact;
23 import org.apache.archiva.rest.api.model.ChecksumSearch;
24 import org.apache.archiva.rest.api.model.SearchRequest;
25 import org.apache.archiva.rest.api.services.SearchService;
26 import org.assertj.core.api.Assertions;
27 import org.junit.After;
28 import org.junit.Before;
29 import org.junit.Test;
31 import java.util.Arrays;
32 import java.util.Collection;
33 import java.util.List;
35 import static org.assertj.core.api.Assertions.assertThat;
38 * @author Olivier Lamy
40 public class SearchServiceTest
41 extends AbstractArchivaRestTest
43 private static final String TEST_REPO = "test-repo";
46 public void quickSearchOnArtifactId()
49 SearchService searchService = getSearchService( authorizationHeader );
51 // START SNIPPET: quick-search
52 List<Artifact> artifacts = searchService.quickSearch( "commons-logging" );
53 // return all artifacts with groupId OR artifactId OR version OR packaging OR className
54 // NOTE : only artifacts with classifier empty are returned
55 // END SNIPPET: quick-search
57 assertNotNull( artifacts );
58 assertTrue( " not 6 results for commons-logging search but " + artifacts.size() + ":" + artifacts,
59 artifacts.size() == 6 );
60 log.info( "artifacts for commons-logging size {} search {}", artifacts.size(), artifacts );
64 * same search but with Guest user
69 public void quickSearchOnArtifactIdGuest()
72 SearchService searchService = getSearchService( null );
74 // START SNIPPET: quick-search
75 List<Artifact> artifacts = searchService.quickSearch( "commons-logging" );
76 // return all artifacts with groupId OR artifactId OR version OR packaging OR className
77 // NOTE : only artifacts with classifier empty are returned
78 // END SNIPPET: quick-search
80 assertNotNull( artifacts );
81 assertTrue( " not 6 results for commons-logging search but " + artifacts.size() + ":" + artifacts,
82 artifacts.size() == 6 );
83 log.info( "artifacts for commons-logging size {} search {}", artifacts.size(), artifacts );
87 public void searchArtifactVersions()
90 // START SNIPPET: searchservice-artifact-versions
91 SearchService searchService = getSearchService( authorizationHeader );
93 List<Artifact> artifacts = searchService.getArtifactVersions( "commons-logging", "commons-logging", "jar" );
95 // END SNIPPET: searchservice-artifact-versions
97 assertNotNull( artifacts );
98 assertTrue( " not 13 results for commons-logging search but " + artifacts.size() + ":" + artifacts,
99 artifacts.size() == 13 );
100 log.info( "artifacts for commons-logging size {} search {}", artifacts.size(), artifacts );
102 for ( Artifact artifact : artifacts )
104 log.info( "url: {}", artifact.getUrl() );
105 String version = artifact.getVersion();
106 assertTrue( artifact.getUrl().contains( version ) );
113 public void searchWithSearchRequestGroupIdAndArtifactIdAndClassifier()
116 SearchService searchService = getSearchService( authorizationHeader );
118 // START SNIPPET: searchservice-with-classifier
119 SearchRequest searchRequest = new SearchRequest();
120 searchRequest.setGroupId( "commons-logging" );
121 searchRequest.setArtifactId( "commons-logging" );
122 searchRequest.setClassifier( "sources" );
124 List<Artifact> artifacts = searchService.searchArtifacts( searchRequest );
125 // END SNIPPET: searchservice-with-classifier
127 assertNotNull( artifacts );
128 assertTrue( " not 2 results for commons-logging search but " + artifacts.size() + ":" + artifacts,
129 artifacts.size() == 2 );
130 log.info( "artifacts for commons-logging size {} search {}", artifacts.size(), artifacts );
134 public void searchWithSearchRequestBundleSymbolicNameOneVersion()
137 SearchService searchService = getSearchService( authorizationHeader );
139 // START SNIPPET: searchservice-with-osgi
140 SearchRequest searchRequest = new SearchRequest();
141 searchRequest.setBundleSymbolicName( "org.apache.karaf.features.command" );
142 // END SNIPPET: searchservice-with-osgi
144 List<Artifact> artifacts = searchService.searchArtifacts( searchRequest );
146 assertNotNull( artifacts );
148 " not 1 results for Bundle Symbolic Name org.apache.karaf.features.command but " + artifacts.size() + ":"
149 + artifacts, artifacts.size() == 1 );
153 public void searchWithSearchRequestBundleSymbolicNameTwoVersion()
156 UiConfiguration uiConfiguration = new UiConfiguration();
157 uiConfiguration.setApplicationUrl( null );
158 getArchivaAdministrationService().setUiConfiguration( uiConfiguration );
160 SearchService searchService = getSearchService( authorizationHeader );
162 SearchRequest searchRequest = new SearchRequest();
163 searchRequest.setBundleSymbolicName( "org.apache.karaf.features.core" );
165 List<Artifact> artifacts = searchService.searchArtifacts( searchRequest );
167 assertThat( artifacts ).isNotNull().hasSize( 2 );
169 for ( Artifact artifact : artifacts )
171 log.info( "url: {}", artifact.getUrl() );
172 String version = artifact.getVersion();
173 Assertions.assertThat( artifact.getUrl() ) //
174 .isEqualTo( "http://localhost:" + getServerPort()
175 + "/repository/test-repo/org/apache/karaf/features/org.apache.karaf.features.core/"
176 + version + "/org.apache.karaf.features.core-" + version + ".jar" );
183 public void searchWithSearchRequestExportPackageOneVersion()
186 SearchService searchService = getSearchService( authorizationHeader );
188 SearchRequest searchRequest = new SearchRequest();
189 searchRequest.setBundleExportPackage( "org.apache.karaf.features.command.completers" );
190 searchRequest.setRepositories( Arrays.asList( TEST_REPO ) );
192 List<Artifact> artifacts = searchService.searchArtifacts( searchRequest );
194 assertNotNull( artifacts );
195 assertTrue( " not 1 results for Bundle ExportPackage org.apache.karaf.features.command.completers but "
196 + artifacts.size() + ":" + artifacts, artifacts.size() == 1 );
198 log.info( "artifact url {}", artifacts.get( 0 ).getUrl() );
203 * ensure we don't return response for an unknown repo
204 */ public void searchWithSearchUnknwownRepoId()
207 SearchService searchService = getSearchService( authorizationHeader );
209 SearchRequest searchRequest = new SearchRequest();
210 searchRequest.setBundleExportPackage( "org.apache.karaf.features.command.completers" );
211 searchRequest.setRepositories( Arrays.asList( "tototititata" ) );
213 List<Artifact> artifacts = searchService.searchArtifacts( searchRequest );
215 assertNotNull( artifacts );
216 assertTrue( " not 0 results for Bundle ExportPackage org.apache.karaf.features.command.completers but " +
217 artifacts.size() + ":" + artifacts, artifacts.size() == 0 );
222 * ensure we revert to all observable repos in case of no repo in the request
223 */ public void searchWithSearchNoRepos()
226 SearchService searchService = getSearchService( authorizationHeader );
228 SearchRequest searchRequest = new SearchRequest();
229 searchRequest.setBundleExportPackage( "org.apache.karaf.features.command.completers" );
231 List<Artifact> artifacts = searchService.searchArtifacts( searchRequest );
233 assertNotNull( artifacts );
234 assertTrue( " not 0 results for Bundle ExportPackage org.apache.karaf.features.command.completers but "
235 + artifacts.size() + ":" + artifacts, artifacts.size() == 1 );
237 log.info( "artifact url {}", artifacts.get( 0 ).getUrl() );
241 public void getAllGroupIds()
244 SearchService searchService = getSearchService( authorizationHeader );
246 Collection<String> groupIds = searchService.getAllGroupIds( Arrays.asList( TEST_REPO ) ).getGroupIds();
247 log.info( "groupIds {}", groupIds );
248 assertFalse( groupIds.isEmpty() );
249 assertTrue( groupIds.contains( "commons-cli" ) );
250 assertTrue( groupIds.contains( "org.apache.felix" ) );
255 * test we don't return 2 artifacts pom + zip one
256 */ public void getSearchArtifactsWithOnlyClassifier()
259 // force guest user creation if not exists
260 if ( getUserService( authorizationHeader ).getGuestUser() == null )
262 assertNotNull( getUserService( authorizationHeader ).createGuestUser() );
265 createAndIndexRepo( TEST_REPO, getProjectDirectory().resolve("src/test/repo-with-classifier-only") );
267 SearchService searchService = getSearchService( authorizationHeader );
269 SearchRequest searchRequest =
270 new SearchRequest( "org.foo", "studio-all-update-site", null, null, null, Arrays.asList( TEST_REPO ) );
272 List<Artifact> artifacts = searchService.searchArtifacts( searchRequest );
273 log.info( "artifacts: {}", artifacts );
274 assertEquals( 1, artifacts.size() );
278 * sha1 commons-logging 1.1 ba24d5de831911b684c92cd289ed5ff826271824
281 public void search_with_sha1()
284 SearchService searchService = getSearchService( authorizationHeader );
286 List<Artifact> artifacts = searchService.getArtifactByChecksum(
287 new ChecksumSearch( null, "ba24d5de831911b684c92cd289ed5ff826271824" ) );
289 Assertions.assertThat( artifacts ).isNotNull().isNotEmpty().hasSize( 1 );
295 * md5 commons-logging 1.1 6b62417e77b000a87de66ee3935edbf5
298 public void search_with_md5()
301 SearchService searchService = getSearchService( authorizationHeader );
303 List<Artifact> artifacts = searchService.getArtifactByChecksum(
304 new ChecksumSearch( null, "6b62417e77b000a87de66ee3935edbf5" ) );
306 Assertions.assertThat( artifacts ).isNotNull().isNotEmpty().hasSize( 1 );
311 public void createRepo()
314 // force guest user creation if not exists
315 if ( getUserService( authorizationHeader ).getGuestUser() == null )
317 assertNotNull( getUserService( authorizationHeader ).createGuestUser() );
320 createAndIndexRepo( TEST_REPO, getProjectDirectory( ).resolve( "src/test/repo-with-osgi" ) );
322 waitForScanToComplete( TEST_REPO );
326 public void deleteRepo()
329 deleteTestRepo( TEST_REPO );