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.

DuplicateArtifactsConsumerTest.java 9.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. package org.apache.archiva.reports.consumers;
  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 junit.framework.TestCase;
  21. import org.apache.archiva.consumers.ConsumerException;
  22. import org.apache.archiva.metadata.model.ArtifactMetadata;
  23. import org.apache.archiva.metadata.model.MetadataFacet;
  24. import org.apache.archiva.metadata.model.facets.RepositoryProblemFacet;
  25. import org.apache.archiva.metadata.repository.MetadataRepository;
  26. import org.apache.archiva.metadata.repository.RepositorySession;
  27. import org.apache.archiva.metadata.repository.RepositorySessionFactory;
  28. import org.apache.archiva.metadata.repository.storage.RepositoryPathTranslator;
  29. import org.apache.archiva.repository.base.managed.BasicManagedRepository;
  30. import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
  31. import org.junit.Before;
  32. import org.junit.Test;
  33. import org.junit.runner.RunWith;
  34. import org.mockito.ArgumentCaptor;
  35. import org.mockito.Matchers;
  36. import org.springframework.context.ApplicationContext;
  37. import org.springframework.test.annotation.DirtiesContext;
  38. import org.springframework.test.context.ContextConfiguration;
  39. import javax.inject.Inject;
  40. import javax.inject.Named;
  41. import java.nio.file.NoSuchFileException;
  42. import java.nio.file.Paths;
  43. import java.util.Arrays;
  44. import java.util.Date;
  45. import static org.mockito.Mockito.*;
  46. @SuppressWarnings( { "ThrowableInstanceNeverThrown" } )
  47. @RunWith( ArchivaSpringJUnit4ClassRunner.class )
  48. @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml" } )
  49. @DirtiesContext( classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD )
  50. public class DuplicateArtifactsConsumerTest
  51. extends TestCase
  52. {
  53. @Inject
  54. @Named( value = "knownRepositoryContentConsumer#duplicate-artifacts" )
  55. private DuplicateArtifactsConsumer consumer;
  56. private BasicManagedRepository config;
  57. private MetadataRepository metadataRepository;
  58. private static final String TEST_REPO = "test-repo";
  59. private static final String TEST_CHECKSUM = "edf5938e646956f445c6ecb719d44579cdeed974";
  60. private static final String TEST_PROJECT = "test-artifact";
  61. private static final String TEST_NAMESPACE = "com.example.test";
  62. private static final String TEST_FILE =
  63. "com/example/test/test-artifact/1.0-SNAPSHOT/test-artifact-1.0-20100308.230825-1.jar";
  64. private static final String TEST_VERSION = "1.0-20100308.230825-1";
  65. private static final ArtifactMetadata TEST_METADATA = createMetadata( TEST_VERSION );
  66. @Inject
  67. @Named( value = "repositoryPathTranslator#maven2" )
  68. private RepositoryPathTranslator pathTranslator;
  69. @Inject
  70. ApplicationContext applicationContext;
  71. RepositorySessionFactory repositorySessionFactory;
  72. RepositorySession session;
  73. @Before
  74. @Override
  75. public void setUp()
  76. throws Exception
  77. {
  78. super.setUp();
  79. assertNotNull( consumer );
  80. config = BasicManagedRepository.newFilesystemInstance(TEST_REPO, TEST_REPO, Paths.get("target").resolve(TEST_REPO));
  81. config.setLocation( Paths.get( "target/test-repository" ).toAbsolutePath().toUri() );
  82. metadataRepository = mock( MetadataRepository.class );
  83. repositorySessionFactory = mock(RepositorySessionFactory.class);
  84. session = mock( RepositorySession.class );
  85. when( repositorySessionFactory.createSession( ) ).thenReturn( session );
  86. when( session.getRepository() ).thenReturn( metadataRepository );
  87. when( pathTranslator.getArtifactForPath( TEST_REPO, TEST_FILE ) ).thenReturn( TEST_METADATA );
  88. consumer.setRepositorySessionFactory( repositorySessionFactory );
  89. }
  90. @Test
  91. public void testConsumerArtifactNotDuplicated()
  92. throws Exception
  93. {
  94. when( metadataRepository.getArtifactsByChecksum(eq(session) , eq(TEST_REPO), eq(TEST_CHECKSUM) ) ).thenReturn(
  95. Arrays.asList( TEST_METADATA ) );
  96. consumer.beginScan( config, new Date() );
  97. consumer.processFile( TEST_FILE );
  98. consumer.completeScan();
  99. verify( metadataRepository, never() ).addMetadataFacet(eq(session) , eq( TEST_REPO ), Matchers.<MetadataFacet>anyObject() );
  100. }
  101. // TODO: Doesn't currently work
  102. // public void testConsumerArtifactNotDuplicatedForOtherSnapshots()
  103. // throws ConsumerException
  104. // {
  105. // when( metadataRepository.getArtifactsByChecksum( TEST_REPO, TEST_CHECKSUM ) ).thenReturn( Arrays.asList(
  106. // TEST_METADATA, createMetadata( "1.0-20100309.002023-2" ) ) );
  107. //
  108. // consumer.beginScan( config, new Date() );
  109. // consumer.processFile( TEST_FILE );
  110. // consumer.completeScan();
  111. //
  112. // verify( metadataRepository, never() ).addMetadataFacet( eq( TEST_REPO ), Matchers.<MetadataFacet>anyObject() );
  113. // }
  114. @Test
  115. public void testConsumerArtifactDuplicated()
  116. throws Exception
  117. {
  118. when( metadataRepository.getArtifactsByChecksum(session , TEST_REPO, TEST_CHECKSUM ) ).thenReturn(
  119. Arrays.asList( TEST_METADATA, createMetadata( "1.0" ) ) );
  120. consumer.beginScan( config, new Date() );
  121. consumer.processFile( TEST_FILE );
  122. consumer.completeScan();
  123. ArgumentCaptor<RepositoryProblemFacet> argument = ArgumentCaptor.forClass( RepositoryProblemFacet.class );
  124. verify( metadataRepository ).addMetadataFacet(eq(session) , eq( TEST_REPO ), argument.capture() );
  125. RepositoryProblemFacet problem = argument.getValue();
  126. assertProblem( problem );
  127. }
  128. @Test
  129. public void testConsumerArtifactDuplicatedButSelfNotInMetadataRepository()
  130. throws Exception
  131. {
  132. when( metadataRepository.getArtifactsByChecksum(session , TEST_REPO, TEST_CHECKSUM ) ).thenReturn(
  133. Arrays.asList( createMetadata( "1.0" ) ) );
  134. consumer.beginScan( config, new Date() );
  135. consumer.processFile( TEST_FILE );
  136. consumer.completeScan();
  137. ArgumentCaptor<RepositoryProblemFacet> argument = ArgumentCaptor.forClass( RepositoryProblemFacet.class );
  138. verify( metadataRepository ).addMetadataFacet(eq(session) , eq( TEST_REPO ), argument.capture() );
  139. RepositoryProblemFacet problem = argument.getValue();
  140. assertProblem( problem );
  141. }
  142. @Test
  143. public void testConsumerArtifactFileNotExist()
  144. throws Exception
  145. {
  146. consumer.beginScan( config, new Date() );
  147. try
  148. {
  149. consumer.processFile( "com/example/test/test-artifact/2.0/test-artifact-2.0.jar" );
  150. fail( "Should have failed to find file" );
  151. }
  152. catch ( ConsumerException e )
  153. {
  154. assertTrue( e.getCause() instanceof NoSuchFileException );
  155. }
  156. finally
  157. {
  158. consumer.completeScan();
  159. }
  160. verify( metadataRepository, never() ).addMetadataFacet(eq(session) , eq( TEST_REPO ), Matchers.<MetadataFacet>anyObject() );
  161. }
  162. @Test
  163. public void testConsumerArtifactNotAnArtifactPathNoResults()
  164. throws Exception
  165. {
  166. consumer.beginScan( config, new Date() );
  167. // No exception unnecessarily for something we can't report on
  168. consumer.processFile( "com/example/invalid-artifact.txt" );
  169. consumer.completeScan();
  170. verify( metadataRepository, never() ).addMetadataFacet(eq(session) , eq( TEST_REPO ), Matchers.<MetadataFacet>anyObject() );
  171. }
  172. @Test
  173. public void testConsumerArtifactNotAnArtifactPathResults()
  174. throws Exception
  175. {
  176. when( metadataRepository.getArtifactsByChecksum(eq(session) , eq( TEST_REPO ), anyString() ) ).thenReturn(
  177. Arrays.asList( TEST_METADATA, createMetadata( "1.0" ) ) );
  178. // override, this feels a little overspecified though
  179. when( pathTranslator.getArtifactForPath( TEST_REPO, "com/example/invalid-artifact.txt" ) ).thenThrow(
  180. new IllegalArgumentException() );
  181. consumer.beginScan( config, new Date() );
  182. // No exception unnecessarily for something we can't report on
  183. consumer.processFile( "com/example/invalid-artifact.txt" );
  184. consumer.completeScan();
  185. verify( metadataRepository, never() ).addMetadataFacet(eq(session) , eq( TEST_REPO ), Matchers.<MetadataFacet>anyObject() );
  186. }
  187. private static void assertProblem( RepositoryProblemFacet problem )
  188. {
  189. assertEquals( TEST_REPO, problem.getRepositoryId() );
  190. assertEquals( TEST_NAMESPACE, problem.getNamespace() );
  191. assertEquals( TEST_PROJECT, problem.getProject() );
  192. assertEquals( TEST_VERSION, problem.getVersion() );
  193. assertEquals( TEST_PROJECT + "-" + TEST_VERSION + ".jar", problem.getId() );
  194. assertNotNull( problem.getMessage() );
  195. assertEquals( "duplicate-artifact", problem.getProblem() );
  196. }
  197. private static ArtifactMetadata createMetadata( String version )
  198. {
  199. ArtifactMetadata artifact = new ArtifactMetadata();
  200. artifact.setId( TEST_PROJECT + "-" + version + ".jar" );
  201. artifact.setNamespace( TEST_NAMESPACE );
  202. artifact.setProject( TEST_PROJECT );
  203. artifact.setProjectVersion( version );
  204. artifact.setVersion( version );
  205. artifact.setRepositoryId( TEST_REPO );
  206. return artifact;
  207. }
  208. }