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;
36 import java.nio.file.Files;
37 import java.nio.file.Path;
38 import java.nio.file.Paths;
39 import java.util.List;
41 import static org.assertj.core.api.Assertions.assertThat;
44 * @author Olivier Lamy
46 public class RepositoriesServiceTest
47 extends AbstractArchivaRestTest
50 @Test( expected = ForbiddenException.class )
51 public void scanRepoKarmaFailed()
54 RepositoriesService service = getRepositoriesService();
57 service.scanRepository( "id", true );
59 catch ( ForbiddenException e )
61 assertEquals( 403, e.getResponse().getStatus() );
67 public void scanRepo()
70 RepositoriesService service = getRepositoriesService( authorizationHeader );
72 ManagedRepositoriesService managedRepositoriesService = getManagedRepositoriesService( authorizationHeader );
74 String repoId = managedRepositoriesService.getManagedRepositories().get( 0 ).getId();
77 while ( timeout > 0 && service.alreadyScanning( repoId ) )
83 assertTrue( service.scanRepository( repoId, true ) );
86 @Test( expected = ForbiddenException.class )
87 public void deleteArtifactKarmaFailed()
92 Artifact artifact = new Artifact();
93 artifact.setGroupId( "commons-logging" );
94 artifact.setArtifactId( "commons-logging" );
95 artifact.setVersion( "1.0.1" );
96 artifact.setPackaging( "jar" );
97 artifact.setContext( SOURCE_REPO_ID );
99 RepositoriesService repositoriesService = getRepositoriesService( null );
101 repositoriesService.deleteArtifact( artifact );
103 catch ( ForbiddenException e )
105 assertEquals( 403, e.getResponse().getStatus() );
111 @Test( expected = BadRequestException.class )
112 public void deleteWithRepoNull()
118 RepositoriesService repositoriesService = getRepositoriesService( authorizationHeader );
120 Artifact artifact = new Artifact();
121 artifact.setGroupId( "commons-logging" );
122 artifact.setArtifactId( "commons-logging" );
123 artifact.setVersion( "1.0.1" );
124 artifact.setPackaging( "jar" );
126 repositoriesService.deleteArtifact( artifact );
128 catch ( BadRequestException e )
130 assertEquals( "not http " + Response.Status.BAD_REQUEST.getStatusCode() + " status",
131 Response.Status.BAD_REQUEST.getStatusCode(), e.getResponse().getStatus() );
138 * delete a version of an artifact without packaging
143 public void deleteArtifactVersion()
146 initSourceTargetRepo();
148 BrowseService browseService = getBrowseService( authorizationHeader, false );
150 List<Artifact> artifacts =
151 browseService.getArtifactDownloadInfos( "org.apache.karaf.features", "org.apache.karaf.features.core",
152 "2.2.2", SOURCE_REPO_ID );
154 log.info( "artifacts: {}", artifacts );
156 assertThat( artifacts ).isNotNull().isNotEmpty().hasSize( 2 );
158 VersionsList versionsList =
159 browseService.getVersionsList( "org.apache.karaf.features", "org.apache.karaf.features.core",
161 assertThat( versionsList.getVersions() ).isNotNull().isNotEmpty().hasSize( 2 );
163 log.info( "artifacts.size: {}", artifacts.size() );
167 Path artifactFile = Paths.get(
168 "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" );
170 assertTrue( "artifact not exists:" + artifactFile, Files.exists(artifactFile) );
172 Artifact artifact = new Artifact();
173 artifact.setGroupId( "org.apache.karaf.features" );
174 artifact.setArtifactId( "org.apache.karaf.features.core" );
175 artifact.setVersion( "2.2.2" );
176 artifact.setContext( SOURCE_REPO_ID );
178 RepositoriesService repositoriesService = getRepositoriesService( authorizationHeader );
180 repositoriesService.deleteArtifact( artifact );
182 assertFalse( "artifact not deleted exists:" + artifactFile, Files.exists(artifactFile) );
185 browseService.getArtifactDownloadInfos( "org.apache.karaf.features", "org.apache.karaf.features.core",
186 "2.2.2", SOURCE_REPO_ID );
188 assertThat( artifacts ).isNotNull().isEmpty();
190 versionsList = browseService.getVersionsList( "org.apache.karaf.features", "org.apache.karaf.features.core",
193 assertThat( versionsList.getVersions() ).isNotNull().isNotEmpty().hasSize( 1 );
204 public void deleteArtifact()
207 initSourceTargetRepo();
209 BrowseService browseService = getBrowseService( authorizationHeader, false );
211 List<Artifact> artifacts =
212 browseService.getArtifactDownloadInfos( "org.apache.karaf.features", "org.apache.karaf.features.core",
213 "2.2.2", SOURCE_REPO_ID );
215 log.info( "artifacts: {}", artifacts );
217 assertThat( artifacts ).isNotNull().isNotEmpty().hasSize( 2 );
219 VersionsList versionsList =
220 browseService.getVersionsList( "org.apache.karaf.features", "org.apache.karaf.features.core",
222 assertThat( versionsList.getVersions() ).isNotNull().isNotEmpty().hasSize( 2 );
224 log.info( "artifacts.size: {}", artifacts.size() );
228 Path artifactFile = Paths.get(
229 "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" );
231 assertTrue( "artifact not exists:" + artifactFile.toString(), Files.exists(artifactFile) );
233 Artifact artifact = new Artifact();
234 artifact.setGroupId( "org.apache.karaf.features" );
235 artifact.setArtifactId( "org.apache.karaf.features.core" );
236 artifact.setVersion( "2.2.2" );
237 artifact.setPackaging( "jar" );
238 artifact.setContext( SOURCE_REPO_ID );
240 RepositoriesService repositoriesService = getRepositoriesService( authorizationHeader );
242 repositoriesService.deleteArtifact( artifact );
244 assertFalse( "artifact not deleted exists:" + artifactFile, Files.exists(artifactFile) );
247 browseService.getArtifactDownloadInfos( "org.apache.karaf.features", "org.apache.karaf.features.core",
248 "2.2.2", SOURCE_REPO_ID );
250 assertThat( artifacts ).isNotNull().isEmpty();
252 versionsList = browseService.getVersionsList( "org.apache.karaf.features", "org.apache.karaf.features.core",
255 assertThat( versionsList.getVersions() ).isNotNull().isNotEmpty().hasSize( 1 );
265 public void deleteArtifactWithClassifier()
268 initSourceTargetRepo();
270 BrowseService browseService = getBrowseService( authorizationHeader, false );
272 List<Artifact> artifacts =
273 browseService.getArtifactDownloadInfos( "commons-logging", "commons-logging", "1.0.1", SOURCE_REPO_ID );
275 assertThat( artifacts ).isNotNull().isNotEmpty().hasSize( 3 );
277 VersionsList versionsList =
278 browseService.getVersionsList( "commons-logging", "commons-logging", SOURCE_REPO_ID );
279 assertThat( versionsList.getVersions() ).isNotNull().isNotEmpty().hasSize( 6 );
281 log.info( "artifacts.size: {}", artifacts.size() );
285 Path artifactFile = Paths.get(
286 "target/test-origin-repo/commons-logging/commons-logging/1.0.1/commons-logging-1.0.1-javadoc.jar" );
288 Path artifactFilemd5 = Paths.get(
289 "target/test-origin-repo/commons-logging/commons-logging/1.0.1/commons-logging-1.0.1-javadoc.jar.md5" );
291 Path artifactFilesha1 = Paths.get(
292 "target/test-origin-repo/commons-logging/commons-logging/1.0.1/commons-logging-1.0.1-javadoc.jar.sha1" );
294 assertTrue( "artifact not exists:" + artifactFile, Files.exists(artifactFile) );
296 assertTrue( "md5 not exists:" + artifactFilemd5, Files.exists(artifactFilemd5) );
297 assertTrue( "sha1 not exists:" + artifactFilesha1, Files.exists(artifactFilesha1) );
299 Artifact artifact = new Artifact();
300 artifact.setGroupId( "commons-logging" );
301 artifact.setArtifactId( "commons-logging" );
302 artifact.setVersion( "1.0.1" );
303 artifact.setClassifier( "javadoc" );
304 artifact.setPackaging( "jar" );
305 artifact.setContext( SOURCE_REPO_ID );
307 RepositoriesService repositoriesService = getRepositoriesService( authorizationHeader );
309 repositoriesService.deleteArtifact( artifact );
311 assertFalse( "artifact not deleted exists:" + artifactFile, Files.exists(artifactFile) );
312 assertFalse( "md5 still exists:" + artifactFilemd5, Files.exists(artifactFilemd5) );
313 assertFalse( "sha1 still exists:" + artifactFilesha1, Files.exists(artifactFilesha1) );
316 browseService.getArtifactDownloadInfos( "commons-logging", "commons-logging", "1.0.1", SOURCE_REPO_ID );
318 log.info( "artifact: {}", artifacts );
320 assertThat( artifacts ).isNotNull().isNotEmpty().hasSize( 2 );
322 versionsList = browseService.getVersionsList( "commons-logging", "commons-logging", SOURCE_REPO_ID );
324 log.info( "versionsList: {}", versionsList );
326 assertThat( versionsList.getVersions() ).isNotNull().isNotEmpty().hasSize( 6 );
337 public void deleteGroupId()
340 initSourceTargetRepo();
343 BrowseService browseService = getBrowseService( authorizationHeader, false );
345 BrowseResult browseResult = browseService.browseGroupId( "org.apache.karaf.features", SOURCE_REPO_ID );
347 assertNotNull( browseResult );
349 log.info( "browseResult: {}", browseResult );
351 assertThat( browseResult.getBrowseResultEntries() ).isNotNull().isNotEmpty().contains(
352 new BrowseResultEntry( "org.apache.karaf.features.org.apache.karaf.features.command", true ),
353 new BrowseResultEntry( "org.apache.karaf.features.org.apache.karaf.features.core", true ) );
356 Paths.get( "target/test-origin-repo/org/apache/karaf/features/org.apache.karaf.features.command" );
358 assertTrue( "directory not exists", Files.exists(directory) );
360 RepositoriesService repositoriesService = getRepositoriesService( authorizationHeader );
361 repositoriesService.deleteGroupId( "org.apache.karaf", SOURCE_REPO_ID );
363 assertFalse( "directory not exists", Files.exists(directory) );
365 browseResult = browseService.browseGroupId( "org.apache.karaf.features", SOURCE_REPO_ID );
367 assertNotNull( browseResult );
369 assertThat( browseResult.getBrowseResultEntries() ).isNotNull().isEmpty();
371 browseResult = browseService.browseGroupId( "org.apache.karaf", SOURCE_REPO_ID );
373 assertNotNull( browseResult );
375 assertThat( browseResult.getBrowseResultEntries() ).isNotNull().isEmpty();
377 log.info( "browseResult empty: {}", browseResult );
386 public void authorizedToDeleteArtifacts()
389 ManagedRepository managedRepository = getTestManagedRepository( "SOURCE_REPO_ID", "SOURCE_REPO_ID" );
392 getManagedRepositoriesService( authorizationHeader ).addManagedRepository( managedRepository );
393 RepositoriesService repositoriesService = getRepositoriesService( authorizationHeader );
394 assertTrue( repositoriesService.isAuthorizedToDeleteArtifacts( managedRepository.getId() ) );
398 cleanQuietlyRepo( managedRepository.getId() );
403 public void notAuthorizedToDeleteArtifacts()
406 ManagedRepository managedRepository = getTestManagedRepository( "SOURCE_REPO_ID", "SOURCE_REPO_ID" );
409 getManagedRepositoriesService( authorizationHeader ).addManagedRepository( managedRepository );
410 RepositoriesService repositoriesService = getRepositoriesService( guestAuthzHeader );
411 assertFalse( repositoriesService.isAuthorizedToDeleteArtifacts( managedRepository.getId() ) );
415 cleanQuietlyRepo( managedRepository.getId() );
419 protected void cleanQuietlyRepo( String id )
423 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( id, true );
425 catch ( Exception e )
427 log.info( "ignore issue deleting test repo: {}", e.getMessage() );
432 public void deleteSnapshot()
435 Path targetRepo = initSnapshotRepo();
439 RepositoriesService repositoriesService = getRepositoriesService( authorizationHeader );
440 //repositoriesService.scanRepositoryDirectoriesNow( SNAPSHOT_REPO_ID );
442 BrowseService browseService = getBrowseService( authorizationHeader, false );
443 List<Artifact> artifacts =
444 browseService.getArtifactDownloadInfos( "org.apache.archiva.redback.components", "spring-quartz",
445 "2.0-SNAPSHOT", SNAPSHOT_REPO_ID );
447 log.info( "artifacts: {}", artifacts );
449 assertThat( artifacts ).isNotNull().isNotEmpty().hasSize( 10 );
451 Path artifactFile = targetRepo.resolve(
452 "org/apache/archiva/redback/components/spring-quartz/2.0-SNAPSHOT/spring-quartz-2.0-20120618.214127-1.jar" );
454 Path artifactFilemd5 = targetRepo.resolve(
455 "org/apache/archiva/redback/components/spring-quartz/2.0-SNAPSHOT/spring-quartz-2.0-20120618.214127-1.jar.md5" );
457 Path artifactFilepom = targetRepo.resolve(
458 "org/apache/archiva/redback/components/spring-quartz/2.0-SNAPSHOT/spring-quartz-2.0-20120618.214127-1.pom" );
460 assertTrue( Files.exists(artifactFile) );
461 assertTrue( Files.exists(artifactFilemd5) );
462 assertTrue( Files.exists(artifactFilepom ));
464 // we delete only one snapshot
466 new Artifact( "org.apache.archiva.redback.components", "spring-quartz", "2.0-20120618.214127-1" );
467 artifact.setPackaging( "jar" );
468 artifact.setRepositoryId( SNAPSHOT_REPO_ID );
469 artifact.setContext( SNAPSHOT_REPO_ID );
471 repositoriesService.deleteArtifact( artifact );
474 browseService.getArtifactDownloadInfos( "org.apache.archiva.redback.components", "spring-quartz",
475 "2.0-SNAPSHOT", SNAPSHOT_REPO_ID );
477 log.info( "artifacts: {}", artifacts );
479 assertThat( artifacts ).isNotNull().isNotEmpty().hasSize( 8 );
481 assertFalse( Files.exists(artifactFile) );
482 assertFalse( Files.exists(artifactFilemd5 ));
483 assertFalse( Files.exists(artifactFilepom ));
485 catch ( Exception e )
487 log.error( e.getMessage(), e );
496 protected Path initSnapshotRepo()
499 Path targetRepo = getBasedir().resolve( "target/repo-with-snapshots" );
500 if ( Files.exists(targetRepo) )
502 org.apache.archiva.common.utils.FileUtils.deleteDirectory( targetRepo );
504 assertFalse( Files.exists(targetRepo) );
506 FileUtils.copyDirectoryToDirectory( getBasedir().resolve( "src/test/repo-with-snapshots" ).toFile(),
507 targetRepo.getParent().toFile() );
509 if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SNAPSHOT_REPO_ID ) != null )
511 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( SNAPSHOT_REPO_ID, true );
512 assertNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SNAPSHOT_REPO_ID ) );
514 ManagedRepository managedRepository = getTestManagedRepository( SNAPSHOT_REPO_ID, "repo-with-snapshots" );
515 /*managedRepository.setId( SNAPSHOT_REPO_ID );
516 managedRepository.setLocation( );
517 managedRepository.setCronExpression( "* * * * * ?" );*/
518 getManagedRepositoriesService( authorizationHeader ).addManagedRepository( managedRepository );
519 assertNotNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SNAPSHOT_REPO_ID ) );
524 protected void cleanSnapshotRepo()
528 if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SNAPSHOT_REPO_ID ) != null )
532 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( SNAPSHOT_REPO_ID, true );
534 getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SNAPSHOT_REPO_ID ) );
536 catch ( Exception e )
538 log.warn( "skip issue while cleaning test repository: this can cause test failure", e );
544 protected ManagedRepository getTestManagedRepository( String id, String path )
546 String location = Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "target/" + path ).toAbsolutePath().toString();
547 return new ManagedRepository( id, id, location, "default", true, true, true, "2 * * * * ?", null, false, 80, 80,
552 protected ManagedRepository getTestManagedRepository()
554 return getTestManagedRepository( "TEST", "test-repo" );
558 static final String SNAPSHOT_REPO_ID = "snapshot-repo";