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.RedbackServiceException;
25 import org.apache.archiva.redback.rest.api.services.UserService;
26 import org.apache.archiva.rest.api.model.BrowseResult;
27 import org.apache.archiva.rest.api.model.BrowseResultEntry;
28 import org.apache.archiva.rest.api.model.VersionsList;
29 import org.apache.archiva.rest.api.services.BrowseService;
30 import org.apache.archiva.rest.api.services.ManagedRepositoriesService;
31 import org.apache.archiva.rest.api.services.RepositoriesService;
32 import org.apache.commons.io.FileUtils;
33 import org.junit.Test;
35 import javax.ws.rs.BadRequestException;
36 import javax.ws.rs.ForbiddenException;
37 import javax.ws.rs.core.Response;
38 import java.nio.file.Files;
39 import java.nio.file.Path;
40 import java.nio.file.Paths;
41 import java.util.List;
42 import java.util.Locale;
44 import static org.assertj.core.api.Assertions.assertThat;
47 * @author Olivier Lamy
49 public class RepositoriesServiceTest
50 extends AbstractArchivaRestTest
53 @Test( expected = ForbiddenException.class )
54 public void scanRepoKarmaFailed()
57 RepositoriesService service = getRepositoriesService();
60 service.scanRepository( "id", true );
62 catch ( ForbiddenException e )
64 assertEquals( 403, e.getResponse().getStatus() );
70 public void scanRepo()
73 RepositoriesService service = getRepositoriesService( authorizationHeader );
75 ManagedRepositoriesService managedRepositoriesService = getManagedRepositoriesService( authorizationHeader );
77 String repoId = managedRepositoriesService.getManagedRepositories().get( 0 ).getId();
80 while ( timeout > 0 && service.alreadyScanning( repoId ) )
86 assertTrue( service.scanRepository( repoId, true ) );
89 @Test( expected = ForbiddenException.class )
90 public void deleteArtifactKarmaFailed()
95 Artifact artifact = new Artifact();
96 artifact.setGroupId( "commons-logging" );
97 artifact.setArtifactId( "commons-logging" );
98 artifact.setVersion( "1.0.1" );
99 artifact.setPackaging( "jar" );
100 artifact.setContext( SOURCE_REPO_ID );
102 RepositoriesService repositoriesService = getRepositoriesService( null );
104 repositoriesService.deleteArtifact( artifact );
106 catch ( ForbiddenException e )
108 assertEquals( 403, e.getResponse().getStatus() );
114 @Test( expected = BadRequestException.class )
115 public void deleteWithRepoNull()
121 RepositoriesService repositoriesService = getRepositoriesService( authorizationHeader );
123 Artifact artifact = new Artifact();
124 artifact.setGroupId( "commons-logging" );
125 artifact.setArtifactId( "commons-logging" );
126 artifact.setVersion( "1.0.1" );
127 artifact.setPackaging( "jar" );
129 repositoriesService.deleteArtifact( artifact );
131 catch ( BadRequestException e )
133 assertEquals( "not http " + Response.Status.BAD_REQUEST.getStatusCode() + " status",
134 Response.Status.BAD_REQUEST.getStatusCode(), e.getResponse().getStatus() );
141 * delete a version of an artifact without packaging
146 public void deleteArtifactVersion()
149 initSourceTargetRepo();
151 BrowseService browseService = getBrowseService( authorizationHeader, false );
153 List<Artifact> artifacts =
154 browseService.getArtifactDownloadInfos( "org.apache.karaf.features", "org.apache.karaf.features.core",
155 "2.2.2", SOURCE_REPO_ID );
157 log.info( "artifacts: {}", artifacts );
159 assertThat( artifacts ).isNotNull().isNotEmpty().hasSize( 2 );
161 VersionsList versionsList =
162 browseService.getVersionsList( "org.apache.karaf.features", "org.apache.karaf.features.core",
164 assertThat( versionsList.getVersions() ).isNotNull().isNotEmpty().hasSize( 2 );
166 log.info( "artifacts.size: {}", artifacts.size() );
170 Path artifactFile = getAppserverBase().resolve(
171 "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" );
173 assertTrue( "artifact not exists:" + artifactFile, Files.exists(artifactFile) );
175 Artifact artifact = new Artifact();
176 artifact.setGroupId( "org.apache.karaf.features" );
177 artifact.setArtifactId( "org.apache.karaf.features.core" );
178 artifact.setVersion( "2.2.2" );
179 artifact.setContext( SOURCE_REPO_ID );
181 RepositoriesService repositoriesService = getRepositoriesService( authorizationHeader );
183 repositoriesService.deleteArtifact( artifact );
185 assertFalse( "artifact not deleted exists:" + artifactFile, Files.exists(artifactFile) );
188 browseService.getArtifactDownloadInfos( "org.apache.karaf.features", "org.apache.karaf.features.core",
189 "2.2.2", SOURCE_REPO_ID );
191 assertThat( artifacts ).isNotNull().isEmpty();
193 versionsList = browseService.getVersionsList( "org.apache.karaf.features", "org.apache.karaf.features.core",
196 assertThat( versionsList.getVersions() ).isNotNull().isNotEmpty().hasSize( 1 );
207 public void deleteArtifact()
210 initSourceTargetRepo();
212 BrowseService browseService = getBrowseService( authorizationHeader, false );
214 List<Artifact> artifacts =
215 browseService.getArtifactDownloadInfos( "org.apache.karaf.features", "org.apache.karaf.features.core",
216 "2.2.2", SOURCE_REPO_ID );
218 log.info( "artifacts: {}", artifacts );
220 assertThat( artifacts ).isNotNull().isNotEmpty().hasSize( 2 );
222 VersionsList versionsList =
223 browseService.getVersionsList( "org.apache.karaf.features", "org.apache.karaf.features.core",
225 assertThat( versionsList.getVersions() ).isNotNull().isNotEmpty().hasSize( 2 );
227 log.info( "artifacts.size: {}", artifacts.size() );
231 Path artifactFile = getAppserverBase().resolve(
232 "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" );
234 assertTrue( "artifact not exists:" + artifactFile.toString(), Files.exists(artifactFile) );
236 Artifact artifact = new Artifact();
237 artifact.setGroupId( "org.apache.karaf.features" );
238 artifact.setArtifactId( "org.apache.karaf.features.core" );
239 artifact.setVersion( "2.2.2" );
240 artifact.setPackaging( "jar" );
241 artifact.setContext( SOURCE_REPO_ID );
243 RepositoriesService repositoriesService = getRepositoriesService( authorizationHeader );
245 repositoriesService.deleteArtifact( artifact );
247 assertFalse( "artifact not deleted exists:" + artifactFile, Files.exists(artifactFile) );
250 browseService.getArtifactDownloadInfos( "org.apache.karaf.features", "org.apache.karaf.features.core",
251 "2.2.2", SOURCE_REPO_ID );
253 assertThat( artifacts ).isNotNull().isEmpty();
255 versionsList = browseService.getVersionsList( "org.apache.karaf.features", "org.apache.karaf.features.core",
258 assertThat( versionsList.getVersions() ).isNotNull().isNotEmpty().hasSize( 1 );
268 public void deleteArtifactWithClassifier()
271 initSourceTargetRepo();
273 BrowseService browseService = getBrowseService( authorizationHeader, false );
275 List<Artifact> artifacts =
276 browseService.getArtifactDownloadInfos( "commons-logging", "commons-logging", "1.0.1", SOURCE_REPO_ID );
278 assertThat( artifacts ).isNotNull().isNotEmpty().hasSize( 3 );
280 VersionsList versionsList =
281 browseService.getVersionsList( "commons-logging", "commons-logging", SOURCE_REPO_ID );
282 assertThat( versionsList.getVersions() ).isNotNull().isNotEmpty().hasSize( 6 );
284 log.info( "artifacts.size: {}", artifacts.size() );
288 Path artifactFile = getAppserverBase().resolve(
289 "data/repositories/test-origin-repo/commons-logging/commons-logging/1.0.1/commons-logging-1.0.1-javadoc.jar" );
291 Path artifactFilemd5 = getAppserverBase().resolve("data/repositories/test-origin-repo/commons-logging/commons-logging/1.0.1/commons-logging-1.0.1-javadoc.jar.md5" );
293 Path artifactFilesha1 = getAppserverBase().resolve("data/repositories/test-origin-repo/commons-logging/commons-logging/1.0.1/commons-logging-1.0.1-javadoc.jar.sha1" );
295 assertTrue( "artifact not exists:" + artifactFile, Files.exists(artifactFile) );
297 assertTrue( "md5 not exists:" + artifactFilemd5, Files.exists(artifactFilemd5) );
298 assertTrue( "sha1 not exists:" + artifactFilesha1, Files.exists(artifactFilesha1) );
300 Artifact artifact = new Artifact();
301 artifact.setGroupId( "commons-logging" );
302 artifact.setArtifactId( "commons-logging" );
303 artifact.setVersion( "1.0.1" );
304 artifact.setClassifier( "javadoc" );
305 artifact.setPackaging( "jar" );
306 artifact.setType( "javadoc" );
307 artifact.setContext( SOURCE_REPO_ID );
309 RepositoriesService repositoriesService = getRepositoriesService( authorizationHeader );
311 repositoriesService.deleteArtifact( artifact );
313 assertFalse( "artifact not deleted exists:" + artifactFile, Files.exists(artifactFile) );
314 assertFalse( "md5 still exists:" + artifactFilemd5, Files.exists(artifactFilemd5) );
315 assertFalse( "sha1 still exists:" + artifactFilesha1, Files.exists(artifactFilesha1) );
318 browseService.getArtifactDownloadInfos( "commons-logging", "commons-logging", "1.0.1", SOURCE_REPO_ID );
320 log.info( "artifact: {}", artifacts );
322 assertThat( artifacts ).isNotNull().isNotEmpty().hasSize( 2 );
324 versionsList = browseService.getVersionsList( "commons-logging", "commons-logging", SOURCE_REPO_ID );
326 log.info( "versionsList: {}", versionsList );
328 assertThat( versionsList.getVersions() ).isNotNull().isNotEmpty().hasSize( 6 );
339 public void deleteGroupId()
342 initSourceTargetRepo();
345 BrowseService browseService = getBrowseService( authorizationHeader, false );
347 BrowseResult browseResult = browseService.browseGroupId( "org.apache.karaf.features", SOURCE_REPO_ID );
349 assertNotNull( browseResult );
351 log.info( "browseResult: {}", browseResult );
353 assertThat( browseResult.getBrowseResultEntries() ).isNotNull().isNotEmpty().contains(
354 new BrowseResultEntry( "org.apache.karaf.features.org.apache.karaf.features.command", true ),
355 new BrowseResultEntry( "org.apache.karaf.features.org.apache.karaf.features.core", true ) );
358 getAppserverBase().resolve("data/repositories/test-origin-repo/org/apache/karaf/features/org.apache.karaf.features.command" );
360 assertTrue( "directory not exists", Files.exists(directory) );
362 RepositoriesService repositoriesService = getRepositoriesService( authorizationHeader );
363 repositoriesService.deleteGroupId( "org.apache.karaf", SOURCE_REPO_ID );
365 assertFalse( "directory not exists", Files.exists(directory) );
367 browseResult = browseService.browseGroupId( "org.apache.karaf.features", SOURCE_REPO_ID );
369 assertNotNull( browseResult );
371 assertThat( browseResult.getBrowseResultEntries() ).isNotNull().isEmpty();
373 browseResult = browseService.browseGroupId( "org.apache.karaf", SOURCE_REPO_ID );
375 assertNotNull( browseResult );
377 assertThat( browseResult.getBrowseResultEntries() ).isNotNull().isEmpty();
379 log.info( "browseResult empty: {}", browseResult );
388 public void authorizedToDeleteArtifacts()
391 ManagedRepository managedRepository = getTestManagedRepository( "SOURCE_REPO_ID", "SOURCE_REPO_ID" );
394 getManagedRepositoriesService( authorizationHeader ).addManagedRepository( managedRepository );
395 RepositoriesService repositoriesService = getRepositoriesService( authorizationHeader );
396 assertTrue( repositoriesService.isAuthorizedToDeleteArtifacts( managedRepository.getId() ) );
400 cleanQuietlyRepo( managedRepository.getId() );
405 public void notAuthorizedToDeleteArtifacts()
408 UserService userService = getUserService( getAdminAuthzHeader() );
409 userService.createGuestUser( );
411 ManagedRepository managedRepository = getTestManagedRepository( "SOURCE_REPO_ID", "SOURCE_REPO_ID" );
414 getManagedRepositoriesService( authorizationHeader ).addManagedRepository( managedRepository );
415 RepositoriesService repositoriesService = getRepositoriesService( );
416 assertFalse( repositoriesService.isAuthorizedToDeleteArtifacts( managedRepository.getId() ) );
420 cleanQuietlyRepo( managedRepository.getId() );
424 protected void cleanQuietlyRepo( String id )
428 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( id, true );
430 catch ( Exception e )
432 log.info( "ignore issue deleting test repo: {}", e.getMessage() );
437 public void deleteSnapshot()
440 Path targetRepo = initSnapshotRepo();
444 RepositoriesService repositoriesService = getRepositoriesService( authorizationHeader );
445 //repositoriesService.scanRepositoryDirectoriesNow( SNAPSHOT_REPO_ID );
447 BrowseService browseService = getBrowseService( authorizationHeader, false );
448 List<Artifact> artifacts =
449 browseService.getArtifactDownloadInfos( "org.apache.archiva.redback.components", "spring-quartz",
450 "2.0-SNAPSHOT", SNAPSHOT_REPO_ID );
452 log.info( "artifacts: {}", artifacts );
454 assertThat( artifacts ).isNotNull().isNotEmpty().hasSize( 10 );
456 Path artifactFile = targetRepo.resolve(
457 "org/apache/archiva/redback/components/spring-quartz/2.0-SNAPSHOT/spring-quartz-2.0-20120618.214127-1.jar" );
459 Path artifactFilemd5 = targetRepo.resolve(
460 "org/apache/archiva/redback/components/spring-quartz/2.0-SNAPSHOT/spring-quartz-2.0-20120618.214127-1.jar.md5" );
462 Path artifactFilepom = targetRepo.resolve(
463 "org/apache/archiva/redback/components/spring-quartz/2.0-SNAPSHOT/spring-quartz-2.0-20120618.214127-1.pom" );
465 assertTrue( Files.exists(artifactFile) );
466 assertTrue( Files.exists(artifactFilemd5) );
467 assertTrue( Files.exists(artifactFilepom ));
469 // we delete only one snapshot
471 new Artifact( "org.apache.archiva.redback.components", "spring-quartz", "2.0-20120618.214127-1" );
472 artifact.setPackaging( "jar" );
473 artifact.setRepositoryId( SNAPSHOT_REPO_ID );
474 artifact.setContext( SNAPSHOT_REPO_ID );
476 repositoriesService.deleteArtifact( artifact );
479 browseService.getArtifactDownloadInfos( "org.apache.archiva.redback.components", "spring-quartz",
480 "2.0-SNAPSHOT", SNAPSHOT_REPO_ID );
482 log.info( "artifacts: {}", artifacts );
484 assertThat( artifacts ).isNotNull().isNotEmpty().hasSize( 8 );
486 assertFalse( Files.exists(artifactFile) );
487 assertFalse( Files.exists(artifactFilemd5 ));
488 assertFalse( Files.exists(artifactFilepom ));
490 catch ( Exception e )
492 log.error( e.getMessage(), e );
501 protected Path initSnapshotRepo()
504 Path targetRepo = getAppserverBase().resolve("data/repositories/repo-with-snapshots" );
505 if ( Files.exists(targetRepo) )
507 org.apache.archiva.common.utils.FileUtils.deleteDirectory( targetRepo );
509 assertFalse( Files.exists(targetRepo) );
511 FileUtils.copyDirectoryToDirectory( getProjectDirectory().resolve( "src/test/repo-with-snapshots" ).toFile(),
512 targetRepo.getParent().toFile() );
514 if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SNAPSHOT_REPO_ID ) != null )
516 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( SNAPSHOT_REPO_ID, true );
517 assertNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SNAPSHOT_REPO_ID ) );
519 ManagedRepository managedRepository = getTestManagedRepository( SNAPSHOT_REPO_ID, "repo-with-snapshots" );
520 /*managedRepository.setId( SNAPSHOT_REPO_ID );
521 managedRepository.setLocation( );
522 managedRepository.setCronExpression( "* * * * * ?" );*/
523 getManagedRepositoriesService( authorizationHeader ).addManagedRepository( managedRepository );
524 assertNotNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SNAPSHOT_REPO_ID ) );
529 protected void cleanSnapshotRepo()
533 if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SNAPSHOT_REPO_ID ) != null )
537 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( SNAPSHOT_REPO_ID, true );
539 getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SNAPSHOT_REPO_ID ) );
541 catch ( Exception e )
543 log.warn( "skip issue while cleaning test repository: this can cause test failure", e );
549 protected ManagedRepository getTestManagedRepository( String id, String path )
551 String location = getAppserverBase().resolve("data/repositories/" + path ).toAbsolutePath().toString();
552 return new ManagedRepository( Locale.getDefault(), id, id, location, "default", true, true, true, "2 * * * * ?", null, false, 80, 80,
557 protected ManagedRepository getTestManagedRepository()
559 return getTestManagedRepository( "TEST", "test-repo" );
563 static final String SNAPSHOT_REPO_ID = "snapshot-repo";