]> source.dussan.org Git - archiva.git/blob
111653350a28be06bd60ba51414f2af5cc226aac
[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.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;
31
32 import java.io.File;
33 import java.util.List;
34
35 /**
36  * @author Olivier Lamy
37  */
38 public class RepositoriesServiceTest
39     extends AbstractArchivaRestTest
40 {
41
42     @Test( expected = ServerWebApplicationException.class )
43     public void scanRepoKarmaFailed()
44         throws Exception
45     {
46         RepositoriesService service = getRepositoriesService();
47         try
48         {
49             service.scanRepository( "id", true );
50         }
51         catch ( ServerWebApplicationException e )
52         {
53             assertEquals( 403, e.getStatus() );
54             throw e;
55         }
56     }
57
58     @Test
59     public void scanRepo()
60         throws Exception
61     {
62         RepositoriesService service = getRepositoriesService( authorizationHeader );
63
64         ManagedRepositoriesService managedRepositoriesService = getManagedRepositoriesService( authorizationHeader );
65
66         String repoId = managedRepositoriesService.getManagedRepositories().get( 0 ).getId();
67
68         int timeout = 20000;
69         while ( timeout > 0 && service.alreadyScanning( repoId ) )
70         {
71             Thread.sleep( 500 );
72             timeout -= 500;
73         }
74
75         assertTrue( service.scanRepository( repoId, true ) );
76     }
77
78     @Test( expected = ServerWebApplicationException.class )
79     public void deleteArtifactKarmaFailed()
80         throws Exception
81     {
82         try
83         {
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 );
90
91             RepositoriesService repositoriesService = getRepositoriesService( null );
92
93             repositoriesService.deleteArtifact( artifact );
94         }
95         catch ( ServerWebApplicationException e )
96         {
97             assertEquals( 403, e.getStatus() );
98             throw e;
99
100         }
101     }
102
103     @Test( expected = ServerWebApplicationException.class )
104     public void deleteWithRepoNull()
105         throws Exception
106     {
107         try
108         {
109
110             RepositoriesService repositoriesService = getRepositoriesService( authorizationHeader );
111
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" );
117
118             repositoriesService.deleteArtifact( artifact );
119         }
120         catch ( ServerWebApplicationException e )
121         {
122             assertEquals( "not http 400 status", 400, e.getStatus() );
123             throw e;
124         }
125     }
126
127
128     @Test
129     public void deleteArtifact()
130         throws Exception
131     {
132         initSourceTargetRepo();
133
134         BrowseService browseService = getBrowseService( authorizationHeader, false );
135
136         List<Artifact> artifacts =
137             browseService.getArtifactDownloadInfos( "commons-logging", "commons-logging", "1.0.1", SOURCE_REPO_ID );
138
139         Assertions.assertThat( artifacts ).isNotNull().isNotEmpty().hasSize( 3 );
140
141         log.info( "artifacts.size: {}", artifacts.size() );
142
143         try
144         {
145             File artifactFile =
146                 new File( "target/test-origin-repo/commons-logging/commons-logging/1.0.1/commons-logging-1.0.1.jar" );
147
148             assertTrue( "artifact not exists:" + artifactFile.getPath(), artifactFile.exists() );
149
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 );
156
157             RepositoriesService repositoriesService = getRepositoriesService( authorizationHeader );
158
159             repositoriesService.deleteArtifact( artifact );
160
161             assertFalse( "artifact not deleted exists:" + artifactFile.getPath(), artifactFile.exists() );
162
163             artifacts =
164                 browseService.getArtifactDownloadInfos( "commons-logging", "commons-logging", "1.0.1", SOURCE_REPO_ID );
165
166             Assertions.assertThat( artifacts ).isNotNull().isEmpty();
167
168         }
169         finally
170         {
171             cleanRepos();
172         }
173     }
174
175     @Test
176     public void authorizedToDeleteArtifacts()
177         throws Exception
178     {
179         ManagedRepository managedRepository = getTestManagedRepository( "SOURCE_REPO_ID", "SOURCE_REPO_ID" );
180         try
181         {
182             getManagedRepositoriesService( authorizationHeader ).addManagedRepository( managedRepository );
183             RepositoriesService repositoriesService = getRepositoriesService( authorizationHeader );
184             assertTrue( repositoriesService.isAuthorizedToDeleteArtifacts( managedRepository.getId() ) );
185         }
186         finally
187         {
188             getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( managedRepository.getId(),
189                                                                                           true );
190         }
191     }
192
193     @Test
194     public void notAuthorizedToDeleteArtifacts()
195         throws Exception
196     {
197         ManagedRepository managedRepository = getTestManagedRepository( "SOURCE_REPO_ID", "SOURCE_REPO_ID" );
198         try
199         {
200             getManagedRepositoriesService( authorizationHeader ).addManagedRepository( managedRepository );
201             RepositoriesService repositoriesService = getRepositoriesService( guestAuthzHeader );
202             assertFalse( repositoriesService.isAuthorizedToDeleteArtifacts( managedRepository.getId() ) );
203         }
204         finally
205         {
206             getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( managedRepository.getId(),
207                                                                                           true );
208         }
209     }
210
211     protected ManagedRepository getTestManagedRepository( String id, String path )
212     {
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,
215                                       true, false );
216     }
217
218     protected ManagedRepository getTestManagedRepository()
219     {
220         return getTestManagedRepository( "TEST", "test-repo" );
221     }
222
223 }