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.redback.rest.api.services.UserService;
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;
37 import java.nio.file.Files;
38 import java.nio.file.Path;
39 import java.util.List;
40 import java.util.Locale;
42 import static org.assertj.core.api.Assertions.assertThat;
45 * @author Olivier Lamy
47 public class RepositoriesServiceTest
48 extends AbstractArchivaRestTest
51 @Test( expected = ForbiddenException.class )
52 public void scanRepoKarmaFailed()
55 RepositoriesService service = getRepositoriesService();
58 service.scanRepository( "id", true );
60 catch ( ForbiddenException e )
62 assertEquals( 403, e.getResponse().getStatus() );
68 public void scanRepo()
71 RepositoriesService service = getRepositoriesService( authorizationHeader );
73 ManagedRepositoriesService managedRepositoriesService = getManagedRepositoriesService( authorizationHeader );
75 String repoId = managedRepositoriesService.getManagedRepositories().get( 0 ).getId();
78 while ( timeout > 0 && service.getScanStatus( repoId ).isAlreadyScanning() )
84 assertTrue( service.scanRepository( repoId, true ).isSuccess() );
87 @Test( expected = ForbiddenException.class )
88 public void deleteArtifactKarmaFailed()
93 Artifact artifact = new Artifact();
94 artifact.setGroupId( "commons-logging" );
95 artifact.setArtifactId( "commons-logging" );
96 artifact.setVersion( "1.0.1" );
97 artifact.setPackaging( "jar" );
98 artifact.setContext( SOURCE_REPO_ID );
100 RepositoriesService repositoriesService = getRepositoriesService( null );
102 repositoriesService.deleteArtifact( artifact );
104 catch ( ForbiddenException e )
106 assertEquals( 403, e.getResponse().getStatus() );
112 @Test( expected = BadRequestException.class )
113 public void deleteWithRepoNull()
119 RepositoriesService repositoriesService = getRepositoriesService( authorizationHeader );
121 Artifact artifact = new Artifact();
122 artifact.setGroupId( "commons-logging" );
123 artifact.setArtifactId( "commons-logging" );
124 artifact.setVersion( "1.0.1" );
125 artifact.setPackaging( "jar" );
127 repositoriesService.deleteArtifact( artifact );
129 catch ( BadRequestException e )
131 assertEquals( "not http " + Response.Status.BAD_REQUEST.getStatusCode() + " status",
132 Response.Status.BAD_REQUEST.getStatusCode(), e.getResponse().getStatus() );
139 * delete a version of an artifact without packaging
144 public void deleteArtifactVersion()
147 initSourceTargetRepo();
149 BrowseService browseService = getBrowseService( authorizationHeader, false );
151 List<Artifact> artifacts =
152 browseService.getArtifactDownloadInfos( "org.apache.karaf.features", "org.apache.karaf.features.core",
153 "2.2.2", SOURCE_REPO_ID );
155 log.info( "artifacts: {}", artifacts );
157 assertThat( artifacts ).isNotNull().isNotEmpty().hasSize( 2 );
159 VersionsList versionsList =
160 browseService.getVersionsList( "org.apache.karaf.features", "org.apache.karaf.features.core",
162 assertThat( versionsList.getVersions() ).isNotNull().isNotEmpty().hasSize( 2 );
164 log.info( "artifacts.size: {}", artifacts.size() );
168 Path artifactFile = getAppserverBase().resolve(
169 "data/repositories/test-origin-repo/org/apache/karaf/features/org.apache.karaf.features.core/2.2.2/org.apache.karaf.features.core-2.2.2.jar" );
171 assertTrue( "artifact not exists:" + artifactFile, Files.exists(artifactFile) );
173 Artifact artifact = new Artifact();
174 artifact.setGroupId( "org.apache.karaf.features" );
175 artifact.setArtifactId( "org.apache.karaf.features.core" );
176 artifact.setVersion( "2.2.2" );
177 artifact.setContext( SOURCE_REPO_ID );
179 RepositoriesService repositoriesService = getRepositoriesService( authorizationHeader );
181 repositoriesService.deleteArtifact( artifact );
183 assertFalse( "artifact not deleted exists:" + artifactFile, Files.exists(artifactFile) );
186 browseService.getArtifactDownloadInfos( "org.apache.karaf.features", "org.apache.karaf.features.core",
187 "2.2.2", SOURCE_REPO_ID );
189 assertThat( artifacts ).isNotNull().isEmpty();
191 versionsList = browseService.getVersionsList( "org.apache.karaf.features", "org.apache.karaf.features.core",
194 assertThat( versionsList.getVersions() ).isNotNull().isNotEmpty().hasSize( 1 );
205 public void deleteArtifact()
208 initSourceTargetRepo();
210 BrowseService browseService = getBrowseService( authorizationHeader, false );
212 List<Artifact> artifacts =
213 browseService.getArtifactDownloadInfos( "org.apache.karaf.features", "org.apache.karaf.features.core",
214 "2.2.2", SOURCE_REPO_ID );
216 log.info( "artifacts: {}", artifacts );
218 assertThat( artifacts ).isNotNull().isNotEmpty().hasSize( 2 );
220 VersionsList versionsList =
221 browseService.getVersionsList( "org.apache.karaf.features", "org.apache.karaf.features.core",
223 assertThat( versionsList.getVersions() ).isNotNull().isNotEmpty().hasSize( 2 );
225 log.info( "artifacts.size: {}", artifacts.size() );
229 Path artifactFile = getAppserverBase().resolve(
230 "data/repositories/test-origin-repo/org/apache/karaf/features/org.apache.karaf.features.core/2.2.2/org.apache.karaf.features.core-2.2.2.jar" );
232 assertTrue( "artifact not exists:" + artifactFile.toString(), Files.exists(artifactFile) );
234 Artifact artifact = new Artifact();
235 artifact.setGroupId( "org.apache.karaf.features" );
236 artifact.setArtifactId( "org.apache.karaf.features.core" );
237 artifact.setVersion( "2.2.2" );
238 artifact.setPackaging( "jar" );
239 artifact.setContext( SOURCE_REPO_ID );
241 RepositoriesService repositoriesService = getRepositoriesService( authorizationHeader );
243 repositoriesService.deleteArtifact( artifact );
245 assertFalse( "artifact not deleted exists:" + artifactFile, Files.exists(artifactFile) );
248 browseService.getArtifactDownloadInfos( "org.apache.karaf.features", "org.apache.karaf.features.core",
249 "2.2.2", SOURCE_REPO_ID );
251 assertThat( artifacts ).isNotNull().isEmpty();
253 versionsList = browseService.getVersionsList( "org.apache.karaf.features", "org.apache.karaf.features.core",
256 assertThat( versionsList.getVersions() ).isNotNull().isNotEmpty().hasSize( 1 );
266 public void deleteArtifactWithClassifier()
269 initSourceTargetRepo();
271 BrowseService browseService = getBrowseService( authorizationHeader, false );
273 List<Artifact> artifacts =
274 browseService.getArtifactDownloadInfos( "commons-logging", "commons-logging", "1.0.1", SOURCE_REPO_ID );
276 assertThat( artifacts ).isNotNull().isNotEmpty().hasSize( 3 );
278 VersionsList versionsList =
279 browseService.getVersionsList( "commons-logging", "commons-logging", SOURCE_REPO_ID );
280 assertThat( versionsList.getVersions() ).isNotNull().isNotEmpty().hasSize( 6 );
282 log.info( "artifacts.size: {}", artifacts.size() );
286 Path artifactFile = getAppserverBase().resolve(
287 "data/repositories/test-origin-repo/commons-logging/commons-logging/1.0.1/commons-logging-1.0.1-javadoc.jar" );
289 Path artifactFilemd5 = getAppserverBase().resolve("data/repositories/test-origin-repo/commons-logging/commons-logging/1.0.1/commons-logging-1.0.1-javadoc.jar.md5" );
291 Path artifactFilesha1 = getAppserverBase().resolve("data/repositories/test-origin-repo/commons-logging/commons-logging/1.0.1/commons-logging-1.0.1-javadoc.jar.sha1" );
293 assertTrue( "artifact not exists:" + artifactFile, Files.exists(artifactFile) );
295 assertTrue( "md5 not exists:" + artifactFilemd5, Files.exists(artifactFilemd5) );
296 assertTrue( "sha1 not exists:" + artifactFilesha1, Files.exists(artifactFilesha1) );
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.setType( "javadoc" );
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 getAppserverBase().resolve("data/repositories/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.getPermissionStatus( managedRepository.getId() ).isAuthorizedToDeleteArtifacts() );
398 cleanQuietlyRepo( managedRepository.getId() );
403 public void notAuthorizedToDeleteArtifacts()
406 UserService userService = getUserService( getAdminAuthzHeader() );
407 userService.createGuestUser( );
409 ManagedRepository managedRepository = getTestManagedRepository( "SOURCE_REPO_ID", "SOURCE_REPO_ID" );
412 getManagedRepositoriesService( authorizationHeader ).addManagedRepository( managedRepository );
413 RepositoriesService repositoriesService = getRepositoriesService( );
414 assertFalse( repositoriesService.getPermissionStatus( managedRepository.getId() ).isAuthorizedToDeleteArtifacts() );
418 cleanQuietlyRepo( managedRepository.getId() );
422 protected void cleanQuietlyRepo( String id )
426 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( id, true );
428 catch ( Exception e )
430 log.info( "ignore issue deleting test repo: {}", e.getMessage() );
435 public void deleteSnapshot()
438 Path targetRepo = initSnapshotRepo();
442 RepositoriesService repositoriesService = getRepositoriesService( authorizationHeader );
443 //repositoriesService.scanRepositoryDirectoriesNow( SNAPSHOT_REPO_ID );
445 BrowseService browseService = getBrowseService( authorizationHeader, false );
446 List<Artifact> artifacts =
447 browseService.getArtifactDownloadInfos( "org.apache.archiva.redback.components", "spring-quartz",
448 "2.0-SNAPSHOT", SNAPSHOT_REPO_ID );
450 log.info( "artifacts: {}", artifacts );
452 assertThat( artifacts ).isNotNull().isNotEmpty().hasSize( 10 );
454 Path artifactFile = targetRepo.resolve(
455 "org/apache/archiva/redback/components/spring-quartz/2.0-SNAPSHOT/spring-quartz-2.0-20120618.214127-1.jar" );
457 Path artifactFilemd5 = targetRepo.resolve(
458 "org/apache/archiva/redback/components/spring-quartz/2.0-SNAPSHOT/spring-quartz-2.0-20120618.214127-1.jar.md5" );
460 Path artifactFilepom = targetRepo.resolve(
461 "org/apache/archiva/redback/components/spring-quartz/2.0-SNAPSHOT/spring-quartz-2.0-20120618.214127-1.pom" );
463 assertTrue( Files.exists(artifactFile) );
464 assertTrue( Files.exists(artifactFilemd5) );
465 assertTrue( Files.exists(artifactFilepom ));
467 // we delete only one snapshot
469 new Artifact( "org.apache.archiva.redback.components", "spring-quartz", "2.0-20120618.214127-1" );
470 artifact.setPackaging( "jar" );
471 artifact.setRepositoryId( SNAPSHOT_REPO_ID );
472 artifact.setContext( SNAPSHOT_REPO_ID );
474 repositoriesService.deleteArtifact( artifact );
477 browseService.getArtifactDownloadInfos( "org.apache.archiva.redback.components", "spring-quartz",
478 "2.0-SNAPSHOT", SNAPSHOT_REPO_ID );
480 log.info( "artifacts: {}", artifacts );
482 assertThat( artifacts ).isNotNull().isNotEmpty().hasSize( 8 );
484 assertFalse( Files.exists(artifactFile) );
485 assertFalse( Files.exists(artifactFilemd5 ));
486 assertFalse( Files.exists(artifactFilepom ));
488 catch ( Exception e )
490 log.error( e.getMessage(), e );
499 protected Path initSnapshotRepo()
502 Path targetRepo = getAppserverBase().resolve("data/repositories/repo-with-snapshots" );
503 if ( Files.exists(targetRepo) )
505 org.apache.archiva.common.utils.FileUtils.deleteDirectory( targetRepo );
507 assertFalse( Files.exists(targetRepo) );
509 FileUtils.copyDirectoryToDirectory( getProjectDirectory().resolve( "src/test/repo-with-snapshots" ).toFile(),
510 targetRepo.getParent().toFile() );
512 if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SNAPSHOT_REPO_ID ) != null )
514 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( SNAPSHOT_REPO_ID, true );
515 assertNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SNAPSHOT_REPO_ID ) );
517 ManagedRepository managedRepository = getTestManagedRepository( SNAPSHOT_REPO_ID, "repo-with-snapshots" );
518 /*managedRepository.setId( SNAPSHOT_REPO_ID );
519 managedRepository.setLocation( );
520 managedRepository.setCronExpression( "* * * * * ?" );*/
521 getManagedRepositoriesService( authorizationHeader ).addManagedRepository( managedRepository );
522 assertNotNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SNAPSHOT_REPO_ID ) );
527 protected void cleanSnapshotRepo()
531 if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SNAPSHOT_REPO_ID ) != null )
535 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( SNAPSHOT_REPO_ID, true );
537 getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SNAPSHOT_REPO_ID ) );
539 catch ( Exception e )
541 log.warn( "skip issue while cleaning test repository: this can cause test failure", e );
547 protected ManagedRepository getTestManagedRepository( String id, String path )
549 String location = getAppserverBase().resolve("data/repositories/" + path ).toAbsolutePath().toString();
550 return new ManagedRepository( Locale.getDefault(), id, id, location, "default", true, true, true, "2 * * * * ?", null, false, 80, 80,
555 protected ManagedRepository getTestManagedRepository()
557 return getTestManagedRepository( "TEST", "test-repo" );
561 static final String SNAPSHOT_REPO_ID = "snapshot-repo";