1 package org.apache.archiva.webdav;
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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
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;
30 import javax.servlet.http.HttpServletResponse;
31 import java.io.InputStream;
32 import java.nio.file.Files;
33 import java.nio.file.Path;
37 * Deploy / Put Test cases for RepositoryServlet.
41 public class RepositoryServletDeployTest
42 extends AbstractRepositoryServletTestCase
44 private static final String ARTIFACT_DEFAULT_LAYOUT = "/path/to/artifact/1.0.0/artifact-1.0.0.jar";
48 public void setUp() throws Exception
56 public void tearDown( ) throws Exception
62 public void testPutWithMissingParentCollection()
65 setupCleanRepo( repoRootInternal );
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 );
72 WebRequest request = new PutMethodWebRequest( putUrl, is, "application/octet-stream" );
74 WebResponse response = getServletUnitClient().getResponse( request );
75 assertResponseCreated( response );
76 assertFileContents( "artifact.jar\n", repoRootInternal, ARTIFACT_DEFAULT_LAYOUT );
81 * test whether trying to overwrite existing relase-artifact is blocked by returning HTTP-code 409
86 public void testReleaseArtifactsRedeploymentValidPath()
89 setupCleanRepo( repoRootInternal );
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";
95 InputStream is = getClass().getResourceAsStream( "/artifact.jar" );
96 // verify that the file exists in resources-dir
97 assertNotNull( "artifact.jar inputstream", is );
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 );
104 is = getClass().getResourceAsStream( "/artifact.jar.sha1" );
105 request = new PutMethodWebRequest( checksumUrl, is, "application/octet-stream" );
106 response = getServletUnitClient().getResponse( request );
107 assertResponseCreated( response );
109 is = getClass().getResourceAsStream( "/maven-metadata.xml" );
110 request = new PutMethodWebRequest( metadataUrl, is, "application/octet-stream" );
111 response = getServletUnitClient().getResponse( request );
112 assertResponseCreated( response );
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 );
122 public void testReleaseArtifactsRedeploymentIsAllowed()
125 setupCleanRepo( repoRootInternal );
127 ManagedRepositoryConfiguration managedRepo = archivaConfiguration.getConfiguration().findManagedRepositoryById( REPOID_INTERNAL );
128 managedRepo.setBlockRedeployments( false );
130 saveConfiguration( archivaConfiguration );
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";
136 InputStream is = getClass().getResourceAsStream( "/artifact.jar" );
137 // verify that the file exists in resources-dir
138 assertNotNull( "artifact.jar inputstream", is );
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 );
145 is = getClass().getResourceAsStream( "/artifact.jar.sha1" );
146 request = new PutMethodWebRequest( checksumUrl, is, "application/octet-stream" );
147 response = getServletUnitClient().getResponse( request );
148 assertResponseCreated( response );
150 is = getClass().getResourceAsStream( "/maven-metadata.xml" );
151 request = new PutMethodWebRequest( metadataUrl, is, "application/octet-stream" );
152 response = getServletUnitClient().getResponse( request );
153 assertResponseCreated( response );
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 );
163 public void testReleaseArtifactsRedeploymentInvalidPath()
166 setupCleanRepo( repoRootInternal );
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";
172 InputStream is = getClass().getResourceAsStream( "/artifact.jar" );
173 // verify that the file exists in resources-dir
174 assertNotNull( "artifact.jar inputstream", is );
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 );
181 is = getClass().getResourceAsStream( "/artifact.jar.sha1" );
182 request = new PutMethodWebRequest( checksumUrl, is, "application/octet-stream" );
183 response = getServletUnitClient().getResponse( request );
184 assertResponseCreated( response );
186 is = getClass().getResourceAsStream( "/maven-metadata.xml" );
187 request = new PutMethodWebRequest( metadataUrl, is, "application/octet-stream" );
188 response = getServletUnitClient().getResponse( request );
189 assertResponseCreated( response );
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 );
199 public void testReleaseArtifactsRedeploymentArtifactIsSnapshot()
202 setupCleanRepo( repoRootInternal );
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";
208 InputStream is = getClass().getResourceAsStream( "/artifact.jar" );
209 // verify that the file exists in resources-dir
210 assertNotNull( "artifact.jar inputstream", is );
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 );
217 is = getClass().getResourceAsStream( "/artifact.jar.sha1" );
218 request = new PutMethodWebRequest( checksumUrl, is, "application/octet-stream" );
219 response = getServletUnitClient().getResponse( request );
220 assertResponseCreated( response );
222 is = getClass().getResourceAsStream( "/maven-metadata.xml" );
223 request = new PutMethodWebRequest( metadataUrl, is, "application/octet-stream" );
224 response = getServletUnitClient().getResponse( request );
225 assertResponseCreated( response );
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 );
235 public void testMkColWithMissingParentCollectionFails()
238 setupCleanRepo( repoRootInternal );
240 String putUrl = "http://machine.com/repository/internal/path/to/";
242 WebRequest request = new MkColMethodWebRequest( putUrl );
244 WebResponse response = getServletUnitClient().getResponse( request );
246 assertEquals(HttpServletResponse.SC_CONFLICT, response.getStatusCode());
248 Path mkColLocalPath = repoRootInternal.resolve( "path/to/");
249 assertFalse( Files.exists(mkColLocalPath));
253 public void testArtifactsDeploymentArtifactIsSnapshot()
256 setupCleanRepo( repoRootInternal );
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";
262 InputStream is = getClass().getResourceAsStream( "/artifact.jar" );
263 // verify that the file exists in resources-dir
264 assertNotNull( "artifact.jar inputstream", is );
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 );
271 is = getClass().getResourceAsStream( "/artifact.jar.sha1" );
272 request = new PutMethodWebRequest( checksumUrl, is, "application/octet-stream" );
273 response = getServletUnitClient().getResponse( request );
274 assertResponseCreated( response );
276 is = getClass().getResourceAsStream( "/maven-metadata.xml" );
277 request = new PutMethodWebRequest( metadataUrl, is, "application/octet-stream" );
278 response = getServletUnitClient().getResponse( request );
279 assertResponseCreated( response );
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 );
287 request = new GetMethodWebRequest( putUrl );
288 response = getServletUnitClient().getResponse( request );
289 assertResponseOK( response );
294 protected void assertResponseNoContent( WebResponse response )
296 assertNotNull( "Should have recieved a response", response );
297 assertEquals( "Should have been a 204/NO CONTENT response code.", HttpServletResponse.SC_NO_CONTENT, response
301 protected void assertResponseCreated( WebResponse response )
303 assertNotNull( "Should have recieved a response", response );
304 assertEquals( "Should have been a 201/CREATED response code.", HttpServletResponse.SC_CREATED, response