]> source.dussan.org Git - archiva.git/blob
3b170401a1d15d02e64a5ed8aa9c08764ab7c783
[archiva.git] /
1 package org.apache.archiva.webdav;
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 com.gargoylesoftware.htmlunit.WebRequest;
23 import com.gargoylesoftware.htmlunit.WebResponse;
24 import org.apache.archiva.configuration.ManagedRepositoryConfiguration;
25 import org.apache.archiva.webdav.httpunit.MkColMethodWebRequest;
26 import org.junit.After;
27 import org.junit.Before;
28 import org.junit.Test;
29
30 import javax.servlet.http.HttpServletResponse;
31 import java.io.InputStream;
32 import java.nio.file.Files;
33 import java.nio.file.Path;
34
35
36 /**
37  * Deploy / Put Test cases for RepositoryServlet.  
38  *
39  *
40  */
41 public class RepositoryServletDeployTest
42     extends AbstractRepositoryServletTestCase
43 {
44     private static final String ARTIFACT_DEFAULT_LAYOUT = "/path/to/artifact/1.0.0/artifact-1.0.0.jar";
45
46     @Before
47     @Override
48     public void setUp() throws Exception
49     {
50         super.setUp();
51         startRepository();
52     }
53
54     @Override
55     @After
56     public void tearDown( ) throws Exception
57     {
58         super.tearDown( );
59     }
60
61     @Test
62     public void testPutWithMissingParentCollection()
63         throws Exception
64     {
65         setupCleanRepo( repoRootInternal );
66
67         String putUrl = "http://machine.com/repository/internal" + ARTIFACT_DEFAULT_LAYOUT;
68         InputStream is = getClass().getResourceAsStream( "/artifact.jar" );
69         // verify that the file exists in resources-dir
70         assertNotNull( "artifact.jar inputstream", is );
71
72         WebRequest request = new PutMethodWebRequest( putUrl, is, "application/octet-stream" );
73
74         WebResponse response = getServletUnitClient().getResponse( request );
75         assertResponseCreated( response );
76         assertFileContents( "artifact.jar\n", repoRootInternal, ARTIFACT_DEFAULT_LAYOUT );
77     }    
78
79     /**
80      * MRM-747
81      * test whether trying to overwrite existing relase-artifact is blocked by returning HTTP-code 409 
82      * 
83      * @throws Exception
84      */
85     @Test
86     public void testReleaseArtifactsRedeploymentValidPath()
87         throws Exception
88     {
89         setupCleanRepo( repoRootInternal );
90
91         String putUrl = "http://machine.com/repository/internal" + ARTIFACT_DEFAULT_LAYOUT;
92         String metadataUrl = "http://machine.com/repository/internal/path/to/artifact/maven-metadata.xml";
93         String checksumUrl = "http://machine.com/repository/internal" + ARTIFACT_DEFAULT_LAYOUT + ".sha1";
94         
95         InputStream is = getClass().getResourceAsStream( "/artifact.jar" );
96         // verify that the file exists in resources-dir
97         assertNotNull( "artifact.jar inputstream", is );
98
99         // send request #1 and verify it's successful
100         WebRequest request = new PutMethodWebRequest( putUrl, is, "application/octet-stream" );
101         WebResponse response = getServletUnitClient().getResponse( request );
102         assertResponseCreated( response );
103         
104         is = getClass().getResourceAsStream( "/artifact.jar.sha1" );
105         request = new PutMethodWebRequest( checksumUrl, is, "application/octet-stream" );
106         response = getServletUnitClient().getResponse( request );
107         assertResponseCreated( response );
108         
109         is = getClass().getResourceAsStream( "/maven-metadata.xml" );
110         request = new PutMethodWebRequest( metadataUrl, is, "application/octet-stream" );
111         response = getServletUnitClient().getResponse( request );
112         assertResponseCreated( response );
113         
114         // send request #2 and verify it's blocked
115         is = getClass().getResourceAsStream( "/artifact.jar" );
116         request = new PutMethodWebRequest( putUrl, is, "application/octet-stream" );
117         response = getServletUnitClient().getResponse( request );
118         assertResponseConflictError( response );        
119     }
120
121     @Test
122     public void testReleaseArtifactsRedeploymentIsAllowed()
123         throws Exception
124     {
125         setupCleanRepo( repoRootInternal );
126         
127         ManagedRepositoryConfiguration managedRepo = archivaConfiguration.getConfiguration().findManagedRepositoryById( REPOID_INTERNAL );
128         managedRepo.setBlockRedeployments( false );
129         
130         saveConfiguration( archivaConfiguration );
131     
132         String putUrl = "http://machine.com/repository/internal" + ARTIFACT_DEFAULT_LAYOUT;
133         String metadataUrl = "http://machine.com/repository/internal/path/to/artifact/maven-metadata.xml";
134         String checksumUrl = "http://machine.com/repository/internal" + ARTIFACT_DEFAULT_LAYOUT + ".sha1";
135         
136         InputStream is = getClass().getResourceAsStream( "/artifact.jar" );
137         // verify that the file exists in resources-dir
138         assertNotNull( "artifact.jar inputstream", is );
139     
140         // send request #1 and verify it's successful
141         WebRequest request = new PutMethodWebRequest( putUrl, is, "application/octet-stream" );
142         WebResponse response = getServletUnitClient().getResponse( request );
143         assertResponseCreated( response );
144         
145         is = getClass().getResourceAsStream( "/artifact.jar.sha1" );
146         request = new PutMethodWebRequest( checksumUrl, is, "application/octet-stream" );
147         response = getServletUnitClient().getResponse( request );
148         assertResponseCreated( response );
149         
150         is = getClass().getResourceAsStream( "/maven-metadata.xml" );
151         request = new PutMethodWebRequest( metadataUrl, is, "application/octet-stream" );
152         response = getServletUnitClient().getResponse( request );
153         assertResponseCreated( response );
154         
155         // send request #2 and verify if it's still successful
156         is = getClass().getResourceAsStream( "/artifact.jar" );
157         request = new PutMethodWebRequest( putUrl, is, "application/octet-stream" );
158         response = getServletUnitClient().getResponse( request );
159         assertResponseNoContent( response );        
160     }
161
162     @Test
163     public void testReleaseArtifactsRedeploymentInvalidPath()
164         throws Exception
165     {
166         setupCleanRepo( repoRootInternal );
167
168         String putUrl = "http://machine.com/repository/internal/artifact.jar";
169         String metadataUrl = "http://machine.com/repository/internal/maven-metadata.xml";
170         String checksumUrl = "http://machine.com/repository/internal/artifact.jar.sha1";
171         
172         InputStream is = getClass().getResourceAsStream( "/artifact.jar" );
173         // verify that the file exists in resources-dir
174         assertNotNull( "artifact.jar inputstream", is );
175
176         // send request #1 and verify it's successful
177         WebRequest request = new PutMethodWebRequest( putUrl, is, "application/octet-stream" );
178         WebResponse response = getServletUnitClient().getResponse( request );
179         assertResponseCreated( response );
180         
181         is = getClass().getResourceAsStream( "/artifact.jar.sha1" );
182         request = new PutMethodWebRequest( checksumUrl, is, "application/octet-stream" );
183         response = getServletUnitClient().getResponse( request );
184         assertResponseCreated( response );
185         
186         is = getClass().getResourceAsStream( "/maven-metadata.xml" );
187         request = new PutMethodWebRequest( metadataUrl, is, "application/octet-stream" );
188         response = getServletUnitClient().getResponse( request );
189         assertResponseCreated( response );
190         
191         // send request #2 and verify it's re-deployed
192         is = getClass().getResourceAsStream( "/artifact.jar" );
193         request = new PutMethodWebRequest( putUrl, is, "application/octet-stream" );
194         response = getServletUnitClient().getResponse( request );
195         assertResponseNoContent( response );
196     } 
197
198     @Test
199     public void testReleaseArtifactsRedeploymentArtifactIsSnapshot()
200         throws Exception
201     {
202         setupCleanRepo( repoRootInternal );
203
204         String putUrl = "http://machine.com/repository/internal/path/to/artifact/1.0-SNAPSHOT/artifact-1.0-SNAPSHOT.jar";
205         String metadataUrl = "http://machine.com/repository/internal/path/to/artifact/maven-metadata.xml";
206         String checksumUrl = "http://machine.com/repository/internal/path/to/artifact/1.0-SNAPSHOT/artifact-1.0-SNAPSHOT.jar.sha1";
207         
208         InputStream is = getClass().getResourceAsStream( "/artifact.jar" );
209         // verify that the file exists in resources-dir
210         assertNotNull( "artifact.jar inputstream", is );
211
212         // send request #1 and verify it's successful
213         WebRequest request = new PutMethodWebRequest( putUrl, is, "application/octet-stream" );
214         WebResponse response = getServletUnitClient().getResponse( request );
215         assertResponseCreated( response );
216         
217         is = getClass().getResourceAsStream( "/artifact.jar.sha1" );
218         request = new PutMethodWebRequest( checksumUrl, is, "application/octet-stream" );
219         response = getServletUnitClient().getResponse( request );
220         assertResponseCreated( response );
221         
222         is = getClass().getResourceAsStream( "/maven-metadata.xml" );
223         request = new PutMethodWebRequest( metadataUrl, is, "application/octet-stream" );
224         response = getServletUnitClient().getResponse( request );
225         assertResponseCreated( response );
226         
227         // send request #2 and verify it's re-deployed
228         is = getClass().getResourceAsStream( "/artifact.jar" );
229         request = new PutMethodWebRequest( putUrl, is, "application/octet-stream" );
230         response = getServletUnitClient().getResponse( request );
231         assertResponseNoContent( response );
232     } 
233
234     @Test
235     public void testMkColWithMissingParentCollectionFails()
236         throws Exception
237     {
238         setupCleanRepo( repoRootInternal );
239
240         String putUrl = "http://machine.com/repository/internal/path/to/";
241
242         WebRequest request = new MkColMethodWebRequest( putUrl );
243
244         WebResponse response = getServletUnitClient().getResponse( request );
245         
246         assertEquals(HttpServletResponse.SC_CONFLICT, response.getStatusCode());
247         
248         Path mkColLocalPath = repoRootInternal.resolve( "path/to/");
249         assertFalse( Files.exists(mkColLocalPath));
250     }
251
252     @Test
253     public void testArtifactsDeploymentArtifactIsSnapshot()
254         throws Exception
255     {
256         setupCleanRepo( repoRootInternal );
257
258         String putUrl = "http://machine.com/repository/internal/path/to/artifact/SNAPSHOT/artifact-SNAPSHOT.jar";
259         String metadataUrl = "http://machine.com/repository/internal/path/to/artifact/maven-metadata.xml";
260         String checksumUrl = "http://machine.com/repository/internal/path/to/artifact/SNAPSHOT/artifact-SNAPSHOT.jar.sha1";
261
262         InputStream is = getClass().getResourceAsStream( "/artifact.jar" );
263         // verify that the file exists in resources-dir
264         assertNotNull( "artifact.jar inputstream", is );
265
266         // send request #1 and verify it's successful
267         WebRequest request = new PutMethodWebRequest( putUrl, is, "application/octet-stream" );
268         WebResponse response = getServletUnitClient().getResponse( request );
269         assertResponseCreated( response );
270
271         is = getClass().getResourceAsStream( "/artifact.jar.sha1" );
272         request = new PutMethodWebRequest( checksumUrl, is, "application/octet-stream" );
273         response = getServletUnitClient().getResponse( request );
274         assertResponseCreated( response );
275
276         is = getClass().getResourceAsStream( "/maven-metadata.xml" );
277         request = new PutMethodWebRequest( metadataUrl, is, "application/octet-stream" );
278         response = getServletUnitClient().getResponse( request );
279         assertResponseCreated( response );
280
281         // send request #2 and verify it's re-deployed
282         is = getClass().getResourceAsStream( "/artifact.jar" );
283         request = new PutMethodWebRequest( putUrl, is, "application/octet-stream" );
284         response = getServletUnitClient().getResponse( request );
285         assertResponseNoContent( response );
286
287         request = new GetMethodWebRequest( putUrl );
288         response = getServletUnitClient().getResponse( request );
289         assertResponseOK( response );
290
291
292     }
293     
294     protected void assertResponseNoContent( WebResponse response )
295     {
296         assertNotNull( "Should have recieved a response", response );
297         assertEquals( "Should have been a 204/NO CONTENT response code.", HttpServletResponse.SC_NO_CONTENT, response
298             .getStatusCode() );
299     }
300     
301     protected void assertResponseCreated( WebResponse response )
302     {
303         assertNotNull( "Should have recieved a response", response );
304         assertEquals( "Should have been a 201/CREATED response code.", HttpServletResponse.SC_CREATED, response
305             .getStatusCode() );
306     }
307 }