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.maven2.model.Artifact;
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.VersionsList;
27 import org.apache.archiva.rest.api.services.BrowseService;
28 import org.apache.archiva.rest.api.services.ManagedRepositoriesService;
29 import org.apache.archiva.rest.api.services.RepositoriesService;
30 import org.apache.commons.io.FileUtils;
31 import org.junit.Test;
33 import javax.ws.rs.BadRequestException;
34 import javax.ws.rs.ForbiddenException;
35 import javax.ws.rs.core.Response;
37 import java.util.List;
39 import static org.assertj.core.api.Assertions.assertThat;
42 * @author Olivier Lamy
44 public class RepositoriesServiceTest
45 extends AbstractArchivaRestTest
48 @Test( expected = ForbiddenException.class )
49 public void scanRepoKarmaFailed()
52 RepositoriesService service = getRepositoriesService();
55 service.scanRepository( "id", true );
57 catch ( ForbiddenException e )
59 assertEquals( 403, e.getResponse().getStatus() );
65 public void scanRepo()
68 RepositoriesService service = getRepositoriesService( authorizationHeader );
70 ManagedRepositoriesService managedRepositoriesService = getManagedRepositoriesService( authorizationHeader );
72 String repoId = managedRepositoriesService.getManagedRepositories().get( 0 ).getId();
75 while ( timeout > 0 && service.alreadyScanning( repoId ) )
81 assertTrue( service.scanRepository( repoId, true ) );
84 @Test( expected = ForbiddenException.class )
85 public void deleteArtifactKarmaFailed()
90 Artifact artifact = new Artifact();
91 artifact.setGroupId( "commons-logging" );
92 artifact.setArtifactId( "commons-logging" );
93 artifact.setVersion( "1.0.1" );
94 artifact.setPackaging( "jar" );
95 artifact.setContext( SOURCE_REPO_ID );
97 RepositoriesService repositoriesService = getRepositoriesService( null );
99 repositoriesService.deleteArtifact( artifact );
101 catch ( ForbiddenException e )
103 assertEquals( 403, e.getResponse().getStatus() );
109 @Test( expected = BadRequestException.class )
110 public void deleteWithRepoNull()
116 RepositoriesService repositoriesService = getRepositoriesService( authorizationHeader );
118 Artifact artifact = new Artifact();
119 artifact.setGroupId( "commons-logging" );
120 artifact.setArtifactId( "commons-logging" );
121 artifact.setVersion( "1.0.1" );
122 artifact.setPackaging( "jar" );
124 repositoriesService.deleteArtifact( artifact );
126 catch ( BadRequestException e )
128 assertEquals( "not http " + Response.Status.BAD_REQUEST.getStatusCode() + " status",
129 Response.Status.BAD_REQUEST.getStatusCode(), e.getResponse().getStatus() );
136 * delete a version of an artifact without packaging
141 public void deleteArtifactVersion()
144 initSourceTargetRepo();
146 BrowseService browseService = getBrowseService( authorizationHeader, false );
148 List<Artifact> artifacts =
149 browseService.getArtifactDownloadInfos( "org.apache.karaf.features", "org.apache.karaf.features.core",
150 "2.2.2", SOURCE_REPO_ID );
152 log.info( "artifacts: {}", artifacts );
154 assertThat( artifacts ).isNotNull().isNotEmpty().hasSize( 2 );
156 VersionsList versionsList =
157 browseService.getVersionsList( "org.apache.karaf.features", "org.apache.karaf.features.core",
159 assertThat( versionsList.getVersions() ).isNotNull().isNotEmpty().hasSize( 2 );
161 log.info( "artifacts.size: {}", artifacts.size() );
165 File artifactFile = new File(
166 "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" );
168 assertTrue( "artifact not exists:" + artifactFile.getPath(), artifactFile.exists() );
170 Artifact artifact = new Artifact();
171 artifact.setGroupId( "org.apache.karaf.features" );
172 artifact.setArtifactId( "org.apache.karaf.features.core" );
173 artifact.setVersion( "2.2.2" );
174 artifact.setContext( SOURCE_REPO_ID );
176 RepositoriesService repositoriesService = getRepositoriesService( authorizationHeader );
178 repositoriesService.deleteArtifact( artifact );
180 assertFalse( "artifact not deleted exists:" + artifactFile.getPath(), artifactFile.exists() );
183 browseService.getArtifactDownloadInfos( "org.apache.karaf.features", "org.apache.karaf.features.core",
184 "2.2.2", SOURCE_REPO_ID );
186 assertThat( artifacts ).isNotNull().isEmpty();
188 versionsList = browseService.getVersionsList( "org.apache.karaf.features", "org.apache.karaf.features.core",
191 assertThat( versionsList.getVersions() ).isNotNull().isNotEmpty().hasSize( 1 );
202 public void deleteArtifact()
205 initSourceTargetRepo();
207 BrowseService browseService = getBrowseService( authorizationHeader, false );
209 List<Artifact> artifacts =
210 browseService.getArtifactDownloadInfos( "org.apache.karaf.features", "org.apache.karaf.features.core",
211 "2.2.2", SOURCE_REPO_ID );
213 log.info( "artifacts: {}", artifacts );
215 assertThat( artifacts ).isNotNull().isNotEmpty().hasSize( 2 );
217 VersionsList versionsList =
218 browseService.getVersionsList( "org.apache.karaf.features", "org.apache.karaf.features.core",
220 assertThat( versionsList.getVersions() ).isNotNull().isNotEmpty().hasSize( 2 );
222 log.info( "artifacts.size: {}", artifacts.size() );
226 File artifactFile = new File(
227 "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" );
229 assertTrue( "artifact not exists:" + artifactFile.getPath(), artifactFile.exists() );
231 Artifact artifact = new Artifact();
232 artifact.setGroupId( "org.apache.karaf.features" );
233 artifact.setArtifactId( "org.apache.karaf.features.core" );
234 artifact.setVersion( "2.2.2" );
235 artifact.setPackaging( "jar" );
236 artifact.setContext( SOURCE_REPO_ID );
238 RepositoriesService repositoriesService = getRepositoriesService( authorizationHeader );
240 repositoriesService.deleteArtifact( artifact );
242 assertFalse( "artifact not deleted exists:" + artifactFile.getPath(), artifactFile.exists() );
245 browseService.getArtifactDownloadInfos( "org.apache.karaf.features", "org.apache.karaf.features.core",
246 "2.2.2", SOURCE_REPO_ID );
248 assertThat( artifacts ).isNotNull().isEmpty();
250 versionsList = browseService.getVersionsList( "org.apache.karaf.features", "org.apache.karaf.features.core",
253 assertThat( versionsList.getVersions() ).isNotNull().isNotEmpty().hasSize( 1 );
263 public void deleteArtifactWithClassifier()
266 initSourceTargetRepo();
268 BrowseService browseService = getBrowseService( authorizationHeader, false );
270 List<Artifact> artifacts =
271 browseService.getArtifactDownloadInfos( "commons-logging", "commons-logging", "1.0.1", SOURCE_REPO_ID );
273 assertThat( artifacts ).isNotNull().isNotEmpty().hasSize( 3 );
275 VersionsList versionsList =
276 browseService.getVersionsList( "commons-logging", "commons-logging", SOURCE_REPO_ID );
277 assertThat( versionsList.getVersions() ).isNotNull().isNotEmpty().hasSize( 6 );
279 log.info( "artifacts.size: {}", artifacts.size() );
283 File artifactFile = new File(
284 "target/test-origin-repo/commons-logging/commons-logging/1.0.1/commons-logging-1.0.1-javadoc.jar" );
286 File artifactFilemd5 = new File(
287 "target/test-origin-repo/commons-logging/commons-logging/1.0.1/commons-logging-1.0.1-javadoc.jar.md5" );
289 File artifactFilesha1 = new File(
290 "target/test-origin-repo/commons-logging/commons-logging/1.0.1/commons-logging-1.0.1-javadoc.jar.sha1" );
292 assertTrue( "artifact not exists:" + artifactFile.getPath(), artifactFile.exists() );
294 assertTrue( "md5 not exists:" + artifactFilemd5.getPath(), artifactFilemd5.exists() );
295 assertTrue( "sha1 not exists:" + artifactFilesha1.getPath(), artifactFilesha1.exists() );
297 Artifact artifact = new Artifact();
298 artifact.setGroupId( "commons-logging" );
299 artifact.setArtifactId( "commons-logging" );
300 artifact.setVersion( "1.0.1" );
301 artifact.setClassifier( "javadoc" );
302 artifact.setPackaging( "jar" );
303 artifact.setContext( SOURCE_REPO_ID );
305 RepositoriesService repositoriesService = getRepositoriesService( authorizationHeader );
307 repositoriesService.deleteArtifact( artifact );
309 assertFalse( "artifact not deleted exists:" + artifactFile.getPath(), artifactFile.exists() );
310 assertFalse( "md5 still exists:" + artifactFilemd5.getPath(), artifactFilemd5.exists() );
311 assertFalse( "sha1 still exists:" + artifactFilesha1.getPath(), artifactFilesha1.exists() );
314 browseService.getArtifactDownloadInfos( "commons-logging", "commons-logging", "1.0.1", SOURCE_REPO_ID );
316 log.info( "artifact: {}", artifacts );
318 assertThat( artifacts ).isNotNull().isNotEmpty().hasSize( 2 );
320 versionsList = browseService.getVersionsList( "commons-logging", "commons-logging", SOURCE_REPO_ID );
322 log.info( "versionsList: {}", versionsList );
324 assertThat( versionsList.getVersions() ).isNotNull().isNotEmpty().hasSize( 6 );
335 public void deleteGroupId()
338 initSourceTargetRepo();
341 BrowseService browseService = getBrowseService( authorizationHeader, false );
343 BrowseResult browseResult = browseService.browseGroupId( "org.apache.karaf.features", SOURCE_REPO_ID );
345 assertNotNull( browseResult );
347 log.info( "browseResult: {}", browseResult );
349 assertThat( browseResult.getBrowseResultEntries() ).isNotNull().isNotEmpty().contains(
350 new BrowseResultEntry( "org.apache.karaf.features.org.apache.karaf.features.command", true ),
351 new BrowseResultEntry( "org.apache.karaf.features.org.apache.karaf.features.core", true ) );
354 new File( "target/test-origin-repo/org/apache/karaf/features/org.apache.karaf.features.command" );
356 assertTrue( "directory not exists", directory.exists() );
358 RepositoriesService repositoriesService = getRepositoriesService( authorizationHeader );
359 repositoriesService.deleteGroupId( "org.apache.karaf", SOURCE_REPO_ID );
361 assertFalse( "directory not exists", directory.exists() );
363 browseResult = browseService.browseGroupId( "org.apache.karaf.features", SOURCE_REPO_ID );
365 assertNotNull( browseResult );
367 assertThat( browseResult.getBrowseResultEntries() ).isNotNull().isEmpty();
369 browseResult = browseService.browseGroupId( "org.apache.karaf", SOURCE_REPO_ID );
371 assertNotNull( browseResult );
373 assertThat( browseResult.getBrowseResultEntries() ).isNotNull().isEmpty();
375 log.info( "browseResult empty: {}", browseResult );
384 public void authorizedToDeleteArtifacts()
387 ManagedRepository managedRepository = getTestManagedRepository( "SOURCE_REPO_ID", "SOURCE_REPO_ID" );
390 getManagedRepositoriesService( authorizationHeader ).addManagedRepository( managedRepository );
391 RepositoriesService repositoriesService = getRepositoriesService( authorizationHeader );
392 assertTrue( repositoriesService.isAuthorizedToDeleteArtifacts( managedRepository.getId() ) );
396 cleanQuietlyRepo( managedRepository.getId() );
401 public void notAuthorizedToDeleteArtifacts()
404 ManagedRepository managedRepository = getTestManagedRepository( "SOURCE_REPO_ID", "SOURCE_REPO_ID" );
407 getManagedRepositoriesService( authorizationHeader ).addManagedRepository( managedRepository );
408 RepositoriesService repositoriesService = getRepositoriesService( guestAuthzHeader );
409 assertFalse( repositoriesService.isAuthorizedToDeleteArtifacts( managedRepository.getId() ) );
413 cleanQuietlyRepo( managedRepository.getId() );
417 protected void cleanQuietlyRepo( String id )
421 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( id, true );
423 catch ( Exception e )
425 log.info( "ignore issue deleting test repo: {}", e.getMessage() );
430 public void deleteSnapshot()
433 File targetRepo = initSnapshotRepo();
437 RepositoriesService repositoriesService = getRepositoriesService( authorizationHeader );
438 //repositoriesService.scanRepositoryDirectoriesNow( SNAPSHOT_REPO_ID );
440 BrowseService browseService = getBrowseService( authorizationHeader, false );
441 List<Artifact> artifacts =
442 browseService.getArtifactDownloadInfos( "org.apache.archiva.redback.components", "spring-quartz",
443 "2.0-SNAPSHOT", SNAPSHOT_REPO_ID );
445 log.info( "artifacts: {}", artifacts );
447 assertThat( artifacts ).isNotNull().isNotEmpty().hasSize( 10 );
449 File artifactFile = new File( targetRepo,
450 "org/apache/archiva/redback/components/spring-quartz/2.0-SNAPSHOT/spring-quartz-2.0-20120618.214127-1.jar" );
452 File artifactFilemd5 = new File( targetRepo,
453 "org/apache/archiva/redback/components/spring-quartz/2.0-SNAPSHOT/spring-quartz-2.0-20120618.214127-1.jar.md5" );
455 File artifactFilepom = new File( targetRepo,
456 "org/apache/archiva/redback/components/spring-quartz/2.0-SNAPSHOT/spring-quartz-2.0-20120618.214127-1.pom" );
458 assertThat( artifactFile ).exists();
459 assertThat( artifactFilemd5 ).exists();
460 assertThat( artifactFilepom ).exists();
462 // we delete only one snapshot
464 new Artifact( "org.apache.archiva.redback.components", "spring-quartz", "2.0-20120618.214127-1" );
465 artifact.setPackaging( "jar" );
466 artifact.setRepositoryId( SNAPSHOT_REPO_ID );
467 artifact.setContext( SNAPSHOT_REPO_ID );
469 repositoriesService.deleteArtifact( artifact );
472 browseService.getArtifactDownloadInfos( "org.apache.archiva.redback.components", "spring-quartz",
473 "2.0-SNAPSHOT", SNAPSHOT_REPO_ID );
475 log.info( "artifacts: {}", artifacts );
477 assertThat( artifacts ).isNotNull().isNotEmpty().hasSize( 8 );
479 assertThat( artifactFile ).doesNotExist();
480 assertThat( artifactFilemd5 ).doesNotExist();
481 assertThat( artifactFilepom ).doesNotExist();
483 catch ( Exception e )
485 log.error( e.getMessage(), e );
494 protected File initSnapshotRepo()
497 File targetRepo = new File( getBasedir(), "target/repo-with-snapshots" );
498 if ( targetRepo.exists() )
500 FileUtils.deleteDirectory( targetRepo );
502 assertFalse( targetRepo.exists() );
504 FileUtils.copyDirectoryToDirectory( new File( getBasedir(), "src/test/repo-with-snapshots" ),
505 targetRepo.getParentFile() );
507 if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SNAPSHOT_REPO_ID ) != null )
509 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( SNAPSHOT_REPO_ID, true );
510 assertNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SNAPSHOT_REPO_ID ) );
512 ManagedRepository managedRepository = getTestManagedRepository( SNAPSHOT_REPO_ID, "repo-with-snapshots" );
513 /*managedRepository.setId( SNAPSHOT_REPO_ID );
514 managedRepository.setLocation( );
515 managedRepository.setCronExpression( "* * * * * ?" );*/
516 getManagedRepositoriesService( authorizationHeader ).addManagedRepository( managedRepository );
517 assertNotNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SNAPSHOT_REPO_ID ) );
522 protected void cleanSnapshotRepo()
526 if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SNAPSHOT_REPO_ID ) != null )
530 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( SNAPSHOT_REPO_ID, true );
532 getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SNAPSHOT_REPO_ID ) );
534 catch ( Exception e )
536 log.warn( "skip issue while cleaning test repository: this can cause test failure", e );
542 protected ManagedRepository getTestManagedRepository( String id, String path )
544 String location = new File( org.apache.archiva.common.utils.FileUtils.getBasedir(), "target/" + path ).getAbsolutePath();
545 return new ManagedRepository( id, id, location, "default", true, true, true, "2 * * * * ?", null, false, 80, 80,
550 protected ManagedRepository getTestManagedRepository()
552 return getTestManagedRepository( "TEST", "test-repo" );
556 static final String SNAPSHOT_REPO_ID = "snapshot-repo";