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