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