1 package org.apache.archiva.reports.consumers;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import junit.framework.TestCase;
23 import org.apache.archiva.admin.model.beans.ManagedRepository;
24 import org.apache.archiva.consumers.ConsumerException;
25 import org.apache.archiva.metadata.model.ArtifactMetadata;
26 import org.apache.archiva.metadata.model.MetadataFacet;
27 import org.apache.archiva.metadata.repository.MetadataRepository;
28 import org.apache.archiva.metadata.repository.RepositorySession;
29 import org.apache.archiva.metadata.repository.RepositorySessionFactory;
30 import org.apache.archiva.metadata.repository.storage.RepositoryPathTranslator;
31 import org.apache.archiva.metadata.model.facets.RepositoryProblemFacet;
32 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 import org.mockito.ArgumentCaptor;
37 import org.mockito.Matchers;
38 import org.springframework.context.ApplicationContext;
39 import org.springframework.test.annotation.DirtiesContext;
40 import org.springframework.test.context.ContextConfiguration;
41
42 import javax.inject.Inject;
43 import javax.inject.Named;
44 import java.io.File;
45 import java.nio.file.NoSuchFileException;
46 import java.util.Arrays;
47 import java.util.Date;
48
49 import static org.mockito.Mockito.*;
50
51 @SuppressWarnings( { "ThrowableInstanceNeverThrown" } )
52 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
53 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml" } )
54 @DirtiesContext( classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD )
55 public class DuplicateArtifactsConsumerTest
56 extends TestCase
57 {
58 @Inject
59 @Named( value = "knownRepositoryContentConsumer#duplicate-artifacts" )
60 private DuplicateArtifactsConsumer consumer;
61
62 private ManagedRepository config;
63
64 private MetadataRepository metadataRepository;
65
66 private static final String TEST_REPO = "test-repo";
67
68 private static final String TEST_CHECKSUM = "edf5938e646956f445c6ecb719d44579cdeed974";
69
70 private static final String TEST_PROJECT = "test-artifact";
71
72 private static final String TEST_NAMESPACE = "com.example.test";
73
74 private static final String TEST_FILE =
75 "com/example/test/test-artifact/1.0-SNAPSHOT/test-artifact-1.0-20100308.230825-1.jar";
76
77 private static final String TEST_VERSION = "1.0-20100308.230825-1";
78
79 private static final ArtifactMetadata TEST_METADATA = createMetadata( TEST_VERSION );
80
81 @Inject
82 @Named( value = "repositoryPathTranslator#maven2" )
83 private RepositoryPathTranslator pathTranslator;
84
85 @Inject
86 ApplicationContext applicationContext;
87
88
89 @Before
90 @Override
91 public void setUp()
92 throws Exception
93 {
94 super.setUp();
95
96 assertNotNull( consumer );
97
98 config = new ManagedRepository();
99 config.setId( TEST_REPO );
100 config.setLocation( new File( "target/test-repository" ).getAbsolutePath() );
101
102 metadataRepository = mock( MetadataRepository.class );
103
104 RepositorySession session = mock( RepositorySession.class );
105 when( session.getRepository() ).thenReturn( metadataRepository );
106
107 RepositorySessionFactory factory = applicationContext.getBean( RepositorySessionFactory.class );
108
109 when( factory.createSession() ).thenReturn( session );
110
111 when( pathTranslator.getArtifactForPath( TEST_REPO, TEST_FILE ) ).thenReturn( TEST_METADATA );
112 }
113
114 @Test
115 public void testConsumerArtifactNotDuplicated()
116 throws Exception
117 {
118 when( metadataRepository.getArtifactsByChecksum( TEST_REPO, TEST_CHECKSUM ) ).thenReturn(
119 Arrays.asList( TEST_METADATA ) );
120
121 consumer.beginScan( config, new Date() );
122 consumer.processFile( TEST_FILE );
123 consumer.completeScan();
124
125 verify( metadataRepository, never() ).addMetadataFacet( eq( TEST_REPO ), Matchers.<MetadataFacet>anyObject() );
126 }
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142 @Test
143 public void testConsumerArtifactDuplicated()
144 throws Exception
145 {
146 when( metadataRepository.getArtifactsByChecksum( TEST_REPO, TEST_CHECKSUM ) ).thenReturn(
147 Arrays.asList( TEST_METADATA, createMetadata( "1.0" ) ) );
148
149 consumer.beginScan( config, new Date() );
150 consumer.processFile( TEST_FILE );
151 consumer.completeScan();
152
153 ArgumentCaptor<RepositoryProblemFacet> argument = ArgumentCaptor.forClass( RepositoryProblemFacet.class );
154 verify( metadataRepository ).addMetadataFacet( eq( TEST_REPO ), argument.capture() );
155 RepositoryProblemFacet problem = argument.getValue();
156 assertProblem( problem );
157 }
158
159 @Test
160 public void testConsumerArtifactDuplicatedButSelfNotInMetadataRepository()
161 throws Exception
162 {
163 when( metadataRepository.getArtifactsByChecksum( TEST_REPO, TEST_CHECKSUM ) ).thenReturn(
164 Arrays.asList( createMetadata( "1.0" ) ) );
165
166 consumer.beginScan( config, new Date() );
167 consumer.processFile( TEST_FILE );
168 consumer.completeScan();
169
170 ArgumentCaptor<RepositoryProblemFacet> argument = ArgumentCaptor.forClass( RepositoryProblemFacet.class );
171 verify( metadataRepository ).addMetadataFacet( eq( TEST_REPO ), argument.capture() );
172 RepositoryProblemFacet problem = argument.getValue();
173 assertProblem( problem );
174 }
175
176 @Test
177 public void testConsumerArtifactFileNotExist()
178 throws Exception
179 {
180 consumer.beginScan( config, new Date() );
181 try
182 {
183 consumer.processFile( "com/example/test/test-artifact/2.0/test-artifact-2.0.jar" );
184 fail( "Should have failed to find file" );
185 }
186 catch ( ConsumerException e )
187 {
188 assertTrue( e.getCause() instanceof NoSuchFileException );
189 }
190 finally
191 {
192 consumer.completeScan();
193 }
194
195 verify( metadataRepository, never() ).addMetadataFacet( eq( TEST_REPO ), Matchers.<MetadataFacet>anyObject() );
196 }
197
198 @Test
199 public void testConsumerArtifactNotAnArtifactPathNoResults()
200 throws Exception
201 {
202 consumer.beginScan( config, new Date() );
203
204 consumer.processFile( "com/example/invalid-artifact.txt" );
205 consumer.completeScan();
206
207 verify( metadataRepository, never() ).addMetadataFacet( eq( TEST_REPO ), Matchers.<MetadataFacet>anyObject() );
208 }
209
210 @Test
211 public void testConsumerArtifactNotAnArtifactPathResults()
212 throws Exception
213 {
214 when( metadataRepository.getArtifactsByChecksum( eq( TEST_REPO ), anyString() ) ).thenReturn(
215 Arrays.asList( TEST_METADATA, createMetadata( "1.0" ) ) );
216
217
218 when( pathTranslator.getArtifactForPath( TEST_REPO, "com/example/invalid-artifact.txt" ) ).thenThrow(
219 new IllegalArgumentException() );
220
221 consumer.beginScan( config, new Date() );
222
223 consumer.processFile( "com/example/invalid-artifact.txt" );
224 consumer.completeScan();
225
226 verify( metadataRepository, never() ).addMetadataFacet( eq( TEST_REPO ), Matchers.<MetadataFacet>anyObject() );
227 }
228
229 private static void assertProblem( RepositoryProblemFacet problem )
230 {
231 assertEquals( TEST_REPO, problem.getRepositoryId() );
232 assertEquals( TEST_NAMESPACE, problem.getNamespace() );
233 assertEquals( TEST_PROJECT, problem.getProject() );
234 assertEquals( TEST_VERSION, problem.getVersion() );
235 assertEquals( TEST_PROJECT + "-" + TEST_VERSION + ".jar", problem.getId() );
236 assertNotNull( problem.getMessage() );
237 assertEquals( "duplicate-artifact", problem.getProblem() );
238 }
239
240 private static ArtifactMetadata createMetadata( String version )
241 {
242 ArtifactMetadata artifact = new ArtifactMetadata();
243 artifact.setId( TEST_PROJECT + "-" + version + ".jar" );
244 artifact.setNamespace( TEST_NAMESPACE );
245 artifact.setProject( TEST_PROJECT );
246 artifact.setProjectVersion( version );
247 artifact.setVersion( version );
248 artifact.setRepositoryId( TEST_REPO );
249 return artifact;
250 }
251 }