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.ManagedRepositoriesService;
26 import org.apache.archiva.rest.api.services.RepositoriesService;
27 import org.apache.cxf.jaxrs.client.ServerWebApplicationException;
28 import org.junit.Test;
33 * @author Olivier Lamy
35 public class RepositoriesServiceTest
36 extends AbstractArchivaRestTest
39 @Test( expected = ServerWebApplicationException.class )
40 public void scanRepoKarmaFailed()
43 RepositoriesService service = getRepositoriesService();
46 service.scanRepository( "id", true );
48 catch ( ServerWebApplicationException e )
50 assertEquals( 403, e.getStatus() );
56 public void scanRepo()
59 RepositoriesService service = getRepositoriesService( authorizationHeader );
61 ManagedRepositoriesService managedRepositoriesService = getManagedRepositoriesService( authorizationHeader );
63 String repoId = managedRepositoriesService.getManagedRepositories().get( 0 ).getId();
66 while ( timeout > 0 && service.alreadyScanning( repoId ) )
72 assertTrue( service.scanRepository( repoId, true ) );
75 @Test( expected = ServerWebApplicationException.class )
76 public void deleteArtifactKarmaFailed()
81 Artifact artifact = new Artifact();
82 artifact.setGroupId( "commons-logging" );
83 artifact.setArtifactId( "commons-logging" );
84 artifact.setVersion( "1.0.1" );
85 artifact.setPackaging( "jar" );
87 RepositoriesService repositoriesService = getRepositoriesService( null );
89 repositoriesService.deleteArtifact( artifact, SOURCE_REPO_ID );
91 catch ( ServerWebApplicationException e )
93 assertEquals( 403, e.getStatus() );
99 @Test( expected = ServerWebApplicationException.class )
100 public void deleteWithRepoNull()
106 RepositoriesService repositoriesService = getRepositoriesService( authorizationHeader );
108 Artifact artifact = new Artifact();
109 artifact.setGroupId( "commons-logging" );
110 artifact.setArtifactId( "commons-logging" );
111 artifact.setVersion( "1.0.1" );
112 artifact.setPackaging( "jar" );
114 repositoriesService.deleteArtifact( artifact, null );
116 catch ( ServerWebApplicationException e )
118 assertEquals( "not http 400 status", 400, e.getStatus() );
125 public void deleteArtifact()
128 initSourceTargetRepo();
132 new File( "target/test-origin-repo/commons-logging/commons-logging/1.0.1/commons-logging-1.0.1.jar" );
134 assertTrue( "artifact not exists:" + artifactFile.getPath(), artifactFile.exists() );
136 Artifact artifact = new Artifact();
137 artifact.setGroupId( "commons-logging" );
138 artifact.setArtifactId( "commons-logging" );
139 artifact.setVersion( "1.0.1" );
140 artifact.setPackaging( "jar" );
142 RepositoriesService repositoriesService = getRepositoriesService( authorizationHeader );
144 repositoriesService.deleteArtifact( artifact, SOURCE_REPO_ID );
146 assertFalse( "artifact not deleted exists:" + artifactFile.getPath(), artifactFile.exists() );
156 public void authorizedToDeleteArtifacts()
159 ManagedRepository managedRepository = getTestManagedRepository( "SOURCE_REPO_ID", "SOURCE_REPO_ID" );
162 getManagedRepositoriesService( authorizationHeader ).addManagedRepository( managedRepository );
163 RepositoriesService repositoriesService = getRepositoriesService( authorizationHeader );
164 assertTrue( repositoriesService.isAuthorizedToDeleteArtifacts( managedRepository.getId() ) );
168 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( managedRepository.getId(),
174 public void notAuthorizedToDeleteArtifacts()
177 ManagedRepository managedRepository = getTestManagedRepository( "SOURCE_REPO_ID", "SOURCE_REPO_ID" );
180 getManagedRepositoriesService( authorizationHeader ).addManagedRepository( managedRepository );
181 RepositoriesService repositoriesService = getRepositoriesService( guestAuthzHeader );
182 assertFalse( repositoriesService.isAuthorizedToDeleteArtifacts( managedRepository.getId() ) );
186 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( managedRepository.getId(),
191 protected ManagedRepository getTestManagedRepository( String id, String path )
193 String location = new File( FileUtil.getBasedir(), "target/" + path ).getAbsolutePath();
194 return new ManagedRepository( id, id, location, "default", true, true, true, "2 * * * * ?", null, false, 80, 80,
198 protected ManagedRepository getTestManagedRepository()
200 return getTestManagedRepository( "TEST", "test-repo" );