]> source.dussan.org Git - archiva.git/blob
147d5c56ec3bbdcddd8819ace394d045f62c57f0
[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         try
340         {
341             String testRepoId = "test-repo";
342             // force guest user creation if not exists
343             if ( getUserService( authorizationHeader ).getGuestUser() == null )
344             {
345                 assertNotNull( getUserService( authorizationHeader ).createGuestUser() );
346             }
347
348             createAndIndexRepo( testRepoId, new File( getBasedir(), "src/test/repo-with-osgi" ).getAbsolutePath(),
349                                 false );
350
351             BrowseService browseService = getBrowseService( authorizationHeader, true );
352
353             List<Artifact> artifactDownloadInfos =
354                 browseService.getArtifactDownloadInfos( "commons-logging", "commons-logging", "1.1", testRepoId );
355
356             log.info( "artifactDownloadInfos {}", artifactDownloadInfos );
357             assertThat( artifactDownloadInfos ).isNotNull().isNotEmpty().hasSize( 3 );
358             deleteTestRepo( testRepoId );
359         }
360         catch ( Exception e )
361         {
362             log.error( e.getMessage(), e );
363             throw e;
364         }
365     }
366
367
368     @Test
369     public void readArtifactContentText()
370         throws Exception
371     {
372         String testRepoId = "test-repo";
373         // force guest user creation if not exists
374         if ( getUserService( authorizationHeader ).getGuestUser() == null )
375         {
376             assertNotNull( getUserService( authorizationHeader ).createGuestUser() );
377         }
378
379         createAndIndexRepo( testRepoId, new File( getBasedir(), "src/test/repo-with-osgi" ).getAbsolutePath(), false );
380
381         BrowseService browseService = getBrowseService( authorizationHeader, true );
382
383         WebClient.client( browseService ).accept( MediaType.TEXT_PLAIN );
384
385         try
386         {
387             String text =
388                 browseService.getArtifactContentText( "commons-logging", "commons-logging", "1.1", "sources", null,
389                                                       "org/apache/commons/logging/LogSource.java",
390                                                       testRepoId ).getContent();
391
392             log.debug( "text: {}", text );
393
394             assertThat( text ).contains( "package org.apache.commons.logging;" ).contains( "public class LogSource {" );
395         }
396         catch ( Exception e )
397         {
398             log.error( e.getMessage(), e );
399             throw e;
400         }
401     }
402
403
404     @Test
405     public void readArtifactContentTextPom()
406         throws Exception
407     {
408         String testRepoId = "test-repo";
409         // force guest user creation if not exists
410         if ( getUserService( authorizationHeader ).getGuestUser() == null )
411         {
412             assertNotNull( getUserService( authorizationHeader ).createGuestUser() );
413         }
414
415         createAndIndexRepo( testRepoId, new File( getBasedir(), "src/test/repo-with-osgi" ).getAbsolutePath(), false );
416
417         BrowseService browseService = getBrowseService( authorizationHeader, true );
418
419         WebClient.client( browseService ).accept( MediaType.TEXT_PLAIN );
420
421         try
422         {
423             String text =
424                 browseService.getArtifactContentText( "commons-logging", "commons-logging", "1.1", null, "pom", null,
425                                                       testRepoId ).getContent();
426
427             log.info( "text: {}", text );
428
429             assertThat( text ).contains(
430                 "<url>http://jakarta.apache.org/commons/${pom.artifactId.substring(8)}/</url>" ).contains(
431                 "<subscribe>commons-dev-subscribe@jakarta.apache.org</subscribe>" );
432         }
433         catch ( Exception e )
434         {
435             log.error( e.getMessage(), e );
436             throw e;
437         }
438     }
439
440
441     @Test
442     public void artifactsNumber()
443         throws Exception
444     {
445         String testRepoId = "test-repo";
446         // force guest user creation if not exists
447         if ( getUserService( authorizationHeader ).getGuestUser() == null )
448         {
449             assertNotNull( getUserService( authorizationHeader ).createGuestUser() );
450         }
451
452         createAndIndexRepo( testRepoId, new File( getBasedir(), "src/test/repo-with-osgi" ).getAbsolutePath(), false );
453
454         BrowseService browseService = getBrowseService( authorizationHeader, true );
455
456         //WebClient.client( browseService ).accept( MediaType.TEXT_PLAIN );
457
458         try
459         {
460             int number = browseService.getArtifacts( testRepoId ).size();
461
462             log.info( "getArtifactsNumber: {}", number );
463
464             assertTrue( number > 1 );
465         }
466         catch ( Exception e )
467         {
468             log.error( e.getMessage(), e );
469             throw e;
470         }
471     }
472
473     @Test
474     public void metadatainbatchmode()
475         throws Exception
476     {
477
478         String testRepoId = "test-repo";
479         // force guest user creation if not exists
480         if ( getUserService( authorizationHeader ).getGuestUser() == null )
481         {
482             assertNotNull( getUserService( authorizationHeader ).createGuestUser() );
483         }
484
485         createAndIndexRepo( testRepoId, new File( getBasedir(), "src/test/repo-with-osgi" ).getAbsolutePath() );
486
487         BrowseService browseService = getBrowseService( authorizationHeader, false );
488
489         Map<String, String> inputMetadata = new HashMap<String, String>( 3 );
490         inputMetadata.put( "buildNumber", "1" );
491         inputMetadata.put( "author", "alecharp" );
492         inputMetadata.put( "jenkins_version", "1.486" );
493
494         MetadataAddRequest metadataAddRequest = new MetadataAddRequest();
495         metadataAddRequest.setGroupId( "commons-cli" );
496         metadataAddRequest.setArtifactId( "commons-cli" );
497         metadataAddRequest.setVersion( "1.0" );
498         metadataAddRequest.setMetadatas( inputMetadata );
499         browseService.importMetadata( metadataAddRequest, testRepoId );
500
501         Map<String, String> metadatas =
502             toMap( browseService.getMetadatas( "commons-cli", "commons-cli", "1.0", testRepoId ) );
503
504         assertThat( metadatas ).isNotNull().isNotEmpty().contains( MapEntry.entry( "buildNumber", "1" ) ).contains(
505             MapEntry.entry( "author", "alecharp" ) ).contains( MapEntry.entry( "jenkins_version", "1.486" ) );
506
507         deleteTestRepo( testRepoId );
508     }
509
510 }