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