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.rest.api.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.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 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( managedRepository.getId(),
327 public void notAuthorizedToDeleteArtifacts()
330 ManagedRepository managedRepository = getTestManagedRepository( "SOURCE_REPO_ID", "SOURCE_REPO_ID" );
333 getManagedRepositoriesService( authorizationHeader ).addManagedRepository( managedRepository );
334 RepositoriesService repositoriesService = getRepositoriesService( guestAuthzHeader );
335 assertFalse( repositoriesService.isAuthorizedToDeleteArtifacts( managedRepository.getId() ) );
339 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( managedRepository.getId(),
345 public void deleteSnapshot()
348 File targetRepo = initSnapshotRepo();
352 RepositoriesService repositoriesService = getRepositoriesService( authorizationHeader );
353 //repositoriesService.scanRepositoryDirectoriesNow( SNAPSHOT_REPO_ID );
355 BrowseService browseService = getBrowseService( authorizationHeader, false );
356 List<Artifact> artifacts =
357 browseService.getArtifactDownloadInfos( "org.apache.archiva.redback.components", "spring-quartz",
358 "2.0-SNAPSHOT", SNAPSHOT_REPO_ID );
360 log.info( "artifacts: {}", artifacts );
362 Assertions.assertThat( artifacts ).isNotNull().isNotEmpty().hasSize( 10 );
364 File artifactFile = new File( targetRepo,
365 "org/apache/archiva/redback/components/spring-quartz/2.0-SNAPSHOT/spring-quartz-2.0-20120618.214127-1.jar" );
367 File artifactFilemd5 = new File( targetRepo,
368 "org/apache/archiva/redback/components/spring-quartz/2.0-SNAPSHOT/spring-quartz-2.0-20120618.214127-1.jar.md5" );
370 File artifactFilepom = new File( targetRepo,
371 "org/apache/archiva/redback/components/spring-quartz/2.0-SNAPSHOT/spring-quartz-2.0-20120618.214127-1.pom" );
373 Assertions.assertThat( artifactFile ).exists();
374 Assertions.assertThat( artifactFilemd5 ).exists();
375 Assertions.assertThat( artifactFilepom ).exists();
377 // we delete only one snapshot
379 new Artifact( "org.apache.archiva.redback.components", "spring-quartz", "2.0-20120618.214127-1" );
380 artifact.setPackaging( "jar" );
381 artifact.setRepositoryId( SNAPSHOT_REPO_ID );
382 artifact.setContext( SNAPSHOT_REPO_ID );
384 repositoriesService.deleteArtifact( artifact );
388 browseService.getArtifactDownloadInfos( "org.apache.archiva.redback.components", "spring-quartz",
389 "2.0-SNAPSHOT", SNAPSHOT_REPO_ID );
391 log.info( "artifacts: {}", artifacts );
393 Assertions.assertThat( artifacts ).isNotNull().isNotEmpty().hasSize( 8 );
395 Assertions.assertThat( artifactFile ).doesNotExist();
396 Assertions.assertThat( artifactFilemd5 ).doesNotExist();
397 Assertions.assertThat( artifactFilepom ).doesNotExist();
399 catch ( Exception e )
401 log.error( e.getMessage(), e );
410 protected File initSnapshotRepo()
413 File targetRepo = new File( getBasedir(), "target/repo-with-snapshots" );
414 if ( targetRepo.exists() )
416 FileUtils.deleteDirectory( targetRepo );
418 assertFalse( targetRepo.exists() );
420 FileUtils.copyDirectoryToDirectory( new File( getBasedir(), "src/test/repo-with-snapshots" ),
421 targetRepo.getParentFile() );
423 if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SNAPSHOT_REPO_ID ) != null )
425 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( SNAPSHOT_REPO_ID, true );
426 assertNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SNAPSHOT_REPO_ID ) );
428 ManagedRepository managedRepository =
429 getTestManagedRepository( SNAPSHOT_REPO_ID, "repo-with-snapshots" );
430 /*managedRepository.setId( SNAPSHOT_REPO_ID );
431 managedRepository.setLocation( );
432 managedRepository.setCronExpression( "* * * * * ?" );*/
433 getManagedRepositoriesService( authorizationHeader ).addManagedRepository( managedRepository );
434 assertNotNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SNAPSHOT_REPO_ID ) );
439 protected void cleanSnapshotRepo()
443 if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SNAPSHOT_REPO_ID ) != null )
445 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( SNAPSHOT_REPO_ID, true );
446 assertNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SNAPSHOT_REPO_ID ) );
451 protected ManagedRepository getTestManagedRepository( String id, String path )
453 String location = new File( FileUtil.getBasedir(), "target/" + path ).getAbsolutePath();
454 return new ManagedRepository( id, id, location, "default", true, true, true, "2 * * * * ?", null, false, 80, 80,
458 protected ManagedRepository getTestManagedRepository()
460 return getTestManagedRepository( "TEST", "test-repo" );
464 static final String SNAPSHOT_REPO_ID = "snapshot-repo";