1 package org.apache.archiva.rest.services;
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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
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;
34 import javax.ws.rs.core.MediaType;
36 import java.util.HashMap;
37 import java.util.List;
40 import static org.fest.assertions.api.Assertions.assertThat;
43 * @author Olivier Lamy
45 public class BrowseServiceTest
46 extends AbstractArchivaRestTest
49 Map<String, String> toMap( List<Entry> entries )
51 Map<String, String> map = new HashMap<String, String>( entries.size() );
53 for ( Entry entry : entries )
55 map.put( entry.getKey(), entry.getValue() );
63 public void metadatagetthenadd()
67 String testRepoId = "test-repo";
68 // force guest user creation if not exists
69 if ( getUserService( authorizationHeader ).getGuestUser() == null )
71 assertNotNull( getUserService( authorizationHeader ).createGuestUser() );
74 createAndIndexRepo( testRepoId, new File( getBasedir(), "src/test/repo-with-osgi" ).getAbsolutePath() );
76 BrowseService browseService = getBrowseService( authorizationHeader, false );
78 Map<String, String> metadatas =
79 toMap( browseService.getMetadatas( "commons-cli", "commons-cli", "1.0", testRepoId ) );
81 assertThat( metadatas ).isNotNull().isEmpty();
83 browseService.addMetadata( "commons-cli", "commons-cli", "1.0", "wine", "bordeaux", testRepoId );
85 metadatas = toMap( browseService.getMetadatas( "commons-cli", "commons-cli", "1.0", testRepoId ) );
87 assertThat( metadatas ).isNotNull().isNotEmpty().contains( MapEntry.entry( "wine", "bordeaux" ) );
89 deleteTestRepo( testRepoId );
95 public void metadatagetthenaddthendelete()
99 String testRepoId = "test-repo";
100 // force guest user creation if not exists
101 if ( getUserService( authorizationHeader ).getGuestUser() == null )
103 assertNotNull( getUserService( authorizationHeader ).createGuestUser() );
106 createAndIndexRepo( testRepoId, new File( getBasedir(), "src/test/repo-with-osgi" ).getAbsolutePath() );
108 BrowseService browseService = getBrowseService( authorizationHeader, false );
110 Map<String, String> metadatas =
111 toMap( browseService.getMetadatas( "commons-cli", "commons-cli", "1.0", testRepoId ) );
113 assertThat( metadatas ).isNotNull().isEmpty();
115 browseService.addMetadata( "commons-cli", "commons-cli", "1.0", "wine", "bordeaux", testRepoId );
117 metadatas = toMap( browseService.getMetadatas( "commons-cli", "commons-cli", "1.0", testRepoId ) );
119 assertThat( metadatas ).isNotNull().isNotEmpty().contains( MapEntry.entry( "wine", "bordeaux" ) );
121 browseService.deleteMetadata( "commons-cli", "commons-cli", "1.0", "wine", testRepoId );
123 metadatas = toMap( browseService.getMetadatas( "commons-cli", "commons-cli", "1.0", testRepoId ) );
125 assertThat( metadatas ).isNotNull().isEmpty();
127 deleteTestRepo( testRepoId );
132 public void browserootGroups()
136 String testRepoId = "test-repo";
137 // force guest user creation if not exists
138 if ( getUserService( authorizationHeader ).getGuestUser() == null )
140 assertNotNull( getUserService( authorizationHeader ).createGuestUser() );
143 createAndIndexRepo( testRepoId, new File( getBasedir(), "src/test/repo-with-osgi" ).getAbsolutePath(), false );
145 BrowseService browseService = getBrowseService( authorizationHeader, false );
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 ) );
153 deleteTestRepo( testRepoId );
158 public void browsegroupId()
162 String testRepoId = "test-repo";
163 // force guest user creation if not exists
164 if ( getUserService( authorizationHeader ).getGuestUser() == null )
166 assertNotNull( getUserService( authorizationHeader ).createGuestUser() );
169 createAndIndexRepo( testRepoId, new File( getBasedir(), "src/test/repo-with-osgi" ).getAbsolutePath(), false );
171 BrowseService browseService = getBrowseService( authorizationHeader, false );
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 ) );
179 deleteTestRepo( testRepoId );
185 public void browsegroupIdWithReleaseStartNumber()
189 String testRepoId = "test-repo";
190 // force guest user creation if not exists
191 if ( getUserService( authorizationHeader ).getGuestUser() == null )
193 assertNotNull( getUserService( authorizationHeader ).createGuestUser() );
196 createAndIndexRepo( testRepoId, new File( getBasedir(), "src/test/repo-with-osgi" ).getAbsolutePath(), false );
198 BrowseService browseService = getBrowseService( authorizationHeader, false );
199 BrowseResult browseResult = browseService.browseGroupId( "commons-logging.commons-logging", testRepoId );
200 log.info( "browseResult: {}", browseResult );
202 deleteTestRepo( testRepoId );
207 public void versionsList()
211 String testRepoId = "test-repo";
212 // force guest user creation if not exists
213 if ( getUserService( authorizationHeader ).getGuestUser() == null )
215 assertNotNull( getUserService( authorizationHeader ).createGuestUser() );
218 createAndIndexRepo( testRepoId, new File( getBasedir(), "src/test/repo-with-osgi" ).getAbsolutePath(), false );
220 BrowseService browseService = getBrowseService( authorizationHeader, false );
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" );
227 deleteTestRepo( testRepoId );
232 public void getProjectVersionMetadata()
235 String testRepoId = "test-repo";
236 // force guest user creation if not exists
237 if ( getUserService( authorizationHeader ).getGuestUser() == null )
239 assertNotNull( getUserService( authorizationHeader ).createGuestUser() );
242 createAndIndexRepo( testRepoId, new File( getBasedir(), "src/test/repo-with-osgi" ).getAbsolutePath(), false );
244 BrowseService browseService = getBrowseService( authorizationHeader, true );
246 ProjectVersionMetadata metadata =
247 browseService.getProjectVersionMetadata( "org.apache.karaf.features", "org.apache.karaf.features.core",
250 assertThat( metadata ).isNotNull();
252 deleteTestRepo( testRepoId );
256 public void readArtifactContentEntries()
259 String testRepoId = "test-repo";
260 // force guest user creation if not exists
261 if ( getUserService( authorizationHeader ).getGuestUser() == null )
263 assertNotNull( getUserService( authorizationHeader ).createGuestUser() );
266 createAndIndexRepo( testRepoId, new File( getBasedir(), "src/test/repo-with-osgi" ).getAbsolutePath(), false );
268 BrowseService browseService = getBrowseService( authorizationHeader, true );
270 List<ArtifactContentEntry> artifactContentEntries =
271 browseService.getArtifactContentEntries( "commons-logging", "commons-logging", "1.1", null, null, null,
274 log.info( "artifactContentEntries: {}", artifactContentEntries );
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 );
283 public void readArtifactContentEntriesRootPath()
286 String testRepoId = "test-repo";
287 // force guest user creation if not exists
288 if ( getUserService( authorizationHeader ).getGuestUser() == null )
290 assertNotNull( getUserService( authorizationHeader ).createGuestUser() );
293 createAndIndexRepo( testRepoId, new File( getBasedir(), "src/test/repo-with-osgi" ).getAbsolutePath(), false );
295 BrowseService browseService = getBrowseService( authorizationHeader, true );
297 List<ArtifactContentEntry> artifactContentEntries =
298 browseService.getArtifactContentEntries( "commons-logging", "commons-logging", "1.1", null, null, "org/",
301 log.info( "artifactContentEntries: {}", artifactContentEntries );
303 assertThat( artifactContentEntries ).isNotNull().isNotEmpty().hasSize( 1 ).contains(
304 new ArtifactContentEntry( "org/apache", false, 1, testRepoId ) );
305 deleteTestRepo( testRepoId );
309 public void readArtifactContentEntriesFilesAndDirectories()
312 String testRepoId = "test-repo";
313 // force guest user creation if not exists
314 if ( getUserService( authorizationHeader ).getGuestUser() == null )
316 assertNotNull( getUserService( authorizationHeader ).createGuestUser() );
319 createAndIndexRepo( testRepoId, new File( getBasedir(), "src/test/repo-with-osgi" ).getAbsolutePath(), false );
321 BrowseService browseService = getBrowseService( authorizationHeader, true );
323 List<ArtifactContentEntry> artifactContentEntries =
324 browseService.getArtifactContentEntries( "commons-logging", "commons-logging", "1.1", null, null,
325 "org/apache/commons/logging/", testRepoId );
327 log.info( "artifactContentEntries: {}", artifactContentEntries );
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 );
336 public void getArtifactDownloadInfos()
341 String testRepoId = "test-repo";
342 // force guest user creation if not exists
343 if ( getUserService( authorizationHeader ).getGuestUser() == null )
345 assertNotNull( getUserService( authorizationHeader ).createGuestUser() );
348 createAndIndexRepo( testRepoId, new File( getBasedir(), "src/test/repo-with-osgi" ).getAbsolutePath(),
351 BrowseService browseService = getBrowseService( authorizationHeader, true );
353 List<Artifact> artifactDownloadInfos =
354 browseService.getArtifactDownloadInfos( "commons-logging", "commons-logging", "1.1", testRepoId );
356 log.info( "artifactDownloadInfos {}", artifactDownloadInfos );
357 assertThat( artifactDownloadInfos ).isNotNull().isNotEmpty().hasSize( 3 );
358 deleteTestRepo( testRepoId );
360 catch ( Exception e )
362 log.error( e.getMessage(), e );
369 public void readArtifactContentText()
372 String testRepoId = "test-repo";
373 // force guest user creation if not exists
374 if ( getUserService( authorizationHeader ).getGuestUser() == null )
376 assertNotNull( getUserService( authorizationHeader ).createGuestUser() );
379 createAndIndexRepo( testRepoId, new File( getBasedir(), "src/test/repo-with-osgi" ).getAbsolutePath(), false );
381 BrowseService browseService = getBrowseService( authorizationHeader, true );
383 WebClient.client( browseService ).accept( MediaType.TEXT_PLAIN );
388 browseService.getArtifactContentText( "commons-logging", "commons-logging", "1.1", "sources", null,
389 "org/apache/commons/logging/LogSource.java",
390 testRepoId ).getContent();
392 log.debug( "text: {}", text );
394 assertThat( text ).contains( "package org.apache.commons.logging;" ).contains( "public class LogSource {" );
396 catch ( Exception e )
398 log.error( e.getMessage(), e );
405 public void readArtifactContentTextPom()
408 String testRepoId = "test-repo";
409 // force guest user creation if not exists
410 if ( getUserService( authorizationHeader ).getGuestUser() == null )
412 assertNotNull( getUserService( authorizationHeader ).createGuestUser() );
415 createAndIndexRepo( testRepoId, new File( getBasedir(), "src/test/repo-with-osgi" ).getAbsolutePath(), false );
417 BrowseService browseService = getBrowseService( authorizationHeader, true );
419 WebClient.client( browseService ).accept( MediaType.TEXT_PLAIN );
424 browseService.getArtifactContentText( "commons-logging", "commons-logging", "1.1", null, "pom", null,
425 testRepoId ).getContent();
427 log.info( "text: {}", text );
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>" );
433 catch ( Exception e )
435 log.error( e.getMessage(), e );
442 public void artifactsNumber()
445 String testRepoId = "test-repo";
446 // force guest user creation if not exists
447 if ( getUserService( authorizationHeader ).getGuestUser() == null )
449 assertNotNull( getUserService( authorizationHeader ).createGuestUser() );
452 createAndIndexRepo( testRepoId, new File( getBasedir(), "src/test/repo-with-osgi" ).getAbsolutePath(), false );
454 BrowseService browseService = getBrowseService( authorizationHeader, true );
456 //WebClient.client( browseService ).accept( MediaType.TEXT_PLAIN );
460 int number = browseService.getArtifacts( testRepoId ).size();
462 log.info( "getArtifactsNumber: {}", number );
464 assertTrue( number > 1 );
466 catch ( Exception e )
468 log.error( e.getMessage(), e );
474 public void metadatainbatchmode()
478 String testRepoId = "test-repo";
479 // force guest user creation if not exists
480 if ( getUserService( authorizationHeader ).getGuestUser() == null )
482 assertNotNull( getUserService( authorizationHeader ).createGuestUser() );
485 createAndIndexRepo( testRepoId, new File( getBasedir(), "src/test/repo-with-osgi" ).getAbsolutePath() );
487 BrowseService browseService = getBrowseService( authorizationHeader, false );
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" );
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 );
501 Map<String, String> metadatas =
502 toMap( browseService.getMetadatas( "commons-cli", "commons-cli", "1.0", testRepoId ) );
504 assertThat( metadatas ).isNotNull().isNotEmpty().contains( MapEntry.entry( "buildNumber", "1" ) ).contains(
505 MapEntry.entry( "author", "alecharp" ) ).contains( MapEntry.entry( "jenkins_version", "1.486" ) );
507 deleteTestRepo( testRepoId );