Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

RepositoryServletDeployTest.java 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. package org.apache.archiva.webdav;
  2. /*
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. */
  20. import com.gargoylesoftware.htmlunit.WebRequest;
  21. import com.gargoylesoftware.htmlunit.WebResponse;
  22. import org.apache.archiva.configuration.ManagedRepositoryConfiguration;
  23. import org.apache.archiva.webdav.mock.httpunit.MkColMethodWebRequest;
  24. import org.junit.After;
  25. import org.junit.Before;
  26. import org.junit.Test;
  27. import javax.servlet.http.HttpServletResponse;
  28. import java.io.InputStream;
  29. import java.nio.file.Files;
  30. import java.nio.file.Path;
  31. /**
  32. * Deploy / Put Test cases for RepositoryServlet.
  33. *
  34. *
  35. */
  36. public class RepositoryServletDeployTest
  37. extends AbstractRepositoryServletTestCase
  38. {
  39. private static final String ARTIFACT_DEFAULT_LAYOUT = "/path/to/artifact/1.0.0/artifact-1.0.0.jar";
  40. @Before
  41. @Override
  42. public void setUp() throws Exception
  43. {
  44. super.setUp();
  45. startRepository();
  46. }
  47. @Override
  48. @After
  49. public void tearDown( ) throws Exception
  50. {
  51. super.tearDown( );
  52. }
  53. @Test
  54. public void testPutWithMissingParentCollection()
  55. throws Exception
  56. {
  57. setupCleanRepo( repoRootInternal );
  58. String putUrl = "http://machine.com/repository/internal" + ARTIFACT_DEFAULT_LAYOUT;
  59. InputStream is = getClass().getResourceAsStream( "/artifact.jar" );
  60. // verify that the file exists in resources-dir
  61. assertNotNull( "artifact.jar inputstream", is );
  62. WebRequest request = new PutMethodWebRequest( putUrl, is, "application/octet-stream" );
  63. WebResponse response = getServletUnitClient().getResponse( request );
  64. assertResponseCreated( response );
  65. assertFileContents( "artifact.jar\n", repoRootInternal, ARTIFACT_DEFAULT_LAYOUT );
  66. }
  67. /**
  68. * MRM-747
  69. * test whether trying to overwrite existing relase-artifact is blocked by returning HTTP-code 409
  70. *
  71. * @throws Exception
  72. */
  73. @Test
  74. public void testReleaseArtifactsRedeploymentValidPath()
  75. throws Exception
  76. {
  77. setupCleanRepo( repoRootInternal );
  78. String putUrl = "http://machine.com/repository/internal" + ARTIFACT_DEFAULT_LAYOUT;
  79. String metadataUrl = "http://machine.com/repository/internal/path/to/artifact/maven-metadata.xml";
  80. String checksumUrl = "http://machine.com/repository/internal" + ARTIFACT_DEFAULT_LAYOUT + ".sha1";
  81. InputStream is = getClass().getResourceAsStream( "/artifact.jar" );
  82. // verify that the file exists in resources-dir
  83. assertNotNull( "artifact.jar inputstream", is );
  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. is = getClass().getResourceAsStream( "/artifact.jar.sha1" );
  89. request = new PutMethodWebRequest( checksumUrl, is, "application/octet-stream" );
  90. response = getServletUnitClient().getResponse( request );
  91. assertResponseCreated( response );
  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. // send request #2 and verify it's blocked
  97. is = getClass().getResourceAsStream( "/artifact.jar" );
  98. request = new PutMethodWebRequest( putUrl, is, "application/octet-stream" );
  99. response = getServletUnitClient().getResponse( request );
  100. assertResponseConflictError( response );
  101. }
  102. @Test
  103. public void testReleaseArtifactsRedeploymentIsAllowed()
  104. throws Exception
  105. {
  106. setupCleanRepo( repoRootInternal );
  107. ManagedRepositoryConfiguration managedRepo = archivaConfiguration.getConfiguration().findManagedRepositoryById( REPOID_INTERNAL );
  108. managedRepo.setBlockRedeployments( false );
  109. saveConfiguration( archivaConfiguration );
  110. String putUrl = "http://machine.com/repository/internal" + ARTIFACT_DEFAULT_LAYOUT;
  111. String metadataUrl = "http://machine.com/repository/internal/path/to/artifact/maven-metadata.xml";
  112. String checksumUrl = "http://machine.com/repository/internal" + ARTIFACT_DEFAULT_LAYOUT + ".sha1";
  113. InputStream is = getClass().getResourceAsStream( "/artifact.jar" );
  114. // verify that the file exists in resources-dir
  115. assertNotNull( "artifact.jar inputstream", is );
  116. // send request #1 and verify it's successful
  117. WebRequest request = new PutMethodWebRequest( putUrl, is, "application/octet-stream" );
  118. WebResponse response = getServletUnitClient().getResponse( request );
  119. assertResponseCreated( response );
  120. is = getClass().getResourceAsStream( "/artifact.jar.sha1" );
  121. request = new PutMethodWebRequest( checksumUrl, is, "application/octet-stream" );
  122. response = getServletUnitClient().getResponse( request );
  123. assertResponseCreated( response );
  124. is = getClass().getResourceAsStream( "/maven-metadata.xml" );
  125. request = new PutMethodWebRequest( metadataUrl, is, "application/octet-stream" );
  126. response = getServletUnitClient().getResponse( request );
  127. assertResponseCreated( response );
  128. // send request #2 and verify if it's still successful
  129. is = getClass().getResourceAsStream( "/artifact.jar" );
  130. request = new PutMethodWebRequest( putUrl, is, "application/octet-stream" );
  131. response = getServletUnitClient().getResponse( request );
  132. assertResponseNoContent( response );
  133. }
  134. @Test
  135. public void testReleaseArtifactsRedeploymentInvalidPath()
  136. throws Exception
  137. {
  138. setupCleanRepo( repoRootInternal );
  139. String putUrl = "http://machine.com/repository/internal/artifact.jar";
  140. String metadataUrl = "http://machine.com/repository/internal/maven-metadata.xml";
  141. String checksumUrl = "http://machine.com/repository/internal/artifact.jar.sha1";
  142. InputStream is = getClass().getResourceAsStream( "/artifact.jar" );
  143. // verify that the file exists in resources-dir
  144. assertNotNull( "artifact.jar inputstream", is );
  145. // send request #1 and verify it's successful
  146. WebRequest request = new PutMethodWebRequest( putUrl, is, "application/octet-stream" );
  147. WebResponse response = getServletUnitClient().getResponse( request );
  148. assertResponseCreated( response );
  149. is = getClass().getResourceAsStream( "/artifact.jar.sha1" );
  150. request = new PutMethodWebRequest( checksumUrl, is, "application/octet-stream" );
  151. response = getServletUnitClient().getResponse( request );
  152. assertResponseCreated( response );
  153. is = getClass().getResourceAsStream( "/maven-metadata.xml" );
  154. request = new PutMethodWebRequest( metadataUrl, is, "application/octet-stream" );
  155. response = getServletUnitClient().getResponse( request );
  156. assertResponseCreated( response );
  157. // send request #2 and verify it's re-deployed
  158. is = getClass().getResourceAsStream( "/artifact.jar" );
  159. request = new PutMethodWebRequest( putUrl, is, "application/octet-stream" );
  160. response = getServletUnitClient().getResponse( request );
  161. assertResponseNoContent( response );
  162. }
  163. @Test
  164. public void testReleaseArtifactsRedeploymentArtifactIsSnapshot()
  165. throws Exception
  166. {
  167. setupCleanRepo( repoRootInternal );
  168. String putUrl = "http://machine.com/repository/internal/path/to/artifact/1.0-SNAPSHOT/artifact-1.0-SNAPSHOT.jar";
  169. String metadataUrl = "http://machine.com/repository/internal/path/to/artifact/maven-metadata.xml";
  170. String checksumUrl = "http://machine.com/repository/internal/path/to/artifact/1.0-SNAPSHOT/artifact-1.0-SNAPSHOT.jar.sha1";
  171. InputStream is = getClass().getResourceAsStream( "/artifact.jar" );
  172. // verify that the file exists in resources-dir
  173. assertNotNull( "artifact.jar inputstream", is );
  174. // send request #1 and verify it's successful
  175. WebRequest request = new PutMethodWebRequest( putUrl, is, "application/octet-stream" );
  176. WebResponse response = getServletUnitClient().getResponse( request );
  177. assertResponseCreated( response );
  178. is = getClass().getResourceAsStream( "/artifact.jar.sha1" );
  179. request = new PutMethodWebRequest( checksumUrl, is, "application/octet-stream" );
  180. response = getServletUnitClient().getResponse( request );
  181. assertResponseCreated( response );
  182. is = getClass().getResourceAsStream( "/maven-metadata.xml" );
  183. request = new PutMethodWebRequest( metadataUrl, is, "application/octet-stream" );
  184. response = getServletUnitClient().getResponse( request );
  185. assertResponseCreated( response );
  186. // send request #2 and verify it's re-deployed
  187. is = getClass().getResourceAsStream( "/artifact.jar" );
  188. request = new PutMethodWebRequest( putUrl, is, "application/octet-stream" );
  189. response = getServletUnitClient().getResponse( request );
  190. assertResponseNoContent( response );
  191. }
  192. @Test
  193. public void testMkColWithMissingParentCollectionFails()
  194. throws Exception
  195. {
  196. setupCleanRepo( repoRootInternal );
  197. String putUrl = "http://machine.com/repository/internal/path/to/";
  198. WebRequest request = new MkColMethodWebRequest( putUrl );
  199. WebResponse response = getServletUnitClient().getResponse( request );
  200. assertEquals(HttpServletResponse.SC_CONFLICT, response.getStatusCode());
  201. Path mkColLocalPath = repoRootInternal.resolve( "path/to/");
  202. assertFalse( Files.exists(mkColLocalPath));
  203. }
  204. @Test
  205. public void testArtifactsDeploymentArtifactIsSnapshot()
  206. throws Exception
  207. {
  208. setupCleanRepo( repoRootInternal );
  209. String putUrl = "http://machine.com/repository/internal/path/to/artifact/SNAPSHOT/artifact-SNAPSHOT.jar";
  210. String metadataUrl = "http://machine.com/repository/internal/path/to/artifact/maven-metadata.xml";
  211. String checksumUrl = "http://machine.com/repository/internal/path/to/artifact/SNAPSHOT/artifact-SNAPSHOT.jar.sha1";
  212. InputStream is = getClass().getResourceAsStream( "/artifact.jar" );
  213. // verify that the file exists in resources-dir
  214. assertNotNull( "artifact.jar inputstream", is );
  215. // send request #1 and verify it's successful
  216. WebRequest request = new PutMethodWebRequest( putUrl, is, "application/octet-stream" );
  217. WebResponse response = getServletUnitClient().getResponse( request );
  218. assertResponseCreated( response );
  219. is = getClass().getResourceAsStream( "/artifact.jar.sha1" );
  220. request = new PutMethodWebRequest( checksumUrl, is, "application/octet-stream" );
  221. response = getServletUnitClient().getResponse( request );
  222. assertResponseCreated( response );
  223. is = getClass().getResourceAsStream( "/maven-metadata.xml" );
  224. request = new PutMethodWebRequest( metadataUrl, is, "application/octet-stream" );
  225. response = getServletUnitClient().getResponse( request );
  226. 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 );
  232. request = new GetMethodWebRequest( putUrl );
  233. response = getServletUnitClient().getResponse( request );
  234. assertResponseOK( response );
  235. }
  236. protected void assertResponseNoContent( WebResponse response )
  237. {
  238. assertNotNull( "Should have recieved a response", response );
  239. assertEquals( "Should have been a 204/NO CONTENT response code.", HttpServletResponse.SC_NO_CONTENT, response
  240. .getStatusCode() );
  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. }