]> source.dussan.org Git - archiva.git/blob
26d2c369dac21eb8944fb6d29db37d096b36021d
[archiva.git] /
1 package org.apache.archiva.rest.services;
2 /*
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
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
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
18  * under the License.
19  */
20
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;
30
31 import java.util.Arrays;
32 import java.util.Collection;
33 import java.util.List;
34
35 import static org.assertj.core.api.Assertions.assertThat;
36
37 /**
38  * @author Olivier Lamy
39  */
40 public class SearchServiceTest
41     extends AbstractArchivaRestTest
42 {
43     private static final String TEST_REPO = "test-repo";
44
45     @Test
46     public void quickSearchOnArtifactId()
47         throws Exception
48     {
49         SearchService searchService = getSearchService( authorizationHeader );
50
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
56
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 );
61     }
62
63     /**
64      * same search but with Guest user
65      *
66      * @throws Exception
67      */
68     @Test
69     public void quickSearchOnArtifactIdGuest()
70         throws Exception
71     {
72         SearchService searchService = getSearchService( null );
73
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
79
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 );
84     }
85
86     @Test
87     public void searchArtifactVersions()
88         throws Exception
89     {
90         // START SNIPPET: searchservice-artifact-versions
91         SearchService searchService = getSearchService( authorizationHeader );
92
93         List<Artifact> artifacts = searchService.getArtifactVersions( "commons-logging", "commons-logging", "jar" );
94
95         // END SNIPPET: searchservice-artifact-versions
96
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 );
101
102         for ( Artifact artifact : artifacts )
103         {
104             log.info( "url: {}", artifact.getUrl() );
105             String version = artifact.getVersion();
106             assertTrue( artifact.getUrl().contains( version ) );
107
108
109         }
110     }
111
112     @Test
113     public void searchWithSearchRequestGroupIdAndArtifactIdAndClassifier()
114         throws Exception
115     {
116         SearchService searchService = getSearchService( authorizationHeader );
117
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" );
123
124         List<Artifact> artifacts = searchService.searchArtifacts( searchRequest );
125         // END SNIPPET: searchservice-with-classifier
126
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 );
131     }
132
133     @Test
134     public void searchWithSearchRequestBundleSymbolicNameOneVersion()
135         throws Exception
136     {
137         SearchService searchService = getSearchService( authorizationHeader );
138
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
143
144         List<Artifact> artifacts = searchService.searchArtifacts( searchRequest );
145
146         assertNotNull( artifacts );
147         assertTrue(
148             " not 1 results for Bundle Symbolic Name org.apache.karaf.features.command but " + artifacts.size() + ":"
149                 + artifacts, artifacts.size() == 1 );
150     }
151
152     @Test
153     public void searchWithSearchRequestBundleSymbolicNameTwoVersion()
154         throws Exception
155     {
156         UiConfiguration uiConfiguration = new UiConfiguration();
157         uiConfiguration.setApplicationUrl( null );
158         getArchivaAdministrationService().setUiConfiguration( uiConfiguration );
159
160         SearchService searchService = getSearchService( authorizationHeader );
161
162         SearchRequest searchRequest = new SearchRequest();
163         searchRequest.setBundleSymbolicName( "org.apache.karaf.features.core" );
164
165         List<Artifact> artifacts = searchService.searchArtifacts( searchRequest );
166
167         assertThat( artifacts ).isNotNull().hasSize( 2 );
168
169         for ( Artifact artifact : artifacts )
170         {
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" );
177
178
179         }
180     }
181
182     @Test
183     public void searchWithSearchRequestExportPackageOneVersion()
184         throws Exception
185     {
186         SearchService searchService = getSearchService( authorizationHeader );
187
188         SearchRequest searchRequest = new SearchRequest();
189         searchRequest.setBundleExportPackage( "org.apache.karaf.features.command.completers" );
190         searchRequest.setRepositories( Arrays.asList( TEST_REPO ) );
191
192         List<Artifact> artifacts = searchService.searchArtifacts( searchRequest );
193
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 );
197
198         log.info( "artifact url {}", artifacts.get( 0 ).getUrl() );
199     }
200
201     @Test
202     /**
203      * ensure we don't return response for an unknown repo
204      */ public void searchWithSearchUnknwownRepoId()
205         throws Exception
206     {
207         SearchService searchService = getSearchService( authorizationHeader );
208
209         SearchRequest searchRequest = new SearchRequest();
210         searchRequest.setBundleExportPackage( "org.apache.karaf.features.command.completers" );
211         searchRequest.setRepositories( Arrays.asList( "tototititata" ) );
212
213         List<Artifact> artifacts = searchService.searchArtifacts( searchRequest );
214
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 );
218     }
219
220     @Test
221     /**
222      * ensure we revert to all observable repos in case of no repo in the request
223      */ public void searchWithSearchNoRepos()
224         throws Exception
225     {
226         SearchService searchService = getSearchService( authorizationHeader );
227
228         SearchRequest searchRequest = new SearchRequest();
229         searchRequest.setBundleExportPackage( "org.apache.karaf.features.command.completers" );
230
231         List<Artifact> artifacts = searchService.searchArtifacts( searchRequest );
232
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 );
236
237         log.info( "artifact url {}", artifacts.get( 0 ).getUrl() );
238     }
239
240     @Test
241     public void getAllGroupIds()
242         throws Exception
243     {
244         SearchService searchService = getSearchService( authorizationHeader );
245
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" ) );
251     }
252
253     @Test
254     /**
255      * test we don't return 2 artifacts pom + zip one
256      */ public void getSearchArtifactsWithOnlyClassifier()
257         throws Exception
258     {
259         // force guest user creation if not exists
260         if ( getUserService( authorizationHeader ).getGuestUser() == null )
261         {
262             assertNotNull( getUserService( authorizationHeader ).createGuestUser() );
263         }
264
265         createAndIndexRepo( TEST_REPO, getProjectDirectory().resolve("src/test/repo-with-classifier-only") );
266
267         SearchService searchService = getSearchService( authorizationHeader );
268
269         SearchRequest searchRequest =
270             new SearchRequest( "org.foo", "studio-all-update-site", null, null, null, Arrays.asList( TEST_REPO ) );
271
272         List<Artifact> artifacts = searchService.searchArtifacts( searchRequest );
273         log.info( "artifacts: {}", artifacts );
274         assertEquals( 1, artifacts.size() );
275     }
276
277     /**
278      * sha1 commons-logging 1.1 ba24d5de831911b684c92cd289ed5ff826271824
279      */
280     @Test
281     public void search_with_sha1()
282         throws Exception
283     {
284         SearchService searchService = getSearchService( authorizationHeader );
285
286         List<Artifact> artifacts = searchService.getArtifactByChecksum(
287             new ChecksumSearch( null, "ba24d5de831911b684c92cd289ed5ff826271824" ) );
288
289         Assertions.assertThat( artifacts ).isNotNull().isNotEmpty().hasSize( 1 );
290
291     }
292
293
294     /**
295      * md5 commons-logging 1.1 6b62417e77b000a87de66ee3935edbf5
296      */
297     @Test
298     public void search_with_md5()
299         throws Exception
300     {
301         SearchService searchService = getSearchService( authorizationHeader );
302
303         List<Artifact> artifacts = searchService.getArtifactByChecksum(
304             new ChecksumSearch( null, "6b62417e77b000a87de66ee3935edbf5" ) );
305
306         Assertions.assertThat( artifacts ).isNotNull().isNotEmpty().hasSize( 1 );
307
308     }
309
310     @Before
311     public void createRepo()
312         throws Exception
313     {
314         // force guest user creation if not exists
315         if ( getUserService( authorizationHeader ).getGuestUser() == null )
316         {
317             assertNotNull( getUserService( authorizationHeader ).createGuestUser() );
318         }
319
320         createAndIndexRepo( TEST_REPO, getProjectDirectory( ).resolve( "src/test/repo-with-osgi" ) );
321
322         waitForScanToComplete( TEST_REPO );
323     }
324
325     @After
326     public void deleteRepo()
327         throws Exception
328     {
329         deleteTestRepo( TEST_REPO );
330     }
331
332 }
333
334