]> source.dussan.org Git - archiva.git/blob
848d4b7a62c23f18c993acadbbf017baee15a0e7
[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.maven2.model.Artifact;
22 import org.apache.archiva.metadata.model.ProjectVersionMetadata;
23 import org.apache.archiva.redback.rest.api.services.RedbackServiceException;
24 import org.apache.archiva.rest.api.model.ArtifactContentEntry;
25 import org.apache.archiva.rest.api.model.BrowseResult;
26 import org.apache.archiva.rest.api.model.BrowseResultEntry;
27 import org.apache.archiva.rest.api.model.Entry;
28 import org.apache.archiva.rest.api.model.MetadataAddRequest;
29 import org.apache.archiva.rest.api.model.VersionsList;
30 import org.apache.archiva.rest.api.services.ArchivaRestServiceException;
31 import org.apache.archiva.rest.api.services.BrowseService;
32 import org.apache.cxf.jaxrs.client.WebClient;
33 import org.assertj.core.data.MapEntry;
34 import org.junit.After;
35 import org.junit.Before;
36 import org.junit.Test;
37
38 import javax.ws.rs.core.MediaType;
39 import java.io.File;
40 import java.io.IOException;
41 import java.util.HashMap;
42 import java.util.List;
43 import java.util.Map;
44
45 import static org.assertj.core.api.Assertions.assertThat;
46
47 /**
48  * @author Olivier Lamy
49  */
50 public class BrowseServiceTest
51     extends AbstractArchivaRestTest
52 {
53     private static final String TEST_REPO_ID = "test-repo";
54
55     Map<String, String> toMap( List<Entry> entries )
56     {
57         Map<String, String> map = new HashMap<>( entries.size() );
58
59         for ( Entry entry : entries )
60         {
61             map.put( entry.getKey(), entry.getValue() );
62         }
63
64         return map;
65     }
66
67     @Test
68     public void metadatagetthenadd()
69         throws Exception
70     {
71         scanRepo( TEST_REPO_ID );
72
73         BrowseService browseService = getBrowseService( authorizationHeader, false );
74
75         Map<String, String> metadatas =
76             toMap( browseService.getMetadatas( "commons-cli", "commons-cli", "1.0", TEST_REPO_ID ) );
77
78         assertThat( metadatas ).isNotNull().isEmpty();
79
80         browseService.addMetadata( "commons-cli", "commons-cli", "1.0", "wine", "bordeaux", TEST_REPO_ID );
81
82         metadatas = toMap( browseService.getMetadatas( "commons-cli", "commons-cli", "1.0", TEST_REPO_ID ) );
83
84         assertThat( metadatas ).isNotNull().isNotEmpty().contains( MapEntry.entry( "wine", "bordeaux" ) );
85     }
86
87
88     @Test
89     public void metadatagetthenaddthendelete()
90         throws Exception
91     {
92         try
93         {
94             scanRepo( TEST_REPO_ID );
95
96             BrowseService browseService = getBrowseService( authorizationHeader, false );
97
98             Map<String, String> metadatas =
99                 toMap( browseService.getMetadatas( "commons-cli", "commons-cli", "1.0", TEST_REPO_ID ) );
100
101             assertThat( metadatas ).isNotNull().isEmpty();
102
103             browseService.addMetadata( "commons-cli", "commons-cli", "1.0", "wine", "bordeaux", TEST_REPO_ID );
104
105             metadatas = toMap( browseService.getMetadatas( "commons-cli", "commons-cli", "1.0", TEST_REPO_ID ) );
106
107             assertThat( metadatas ).isNotNull().isNotEmpty().contains( MapEntry.entry( "wine", "bordeaux" ) );
108
109             browseService.deleteMetadata( "commons-cli", "commons-cli", "1.0", "wine", TEST_REPO_ID );
110
111             metadatas = toMap( browseService.getMetadatas( "commons-cli", "commons-cli", "1.0", TEST_REPO_ID ) );
112
113             assertThat( metadatas ).isNotNull().isEmpty();
114         }
115         catch ( ArchivaRestServiceException e )
116         {
117             log.error( e.getMessage(), e );
118             throw e;
119         }
120     }
121
122     @Test
123     public void browserootGroups()
124         throws Exception
125     {
126         BrowseService browseService = getBrowseService( authorizationHeader, false );
127
128         BrowseResult browseResult = browseService.getRootGroups( TEST_REPO_ID );
129         assertThat( browseResult ).isNotNull();
130         assertThat( browseResult.getBrowseResultEntries() ) //
131             .isNotNull() //
132             .isNotEmpty() //
133             .hasSize( 3 ) //
134             .contains( new BrowseResultEntry( "commons-cli", false ), //
135                        new BrowseResultEntry( "commons-logging", false ), //
136                        new BrowseResultEntry( "org.apache", false ) );
137     }
138
139     @Test
140     public void browsegroupId()
141         throws Exception
142     {
143         BrowseService browseService = getBrowseService( authorizationHeader, false );
144
145         BrowseResult browseResult = browseService.browseGroupId( "org.apache", TEST_REPO_ID );
146         assertThat( browseResult ).isNotNull();
147         assertThat( browseResult.getBrowseResultEntries() ) //
148             .isNotNull() //
149             .isNotEmpty() //
150             .hasSize( 2 ) //
151             .contains( new BrowseResultEntry( "org.apache.felix", false ), //
152                        new BrowseResultEntry( "org.apache.karaf.features", false ) );
153     }
154
155
156     @Test
157     public void browsegroupIdWithReleaseStartNumber()
158         throws Exception
159     {
160         BrowseService browseService = getBrowseService( authorizationHeader, false );
161         BrowseResult browseResult = browseService.browseGroupId( "commons-logging.commons-logging", TEST_REPO_ID );
162         log.info( "browseResult: {}", browseResult );
163     }
164
165     @Test
166     public void versionsList()
167         throws Exception
168     {
169         BrowseService browseService = getBrowseService( authorizationHeader, false );
170
171         VersionsList versions =
172             browseService.getVersionsList( "org.apache.karaf.features", "org.apache.karaf.features.core",
173                                            TEST_REPO_ID );
174         assertThat( versions ).isNotNull();
175         assertThat( versions.getVersions() ) //
176             .isNotNull() //
177             .isNotEmpty() //
178             .hasSize( 2 ) //
179             .contains( "2.2.1", "2.2.2" );
180     }
181
182     @Test
183     public void getProjectVersionMetadata()
184         throws Exception
185     {
186         BrowseService browseService = getBrowseService( authorizationHeader, true );
187
188         ProjectVersionMetadata metadata =
189             browseService.getProjectVersionMetadata( "org.apache.karaf.features", "org.apache.karaf.features.core",
190                                                      TEST_REPO_ID );
191
192         assertThat( metadata ).isNotNull();
193     }
194
195     @Test
196     public void readArtifactContentEntries()
197         throws Exception
198     {
199         BrowseService browseService = getBrowseService( authorizationHeader, true );
200
201         List<ArtifactContentEntry> artifactContentEntries =
202             browseService.getArtifactContentEntries( "commons-logging", "commons-logging", "1.1", null, null, null,
203                                                      TEST_REPO_ID );
204
205         log.info( "artifactContentEntries: {}", artifactContentEntries );
206
207         assertThat( artifactContentEntries ).isNotNull() //
208             .isNotEmpty() //
209             .hasSize( 2 ) //
210             .contains( new ArtifactContentEntry( "org", false, 0, TEST_REPO_ID ), //
211                        new ArtifactContentEntry( "META-INF", false, 0, TEST_REPO_ID ) );
212     }
213
214     @Test
215     public void readArtifactContentEntriesRootPath()
216         throws Exception
217     {
218         BrowseService browseService = getBrowseService( authorizationHeader, true );
219
220         List<ArtifactContentEntry> artifactContentEntries =
221             browseService.getArtifactContentEntries( "commons-logging", "commons-logging", "1.1", null, null, "org/",
222                                                      TEST_REPO_ID );
223
224         log.info( "artifactContentEntries: {}", artifactContentEntries );
225
226         assertThat( artifactContentEntries ).isNotNull() //
227             .isNotEmpty() //
228             .hasSize( 1 ) //
229             .contains( new ArtifactContentEntry( "org/apache", false, 1, TEST_REPO_ID ) );
230     }
231
232     @Test
233     public void readArtifactContentEntriesFilesAndDirectories()
234         throws Exception
235     {
236         BrowseService browseService = getBrowseService( authorizationHeader, true );
237
238         List<ArtifactContentEntry> artifactContentEntries =
239             browseService.getArtifactContentEntries( "commons-logging", "commons-logging", "1.1", null, null,
240                                                      "org/apache/commons/logging/", TEST_REPO_ID );
241
242         log.info( "artifactContentEntries: {}", artifactContentEntries );
243
244         assertThat( artifactContentEntries ).isNotNull().isNotEmpty().hasSize( 10 ).contains(
245             new ArtifactContentEntry( "org/apache/commons/logging/impl", false, 4, TEST_REPO_ID ),
246             new ArtifactContentEntry( "org/apache/commons/logging/LogSource.class", true, 4, TEST_REPO_ID ) );
247     }
248
249     @Test
250     public void getArtifactDownloadInfos()
251         throws Exception
252     {
253         BrowseService browseService = getBrowseService( authorizationHeader, true );
254
255         List<Artifact> artifactDownloadInfos =
256             browseService.getArtifactDownloadInfos( "commons-logging", "commons-logging", "1.1", TEST_REPO_ID );
257
258         log.info( "artifactDownloadInfos {}", artifactDownloadInfos );
259         assertThat( artifactDownloadInfos ).isNotNull().isNotEmpty().hasSize( 3 );
260     }
261
262
263     @Test
264     public void getArtifactsByMetadata()
265         throws Exception
266     {
267         // START SNIPPET: get-artifacts-by-metadata
268         BrowseService browseService = getBrowseService( authorizationHeader, true );
269
270         List<Artifact> artifactDownloadInfos = browseService.getArtifactsByMetadata( "type", "pom", TEST_REPO_ID );
271
272         assertThat( artifactDownloadInfos ).isNotNull().isNotEmpty().hasSize( 11 );
273         // END SNIPPET: get-artifacts-by-metadata
274     }
275
276
277     @Test
278     public void getArtifactsByProjectVersionMetadata()
279         throws Exception
280     {
281         // START SNIPPET: get-artifacts-by-project-version-metadata
282         BrowseService browseService = getBrowseService( authorizationHeader, true );
283
284         browseService.addMetadata( "commons-cli", "commons-cli", "1.0", "wine", "bordeaux", TEST_REPO_ID );
285
286         List<Artifact> artifactDownloadInfos =
287             browseService.getArtifactsByProjectVersionMetadata( "wine", "bordeaux", TEST_REPO_ID );
288
289         assertThat( artifactDownloadInfos ).isNotNull().isNotEmpty().hasSize( 3 );
290         // END SNIPPET: get-artifacts-by-project-version-metadata
291     }
292
293
294     @Test
295     public void getArtifactsByProjectVersionMetadataWithNoRepository()
296         throws Exception
297     {
298         BrowseService browseService = getBrowseService( authorizationHeader, true );
299
300         browseService.addMetadata( "commons-cli", "commons-cli", "1.0", "wine", "bordeaux", TEST_REPO_ID );
301
302         List<Artifact> artifactDownloadInfos =
303             browseService.getArtifactsByProjectVersionMetadata( "wine", "bordeaux", null );
304
305         assertThat( artifactDownloadInfos ).isNotNull().isNotEmpty().hasSize( 3 );
306     }
307
308
309     @Test
310     public void getArtifactsByProperty()
311         throws Exception
312     {
313         // START SNIPPET: get-artifacts-by-property
314         BrowseService browseService = getBrowseService( authorizationHeader, true );
315
316         List<Artifact> artifactDownloadInfos =
317             browseService.getArtifactsByProperty( "org.name", "The Apache Software Foundation", TEST_REPO_ID );
318
319         assertThat( artifactDownloadInfos ).isNotNull().isNotEmpty().hasSize( 7 );
320         // END SNIPPET: get-artifacts-by-property
321     }
322
323
324     @Test
325     public void searchArtifacts()
326         throws Exception
327     {
328         // START SNIPPET: search-artifacts
329         BrowseService browseService = getBrowseService( authorizationHeader, true );
330
331         List<Artifact> artifactDownloadInfos =
332             browseService.searchArtifacts( "The Apache Software Foundation", TEST_REPO_ID, true );
333
334         assertThat( artifactDownloadInfos ).isNotNull().isNotEmpty().hasSize( 7 );
335         // END SNIPPET: search-artifacts
336     }
337
338
339     @Test
340     public void searchArtifactsByField()
341         throws Exception
342     {
343         // START SNIPPET: search-artifacts-by-field
344         BrowseService browseService = getBrowseService( authorizationHeader, true );
345
346         List<Artifact> artifactDownloadInfos =
347             browseService.searchArtifacts( "org.name", "The Apache Software Foundation", TEST_REPO_ID, true );
348
349         assertThat( artifactDownloadInfos ).isNotNull().isNotEmpty().hasSize( 7 );
350         // END SNIPPET: search-artifacts-by-field
351     }
352
353
354     @Test
355     public void readArtifactContentText()
356         throws Exception
357     {
358         BrowseService browseService = getBrowseService( authorizationHeader, true );
359
360         WebClient.client( browseService ).accept( MediaType.TEXT_PLAIN );
361
362         String text =
363             browseService.getArtifactContentText( "commons-logging", "commons-logging", "1.1", "sources", null,
364                                                   "org/apache/commons/logging/LogSource.java",
365                                                   TEST_REPO_ID ).getContent();
366
367         log.debug( "text: {}", text );
368
369         assertThat( text ).contains( "package org.apache.commons.logging;" ).contains( "public class LogSource {" );
370     }
371
372
373     @Test
374     public void readArtifactContentTextPom()
375         throws Exception
376     {
377         BrowseService browseService = getBrowseService( authorizationHeader, true );
378
379         WebClient.client( browseService ).accept( MediaType.TEXT_PLAIN );
380
381         String text =
382             browseService.getArtifactContentText( "commons-logging", "commons-logging", "1.1", null, "pom", null,
383                                                   TEST_REPO_ID ).getContent();
384
385         log.info( "text: {}", text );
386
387         assertThat( text ).contains(
388             "<url>http://jakarta.apache.org/commons/${pom.artifactId.substring(8)}/</url>" ).contains(
389             "<subscribe>commons-dev-subscribe@jakarta.apache.org</subscribe>" );
390     }
391
392
393     @Test
394     public void artifactsNumber()
395         throws Exception
396     {
397         BrowseService browseService = getBrowseService( authorizationHeader, true );
398
399         //WebClient.client( browseService ).accept( MediaType.TEXT_PLAIN );
400
401         int number = browseService.getArtifacts( TEST_REPO_ID ).size();
402
403         log.info( "getArtifactsNumber: {}", number );
404
405         assertTrue( number > 1 );
406     }
407
408     @Test
409     public void metadatainbatchmode()
410         throws Exception
411     {
412         scanRepo( TEST_REPO_ID );
413
414         BrowseService browseService = getBrowseService( authorizationHeader, false );
415
416         Map<String, String> inputMetadata = new HashMap<>( 3 );
417         inputMetadata.put( "buildNumber", "1" );
418         inputMetadata.put( "author", "alecharp" );
419         inputMetadata.put( "jenkins_version", "1.486" );
420
421         MetadataAddRequest metadataAddRequest = new MetadataAddRequest();
422         metadataAddRequest.setGroupId( "commons-cli" );
423         metadataAddRequest.setArtifactId( "commons-cli" );
424         metadataAddRequest.setVersion( "1.0" );
425         metadataAddRequest.setMetadatas( inputMetadata );
426         browseService.importMetadata( metadataAddRequest, TEST_REPO_ID );
427
428         Map<String, String> metadatas =
429             toMap( browseService.getMetadatas( "commons-cli", "commons-cli", "1.0", TEST_REPO_ID ) );
430
431         assertThat( metadatas ).isNotNull().isNotEmpty().contains( MapEntry.entry( "buildNumber", "1" ) ).contains(
432             MapEntry.entry( "author", "alecharp" ) ).contains( MapEntry.entry( "jenkins_version", "1.486" ) );
433     }
434
435     @Before
436     public void initialiseTestRepo()
437         throws RedbackServiceException, ArchivaRestServiceException, IOException, InterruptedException
438     {
439         // force guest user creation if not exists
440         if ( getUserService( authorizationHeader ).getGuestUser() == null )
441         {
442             assertNotNull( getUserService( authorizationHeader ).createGuestUser() );
443         }
444
445         createAndIndexRepo( TEST_REPO_ID, new File( getBasedir(), "src/test/repo-with-osgi" ).getAbsolutePath(),
446                             false );
447
448         waitForScanToComplete( TEST_REPO_ID );
449     }
450
451     @After
452     public void deleteTestRepo()
453         throws Exception
454     {
455         scanRepo( TEST_REPO_ID );
456         waitForScanToComplete( TEST_REPO_ID );
457         deleteTestRepo( TEST_REPO_ID );
458     }
459 }