1 package org.apache.archiva.rest.services;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import org.apache.archiva.admin.model.beans.ManagedRepository;
23 import org.apache.archiva.common.utils.FileUtil;
24 import org.apache.archiva.maven2.model.Artifact;
25 import org.apache.archiva.rest.api.model.BrowseResult;
26 import org.apache.archiva.rest.api.model.BrowseResultEntry;
27 import org.apache.archiva.rest.api.model.VersionsList;
28 import org.apache.archiva.rest.api.services.BrowseService;
29 import org.apache.archiva.rest.api.services.ManagedRepositoriesService;
30 import org.apache.archiva.rest.api.services.RepositoriesService;
31 import org.apache.commons.io.FileUtils;
32 import org.junit.Test;
34 import javax.ws.rs.ForbiddenException;
36 import java.util.List;
38 import static org.assertj.core.api.Assertions.assertThat;
41 * @author Olivier Lamy
43 public class RepositoriesServiceTest
44 extends AbstractArchivaRestTest
47 @Test( expected = ForbiddenException.class )
48 public void scanRepoKarmaFailed()
51 RepositoriesService service = getRepositoriesService();
54 service.scanRepository( "id", true );
56 catch ( ForbiddenException e )
58 assertEquals( 403, e.getResponse().getStatus() );
64 public void scanRepo()
67 RepositoriesService service = getRepositoriesService( authorizationHeader );
69 ManagedRepositoriesService managedRepositoriesService = getManagedRepositoriesService( authorizationHeader );
71 String repoId = managedRepositoriesService.getManagedRepositories().get( 0 ).getId();
74 while ( timeout > 0 && service.alreadyScanning( repoId ) )
80 assertTrue( service.scanRepository( repoId, true ) );
83 @Test( expected = ForbiddenException.class )
84 public void deleteArtifactKarmaFailed()
89 Artifact artifact = new Artifact();
90 artifact.setGroupId( "commons-logging" );
91 artifact.setArtifactId( "commons-logging" );
92 artifact.setVersion( "1.0.1" );
93 artifact.setPackaging( "jar" );
94 artifact.setContext( SOURCE_REPO_ID );
96 RepositoriesService repositoriesService = getRepositoriesService( null );
98 repositoriesService.deleteArtifact( artifact );
100 catch ( ForbiddenException e )
102 assertEquals( 403, e.getResponse().getStatus() );
108 @Test( expected = ForbiddenException.class )
109 public void deleteWithRepoNull()
115 RepositoriesService repositoriesService = getRepositoriesService( authorizationHeader );
117 Artifact artifact = new Artifact();
118 artifact.setGroupId( "commons-logging" );
119 artifact.setArtifactId( "commons-logging" );
120 artifact.setVersion( "1.0.1" );
121 artifact.setPackaging( "jar" );
123 repositoriesService.deleteArtifact( artifact );
125 catch ( ForbiddenException e )
127 assertEquals( "not http 400 status", 400, e.getResponse().getStatus() );
134 * delete a version of an artifact without packaging
139 public void deleteArtifactVersion()
142 initSourceTargetRepo();
144 BrowseService browseService = getBrowseService( authorizationHeader, false );
146 List<Artifact> artifacts =
147 browseService.getArtifactDownloadInfos( "org.apache.karaf.features", "org.apache.karaf.features.core",
148 "2.2.2", SOURCE_REPO_ID );
150 log.info( "artifacts: {}", artifacts );
152 assertThat( artifacts ).isNotNull().isNotEmpty().hasSize( 2 );
154 VersionsList versionsList =
155 browseService.getVersionsList( "org.apache.karaf.features", "org.apache.karaf.features.core",
157 assertThat( versionsList.getVersions() ).isNotNull().isNotEmpty().hasSize( 2 );
159 log.info( "artifacts.size: {}", artifacts.size() );
163 File artifactFile = new File(
164 "target/test-origin-repo/org/apache/karaf/features/org.apache.karaf.features.core/2.2.2/org.apache.karaf.features.core-2.2.2.jar" );
166 assertTrue( "artifact not exists:" + artifactFile.getPath(), artifactFile.exists() );
168 Artifact artifact = new Artifact();
169 artifact.setGroupId( "org.apache.karaf.features" );
170 artifact.setArtifactId( "org.apache.karaf.features.core" );
171 artifact.setVersion( "2.2.2" );
172 artifact.setContext( SOURCE_REPO_ID );
174 RepositoriesService repositoriesService = getRepositoriesService( authorizationHeader );
176 repositoriesService.deleteArtifact( artifact );
178 assertFalse( "artifact not deleted exists:" + artifactFile.getPath(), artifactFile.exists() );
181 browseService.getArtifactDownloadInfos( "org.apache.karaf.features", "org.apache.karaf.features.core",
182 "2.2.2", SOURCE_REPO_ID );
184 assertThat( artifacts ).isNotNull().isEmpty();
186 versionsList = browseService.getVersionsList( "org.apache.karaf.features", "org.apache.karaf.features.core",
189 assertThat( versionsList.getVersions() ).isNotNull().isNotEmpty().hasSize( 1 );
200 public void deleteArtifact()
203 initSourceTargetRepo();
205 BrowseService browseService = getBrowseService( authorizationHeader, false );
207 List<Artifact> artifacts =
208 browseService.getArtifactDownloadInfos( "org.apache.karaf.features", "org.apache.karaf.features.core",
209 "2.2.2", SOURCE_REPO_ID );
211 log.info( "artifacts: {}", artifacts );
213 assertThat( artifacts ).isNotNull().isNotEmpty().hasSize( 2 );
215 VersionsList versionsList =
216 browseService.getVersionsList( "org.apache.karaf.features", "org.apache.karaf.features.core",
218 assertThat( versionsList.getVersions() ).isNotNull().isNotEmpty().hasSize( 2 );
220 log.info( "artifacts.size: {}", artifacts.size() );
224 File artifactFile = new File(
225 "target/test-origin-repo/org/apache/karaf/features/org.apache.karaf.features.core/2.2.2/org.apache.karaf.features.core-2.2.2.jar" );
227 assertTrue( "artifact not exists:" + artifactFile.getPath(), artifactFile.exists() );
229 Artifact artifact = new Artifact();
230 artifact.setGroupId( "org.apache.karaf.features" );
231 artifact.setArtifactId( "org.apache.karaf.features.core" );
232 artifact.setVersion( "2.2.2" );
233 artifact.setPackaging( "jar" );
234 artifact.setContext( SOURCE_REPO_ID );
236 RepositoriesService repositoriesService = getRepositoriesService( authorizationHeader );
238 repositoriesService.deleteArtifact( artifact );
240 assertFalse( "artifact not deleted exists:" + artifactFile.getPath(), artifactFile.exists() );
243 browseService.getArtifactDownloadInfos( "org.apache.karaf.features", "org.apache.karaf.features.core",
244 "2.2.2", SOURCE_REPO_ID );
246 assertThat( artifacts ).isNotNull().isEmpty();
248 versionsList = browseService.getVersionsList( "org.apache.karaf.features", "org.apache.karaf.features.core",
251 assertThat( versionsList.getVersions() ).isNotNull().isNotEmpty().hasSize( 1 );
261 public void deleteArtifactWithClassifier()
264 initSourceTargetRepo();
266 BrowseService browseService = getBrowseService( authorizationHeader, false );
268 List<Artifact> artifacts =
269 browseService.getArtifactDownloadInfos( "commons-logging", "commons-logging", "1.0.1", SOURCE_REPO_ID );
271 assertThat( artifacts ).isNotNull().isNotEmpty().hasSize( 3 );
273 VersionsList versionsList =
274 browseService.getVersionsList( "commons-logging", "commons-logging", SOURCE_REPO_ID );
275 assertThat( versionsList.getVersions() ).isNotNull().isNotEmpty().hasSize( 6 );
277 log.info( "artifacts.size: {}", artifacts.size() );
281 File artifactFile = new File(
282 "target/test-origin-repo/commons-logging/commons-logging/1.0.1/commons-logging-1.0.1-javadoc.jar" );
284 File artifactFilemd5 = new File(
285 "target/test-origin-repo/commons-logging/commons-logging/1.0.1/commons-logging-1.0.1-javadoc.jar.md5" );
287 File artifactFilesha1 = new File(
288 "target/test-origin-repo/commons-logging/commons-logging/1.0.1/commons-logging-1.0.1-javadoc.jar.sha1" );
290 assertTrue( "artifact not exists:" + artifactFile.getPath(), artifactFile.exists() );
292 assertTrue( "md5 not exists:" + artifactFilemd5.getPath(), artifactFilemd5.exists() );
293 assertTrue( "sha1 not exists:" + artifactFilesha1.getPath(), artifactFilesha1.exists() );
295 Artifact artifact = new Artifact();
296 artifact.setGroupId( "commons-logging" );
297 artifact.setArtifactId( "commons-logging" );
298 artifact.setVersion( "1.0.1" );
299 artifact.setClassifier( "javadoc" );
300 artifact.setPackaging( "jar" );
301 artifact.setContext( SOURCE_REPO_ID );
303 RepositoriesService repositoriesService = getRepositoriesService( authorizationHeader );
305 repositoriesService.deleteArtifact( artifact );
307 assertFalse( "artifact not deleted exists:" + artifactFile.getPath(), artifactFile.exists() );
308 assertFalse( "md5 still exists:" + artifactFilemd5.getPath(), artifactFilemd5.exists() );
309 assertFalse( "sha1 still exists:" + artifactFilesha1.getPath(), artifactFilesha1.exists() );
312 browseService.getArtifactDownloadInfos( "commons-logging", "commons-logging", "1.0.1", SOURCE_REPO_ID );
314 log.info( "artifact: {}", artifacts );
316 assertThat( artifacts ).isNotNull().isNotEmpty().hasSize( 2 );
318 versionsList = browseService.getVersionsList( "commons-logging", "commons-logging", SOURCE_REPO_ID );
320 log.info( "versionsList: {}", versionsList );
322 assertThat( versionsList.getVersions() ).isNotNull().isNotEmpty().hasSize( 6 );
333 public void deleteGroupId()
336 initSourceTargetRepo();
339 BrowseService browseService = getBrowseService( authorizationHeader, false );
341 BrowseResult browseResult = browseService.browseGroupId( "org.apache.karaf.features", SOURCE_REPO_ID );
343 assertNotNull( browseResult );
345 log.info( "browseResult: {}", browseResult );
347 assertThat( browseResult.getBrowseResultEntries() ).isNotNull().isNotEmpty().contains(
348 new BrowseResultEntry( "org.apache.karaf.features.org.apache.karaf.features.command", true ),
349 new BrowseResultEntry( "org.apache.karaf.features.org.apache.karaf.features.core", true ) );
352 new File( "target/test-origin-repo/org/apache/karaf/features/org.apache.karaf.features.command" );
354 assertTrue( "directory not exists", directory.exists() );
356 RepositoriesService repositoriesService = getRepositoriesService( authorizationHeader );
357 repositoriesService.deleteGroupId( "org.apache.karaf", SOURCE_REPO_ID );
359 assertFalse( "directory not exists", directory.exists() );
361 browseResult = browseService.browseGroupId( "org.apache.karaf.features", SOURCE_REPO_ID );
363 assertNotNull( browseResult );
365 assertThat( browseResult.getBrowseResultEntries() ).isNotNull().isEmpty();
367 browseResult = browseService.browseGroupId( "org.apache.karaf", SOURCE_REPO_ID );
369 assertNotNull( browseResult );
371 assertThat( browseResult.getBrowseResultEntries() ).isNotNull().isEmpty();
373 log.info( "browseResult empty: {}", browseResult );
382 public void authorizedToDeleteArtifacts()
385 ManagedRepository managedRepository = getTestManagedRepository( "SOURCE_REPO_ID", "SOURCE_REPO_ID" );
388 getManagedRepositoriesService( authorizationHeader ).addManagedRepository( managedRepository );
389 RepositoriesService repositoriesService = getRepositoriesService( authorizationHeader );
390 assertTrue( repositoriesService.isAuthorizedToDeleteArtifacts( managedRepository.getId() ) );
394 cleanQuietlyRepo( managedRepository.getId() );
399 public void notAuthorizedToDeleteArtifacts()
402 ManagedRepository managedRepository = getTestManagedRepository( "SOURCE_REPO_ID", "SOURCE_REPO_ID" );
405 getManagedRepositoriesService( authorizationHeader ).addManagedRepository( managedRepository );
406 RepositoriesService repositoriesService = getRepositoriesService( guestAuthzHeader );
407 assertFalse( repositoriesService.isAuthorizedToDeleteArtifacts( managedRepository.getId() ) );
411 cleanQuietlyRepo( managedRepository.getId() );
415 protected void cleanQuietlyRepo( String id )
419 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( id, true );
421 catch ( Exception e )
423 log.info( "ignore issue deleting test repo: {}", e.getMessage() );
428 public void deleteSnapshot()
431 File targetRepo = initSnapshotRepo();
435 RepositoriesService repositoriesService = getRepositoriesService( authorizationHeader );
436 //repositoriesService.scanRepositoryDirectoriesNow( SNAPSHOT_REPO_ID );
438 BrowseService browseService = getBrowseService( authorizationHeader, false );
439 List<Artifact> artifacts =
440 browseService.getArtifactDownloadInfos( "org.apache.archiva.redback.components", "spring-quartz",
441 "2.0-SNAPSHOT", SNAPSHOT_REPO_ID );
443 log.info( "artifacts: {}", artifacts );
445 assertThat( artifacts ).isNotNull().isNotEmpty().hasSize( 10 );
447 File artifactFile = new File( targetRepo,
448 "org/apache/archiva/redback/components/spring-quartz/2.0-SNAPSHOT/spring-quartz-2.0-20120618.214127-1.jar" );
450 File artifactFilemd5 = new File( targetRepo,
451 "org/apache/archiva/redback/components/spring-quartz/2.0-SNAPSHOT/spring-quartz-2.0-20120618.214127-1.jar.md5" );
453 File artifactFilepom = new File( targetRepo,
454 "org/apache/archiva/redback/components/spring-quartz/2.0-SNAPSHOT/spring-quartz-2.0-20120618.214127-1.pom" );
456 assertThat( artifactFile ).exists();
457 assertThat( artifactFilemd5 ).exists();
458 assertThat( artifactFilepom ).exists();
460 // we delete only one snapshot
462 new Artifact( "org.apache.archiva.redback.components", "spring-quartz", "2.0-20120618.214127-1" );
463 artifact.setPackaging( "jar" );
464 artifact.setRepositoryId( SNAPSHOT_REPO_ID );
465 artifact.setContext( SNAPSHOT_REPO_ID );
467 repositoriesService.deleteArtifact( artifact );
470 browseService.getArtifactDownloadInfos( "org.apache.archiva.redback.components", "spring-quartz",
471 "2.0-SNAPSHOT", SNAPSHOT_REPO_ID );
473 log.info( "artifacts: {}", artifacts );
475 assertThat( artifacts ).isNotNull().isNotEmpty().hasSize( 8 );
477 assertThat( artifactFile ).doesNotExist();
478 assertThat( artifactFilemd5 ).doesNotExist();
479 assertThat( artifactFilepom ).doesNotExist();
481 catch ( Exception e )
483 log.error( e.getMessage(), e );
492 protected File initSnapshotRepo()
495 File targetRepo = new File( getBasedir(), "target/repo-with-snapshots" );
496 if ( targetRepo.exists() )
498 FileUtils.deleteDirectory( targetRepo );
500 assertFalse( targetRepo.exists() );
502 FileUtils.copyDirectoryToDirectory( new File( getBasedir(), "src/test/repo-with-snapshots" ),
503 targetRepo.getParentFile() );
505 if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SNAPSHOT_REPO_ID ) != null )
507 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( SNAPSHOT_REPO_ID, true );
508 assertNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SNAPSHOT_REPO_ID ) );
510 ManagedRepository managedRepository = getTestManagedRepository( SNAPSHOT_REPO_ID, "repo-with-snapshots" );
511 /*managedRepository.setId( SNAPSHOT_REPO_ID );
512 managedRepository.setLocation( );
513 managedRepository.setCronExpression( "* * * * * ?" );*/
514 getManagedRepositoriesService( authorizationHeader ).addManagedRepository( managedRepository );
515 assertNotNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SNAPSHOT_REPO_ID ) );
520 protected void cleanSnapshotRepo()
524 if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SNAPSHOT_REPO_ID ) != null )
528 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( SNAPSHOT_REPO_ID, true );
530 getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SNAPSHOT_REPO_ID ) );
532 catch ( Exception e )
534 log.warn( "skip issue while cleaning test repository: this can cause test failure", e );
540 protected ManagedRepository getTestManagedRepository( String id, String path )
542 String location = new File( FileUtil.getBasedir(), "target/" + path ).getAbsolutePath();
543 return new ManagedRepository( id, id, location, "default", true, true, true, "2 * * * * ?", null, false, 80, 80,
548 protected ManagedRepository getTestManagedRepository()
550 return getTestManagedRepository( "TEST", "test-repo" );
554 static final String SNAPSHOT_REPO_ID = "snapshot-repo";