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()
339 String testRepoId = "test-repo";
340 // force guest user creation if not exists
341 if ( getUserService( authorizationHeader ).getGuestUser() == null )
343 assertNotNull( getUserService( authorizationHeader ).createGuestUser() );
346 createAndIndexRepo( testRepoId, new File( getBasedir(), "src/test/repo-with-osgi" ).getAbsolutePath(), false );
348 BrowseService browseService = getBrowseService( authorizationHeader, true );
350 List<Artifact> artifactDownloadInfos =
351 browseService.getArtifactDownloadInfos( "commons-logging", "commons-logging", "1.1", testRepoId );
353 log.info( "artifactDownloadInfos {}", artifactDownloadInfos );
354 assertThat( artifactDownloadInfos ).isNotNull().isNotEmpty().hasSize( 3 );
355 deleteTestRepo( testRepoId );
360 public void readArtifactContentText()
363 String testRepoId = "test-repo";
364 // force guest user creation if not exists
365 if ( getUserService( authorizationHeader ).getGuestUser() == null )
367 assertNotNull( getUserService( authorizationHeader ).createGuestUser() );
370 createAndIndexRepo( testRepoId, new File( getBasedir(), "src/test/repo-with-osgi" ).getAbsolutePath(), false );
372 BrowseService browseService = getBrowseService( authorizationHeader, true );
374 WebClient.client( browseService ).accept( MediaType.TEXT_PLAIN );
379 browseService.getArtifactContentText( "commons-logging", "commons-logging", "1.1", "sources", null,
380 "org/apache/commons/logging/LogSource.java",
381 testRepoId ).getContent();
383 log.debug( "text: {}", text );
385 assertThat( text ).contains( "package org.apache.commons.logging;" ).contains( "public class LogSource {" );
387 catch ( Exception e )
389 log.error( e.getMessage(), e );
396 public void readArtifactContentTextPom()
399 String testRepoId = "test-repo";
400 // force guest user creation if not exists
401 if ( getUserService( authorizationHeader ).getGuestUser() == null )
403 assertNotNull( getUserService( authorizationHeader ).createGuestUser() );
406 createAndIndexRepo( testRepoId, new File( getBasedir(), "src/test/repo-with-osgi" ).getAbsolutePath(), false );
408 BrowseService browseService = getBrowseService( authorizationHeader, true );
410 WebClient.client( browseService ).accept( MediaType.TEXT_PLAIN );
415 browseService.getArtifactContentText( "commons-logging", "commons-logging", "1.1", null, "pom", null,
416 testRepoId ).getContent();
418 log.info( "text: {}", text );
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>" );
424 catch ( Exception e )
426 log.error( e.getMessage(), e );
433 public void artifactsNumber()
436 String testRepoId = "test-repo";
437 // force guest user creation if not exists
438 if ( getUserService( authorizationHeader ).getGuestUser() == null )
440 assertNotNull( getUserService( authorizationHeader ).createGuestUser() );
443 createAndIndexRepo( testRepoId, new File( getBasedir(), "src/test/repo-with-osgi" ).getAbsolutePath(), false );
445 BrowseService browseService = getBrowseService( authorizationHeader, true );
447 //WebClient.client( browseService ).accept( MediaType.TEXT_PLAIN );
451 int number = browseService.getArtifacts( testRepoId ).size();
453 log.info( "getArtifactsNumber: {}", number );
455 assertTrue( number > 1 );
457 catch ( Exception e )
459 log.error( e.getMessage(), e );
465 public void metadatainbatchmode()
469 String testRepoId = "test-repo";
470 // force guest user creation if not exists
471 if ( getUserService( authorizationHeader ).getGuestUser() == null )
473 assertNotNull( getUserService( authorizationHeader ).createGuestUser() );
476 createAndIndexRepo( testRepoId, new File( getBasedir(), "src/test/repo-with-osgi" ).getAbsolutePath() );
478 BrowseService browseService = getBrowseService( authorizationHeader, false );
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" );
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 );
492 Map<String, String> metadatas =
493 toMap( browseService.getMetadatas( "commons-cli", "commons-cli", "1.0", testRepoId ) );
495 assertThat( metadatas ).isNotNull().isNotEmpty().contains( MapEntry.entry( "buildNumber", "1" ) ).contains(
496 MapEntry.entry( "author", "alecharp" ) ).contains( MapEntry.entry( "jenkins_version", "1.486" ) );
498 deleteTestRepo( testRepoId );