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