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