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