]> source.dussan.org Git - archiva.git/blob
c0ad360aa8f0ee1d94265a38afb82c8a1c298549
[archiva.git] /
1 package org.apache.archiva.rest.services;
2
3 /*
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
11  *
12  *   http://www.apache.org/licenses/LICENSE-2.0
13  *
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
19  * under the License.
20  */
21
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;
29
30 import java.io.File;
31
32 /**
33  * @author Olivier Lamy
34  */
35 public class RepositoriesServiceTest
36     extends AbstractArchivaRestTest
37 {
38
39     @Test( expected = ServerWebApplicationException.class )
40     public void scanRepoKarmaFailed()
41         throws Exception
42     {
43         RepositoriesService service = getRepositoriesService();
44         try
45         {
46             service.scanRepository( "id", true );
47         }
48         catch ( ServerWebApplicationException e )
49         {
50             assertEquals( 403, e.getStatus() );
51             throw e;
52         }
53     }
54
55     @Test
56     public void scanRepo()
57         throws Exception
58     {
59         RepositoriesService service = getRepositoriesService( authorizationHeader );
60
61         ManagedRepositoriesService managedRepositoriesService = getManagedRepositoriesService( authorizationHeader );
62
63         String repoId = managedRepositoriesService.getManagedRepositories().get( 0 ).getId();
64
65         int timeout = 20000;
66         while ( timeout > 0 && service.alreadyScanning( repoId ) )
67         {
68             Thread.sleep( 500 );
69             timeout -= 500;
70         }
71
72         assertTrue( service.scanRepository( repoId, true ) );
73     }
74
75     @Test( expected = ServerWebApplicationException.class )
76     public void deleteArtifactKarmaFailed()
77         throws Exception
78     {
79         try
80         {
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" );
86
87             RepositoriesService repositoriesService = getRepositoriesService( null );
88
89             repositoriesService.deleteArtifact( artifact, SOURCE_REPO_ID );
90         }
91         catch ( ServerWebApplicationException e )
92         {
93             assertEquals( 403, e.getStatus() );
94             throw e;
95
96         }
97     }
98
99     @Test( expected = ServerWebApplicationException.class )
100     public void deleteWithRepoNull()
101         throws Exception
102     {
103         try
104         {
105
106             RepositoriesService repositoriesService = getRepositoriesService( authorizationHeader );
107
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" );
113
114             repositoriesService.deleteArtifact( artifact, null );
115         }
116         catch ( ServerWebApplicationException e )
117         {
118             assertEquals( "not http 400 status", 400, e.getStatus() );
119             throw e;
120         }
121     }
122
123
124     @Test
125     public void deleteArtifact()
126         throws Exception
127     {
128         initSourceTargetRepo();
129         try
130         {
131             File artifactFile =
132                 new File( "target/test-origin-repo/commons-logging/commons-logging/1.0.1/commons-logging-1.0.1.jar" );
133
134             assertTrue( "artifact not exists:" + artifactFile.getPath(), artifactFile.exists() );
135
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" );
141
142             RepositoriesService repositoriesService = getRepositoriesService( authorizationHeader );
143
144             repositoriesService.deleteArtifact( artifact, SOURCE_REPO_ID );
145
146             assertFalse( "artifact not deleted exists:" + artifactFile.getPath(), artifactFile.exists() );
147
148         }
149         finally
150         {
151             cleanRepos();
152         }
153     }
154
155     @Test
156     public void authorizedToDeleteArtifacts()
157         throws Exception
158     {
159         ManagedRepository managedRepository = getTestManagedRepository( "SOURCE_REPO_ID", "SOURCE_REPO_ID" );
160         try
161         {
162             getManagedRepositoriesService( authorizationHeader ).addManagedRepository( managedRepository );
163             RepositoriesService repositoriesService = getRepositoriesService( authorizationHeader );
164             assertTrue( repositoriesService.isAuthorizedToDeleteArtifacts( managedRepository.getId() ) );
165         }
166         finally
167         {
168             getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( managedRepository.getId(),
169                                                                                           true );
170         }
171     }
172
173     @Test
174     public void notAuthorizedToDeleteArtifacts()
175         throws Exception
176     {
177         ManagedRepository managedRepository = getTestManagedRepository( "SOURCE_REPO_ID", "SOURCE_REPO_ID" );
178         try
179         {
180             getManagedRepositoriesService( authorizationHeader ).addManagedRepository( managedRepository );
181             RepositoriesService repositoriesService = getRepositoriesService( guestAuthzHeader );
182             assertFalse( repositoriesService.isAuthorizedToDeleteArtifacts( managedRepository.getId() ) );
183         }
184         finally
185         {
186             getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( managedRepository.getId(),
187                                                                                           true );
188         }
189     }
190
191     protected ManagedRepository getTestManagedRepository( String id, String path )
192     {
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,
195                                       true, false );
196     }
197
198     protected ManagedRepository getTestManagedRepository()
199     {
200         return getTestManagedRepository( "TEST", "test-repo" );
201     }
202
203 }