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.apache.cxf.jaxrs.client.ServerWebApplicationException;
33 import org.fest.assertions.api.Assertions;
34 import org.junit.Test;
37 import java.util.List;
40 * @author Olivier Lamy
42 public class RepositoriesServiceTest
43 extends AbstractArchivaRestTest
46 @Test( expected = ServerWebApplicationException.class )
47 public void scanRepoKarmaFailed()
50 RepositoriesService service = getRepositoriesService();
53 service.scanRepository( "id", true );
55 catch ( ServerWebApplicationException e )
57 assertEquals( 403, e.getStatus() );
63 public void scanRepo()
66 RepositoriesService service = getRepositoriesService( authorizationHeader );
68 ManagedRepositoriesService managedRepositoriesService = getManagedRepositoriesService( authorizationHeader );
70 String repoId = managedRepositoriesService.getManagedRepositories().get( 0 ).getId();
73 while ( timeout > 0 && service.alreadyScanning( repoId ) )
79 assertTrue( service.scanRepository( repoId, true ) );
82 @Test( expected = ServerWebApplicationException.class )
83 public void deleteArtifactKarmaFailed()
88 Artifact artifact = new Artifact();
89 artifact.setGroupId( "commons-logging" );
90 artifact.setArtifactId( "commons-logging" );
91 artifact.setVersion( "1.0.1" );
92 artifact.setPackaging( "jar" );
93 artifact.setContext( SOURCE_REPO_ID );
95 RepositoriesService repositoriesService = getRepositoriesService( null );
97 repositoriesService.deleteArtifact( artifact );
99 catch ( ServerWebApplicationException e )
101 assertEquals( 403, e.getStatus() );
107 @Test( expected = ServerWebApplicationException.class )
108 public void deleteWithRepoNull()
114 RepositoriesService repositoriesService = getRepositoriesService( authorizationHeader );
116 Artifact artifact = new Artifact();
117 artifact.setGroupId( "commons-logging" );
118 artifact.setArtifactId( "commons-logging" );
119 artifact.setVersion( "1.0.1" );
120 artifact.setPackaging( "jar" );
122 repositoriesService.deleteArtifact( artifact );
124 catch ( ServerWebApplicationException e )
126 assertEquals( "not http 400 status", 400, e.getStatus() );
133 public void deleteArtifact()
136 initSourceTargetRepo();
138 BrowseService browseService = getBrowseService( authorizationHeader, false );
140 List<Artifact> artifacts =
141 browseService.getArtifactDownloadInfos( "org.apache.karaf.features", "org.apache.karaf.features.core",
142 "2.2.2", SOURCE_REPO_ID );
144 log.info( "artifacts: {}", artifacts );
146 Assertions.assertThat( artifacts ).isNotNull().isNotEmpty().hasSize( 2 );
148 VersionsList versionsList =
149 browseService.getVersionsList( "org.apache.karaf.features", "org.apache.karaf.features.core",
151 Assertions.assertThat( versionsList.getVersions() ).isNotNull().isNotEmpty().hasSize( 2 );
153 log.info( "artifacts.size: {}", artifacts.size() );
157 File artifactFile = new File(
158 "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" );
160 assertTrue( "artifact not exists:" + artifactFile.getPath(), artifactFile.exists() );
162 Artifact artifact = new Artifact();
163 artifact.setGroupId( "org.apache.karaf.features" );
164 artifact.setArtifactId( "org.apache.karaf.features.core" );
165 artifact.setVersion( "2.2.2" );
166 artifact.setPackaging( "jar" );
167 artifact.setContext( SOURCE_REPO_ID );
169 RepositoriesService repositoriesService = getRepositoriesService( authorizationHeader );
171 repositoriesService.deleteArtifact( artifact );
173 assertFalse( "artifact not deleted exists:" + artifactFile.getPath(), artifactFile.exists() );
176 browseService.getArtifactDownloadInfos( "org.apache.karaf.features", "org.apache.karaf.features.core",
177 "2.2.2", SOURCE_REPO_ID );
179 Assertions.assertThat( artifacts ).isNotNull().isEmpty();
181 versionsList = browseService.getVersionsList( "org.apache.karaf.features", "org.apache.karaf.features.core",
184 Assertions.assertThat( versionsList.getVersions() ).isNotNull().isNotEmpty().hasSize( 1 );
194 public void deleteArtifactWithClassifier()
197 initSourceTargetRepo();
199 BrowseService browseService = getBrowseService( authorizationHeader, false );
201 List<Artifact> artifacts =
202 browseService.getArtifactDownloadInfos( "commons-logging", "commons-logging", "1.0.1", SOURCE_REPO_ID );
204 Assertions.assertThat( artifacts ).isNotNull().isNotEmpty().hasSize( 3 );
206 VersionsList versionsList =
207 browseService.getVersionsList( "commons-logging", "commons-logging", SOURCE_REPO_ID );
208 Assertions.assertThat( versionsList.getVersions() ).isNotNull().isNotEmpty().hasSize( 6 );
210 log.info( "artifacts.size: {}", artifacts.size() );
214 File artifactFile = new File(
215 "target/test-origin-repo/commons-logging/commons-logging/1.0.1/commons-logging-1.0.1-javadoc.jar" );
217 File artifactFilemd5 = new File(
218 "target/test-origin-repo/commons-logging/commons-logging/1.0.1/commons-logging-1.0.1-javadoc.jar.md5" );
220 File artifactFilesha1 = new File(
221 "target/test-origin-repo/commons-logging/commons-logging/1.0.1/commons-logging-1.0.1-javadoc.jar.sha1" );
223 assertTrue( "artifact not exists:" + artifactFile.getPath(), artifactFile.exists() );
225 assertTrue( "md5 not exists:" + artifactFilemd5.getPath(), artifactFilemd5.exists() );
226 assertTrue( "sha1 not exists:" + artifactFilesha1.getPath(), artifactFilesha1.exists() );
228 Artifact artifact = new Artifact();
229 artifact.setGroupId( "commons-logging" );
230 artifact.setArtifactId( "commons-logging" );
231 artifact.setVersion( "1.0.1" );
232 artifact.setClassifier( "javadoc" );
233 artifact.setPackaging( "jar" );
234 artifact.setContext( SOURCE_REPO_ID );
236 RepositoriesService repositoriesService = getRepositoriesService( authorizationHeader );
238 repositoriesService.deleteArtifact( artifact );
240 assertFalse( "artifact not deleted exists:" + artifactFile.getPath(), artifactFile.exists() );
241 assertFalse( "md5 still exists:" + artifactFilemd5.getPath(), artifactFilemd5.exists() );
242 assertFalse( "sha1 still exists:" + artifactFilesha1.getPath(), artifactFilesha1.exists() );
245 browseService.getArtifactDownloadInfos( "commons-logging", "commons-logging", "1.0.1", SOURCE_REPO_ID );
247 log.info( "artifact: {}", artifacts );
249 Assertions.assertThat( artifacts ).isNotNull().isNotEmpty().hasSize( 2 );
251 versionsList = browseService.getVersionsList( "commons-logging", "commons-logging", SOURCE_REPO_ID );
253 log.info( "versionsList: {}", versionsList );
255 Assertions.assertThat( versionsList.getVersions() ).isNotNull().isNotEmpty().hasSize( 6 );
266 public void deleteGroupId()
269 initSourceTargetRepo();
272 BrowseService browseService = getBrowseService( authorizationHeader, false );
274 BrowseResult browseResult = browseService.browseGroupId( "org.apache.karaf.features", SOURCE_REPO_ID );
276 assertNotNull( browseResult );
278 log.info( "browseResult: {}", browseResult );
280 Assertions.assertThat( browseResult.getBrowseResultEntries() ).isNotNull().isNotEmpty().contains(
281 new BrowseResultEntry( "org.apache.karaf.features.org.apache.karaf.features.command", true ),
282 new BrowseResultEntry( "org.apache.karaf.features.org.apache.karaf.features.core", true ) );
285 new File( "target/test-origin-repo/org/apache/karaf/features/org.apache.karaf.features.command" );
287 assertTrue( "directory not exists", directory.exists() );
289 RepositoriesService repositoriesService = getRepositoriesService( authorizationHeader );
290 repositoriesService.deleteGroupId( "org.apache.karaf.features", SOURCE_REPO_ID );
292 assertFalse( "directory not exists", directory.exists() );
294 browseResult = browseService.browseGroupId( "org.apache.karaf.features", SOURCE_REPO_ID );
296 assertNotNull( browseResult );
298 Assertions.assertThat( browseResult.getBrowseResultEntries() ).isNotNull().isEmpty();
300 log.info( "browseResult: {}", browseResult );
309 public void authorizedToDeleteArtifacts()
312 ManagedRepository managedRepository = getTestManagedRepository( "SOURCE_REPO_ID", "SOURCE_REPO_ID" );
315 getManagedRepositoriesService( authorizationHeader ).addManagedRepository( managedRepository );
316 RepositoriesService repositoriesService = getRepositoriesService( authorizationHeader );
317 assertTrue( repositoriesService.isAuthorizedToDeleteArtifacts( managedRepository.getId() ) );
321 cleanQuietlyRepo( managedRepository.getId() );
326 public void notAuthorizedToDeleteArtifacts()
329 ManagedRepository managedRepository = getTestManagedRepository( "SOURCE_REPO_ID", "SOURCE_REPO_ID" );
332 getManagedRepositoriesService( authorizationHeader ).addManagedRepository( managedRepository );
333 RepositoriesService repositoriesService = getRepositoriesService( guestAuthzHeader );
334 assertFalse( repositoriesService.isAuthorizedToDeleteArtifacts( managedRepository.getId() ) );
338 cleanQuietlyRepo( managedRepository.getId() );
342 protected void cleanQuietlyRepo( String id )
346 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( id, true );
348 catch ( Exception e )
350 log.info( "ignore issue deleting test repo: {}", e.getMessage() );
355 public void deleteSnapshot()
358 File targetRepo = initSnapshotRepo();
362 RepositoriesService repositoriesService = getRepositoriesService( authorizationHeader );
363 //repositoriesService.scanRepositoryDirectoriesNow( SNAPSHOT_REPO_ID );
365 BrowseService browseService = getBrowseService( authorizationHeader, false );
366 List<Artifact> artifacts =
367 browseService.getArtifactDownloadInfos( "org.apache.archiva.redback.components", "spring-quartz",
368 "2.0-SNAPSHOT", SNAPSHOT_REPO_ID );
370 log.info( "artifacts: {}", artifacts );
372 Assertions.assertThat( artifacts ).isNotNull().isNotEmpty().hasSize( 10 );
374 File artifactFile = new File( targetRepo,
375 "org/apache/archiva/redback/components/spring-quartz/2.0-SNAPSHOT/spring-quartz-2.0-20120618.214127-1.jar" );
377 File artifactFilemd5 = new File( targetRepo,
378 "org/apache/archiva/redback/components/spring-quartz/2.0-SNAPSHOT/spring-quartz-2.0-20120618.214127-1.jar.md5" );
380 File artifactFilepom = new File( targetRepo,
381 "org/apache/archiva/redback/components/spring-quartz/2.0-SNAPSHOT/spring-quartz-2.0-20120618.214127-1.pom" );
383 Assertions.assertThat( artifactFile ).exists();
384 Assertions.assertThat( artifactFilemd5 ).exists();
385 Assertions.assertThat( artifactFilepom ).exists();
387 // we delete only one snapshot
389 new Artifact( "org.apache.archiva.redback.components", "spring-quartz", "2.0-20120618.214127-1" );
390 artifact.setPackaging( "jar" );
391 artifact.setRepositoryId( SNAPSHOT_REPO_ID );
392 artifact.setContext( SNAPSHOT_REPO_ID );
394 repositoriesService.deleteArtifact( artifact );
397 browseService.getArtifactDownloadInfos( "org.apache.archiva.redback.components", "spring-quartz",
398 "2.0-SNAPSHOT", SNAPSHOT_REPO_ID );
400 log.info( "artifacts: {}", artifacts );
402 Assertions.assertThat( artifacts ).isNotNull().isNotEmpty().hasSize( 8 );
404 Assertions.assertThat( artifactFile ).doesNotExist();
405 Assertions.assertThat( artifactFilemd5 ).doesNotExist();
406 Assertions.assertThat( artifactFilepom ).doesNotExist();
408 catch ( Exception e )
410 log.error( e.getMessage(), e );
419 protected File initSnapshotRepo()
422 File targetRepo = new File( getBasedir(), "target/repo-with-snapshots" );
423 if ( targetRepo.exists() )
425 FileUtils.deleteDirectory( targetRepo );
427 assertFalse( targetRepo.exists() );
429 FileUtils.copyDirectoryToDirectory( new File( getBasedir(), "src/test/repo-with-snapshots" ),
430 targetRepo.getParentFile() );
432 if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SNAPSHOT_REPO_ID ) != null )
434 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( SNAPSHOT_REPO_ID, true );
435 assertNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SNAPSHOT_REPO_ID ) );
437 ManagedRepository managedRepository = getTestManagedRepository( SNAPSHOT_REPO_ID, "repo-with-snapshots" );
438 /*managedRepository.setId( SNAPSHOT_REPO_ID );
439 managedRepository.setLocation( );
440 managedRepository.setCronExpression( "* * * * * ?" );*/
441 getManagedRepositoriesService( authorizationHeader ).addManagedRepository( managedRepository );
442 assertNotNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SNAPSHOT_REPO_ID ) );
447 protected void cleanSnapshotRepo()
451 if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SNAPSHOT_REPO_ID ) != null )
455 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( SNAPSHOT_REPO_ID, true );
457 getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SNAPSHOT_REPO_ID ) );
459 catch ( Exception e )
461 log.warn( "skip issue while cleaning test repository: this can cause test failure", e );
467 protected ManagedRepository getTestManagedRepository( String id, String path )
469 String location = new File( FileUtil.getBasedir(), "target/" + path ).getAbsolutePath();
470 return new ManagedRepository( id, id, location, "default", true, true, true, "2 * * * * ?", null, false, 80, 80,
474 protected ManagedRepository getTestManagedRepository()
476 return getTestManagedRepository( "TEST", "test-repo" );
480 static final String SNAPSHOT_REPO_ID = "snapshot-repo";