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.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.data.MapEntry;
31 import org.junit.Test;
33 import javax.ws.rs.core.MediaType;
35 import java.util.HashMap;
36 import java.util.List;
39 import static org.fest.assertions.api.Assertions.assertThat;
42 * @author Olivier Lamy
44 public class BrowseServiceTest
45 extends AbstractArchivaRestTest
48 Map<String, String> toMap( List<Entry> entries )
50 Map<String, String> map = new HashMap<String, String>( entries.size() );
52 for ( Entry entry : entries )
54 map.put( entry.getKey(), entry.getValue() );
62 public void metadatagetthenadd()
66 String testRepoId = "test-repo";
67 // force guest user creation if not exists
68 if ( getUserService( authorizationHeader ).getGuestUser() == null )
70 assertNotNull( getUserService( authorizationHeader ).createGuestUser() );
73 createAndIndexRepo( testRepoId, new File( getBasedir(), "src/test/repo-with-osgi" ).getAbsolutePath() );
75 BrowseService browseService = getBrowseService( authorizationHeader, false );
77 Map<String, String> metadatas =
78 toMap( browseService.getMetadatas( "commons-cli", "commons-cli", "1.0", testRepoId ) );
80 assertThat( metadatas ).isNotNull().isEmpty();
82 browseService.addMetadata( "commons-cli", "commons-cli", "1.0", "wine", "bordeaux", testRepoId );
84 metadatas = toMap( browseService.getMetadatas( "commons-cli", "commons-cli", "1.0", testRepoId ) );
86 assertThat( metadatas ).isNotNull().isNotEmpty().contains( MapEntry.entry( "wine", "bordeaux" ) );
88 deleteTestRepo( testRepoId );
94 public void metadatagetthenaddthendelete()
98 String testRepoId = "test-repo";
99 // force guest user creation if not exists
100 if ( getUserService( authorizationHeader ).getGuestUser() == null )
102 assertNotNull( getUserService( authorizationHeader ).createGuestUser() );
105 createAndIndexRepo( testRepoId, new File( getBasedir(), "src/test/repo-with-osgi" ).getAbsolutePath() );
107 BrowseService browseService = getBrowseService( authorizationHeader, false );
109 Map<String, String> metadatas =
110 toMap( browseService.getMetadatas( "commons-cli", "commons-cli", "1.0", testRepoId ) );
112 assertThat( metadatas ).isNotNull().isEmpty();
114 browseService.addMetadata( "commons-cli", "commons-cli", "1.0", "wine", "bordeaux", testRepoId );
116 metadatas = toMap( browseService.getMetadatas( "commons-cli", "commons-cli", "1.0", testRepoId ) );
118 assertThat( metadatas ).isNotNull().isNotEmpty().contains( MapEntry.entry( "wine", "bordeaux" ) );
120 browseService.deleteMetadata( "commons-cli", "commons-cli", "1.0", "wine", testRepoId );
122 metadatas = toMap( browseService.getMetadatas( "commons-cli", "commons-cli", "1.0", testRepoId ) );
124 assertThat( metadatas ).isNotNull().isEmpty();
126 deleteTestRepo( testRepoId );
131 public void browserootGroups()
135 String testRepoId = "test-repo";
136 // force guest user creation if not exists
137 if ( getUserService( authorizationHeader ).getGuestUser() == null )
139 assertNotNull( getUserService( authorizationHeader ).createGuestUser() );
142 createAndIndexRepo( testRepoId, new File( getBasedir(), "src/test/repo-with-osgi" ).getAbsolutePath(), false );
144 BrowseService browseService = getBrowseService( authorizationHeader, false );
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 ) );
152 deleteTestRepo( testRepoId );
157 public void browsegroupId()
161 String testRepoId = "test-repo";
162 // force guest user creation if not exists
163 if ( getUserService( authorizationHeader ).getGuestUser() == null )
165 assertNotNull( getUserService( authorizationHeader ).createGuestUser() );
168 createAndIndexRepo( testRepoId, new File( getBasedir(), "src/test/repo-with-osgi" ).getAbsolutePath(), false );
170 BrowseService browseService = getBrowseService( authorizationHeader, false );
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 ) );
178 deleteTestRepo( testRepoId );
184 public void browsegroupIdWithReleaseStartNumber()
188 String testRepoId = "test-repo";
189 // force guest user creation if not exists
190 if ( getUserService( authorizationHeader ).getGuestUser() == null )
192 assertNotNull( getUserService( authorizationHeader ).createGuestUser() );
195 createAndIndexRepo( testRepoId, new File( getBasedir(), "src/test/repo-with-osgi" ).getAbsolutePath(), false );
197 BrowseService browseService = getBrowseService( authorizationHeader, false );
198 BrowseResult browseResult = browseService.browseGroupId( "commons-logging.commons-logging", testRepoId );
199 log.info( "browseResult: {}", browseResult );
201 deleteTestRepo( testRepoId );
206 public void versionsList()
210 String testRepoId = "test-repo";
211 // force guest user creation if not exists
212 if ( getUserService( authorizationHeader ).getGuestUser() == null )
214 assertNotNull( getUserService( authorizationHeader ).createGuestUser() );
217 createAndIndexRepo( testRepoId, new File( getBasedir(), "src/test/repo-with-osgi" ).getAbsolutePath(), false );
219 BrowseService browseService = getBrowseService( authorizationHeader, false );
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" );
226 deleteTestRepo( testRepoId );
231 public void getProjectVersionMetadata()
234 String testRepoId = "test-repo";
235 // force guest user creation if not exists
236 if ( getUserService( authorizationHeader ).getGuestUser() == null )
238 assertNotNull( getUserService( authorizationHeader ).createGuestUser() );
241 createAndIndexRepo( testRepoId, new File( getBasedir(), "src/test/repo-with-osgi" ).getAbsolutePath(), false );
243 BrowseService browseService = getBrowseService( authorizationHeader, true );
245 ProjectVersionMetadata metadata =
246 browseService.getProjectVersionMetadata( "org.apache.karaf.features", "org.apache.karaf.features.core",
249 assertThat( metadata ).isNotNull();
251 deleteTestRepo( testRepoId );
255 public void readArtifactContentEntries()
258 String testRepoId = "test-repo";
259 // force guest user creation if not exists
260 if ( getUserService( authorizationHeader ).getGuestUser() == null )
262 assertNotNull( getUserService( authorizationHeader ).createGuestUser() );
265 createAndIndexRepo( testRepoId, new File( getBasedir(), "src/test/repo-with-osgi" ).getAbsolutePath(), false );
267 BrowseService browseService = getBrowseService( authorizationHeader, true );
269 List<ArtifactContentEntry> artifactContentEntries =
270 browseService.getArtifactContentEntries( "commons-logging", "commons-logging", "1.1", null, null, null,
273 log.info( "artifactContentEntries: {}", artifactContentEntries );
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 );
282 public void readArtifactContentEntriesRootPath()
285 String testRepoId = "test-repo";
286 // force guest user creation if not exists
287 if ( getUserService( authorizationHeader ).getGuestUser() == null )
289 assertNotNull( getUserService( authorizationHeader ).createGuestUser() );
292 createAndIndexRepo( testRepoId, new File( getBasedir(), "src/test/repo-with-osgi" ).getAbsolutePath(), false );
294 BrowseService browseService = getBrowseService( authorizationHeader, true );
296 List<ArtifactContentEntry> artifactContentEntries =
297 browseService.getArtifactContentEntries( "commons-logging", "commons-logging", "1.1", null, null, "org/",
300 log.info( "artifactContentEntries: {}", artifactContentEntries );
302 assertThat( artifactContentEntries ).isNotNull().isNotEmpty().hasSize( 1 ).contains(
303 new ArtifactContentEntry( "org/apache", false, 1, testRepoId ) );
304 deleteTestRepo( testRepoId );
308 public void readArtifactContentEntriesFilesAndDirectories()
311 String testRepoId = "test-repo";
312 // force guest user creation if not exists
313 if ( getUserService( authorizationHeader ).getGuestUser() == null )
315 assertNotNull( getUserService( authorizationHeader ).createGuestUser() );
318 createAndIndexRepo( testRepoId, new File( getBasedir(), "src/test/repo-with-osgi" ).getAbsolutePath(), false );
320 BrowseService browseService = getBrowseService( authorizationHeader, true );
322 List<ArtifactContentEntry> artifactContentEntries =
323 browseService.getArtifactContentEntries( "commons-logging", "commons-logging", "1.1", null, null,
324 "org/apache/commons/logging/", testRepoId );
326 log.info( "artifactContentEntries: {}", artifactContentEntries );
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 );
335 public void getArtifactDownloadInfos()
338 String testRepoId = "test-repo";
339 // force guest user creation if not exists
340 if ( getUserService( authorizationHeader ).getGuestUser() == null )
342 assertNotNull( getUserService( authorizationHeader ).createGuestUser() );
345 createAndIndexRepo( testRepoId, new File( getBasedir(), "src/test/repo-with-osgi" ).getAbsolutePath(), false );
347 BrowseService browseService = getBrowseService( authorizationHeader, true );
349 List<Artifact> artifactDownloadInfos =
350 browseService.getArtifactDownloadInfos( "commons-logging", "commons-logging", "1.1", testRepoId );
352 log.info( "artifactDownloadInfos {}", artifactDownloadInfos );
353 assertThat( artifactDownloadInfos ).isNotNull().isNotEmpty().hasSize( 3 );
354 deleteTestRepo( testRepoId );
359 public void readArtifactContentText()
362 String testRepoId = "test-repo";
363 // force guest user creation if not exists
364 if ( getUserService( authorizationHeader ).getGuestUser() == null )
366 assertNotNull( getUserService( authorizationHeader ).createGuestUser() );
369 createAndIndexRepo( testRepoId, new File( getBasedir(), "src/test/repo-with-osgi" ).getAbsolutePath(), false );
371 BrowseService browseService = getBrowseService( authorizationHeader, true );
373 WebClient.client( browseService ).accept( MediaType.TEXT_PLAIN );
378 browseService.getArtifactContentText( "commons-logging", "commons-logging", "1.1", "sources", null,
379 "org/apache/commons/logging/LogSource.java",
380 testRepoId ).getContent();
382 log.debug( "text: {}", text );
384 assertThat( text ).contains( "package org.apache.commons.logging;" ).contains( "public class LogSource {" );
386 catch ( Exception e )
388 log.error( e.getMessage(), e );
395 public void readArtifactContentTextPom()
398 String testRepoId = "test-repo";
399 // force guest user creation if not exists
400 if ( getUserService( authorizationHeader ).getGuestUser() == null )
402 assertNotNull( getUserService( authorizationHeader ).createGuestUser() );
405 createAndIndexRepo( testRepoId, new File( getBasedir(), "src/test/repo-with-osgi" ).getAbsolutePath(), false );
407 BrowseService browseService = getBrowseService( authorizationHeader, true );
409 WebClient.client( browseService ).accept( MediaType.TEXT_PLAIN );
414 browseService.getArtifactContentText( "commons-logging", "commons-logging", "1.1", null, "pom", null,
415 testRepoId ).getContent();
417 log.info( "text: {}", text );
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>" );
423 catch ( Exception e )
425 log.error( e.getMessage(), e );