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.BadRequestException;
35 import javax.ws.rs.ForbiddenException;
36 import javax.ws.rs.core.Response;
38 import java.util.List;
40 import static org.assertj.core.api.Assertions.assertThat;
43 * @author Olivier Lamy
45 public class RepositoriesServiceTest
46 extends AbstractArchivaRestTest
49 @Test( expected = ForbiddenException.class )
50 public void scanRepoKarmaFailed()
53 RepositoriesService service = getRepositoriesService();
56 service.scanRepository( "id", true );
58 catch ( ForbiddenException e )
60 assertEquals( 403, e.getResponse().getStatus() );
66 public void scanRepo()
69 RepositoriesService service = getRepositoriesService( authorizationHeader );
71 ManagedRepositoriesService managedRepositoriesService = getManagedRepositoriesService( authorizationHeader );
73 String repoId = managedRepositoriesService.getManagedRepositories().get( 0 ).getId();
76 while ( timeout > 0 && service.alreadyScanning( repoId ) )
82 assertTrue( service.scanRepository( repoId, true ) );
85 @Test( expected = ForbiddenException.class )
86 public void deleteArtifactKarmaFailed()
91 Artifact artifact = new Artifact();
92 artifact.setGroupId( "commons-logging" );
93 artifact.setArtifactId( "commons-logging" );
94 artifact.setVersion( "1.0.1" );
95 artifact.setPackaging( "jar" );
96 artifact.setContext( SOURCE_REPO_ID );
98 RepositoriesService repositoriesService = getRepositoriesService( null );
100 repositoriesService.deleteArtifact( artifact );
102 catch ( ForbiddenException e )
104 assertEquals( 403, e.getResponse().getStatus() );
110 @Test( expected = BadRequestException.class )
111 public void deleteWithRepoNull()
117 RepositoriesService repositoriesService = getRepositoriesService( authorizationHeader );
119 Artifact artifact = new Artifact();
120 artifact.setGroupId( "commons-logging" );
121 artifact.setArtifactId( "commons-logging" );
122 artifact.setVersion( "1.0.1" );
123 artifact.setPackaging( "jar" );
125 repositoriesService.deleteArtifact( artifact );
127 catch ( BadRequestException e )
129 assertEquals( "not http " + Response.Status.BAD_REQUEST.getStatusCode() + " status",
130 Response.Status.BAD_REQUEST.getStatusCode(), e.getResponse().getStatus() );
137 * delete a version of an artifact without packaging
142 public void deleteArtifactVersion()
145 initSourceTargetRepo();
147 BrowseService browseService = getBrowseService( authorizationHeader, false );
149 List<Artifact> artifacts =
150 browseService.getArtifactDownloadInfos( "org.apache.karaf.features", "org.apache.karaf.features.core",
151 "2.2.2", SOURCE_REPO_ID );
153 log.info( "artifacts: {}", artifacts );
155 assertThat( artifacts ).isNotNull().isNotEmpty().hasSize( 2 );
157 VersionsList versionsList =
158 browseService.getVersionsList( "org.apache.karaf.features", "org.apache.karaf.features.core",
160 assertThat( versionsList.getVersions() ).isNotNull().isNotEmpty().hasSize( 2 );
162 log.info( "artifacts.size: {}", artifacts.size() );
166 File artifactFile = new File(
167 "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" );
169 assertTrue( "artifact not exists:" + artifactFile.getPath(), artifactFile.exists() );
171 Artifact artifact = new Artifact();
172 artifact.setGroupId( "org.apache.karaf.features" );
173 artifact.setArtifactId( "org.apache.karaf.features.core" );
174 artifact.setVersion( "2.2.2" );
175 artifact.setContext( SOURCE_REPO_ID );
177 RepositoriesService repositoriesService = getRepositoriesService( authorizationHeader );
179 repositoriesService.deleteArtifact( artifact );
181 assertFalse( "artifact not deleted exists:" + artifactFile.getPath(), artifactFile.exists() );
184 browseService.getArtifactDownloadInfos( "org.apache.karaf.features", "org.apache.karaf.features.core",
185 "2.2.2", SOURCE_REPO_ID );
187 assertThat( artifacts ).isNotNull().isEmpty();
189 versionsList = browseService.getVersionsList( "org.apache.karaf.features", "org.apache.karaf.features.core",
192 assertThat( versionsList.getVersions() ).isNotNull().isNotEmpty().hasSize( 1 );
203 public void deleteArtifact()
206 initSourceTargetRepo();
208 BrowseService browseService = getBrowseService( authorizationHeader, false );
210 List<Artifact> artifacts =
211 browseService.getArtifactDownloadInfos( "org.apache.karaf.features", "org.apache.karaf.features.core",
212 "2.2.2", SOURCE_REPO_ID );
214 log.info( "artifacts: {}", artifacts );
216 assertThat( artifacts ).isNotNull().isNotEmpty().hasSize( 2 );
218 VersionsList versionsList =
219 browseService.getVersionsList( "org.apache.karaf.features", "org.apache.karaf.features.core",
221 assertThat( versionsList.getVersions() ).isNotNull().isNotEmpty().hasSize( 2 );
223 log.info( "artifacts.size: {}", artifacts.size() );
227 File artifactFile = new File(
228 "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" );
230 assertTrue( "artifact not exists:" + artifactFile.getPath(), artifactFile.exists() );
232 Artifact artifact = new Artifact();
233 artifact.setGroupId( "org.apache.karaf.features" );
234 artifact.setArtifactId( "org.apache.karaf.features.core" );
235 artifact.setVersion( "2.2.2" );
236 artifact.setPackaging( "jar" );
237 artifact.setContext( SOURCE_REPO_ID );
239 RepositoriesService repositoriesService = getRepositoriesService( authorizationHeader );
241 repositoriesService.deleteArtifact( artifact );
243 assertFalse( "artifact not deleted exists:" + artifactFile.getPath(), artifactFile.exists() );
246 browseService.getArtifactDownloadInfos( "org.apache.karaf.features", "org.apache.karaf.features.core",
247 "2.2.2", SOURCE_REPO_ID );
249 assertThat( artifacts ).isNotNull().isEmpty();
251 versionsList = browseService.getVersionsList( "org.apache.karaf.features", "org.apache.karaf.features.core",
254 assertThat( versionsList.getVersions() ).isNotNull().isNotEmpty().hasSize( 1 );
264 public void deleteArtifactWithClassifier()
267 initSourceTargetRepo();
269 BrowseService browseService = getBrowseService( authorizationHeader, false );
271 List<Artifact> artifacts =
272 browseService.getArtifactDownloadInfos( "commons-logging", "commons-logging", "1.0.1", SOURCE_REPO_ID );
274 assertThat( artifacts ).isNotNull().isNotEmpty().hasSize( 3 );
276 VersionsList versionsList =
277 browseService.getVersionsList( "commons-logging", "commons-logging", SOURCE_REPO_ID );
278 assertThat( versionsList.getVersions() ).isNotNull().isNotEmpty().hasSize( 6 );
280 log.info( "artifacts.size: {}", artifacts.size() );
284 File artifactFile = new File(
285 "target/test-origin-repo/commons-logging/commons-logging/1.0.1/commons-logging-1.0.1-javadoc.jar" );
287 File artifactFilemd5 = new File(
288 "target/test-origin-repo/commons-logging/commons-logging/1.0.1/commons-logging-1.0.1-javadoc.jar.md5" );
290 File artifactFilesha1 = new File(
291 "target/test-origin-repo/commons-logging/commons-logging/1.0.1/commons-logging-1.0.1-javadoc.jar.sha1" );
293 assertTrue( "artifact not exists:" + artifactFile.getPath(), artifactFile.exists() );
295 assertTrue( "md5 not exists:" + artifactFilemd5.getPath(), artifactFilemd5.exists() );
296 assertTrue( "sha1 not exists:" + artifactFilesha1.getPath(), artifactFilesha1.exists() );
298 Artifact artifact = new Artifact();
299 artifact.setGroupId( "commons-logging" );
300 artifact.setArtifactId( "commons-logging" );
301 artifact.setVersion( "1.0.1" );
302 artifact.setClassifier( "javadoc" );
303 artifact.setPackaging( "jar" );
304 artifact.setContext( SOURCE_REPO_ID );
306 RepositoriesService repositoriesService = getRepositoriesService( authorizationHeader );
308 repositoriesService.deleteArtifact( artifact );
310 assertFalse( "artifact not deleted exists:" + artifactFile.getPath(), artifactFile.exists() );
311 assertFalse( "md5 still exists:" + artifactFilemd5.getPath(), artifactFilemd5.exists() );
312 assertFalse( "sha1 still exists:" + artifactFilesha1.getPath(), artifactFilesha1.exists() );
315 browseService.getArtifactDownloadInfos( "commons-logging", "commons-logging", "1.0.1", SOURCE_REPO_ID );
317 log.info( "artifact: {}", artifacts );
319 assertThat( artifacts ).isNotNull().isNotEmpty().hasSize( 2 );
321 versionsList = browseService.getVersionsList( "commons-logging", "commons-logging", SOURCE_REPO_ID );
323 log.info( "versionsList: {}", versionsList );
325 assertThat( versionsList.getVersions() ).isNotNull().isNotEmpty().hasSize( 6 );
336 public void deleteGroupId()
339 initSourceTargetRepo();
342 BrowseService browseService = getBrowseService( authorizationHeader, false );
344 BrowseResult browseResult = browseService.browseGroupId( "org.apache.karaf.features", SOURCE_REPO_ID );
346 assertNotNull( browseResult );
348 log.info( "browseResult: {}", browseResult );
350 assertThat( browseResult.getBrowseResultEntries() ).isNotNull().isNotEmpty().contains(
351 new BrowseResultEntry( "org.apache.karaf.features.org.apache.karaf.features.command", true ),
352 new BrowseResultEntry( "org.apache.karaf.features.org.apache.karaf.features.core", true ) );
355 new File( "target/test-origin-repo/org/apache/karaf/features/org.apache.karaf.features.command" );
357 assertTrue( "directory not exists", directory.exists() );
359 RepositoriesService repositoriesService = getRepositoriesService( authorizationHeader );
360 repositoriesService.deleteGroupId( "org.apache.karaf", SOURCE_REPO_ID );
362 assertFalse( "directory not exists", directory.exists() );
364 browseResult = browseService.browseGroupId( "org.apache.karaf.features", SOURCE_REPO_ID );
366 assertNotNull( browseResult );
368 assertThat( browseResult.getBrowseResultEntries() ).isNotNull().isEmpty();
370 browseResult = browseService.browseGroupId( "org.apache.karaf", SOURCE_REPO_ID );
372 assertNotNull( browseResult );
374 assertThat( browseResult.getBrowseResultEntries() ).isNotNull().isEmpty();
376 log.info( "browseResult empty: {}", browseResult );
385 public void authorizedToDeleteArtifacts()
388 ManagedRepository managedRepository = getTestManagedRepository( "SOURCE_REPO_ID", "SOURCE_REPO_ID" );
391 getManagedRepositoriesService( authorizationHeader ).addManagedRepository( managedRepository );
392 RepositoriesService repositoriesService = getRepositoriesService( authorizationHeader );
393 assertTrue( repositoriesService.isAuthorizedToDeleteArtifacts( managedRepository.getId() ) );
397 cleanQuietlyRepo( managedRepository.getId() );
402 public void notAuthorizedToDeleteArtifacts()
405 ManagedRepository managedRepository = getTestManagedRepository( "SOURCE_REPO_ID", "SOURCE_REPO_ID" );
408 getManagedRepositoriesService( authorizationHeader ).addManagedRepository( managedRepository );
409 RepositoriesService repositoriesService = getRepositoriesService( guestAuthzHeader );
410 assertFalse( repositoriesService.isAuthorizedToDeleteArtifacts( managedRepository.getId() ) );
414 cleanQuietlyRepo( managedRepository.getId() );
418 protected void cleanQuietlyRepo( String id )
422 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( id, true );
424 catch ( Exception e )
426 log.info( "ignore issue deleting test repo: {}", e.getMessage() );
431 public void deleteSnapshot()
434 File targetRepo = initSnapshotRepo();
438 RepositoriesService repositoriesService = getRepositoriesService( authorizationHeader );
439 //repositoriesService.scanRepositoryDirectoriesNow( SNAPSHOT_REPO_ID );
441 BrowseService browseService = getBrowseService( authorizationHeader, false );
442 List<Artifact> artifacts =
443 browseService.getArtifactDownloadInfos( "org.apache.archiva.redback.components", "spring-quartz",
444 "2.0-SNAPSHOT", SNAPSHOT_REPO_ID );
446 log.info( "artifacts: {}", artifacts );
448 assertThat( artifacts ).isNotNull().isNotEmpty().hasSize( 10 );
450 File artifactFile = new File( targetRepo,
451 "org/apache/archiva/redback/components/spring-quartz/2.0-SNAPSHOT/spring-quartz-2.0-20120618.214127-1.jar" );
453 File artifactFilemd5 = new File( targetRepo,
454 "org/apache/archiva/redback/components/spring-quartz/2.0-SNAPSHOT/spring-quartz-2.0-20120618.214127-1.jar.md5" );
456 File artifactFilepom = new File( targetRepo,
457 "org/apache/archiva/redback/components/spring-quartz/2.0-SNAPSHOT/spring-quartz-2.0-20120618.214127-1.pom" );
459 assertThat( artifactFile ).exists();
460 assertThat( artifactFilemd5 ).exists();
461 assertThat( artifactFilepom ).exists();
463 // we delete only one snapshot
465 new Artifact( "org.apache.archiva.redback.components", "spring-quartz", "2.0-20120618.214127-1" );
466 artifact.setPackaging( "jar" );
467 artifact.setRepositoryId( SNAPSHOT_REPO_ID );
468 artifact.setContext( SNAPSHOT_REPO_ID );
470 repositoriesService.deleteArtifact( artifact );
473 browseService.getArtifactDownloadInfos( "org.apache.archiva.redback.components", "spring-quartz",
474 "2.0-SNAPSHOT", SNAPSHOT_REPO_ID );
476 log.info( "artifacts: {}", artifacts );
478 assertThat( artifacts ).isNotNull().isNotEmpty().hasSize( 8 );
480 assertThat( artifactFile ).doesNotExist();
481 assertThat( artifactFilemd5 ).doesNotExist();
482 assertThat( artifactFilepom ).doesNotExist();
484 catch ( Exception e )
486 log.error( e.getMessage(), e );
495 protected File initSnapshotRepo()
498 File targetRepo = new File( getBasedir(), "target/repo-with-snapshots" );
499 if ( targetRepo.exists() )
501 FileUtils.deleteDirectory( targetRepo );
503 assertFalse( targetRepo.exists() );
505 FileUtils.copyDirectoryToDirectory( new File( getBasedir(), "src/test/repo-with-snapshots" ),
506 targetRepo.getParentFile() );
508 if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SNAPSHOT_REPO_ID ) != null )
510 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( SNAPSHOT_REPO_ID, true );
511 assertNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SNAPSHOT_REPO_ID ) );
513 ManagedRepository managedRepository = getTestManagedRepository( SNAPSHOT_REPO_ID, "repo-with-snapshots" );
514 /*managedRepository.setId( SNAPSHOT_REPO_ID );
515 managedRepository.setLocation( );
516 managedRepository.setCronExpression( "* * * * * ?" );*/
517 getManagedRepositoriesService( authorizationHeader ).addManagedRepository( managedRepository );
518 assertNotNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SNAPSHOT_REPO_ID ) );
523 protected void cleanSnapshotRepo()
527 if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SNAPSHOT_REPO_ID ) != null )
531 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( SNAPSHOT_REPO_ID, true );
533 getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SNAPSHOT_REPO_ID ) );
535 catch ( Exception e )
537 log.warn( "skip issue while cleaning test repository: this can cause test failure", e );
543 protected ManagedRepository getTestManagedRepository( String id, String path )
545 String location = new File( FileUtil.getBasedir(), "target/" + path ).getAbsolutePath();
546 return new ManagedRepository( id, id, location, "default", true, true, true, "2 * * * * ?", null, false, 80, 80,
551 protected ManagedRepository getTestManagedRepository()
553 return getTestManagedRepository( "TEST", "test-repo" );
557 static final String SNAPSHOT_REPO_ID = "snapshot-repo";