You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ManagedDefaultTransferTest.java 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. package org.apache.archiva.maven.proxy;
  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. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. import org.apache.archiva.policies.CachedFailuresPolicy;
  20. import org.apache.archiva.policies.ChecksumPolicy;
  21. import org.apache.archiva.policies.ReleasesPolicy;
  22. import org.apache.archiva.policies.SnapshotsPolicy;
  23. import org.apache.archiva.repository.content.BaseRepositoryContentLayout;
  24. import org.apache.archiva.repository.content.Artifact;
  25. import org.apache.archiva.repository.storage.StorageAsset;
  26. import org.apache.commons.io.FileUtils;
  27. import org.apache.commons.lang3.StringUtils;
  28. import org.apache.maven.wagon.ResourceDoesNotExistException;
  29. import org.junit.Test;
  30. import java.io.File;
  31. import java.nio.charset.Charset;
  32. import java.nio.file.Files;
  33. import java.nio.file.Path;
  34. import java.nio.file.Paths;
  35. import java.nio.file.attribute.FileTime;
  36. import java.util.concurrent.TimeUnit;
  37. import static org.junit.Assert.*;
  38. import static org.mockito.ArgumentMatchers.any;
  39. import static org.mockito.ArgumentMatchers.eq;
  40. import static org.mockito.Mockito.*;
  41. /**
  42. * ManagedDefaultTransferTest
  43. */
  44. public class ManagedDefaultTransferTest
  45. extends AbstractProxyTestCase
  46. {
  47. @Test
  48. public void testGetDefaultLayoutNotPresentConnectorOffline()
  49. throws Exception
  50. {
  51. String path = "org/apache/maven/test/get-default-layout/1.0/get-default-layout-1.0.jar";
  52. setupTestableManagedRepository( path );
  53. Path expectedFile = managedDefaultDir.resolve(path);
  54. BaseRepositoryContentLayout layout = managedDefaultRepository.getLayout( BaseRepositoryContentLayout.class );
  55. Artifact artifact = layout.getArtifact( path );
  56. // Ensure file isn't present first.
  57. assertNotExistsInManagedDefaultRepo( expectedFile );
  58. // Configure Connector (usually done within archiva.xml configuration)
  59. saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.ONCE, SnapshotsPolicy.ONCE,
  60. CachedFailuresPolicy.NO, true );
  61. // Attempt the proxy fetch.
  62. StorageAsset downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository.getRepository(), artifact );
  63. assertNull( "File should not have been downloaded", downloadedFile );
  64. }
  65. @Test
  66. public void testGetDefaultLayoutNotPresent()
  67. throws Exception
  68. {
  69. String path = "org/apache/maven/test/get-default-layout/1.0/get-default-layout-1.0.jar";
  70. setupTestableManagedRepository( path );
  71. Path expectedFile = managedDefaultDir.resolve(path);
  72. BaseRepositoryContentLayout layout = managedDefaultRepository.getLayout( BaseRepositoryContentLayout.class );
  73. Artifact artifact = layout.getArtifact( path );
  74. // Ensure file isn't present first.
  75. assertNotExistsInManagedDefaultRepo( expectedFile );
  76. // Configure Connector (usually done within archiva.xml configuration)
  77. saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.ONCE, SnapshotsPolicy.ONCE,
  78. CachedFailuresPolicy.NO, false );
  79. // Attempt the proxy fetch.
  80. StorageAsset downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository.getRepository(), artifact );
  81. Path sourceFile = Paths.get(REPOPATH_PROXIED1, path);
  82. assertFileEquals( expectedFile, downloadedFile.getFilePath(), sourceFile );
  83. assertNoTempFiles( expectedFile );
  84. }
  85. @Test
  86. public void testGetDefaultLayoutNotPresentPassthrough()
  87. throws Exception
  88. {
  89. String path = "org/apache/maven/test/get-default-layout/1.0/get-default-layout-1.0.jar.asc";
  90. setupTestableManagedRepository( path );
  91. Path expectedFile = managedDefaultDir.resolve(path);
  92. // Ensure file isn't present first.
  93. assertNotExistsInManagedDefaultRepo( expectedFile );
  94. // Configure Connector (usually done within archiva.xml configuration)
  95. saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.ONCE, SnapshotsPolicy.ONCE,
  96. CachedFailuresPolicy.NO, false );
  97. // Attempt the proxy fetch.
  98. StorageAsset downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository.getRepository(), path );
  99. Path sourceFile = Paths.get(REPOPATH_PROXIED1, path);
  100. assertNotNull(downloadedFile);
  101. assertFileEquals( expectedFile, downloadedFile.getFilePath(), sourceFile );
  102. assertFalse( Files.exists( downloadedFile.getParent().getFilePath().resolve(downloadedFile.getName() + ".sha1" )) );
  103. assertFalse( Files.exists(downloadedFile.getParent().getFilePath().resolve(downloadedFile.getName() + ".md5" ) ));
  104. assertFalse( Files.exists( downloadedFile.getParent().getFilePath().resolve(downloadedFile.getName() + ".sha256" ) ));
  105. assertNoTempFiles( expectedFile );
  106. }
  107. /**
  108. * The attempt here should result in no file being transferred.
  109. * <p/>
  110. * The file exists locally, and the policy is ONCE.
  111. *
  112. * @throws Exception
  113. */
  114. @Test
  115. public void testGetDefaultLayoutAlreadyPresentPolicyOnce()
  116. throws Exception
  117. {
  118. String path = "org/apache/maven/test/get-default-layout-present/1.0/get-default-layout-present-1.0.jar";
  119. setupTestableManagedRepository( path );
  120. Path expectedFile = managedDefaultDir.resolve(path);
  121. BaseRepositoryContentLayout layout = managedDefaultRepository.getLayout( BaseRepositoryContentLayout.class );
  122. Artifact artifact = layout.getArtifact( path );
  123. assertTrue( Files.exists(expectedFile) );
  124. // Configure Connector (usually done within archiva.xml configuration)
  125. saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.ONCE, SnapshotsPolicy.ONCE,
  126. CachedFailuresPolicy.NO, false );
  127. // Attempt the proxy fetch.
  128. StorageAsset downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository.getRepository(), artifact );
  129. assertFileEquals( expectedFile, downloadedFile.getFilePath(), expectedFile );
  130. assertNoTempFiles( expectedFile );
  131. }
  132. /**
  133. * The attempt here should result in no file being transferred.
  134. * <p/>
  135. * The file exists locally, and the policy is ONCE.
  136. *
  137. * @throws Exception
  138. */
  139. @Test
  140. public void testGetDefaultLayoutAlreadyPresentPassthrough()
  141. throws Exception
  142. {
  143. String path = "org/apache/maven/test/get-default-layout-present/1.0/get-default-layout-present-1.0.jar.asc";
  144. setupTestableManagedRepository( path );
  145. Path expectedFile = managedDefaultDir.resolve(path);
  146. Path remoteFile = Paths.get(REPOPATH_PROXIED1, path);
  147. assertTrue( Files.exists(expectedFile) );
  148. // Set the managed File to be newer than local.
  149. setManagedOlderThanRemote( expectedFile, remoteFile );
  150. long originalModificationTime = Files.getLastModifiedTime(expectedFile).toMillis();
  151. // Configure Connector (usually done within archiva.xml configuration)
  152. saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.ONCE, SnapshotsPolicy.ONCE,
  153. CachedFailuresPolicy.NO, false );
  154. // Attempt the proxy fetch.
  155. StorageAsset downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository.getRepository(), path );
  156. assertNotDownloaded( downloadedFile );
  157. assertNotModified( expectedFile, originalModificationTime );
  158. assertNoTempFiles( expectedFile );
  159. }
  160. /**
  161. * <p>
  162. * Request a file, that exists locally, and remotely.
  163. * </p>
  164. * <p>
  165. * All policies are set to IGNORE.
  166. * </p>
  167. * <p>
  168. * Managed file is newer than remote file.
  169. * </p>
  170. * <p>
  171. * Transfer should not have occured, as managed file is newer.
  172. * </p>
  173. *
  174. * @throws Exception
  175. */
  176. @Test
  177. public void testGetDefaultLayoutAlreadyPresentNewerThanRemotePolicyIgnored()
  178. throws Exception
  179. {
  180. String path = "org/apache/maven/test/get-default-layout-present/1.0/get-default-layout-present-1.0.jar";
  181. setupTestableManagedRepository( path );
  182. Path expectedFile = managedDefaultDir.resolve(path);
  183. Path remoteFile = Paths.get(REPOPATH_PROXIED1, path);
  184. // Set the managed File to be newer than local.
  185. setManagedNewerThanRemote( expectedFile, remoteFile );
  186. long originalModificationTime = Files.getLastModifiedTime( expectedFile).toMillis();
  187. BaseRepositoryContentLayout layout = managedDefaultRepository.getLayout( BaseRepositoryContentLayout.class );
  188. Artifact artifact = layout.getArtifact( path );
  189. assertTrue( Files.exists(expectedFile) );
  190. // Configure Connector (usually done within archiva.xml configuration)
  191. saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
  192. SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO, false );
  193. // Attempt the proxy fetch.
  194. StorageAsset downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository.getRepository(), artifact );
  195. assertNotDownloaded( downloadedFile );
  196. assertNotModified( expectedFile, originalModificationTime );
  197. assertNoTempFiles( expectedFile );
  198. }
  199. /**
  200. * <p>
  201. * Request a file, that exists locally, and remotely.
  202. * </p>
  203. * <p>
  204. * All policies are set to IGNORE.
  205. * </p>
  206. * <p>
  207. * Managed file is older than Remote file.
  208. * </p>
  209. * <p>
  210. * Transfer should have occured, as managed file is older than remote.
  211. * </p>
  212. *
  213. * @throws Exception
  214. */
  215. @Test
  216. public void testGetDefaultLayoutAlreadyPresentOlderThanRemotePolicyIgnored()
  217. throws Exception
  218. {
  219. String path = "org/apache/maven/test/get-default-layout-present/1.0/get-default-layout-present-1.0.jar";
  220. setupTestableManagedRepository( path );
  221. Path expectedFile = managedDefaultDir.resolve(path);
  222. Path remoteFile = Paths.get(REPOPATH_PROXIED1, path);
  223. // Set the managed file to be newer than remote file.
  224. setManagedOlderThanRemote( expectedFile, remoteFile );
  225. BaseRepositoryContentLayout layout = managedDefaultRepository.getLayout( BaseRepositoryContentLayout.class );
  226. Artifact artifact = layout.getArtifact( path );
  227. assertTrue( Files.exists(expectedFile) );
  228. // Configure Connector (usually done within archiva.xml configuration)
  229. saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
  230. SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO, false );
  231. // Attempt the proxy fetch.
  232. StorageAsset downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository.getRepository(), artifact );
  233. Path proxiedFile = Paths.get(REPOPATH_PROXIED1, path);
  234. assertFileEquals( expectedFile, downloadedFile.getFilePath(), proxiedFile );
  235. assertNoTempFiles( expectedFile );
  236. }
  237. /**
  238. * The attempt here should result in file being transferred.
  239. * <p/>
  240. * The file exists locally, is over 6 years old, and the policy is DAILY.
  241. *
  242. * @throws Exception
  243. */
  244. @Test
  245. public void testGetDefaultLayoutRemoteUpdate()
  246. throws Exception
  247. {
  248. String path = "org/apache/maven/test/get-default-layout-present/1.0/get-default-layout-present-1.0.jar";
  249. setupTestableManagedRepository( path );
  250. Path expectedFile = managedDefaultDir.resolve(path);
  251. BaseRepositoryContentLayout layout = managedDefaultRepository.getLayout( BaseRepositoryContentLayout.class );
  252. Artifact artifact = layout.getArtifact( path );
  253. assertTrue( Files.exists(expectedFile) );
  254. Files.setLastModifiedTime( expectedFile, FileTime.from(getPastDate().getTime(), TimeUnit.MILLISECONDS ));
  255. // Configure Connector (usually done within archiva.xml configuration)
  256. saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.DAILY, SnapshotsPolicy.DAILY,
  257. CachedFailuresPolicy.NO, false );
  258. // Attempt the proxy fetch.
  259. StorageAsset downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository.getRepository(), artifact );
  260. Path proxiedFile = Paths.get(REPOPATH_PROXIED1, path);
  261. assertFileEquals( expectedFile, downloadedFile.getFilePath(), proxiedFile );
  262. assertNoTempFiles( expectedFile );
  263. }
  264. @Test
  265. public void testGetWhenInBothProxiedRepos()
  266. throws Exception
  267. {
  268. String path = "org/apache/maven/test/get-in-both-proxies/1.0/get-in-both-proxies-1.0.jar";
  269. setupTestableManagedRepository( path );
  270. Path expectedFile = managedDefaultDir.resolve(path);
  271. BaseRepositoryContentLayout layout = managedDefaultRepository.getLayout( BaseRepositoryContentLayout.class );
  272. Artifact artifact = layout.getArtifact( path );
  273. assertNotExistsInManagedDefaultRepo( expectedFile );
  274. // Configure Connector (usually done within archiva.xml configuration)
  275. saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, false );
  276. saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED2, false );
  277. // Attempt the proxy fetch.
  278. StorageAsset downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository.getRepository(), artifact );
  279. Path proxied1File = Paths.get(REPOPATH_PROXIED1, path);
  280. Path proxied2File = Paths.get(REPOPATH_PROXIED2, path);
  281. assertFileEquals( expectedFile, downloadedFile.getFilePath(), proxied1File );
  282. assertNoTempFiles( expectedFile );
  283. // TODO: is this check even needed if it passes above?
  284. String actualContents = FileUtils.readFileToString( downloadedFile.getFilePath().toFile(), Charset.defaultCharset() );
  285. String badContents = FileUtils.readFileToString( proxied2File.toFile(), Charset.defaultCharset() );
  286. assertFalse( "Downloaded file contents should not be that of proxy 2",
  287. StringUtils.equals( actualContents, badContents ) );
  288. }
  289. @Test
  290. public void testGetInSecondProxiedRepo()
  291. throws Exception
  292. {
  293. String path = "org/apache/maven/test/get-in-second-proxy/1.0/get-in-second-proxy-1.0.jar";
  294. setupTestableManagedRepository( path );
  295. Path expectedFile = managedDefaultDir.resolve(path);
  296. BaseRepositoryContentLayout layout = managedDefaultRepository.getLayout( BaseRepositoryContentLayout.class );
  297. Artifact artifact = layout.getArtifact( path );
  298. assertNotExistsInManagedDefaultRepo( expectedFile );
  299. // Configure Connector (usually done within archiva.xml configuration)
  300. saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, false );
  301. saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED2, false );
  302. // Attempt the proxy fetch.
  303. StorageAsset downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository.getRepository(), artifact );
  304. Path proxied2File = Paths.get(REPOPATH_PROXIED2, path);
  305. assertFileEquals( expectedFile, downloadedFile.getFilePath(), proxied2File );
  306. assertNoTempFiles( expectedFile );
  307. }
  308. @Test
  309. public void testNotFoundInAnyProxies()
  310. throws Exception
  311. {
  312. String path = "org/apache/maven/test/does-not-exist/1.0/does-not-exist-1.0.jar";
  313. setupTestableManagedRepository( path );
  314. Path expectedFile = managedDefaultDir.resolve(path);
  315. BaseRepositoryContentLayout layout = managedDefaultRepository.getLayout( BaseRepositoryContentLayout.class );
  316. Artifact artifact = layout.getArtifact( path );
  317. assertNotExistsInManagedDefaultRepo( expectedFile );
  318. // Configure Connector (usually done within archiva.xml configuration)
  319. saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, false );
  320. saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED2, false );
  321. // Attempt the proxy fetch.
  322. StorageAsset downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository.getRepository(), artifact );
  323. assertNull( "File returned was: " + downloadedFile + "; should have got a not found exception",
  324. downloadedFile );
  325. assertNoTempFiles( expectedFile );
  326. }
  327. @Test
  328. public void testGetInSecondProxiedRepoFirstFails()
  329. throws Exception
  330. {
  331. String path = "org/apache/maven/test/get-in-second-proxy/1.0/get-in-second-proxy-1.0.jar";
  332. setupTestableManagedRepository( path );
  333. Path expectedFile = managedDefaultDir.resolve(path);
  334. BaseRepositoryContentLayout layout = managedDefaultRepository.getLayout( BaseRepositoryContentLayout.class );
  335. Artifact artifact = layout.getArtifact( path );
  336. assertNotExistsInManagedDefaultRepo( expectedFile );
  337. // Configure Repository (usually done within archiva.xml configuration)
  338. saveRemoteRepositoryConfig( "badproxied", "Bad Proxied", "" +
  339. "http://bad.machine.com/repo/", "default" );
  340. doThrow( new ResourceDoesNotExistException( "transfer failed" ) ).when( wagonMock ).get( eq( path ), any( ) );
  341. // Configure Connector (usually done within archiva.xml configuration)
  342. saveConnector( ID_DEFAULT_MANAGED, "badproxied", false );
  343. saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED2, false );
  344. // Attempt the proxy fetch.
  345. StorageAsset downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository.getRepository(), artifact );
  346. verify( wagonMock, atLeastOnce( ) ).get( eq( path ), any( ) );
  347. Path proxied2File = Paths.get(REPOPATH_PROXIED2, path);
  348. assertFileEquals( expectedFile, downloadedFile.getFilePath(), proxied2File );
  349. assertNoTempFiles( expectedFile );
  350. }
  351. @Test
  352. public void testGetAllRepositoriesFail()
  353. throws Exception
  354. {
  355. String path = "org/apache/maven/test/get-in-second-proxy/1.0/get-in-second-proxy-1.0.jar";
  356. setupTestableManagedRepository( path );
  357. Path expectedFile = managedDefaultDir.resolve( path );
  358. BaseRepositoryContentLayout layout = managedDefaultRepository.getLayout( BaseRepositoryContentLayout.class );
  359. Artifact artifact = layout.getArtifact( path );
  360. assertNotExistsInManagedDefaultRepo( expectedFile );
  361. // Configure Repository (usually done within archiva.xml configuration)
  362. saveRemoteRepositoryConfig( "badproxied1", "Bad Proxied 1", "http://bad.machine.com/repo/", "default" );
  363. saveRemoteRepositoryConfig( "badproxied2", "Bad Proxied 2", "http://dead.machine.com/repo/", "default" );
  364. // Configure Connector (usually done within archiva.xml configuration)
  365. saveConnector( ID_DEFAULT_MANAGED, "badproxied1", false );
  366. saveConnector( ID_DEFAULT_MANAGED, "badproxied2", false );
  367. Path tmpFile = expectedFile.getParent().resolve(expectedFile.getFileName() + ".tmp" );
  368. doThrow( new ResourceDoesNotExistException( "Can't find resource." ) ).when( wagonMock ).get( eq( path ), any( ) );
  369. StorageAsset downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository.getRepository(), artifact );
  370. verify( wagonMock, atLeastOnce( ) ).get( eq( path ), any( ) );
  371. assertNotDownloaded( downloadedFile );
  372. assertNoTempFiles( expectedFile );
  373. // TODO: do not want failures to present as a not found [MRM-492]
  374. // TODO: How much information on each failure should we pass back to the user vs. logging in the proxy?
  375. }
  376. }