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.services.BrowseService;
26 import org.apache.archiva.rest.api.services.ManagedRepositoriesService;
27 import org.apache.archiva.rest.api.services.RepositoriesService;
28 import org.apache.cxf.jaxrs.client.ServerWebApplicationException;
29 import org.fest.assertions.Assertions;
30 import org.junit.Test;
33 import java.util.List;
36 * @author Olivier Lamy
38 public class RepositoriesServiceTest
39 extends AbstractArchivaRestTest
42 @Test( expected = ServerWebApplicationException.class )
43 public void scanRepoKarmaFailed()
46 RepositoriesService service = getRepositoriesService();
49 service.scanRepository( "id", true );
51 catch ( ServerWebApplicationException e )
53 assertEquals( 403, e.getStatus() );
59 public void scanRepo()
62 RepositoriesService service = getRepositoriesService( authorizationHeader );
64 ManagedRepositoriesService managedRepositoriesService = getManagedRepositoriesService( authorizationHeader );
66 String repoId = managedRepositoriesService.getManagedRepositories().get( 0 ).getId();
69 while ( timeout > 0 && service.alreadyScanning( repoId ) )
75 assertTrue( service.scanRepository( repoId, true ) );
78 @Test( expected = ServerWebApplicationException.class )
79 public void deleteArtifactKarmaFailed()
84 Artifact artifact = new Artifact();
85 artifact.setGroupId( "commons-logging" );
86 artifact.setArtifactId( "commons-logging" );
87 artifact.setVersion( "1.0.1" );
88 artifact.setPackaging( "jar" );
89 artifact.setContext( SOURCE_REPO_ID );
91 RepositoriesService repositoriesService = getRepositoriesService( null );
93 repositoriesService.deleteArtifact( artifact );
95 catch ( ServerWebApplicationException e )
97 assertEquals( 403, e.getStatus() );
103 @Test( expected = ServerWebApplicationException.class )
104 public void deleteWithRepoNull()
110 RepositoriesService repositoriesService = getRepositoriesService( authorizationHeader );
112 Artifact artifact = new Artifact();
113 artifact.setGroupId( "commons-logging" );
114 artifact.setArtifactId( "commons-logging" );
115 artifact.setVersion( "1.0.1" );
116 artifact.setPackaging( "jar" );
118 repositoriesService.deleteArtifact( artifact );
120 catch ( ServerWebApplicationException e )
122 assertEquals( "not http 400 status", 400, e.getStatus() );
129 public void deleteArtifact()
132 initSourceTargetRepo();
134 BrowseService browseService = getBrowseService( authorizationHeader, false );
136 List<Artifact> artifacts =
137 browseService.getArtifactDownloadInfos( "commons-logging", "commons-logging", "1.0.1", SOURCE_REPO_ID );
139 Assertions.assertThat( artifacts ).isNotNull().isNotEmpty().hasSize( 3 );
141 log.info( "artifacts.size: {}", artifacts.size() );
146 new File( "target/test-origin-repo/commons-logging/commons-logging/1.0.1/commons-logging-1.0.1.jar" );
148 assertTrue( "artifact not exists:" + artifactFile.getPath(), artifactFile.exists() );
150 Artifact artifact = new Artifact();
151 artifact.setGroupId( "commons-logging" );
152 artifact.setArtifactId( "commons-logging" );
153 artifact.setVersion( "1.0.1" );
154 artifact.setPackaging( "jar" );
155 artifact.setContext( SOURCE_REPO_ID );
157 RepositoriesService repositoriesService = getRepositoriesService( authorizationHeader );
159 repositoriesService.deleteArtifact( artifact );
161 assertFalse( "artifact not deleted exists:" + artifactFile.getPath(), artifactFile.exists() );
164 browseService.getArtifactDownloadInfos( "commons-logging", "commons-logging", "1.0.1", SOURCE_REPO_ID );
166 Assertions.assertThat( artifacts ).isNotNull().isEmpty();
176 public void authorizedToDeleteArtifacts()
179 ManagedRepository managedRepository = getTestManagedRepository( "SOURCE_REPO_ID", "SOURCE_REPO_ID" );
182 getManagedRepositoriesService( authorizationHeader ).addManagedRepository( managedRepository );
183 RepositoriesService repositoriesService = getRepositoriesService( authorizationHeader );
184 assertTrue( repositoriesService.isAuthorizedToDeleteArtifacts( managedRepository.getId() ) );
188 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( managedRepository.getId(),
194 public void notAuthorizedToDeleteArtifacts()
197 ManagedRepository managedRepository = getTestManagedRepository( "SOURCE_REPO_ID", "SOURCE_REPO_ID" );
200 getManagedRepositoriesService( authorizationHeader ).addManagedRepository( managedRepository );
201 RepositoriesService repositoriesService = getRepositoriesService( guestAuthzHeader );
202 assertFalse( repositoriesService.isAuthorizedToDeleteArtifacts( managedRepository.getId() ) );
206 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( managedRepository.getId(),
211 protected ManagedRepository getTestManagedRepository( String id, String path )
213 String location = new File( FileUtil.getBasedir(), "target/" + path ).getAbsolutePath();
214 return new ManagedRepository( id, id, location, "default", true, true, true, "2 * * * * ?", null, false, 80, 80,
218 protected ManagedRepository getTestManagedRepository()
220 return getTestManagedRepository( "TEST", "test-repo" );