]> source.dussan.org Git - archiva.git/blob
1bad73f909022d5577f8c784e2062b294019befe
[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.meterware.httpunit.PutMethodWebRequest;
23 import com.meterware.httpunit.WebRequest;
24 import com.meterware.httpunit.WebResponse;
25
26 import java.io.File;
27 import java.io.InputStream;
28
29 import javax.servlet.http.HttpServletResponse;
30
31 import org.apache.archiva.configuration.ManagedRepositoryConfiguration;
32 import org.apache.archiva.webdav.httpunit.MkColMethodWebRequest;
33 import org.junit.Test;
34
35
36 /**
37  * Deploy / Put Test cases for RepositoryServlet.  
38  *
39  * @version $Id$
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     @Test
47     public void testPutWithMissingParentCollection()
48         throws Exception
49     {
50         setupCleanRepo( repoRootInternal );
51
52         String putUrl = "http://machine.com/repository/internal" + ARTIFACT_DEFAULT_LAYOUT;
53         InputStream is = getClass().getResourceAsStream( "/artifact.jar" );
54         // verify that the file exists in resources-dir
55         assertNotNull( "artifact.jar inputstream", is );
56
57         WebRequest request = new PutMethodWebRequest( putUrl, is, "application/octet-stream" );
58
59         WebResponse response = getServletUnitClient().getResponse( request );
60         assertResponseCreated( response );
61         assertFileContents( "artifact.jar\n", repoRootInternal, ARTIFACT_DEFAULT_LAYOUT );
62     }    
63
64     /**
65      * MRM-747
66      * test whether trying to overwrite existing relase-artifact is blocked by returning HTTP-code 409 
67      * 
68      * @throws Exception
69      */
70     @Test
71     public void testReleaseArtifactsRedeploymentValidPath()
72         throws Exception
73     {
74         setupCleanRepo( repoRootInternal );
75
76         String putUrl = "http://machine.com/repository/internal" + ARTIFACT_DEFAULT_LAYOUT;
77         String metadataUrl = "http://machine.com/repository/internal/path/to/artifact/maven-metadata.xml";
78         String checksumUrl = "http://machine.com/repository/internal" + ARTIFACT_DEFAULT_LAYOUT + ".sha1";
79         
80         InputStream is = getClass().getResourceAsStream( "/artifact.jar" );
81         // verify that the file exists in resources-dir
82         assertNotNull( "artifact.jar inputstream", is );
83
84         // send request #1 and verify it's successful
85         WebRequest request = new PutMethodWebRequest( putUrl, is, "application/octet-stream" );
86         WebResponse response = getServletUnitClient().getResponse( request );
87         assertResponseCreated( response );
88         
89         is = getClass().getResourceAsStream( "/artifact.jar.sha1" );
90         request = new PutMethodWebRequest( checksumUrl, is, "application/octet-stream" );
91         response = getServletUnitClient().getResponse( request );
92         assertResponseCreated( response );
93         
94         is = getClass().getResourceAsStream( "/maven-metadata.xml" );
95         request = new PutMethodWebRequest( metadataUrl, is, "application/octet-stream" );
96         response = getServletUnitClient().getResponse( request );
97         assertResponseCreated( response );
98         
99         // send request #2 and verify it's blocked
100         is = getClass().getResourceAsStream( "/artifact.jar" );
101         request = new PutMethodWebRequest( putUrl, is, "application/octet-stream" );
102         response = getServletUnitClient().getResponse( request );
103         assertResponseConflictError( response );        
104     }
105
106     @Test
107     public void testReleaseArtifactsRedeploymentIsAllowed()
108         throws Exception
109     {
110         setupCleanRepo( repoRootInternal );
111         
112         ManagedRepositoryConfiguration managedRepo = archivaConfiguration.getConfiguration().findManagedRepositoryById( REPOID_INTERNAL );
113         managedRepo.setBlockRedeployments( false );
114         
115         saveConfiguration( archivaConfiguration );
116     
117         String putUrl = "http://machine.com/repository/internal" + ARTIFACT_DEFAULT_LAYOUT;
118         String metadataUrl = "http://machine.com/repository/internal/path/to/artifact/maven-metadata.xml";
119         String checksumUrl = "http://machine.com/repository/internal" + ARTIFACT_DEFAULT_LAYOUT + ".sha1";
120         
121         InputStream is = getClass().getResourceAsStream( "/artifact.jar" );
122         // verify that the file exists in resources-dir
123         assertNotNull( "artifact.jar inputstream", is );
124     
125         // send request #1 and verify it's successful
126         WebRequest request = new PutMethodWebRequest( putUrl, is, "application/octet-stream" );
127         WebResponse response = getServletUnitClient().getResponse( request );
128         assertResponseCreated( response );
129         
130         is = getClass().getResourceAsStream( "/artifact.jar.sha1" );
131         request = new PutMethodWebRequest( checksumUrl, is, "application/octet-stream" );
132         response = getServletUnitClient().getResponse( request );
133         assertResponseCreated( response );
134         
135         is = getClass().getResourceAsStream( "/maven-metadata.xml" );
136         request = new PutMethodWebRequest( metadataUrl, is, "application/octet-stream" );
137         response = getServletUnitClient().getResponse( request );
138         assertResponseCreated( response );
139         
140         // send request #2 and verify if it's still successful
141         is = getClass().getResourceAsStream( "/artifact.jar" );
142         request = new PutMethodWebRequest( putUrl, is, "application/octet-stream" );
143         response = getServletUnitClient().getResponse( request );
144         assertResponseNoContent( response );        
145     }
146
147     @Test
148     public void testReleaseArtifactsRedeploymentInvalidPath()
149         throws Exception
150     {
151         setupCleanRepo( repoRootInternal );
152
153         String putUrl = "http://machine.com/repository/internal/artifact.jar";
154         String metadataUrl = "http://machine.com/repository/internal/maven-metadata.xml";
155         String checksumUrl = "http://machine.com/repository/internal/artifact.jar.sha1";
156         
157         InputStream is = getClass().getResourceAsStream( "/artifact.jar" );
158         // verify that the file exists in resources-dir
159         assertNotNull( "artifact.jar inputstream", is );
160
161         // send request #1 and verify it's successful
162         WebRequest request = new PutMethodWebRequest( putUrl, is, "application/octet-stream" );
163         WebResponse response = getServletUnitClient().getResponse( request );
164         assertResponseCreated( response );
165         
166         is = getClass().getResourceAsStream( "/artifact.jar.sha1" );
167         request = new PutMethodWebRequest( checksumUrl, is, "application/octet-stream" );
168         response = getServletUnitClient().getResponse( request );
169         assertResponseCreated( response );
170         
171         is = getClass().getResourceAsStream( "/maven-metadata.xml" );
172         request = new PutMethodWebRequest( metadataUrl, is, "application/octet-stream" );
173         response = getServletUnitClient().getResponse( request );
174         assertResponseCreated( response );
175         
176         // send request #2 and verify it's re-deployed
177         is = getClass().getResourceAsStream( "/artifact.jar" );
178         request = new PutMethodWebRequest( putUrl, is, "application/octet-stream" );
179         response = getServletUnitClient().getResponse( request );
180         assertResponseNoContent( response );
181     } 
182
183     @Test
184     public void testReleaseArtifactsRedeploymentArtifactIsSnapshot()
185         throws Exception
186     {
187         setupCleanRepo( repoRootInternal );
188
189         String putUrl = "http://machine.com/repository/internal/path/to/artifact/1.0-SNAPSHOT/artifact-1.0-SNAPSHOT.jar";
190         String metadataUrl = "http://machine.com/repository/internal/path/to/artifact/maven-metadata.xml";
191         String checksumUrl = "http://machine.com/repository/internal/path/to/artifact/1.0-SNAPSHOT/artifact-1.0-SNAPSHOT.jar.sha1";
192         
193         InputStream is = getClass().getResourceAsStream( "/artifact.jar" );
194         // verify that the file exists in resources-dir
195         assertNotNull( "artifact.jar inputstream", is );
196
197         // send request #1 and verify it's successful
198         WebRequest request = new PutMethodWebRequest( putUrl, is, "application/octet-stream" );
199         WebResponse response = getServletUnitClient().getResponse( request );
200         assertResponseCreated( response );
201         
202         is = getClass().getResourceAsStream( "/artifact.jar.sha1" );
203         request = new PutMethodWebRequest( checksumUrl, is, "application/octet-stream" );
204         response = getServletUnitClient().getResponse( request );
205         assertResponseCreated( response );
206         
207         is = getClass().getResourceAsStream( "/maven-metadata.xml" );
208         request = new PutMethodWebRequest( metadataUrl, is, "application/octet-stream" );
209         response = getServletUnitClient().getResponse( request );
210         assertResponseCreated( response );
211         
212         // send request #2 and verify it's re-deployed
213         is = getClass().getResourceAsStream( "/artifact.jar" );
214         request = new PutMethodWebRequest( putUrl, is, "application/octet-stream" );
215         response = getServletUnitClient().getResponse( request );
216         assertResponseNoContent( response );
217     } 
218
219     @Test
220     public void testMkColWithMissingParentCollectionFails()
221         throws Exception
222     {
223         setupCleanRepo( repoRootInternal );
224
225         String putUrl = "http://machine.com/repository/internal/path/to/";
226
227         WebRequest request = new MkColMethodWebRequest( putUrl );
228
229         WebResponse response = getServletUnitClient().getResponse( request );
230         
231         assertEquals(HttpServletResponse.SC_CONFLICT, response.getResponseCode());
232         
233         File mkColLocalPath = new File(repoRootInternal, "path/to/");
234         assertFalse(mkColLocalPath.exists());
235     }
236     
237     protected void assertResponseNoContent( WebResponse response )
238     {
239         assertNotNull( "Should have recieved a response", response );
240         assertEquals( "Should have been a 204/NO CONTENT response code.", HttpServletResponse.SC_NO_CONTENT, response
241             .getResponseCode() );
242     }
243     
244     protected void assertResponseCreated( WebResponse response )
245     {
246         assertNotNull( "Should have recieved a response", response );
247         assertEquals( "Should have been a 201/CREATED response code.", HttpServletResponse.SC_CREATED, response
248             .getResponseCode() );
249     }
250 }