]> source.dussan.org Git - archiva.git/blob
8ec608b2f1079f60897d14f8222b8b13d22f4e10
[archiva.git] /
1 package org.apache.archiva.webdav;
2
3 /*
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
11  *
12  *  http://www.apache.org/licenses/LICENSE-2.0
13  *
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
19  * under the License.
20  */
21
22
23 import com.gargoylesoftware.htmlunit.WebRequest;
24 import com.gargoylesoftware.htmlunit.WebResponse;
25 import org.apache.archiva.common.utils.FileUtils;
26 import org.apache.archiva.configuration.Configuration;
27 import org.apache.archiva.configuration.ManagedRepositoryConfiguration;
28 import org.apache.archiva.configuration.RepositoryGroupConfiguration;
29 import org.apache.archiva.maven.metadata.MavenMetadataReader;
30 import org.apache.archiva.model.ArchivaRepositoryMetadata;
31 import org.apache.commons.lang3.StringUtils;
32 import org.junit.After;
33 import org.junit.Before;
34 import org.junit.Test;
35
36 import javax.servlet.http.HttpServletResponse;
37 import java.io.InputStream;
38 import java.nio.charset.Charset;
39 import java.nio.file.Files;
40 import java.nio.file.Path;
41 import java.nio.file.Paths;
42 import java.util.ArrayList;
43 import java.util.List;
44
45 import static org.assertj.core.api.Assertions.assertThat;
46
47
48 /**
49  * RepositoryServletRepositoryGroupTest
50  * <p/>
51  * Test Case 1.  Accessing a valid repository group root url (e.g. http://machine.com/repository/repository-group/) returns a Bad Request (HTTP 400)
52  * Test Case 2.  Accessing an invalid repository group root url is forwarded to managed repository checking (this is not covered here)
53  * Test Case 3.  Accessing an artifact in a valid repository group will iterate over the managed repositories in the repository group
54  * Test Case 3.a.  If an invalid managed repository is encountered (managed repository doesn't exist),
55  * a Not Found (HTTP 404) is returned and the iteration is broken
56  * Test Case 3.b.  If an artifact is not found in a valid managed repository (after proxying, etc.),
57  * a Not Found (HTTP 404) is set but not returned yet, the iteration continues to the next managed repository.
58  * The Not Found (HTTP 404) is returned after exhausting all valid managed repositories
59  * Test Case 3.c.  If an artifact is found in a valid managed repository,
60  * the artifact is returned, the iteration is broken and any Not Found (HTTP 404) is disregarded
61  * Test Case 4.  Accessing a valid repository group with any http write method returns a Bad Request (HTTP 400)
62  */
63 public class RepositoryServletRepositoryGroupTest
64     extends AbstractRepositoryServletTestCase
65 {
66     protected Path repoRootFirst;
67
68     protected Path repoRootLast;
69
70     protected Path repoRootInvalid;
71
72     protected static final String MANAGED_REPO_FIRST = "first";
73
74     protected static final String MANAGED_REPO_LAST = "last";
75
76     protected static final String MANAGED_REPO_INVALID = "invalid";
77
78     protected static final String REPO_GROUP_WITH_VALID_REPOS = "group-with-valid-repos";
79
80     protected static final String REPO_GROUP_WITH_INVALID_REPOS = "group-with-invalid-repos";
81
82
83     @Override
84     @Before
85     public void setUp()
86         throws Exception
87     {
88         super.setUp();
89
90         String appserverBase = getAppserverBase().toAbsolutePath().toString();
91         log.debug( "Appserver Base {}", appserverBase );
92
93         Configuration configuration = archivaConfiguration.getConfiguration();
94
95         repoRootFirst = Paths.get( appserverBase, "data/repositories/" + MANAGED_REPO_FIRST );
96         repoRootLast = Paths.get( appserverBase, "data/repositories/" + MANAGED_REPO_LAST );
97
98         configuration.addManagedRepository(
99             createManagedRepository( MANAGED_REPO_FIRST, "First Test Repo", repoRootFirst, true ) );
100         configuration.addManagedRepository(
101             createManagedRepository( MANAGED_REPO_LAST, "Last Test Repo", repoRootLast, true ) );
102
103         List<String> managedRepoIds = new ArrayList<>();
104         managedRepoIds.add( MANAGED_REPO_FIRST );
105         managedRepoIds.add( MANAGED_REPO_LAST );
106
107         configuration.addRepositoryGroup( createRepositoryGroup( REPO_GROUP_WITH_VALID_REPOS, managedRepoIds ) );
108
109         // Create the repository group with an invalid managed repository
110         repoRootInvalid = Paths.get( appserverBase, "data/repositories/" + MANAGED_REPO_INVALID );
111         ManagedRepositoryConfiguration managedRepositoryConfiguration =
112             createManagedRepository( MANAGED_REPO_INVALID, "Invalid Test Repo", repoRootInvalid, true );
113
114         configuration.addManagedRepository( managedRepositoryConfiguration );
115
116         List<String> invalidManagedRepoIds = new ArrayList<>();
117         invalidManagedRepoIds.add( MANAGED_REPO_FIRST );
118         invalidManagedRepoIds.add( MANAGED_REPO_INVALID );
119         invalidManagedRepoIds.add( MANAGED_REPO_LAST );
120
121         configuration.addRepositoryGroup(
122             createRepositoryGroup( REPO_GROUP_WITH_INVALID_REPOS, invalidManagedRepoIds ) );
123
124         configuration.removeManagedRepository( managedRepositoryConfiguration );
125         org.apache.archiva.common.utils.FileUtils.deleteDirectory( repoRootInvalid );
126
127         repositoryRegistry.reload();
128         saveConfiguration( archivaConfiguration );
129
130         startRepository();
131     }
132
133
134     @Override
135     @After
136     public void tearDown()
137         throws Exception
138     {
139         setupCleanRepo( repoRootFirst );
140         setupCleanRepo( repoRootLast );
141
142         repositoryRegistry.destroy();
143
144         super.tearDown();
145
146         String appserverBase = System.getProperty( "appserver.base" );
147         if ( StringUtils.isNotEmpty( appserverBase ) )
148         {
149             FileUtils.deleteDirectory( Paths.get( appserverBase ) );
150         }
151     }
152
153     /*
154     * Test Case 3.c
155     */
156     @Test
157     public void testGetFromFirstManagedRepositoryReturnOk()
158         throws Exception
159     {
160         String resourceName = "dummy/dummy-first-resource/1.0/dummy-first-resource-1.0.txt";
161
162         Path dummyInternalResourceFile = repoRootFirst.resolve( resourceName );
163         Files.createDirectories( dummyInternalResourceFile.getParent() );
164         org.apache.archiva.common.utils.FileUtils.writeStringToFile( dummyInternalResourceFile, Charset.defaultCharset(), "first" );
165
166         WebRequest request = new GetMethodWebRequest(
167             "http://machine.com/repository/" + REPO_GROUP_WITH_VALID_REPOS + "/" + resourceName );
168         WebResponse response = getServletUnitClient().getResponse( request );
169
170         assertResponseOK( response );
171         assertThat( response.getContentAsString() ).isEqualTo( "first" );
172     }
173
174     /*
175     * Test Case 3.c
176     */
177     @Test
178     public void testGetFromLastManagedRepositoryReturnOk()
179         throws Exception
180     {
181         String resourceName = "dummy/dummy-last-resource/1.0/dummy-last-resource-1.0.txt";
182
183         Path dummyReleasesResourceFile = repoRootLast.resolve( resourceName );
184         Files.createDirectories(dummyReleasesResourceFile.getParent());
185         org.apache.archiva.common.utils.FileUtils.writeStringToFile(dummyReleasesResourceFile, Charset.defaultCharset(), "last");
186
187         WebRequest request = new GetMethodWebRequest(
188             "http://machine.com/repository/" + REPO_GROUP_WITH_VALID_REPOS + "/" + resourceName );
189         WebResponse response = getServletUnitClient().getResponse( request );
190
191         assertResponseOK( response );
192
193         assertThat( response.getContentAsString() ).isEqualTo( "last" );
194     }
195
196     /*
197     * Test Case 3.b
198     */
199     @Test
200     public void testGetFromValidRepositoryGroupReturnNotFound()
201         throws Exception
202     {
203         String resourceName = "dummy/dummy-no-resource/1.0/dummy-no-resource-1.0.txt";
204
205         WebRequest request = new GetMethodWebRequest(
206             "http://machine.com/repository/" + REPO_GROUP_WITH_VALID_REPOS + "/" + resourceName );
207         WebResponse response = getServletUnitClient().getResponse( request );
208
209         assertResponseNotFound( response );
210     }
211
212     /*
213     * Test Case 4
214     */
215     @Test
216     public void testPutValidRepositoryGroupReturnBadRequest()
217         throws Exception
218     {
219         String resourceName = "dummy/dummy-put-resource/1.0/dummy-put-resource-1.0.txt";
220         String putUrl = "http://machine.com/repository/" + REPO_GROUP_WITH_VALID_REPOS + "/" + resourceName;
221         InputStream is = getClass().getResourceAsStream( "/artifact.jar" );
222
223         WebRequest request = new PutMethodWebRequest( putUrl, is, "text/plain" );
224         WebResponse response = getServletUnitClient().getResponse( request );
225
226         assertResponseMethodNotAllowed( response );
227     }
228
229     // MRM-872
230     @Test
231     public void testGetMergedMetadata()
232         throws Exception
233     {
234         // first metadata file        
235         String resourceName = "dummy/dummy-merged-metadata-resource/maven-metadata.xml";
236
237         Path dummyInternalResourceFile =  repoRootFirst.resolve( resourceName );
238         Files.createDirectories(dummyInternalResourceFile.getParent());
239         org.apache.archiva.common.utils.FileUtils.writeStringToFile( dummyInternalResourceFile,
240             Charset.defaultCharset(),  "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
241             + "<metadata>\n<groupId>dummy</groupId>\n<artifactId>dummy-merged-metadata-resource</artifactId>\n"
242             + "<versioning>\n<latest>1.0</latest>\n<release>1.0</release>\n<versions>\n<version>1.0</version>\n"
243             + "<version>2.5</version>\n</versions>\n<lastUpdated>20080708095554</lastUpdated>\n</versioning>\n</metadata>" );
244
245         //second metadata file
246         resourceName = "dummy/dummy-merged-metadata-resource/maven-metadata.xml";
247         dummyInternalResourceFile = repoRootLast.resolve( resourceName );
248         Files.createDirectories(dummyInternalResourceFile.getParent());
249         org.apache.archiva.common.utils.FileUtils.writeStringToFile( dummyInternalResourceFile, Charset.defaultCharset(), "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
250             + "<metadata><groupId>dummy</groupId><artifactId>dummy-merged-metadata-resource</artifactId>"
251             + "<versioning><latest>2.0</latest><release>2.0</release><versions><version>1.0</version>"
252             + "<version>1.5</version><version>2.0</version></versions><lastUpdated>20080709095554</lastUpdated>"
253             + "</versioning></metadata>" );
254
255         WebRequest request = new GetMethodWebRequest(
256             "http://machine.com/repository/" + REPO_GROUP_WITH_VALID_REPOS + "/dummy/"
257                 + "dummy-merged-metadata-resource/maven-metadata.xml" );
258         WebResponse response = getServletUnitClient().getResource( request );
259
260         Path returnedMetadata = getProjectBase().resolve( "target/test-classes/retrievedMetadataFile.xml" );
261         org.apache.archiva.common.utils.FileUtils.writeStringToFile( returnedMetadata, Charset.defaultCharset(), response.getContentAsString() );
262         MavenMetadataReader metadataReader = new MavenMetadataReader( );
263         ArchivaRepositoryMetadata metadata = metadataReader.read( returnedMetadata );
264
265         assertResponseOK( response );
266
267         assertThat( metadata.getAvailableVersions() ).isNotNull()
268             .hasSize( 4 ).contains( "1.0", "1.5", "2.0", "2.5" );
269
270
271         //check if the checksum files were generated
272         Path checksumFileSha1 = repoRootFirst.resolve( resourceName + ".sha1" );
273         Files.createDirectories(checksumFileSha1.getParent());
274         org.apache.archiva.common.utils.FileUtils.writeStringToFile(checksumFileSha1, Charset.defaultCharset(), "3290853214d3687134");
275
276         Path checksumFileMd5 = repoRootFirst.resolve( resourceName + ".md5" );
277         Files.createDirectories(checksumFileMd5.getParent());
278         org.apache.archiva.common.utils.FileUtils.writeStringToFile(checksumFileMd5, Charset.defaultCharset(), "98745897234eda12836423");
279
280         // request the sha1 checksum of the metadata
281         request = new GetMethodWebRequest( "http://machine.com/repository/" + REPO_GROUP_WITH_VALID_REPOS + "/dummy/"
282                                                + "dummy-merged-metadata-resource/maven-metadata.xml.sha1" );
283         response = getServletUnitClient().getResource( request );
284
285         assertResponseOK( response );
286
287         assertThat( response.getContentAsString() )
288             .startsWith( "f8a7a858a46887368adf0b30874de1f807d91453" );
289
290         // request the md5 checksum of the metadata
291         request = new GetMethodWebRequest( "http://machine.com/repository/" + REPO_GROUP_WITH_VALID_REPOS + "/dummy/"
292                                                + "dummy-merged-metadata-resource/maven-metadata.xml.md5" );
293         response = getServletUnitClient().getResource( request );
294
295         assertResponseOK( response );
296
297         assertThat( response.getContentAsString() )
298             .startsWith( "cec864b66849153dd45fddb7cdde12b2" );
299     }
300
301     // MRM-901
302     @Test
303     public void testBrowseWithTwoArtifactsWithSameGroupIdInRepos()
304         throws Exception
305     {
306         String resourceName = "dummy/dummy-artifact/1.0/dummy-artifact-1.0.txt";
307
308         Path dummyInternalResourceFile = repoRootFirst.resolve( resourceName );
309         Files.createDirectories(dummyInternalResourceFile.getParent());
310         org.apache.archiva.common.utils.FileUtils.writeStringToFile(dummyInternalResourceFile, Charset.defaultCharset(), "first");
311
312         resourceName = "dummy/dummy-artifact/2.0/dummy-artifact-2.0.txt";
313
314         Path dummyReleasesResourceFile = repoRootLast.resolve( resourceName );
315         Files.createDirectories(dummyReleasesResourceFile.getParent());
316         org.apache.archiva.common.utils.FileUtils.writeStringToFile(dummyReleasesResourceFile, Charset.defaultCharset(), "last");
317
318         WebRequest request = new GetMethodWebRequest(
319             "http://machine.com/repository/" + REPO_GROUP_WITH_VALID_REPOS + "/dummy/dummy-artifact/" );
320         WebResponse response = getServletUnitClient().getResource( request );
321
322         assertResponseOK( response );
323
324         assertThat( response.getContentAsString() ).contains( "Collection" )
325             .contains( "dummy/dummy-artifact" )
326             .contains( "1.0" )
327             .contains( "2.0" );
328
329     }
330
331     protected void assertResponseMethodNotAllowed( WebResponse response )
332     {
333
334         assertThat( response ).isNotNull();
335
336         assertThat( response.getStatusCode() ).isEqualTo( HttpServletResponse.SC_METHOD_NOT_ALLOWED );
337     }
338
339     protected RepositoryGroupConfiguration createRepositoryGroup( String id, List<String> repositories )
340     {
341         RepositoryGroupConfiguration repoGroupConfiguration = new RepositoryGroupConfiguration();
342         repoGroupConfiguration.setId( id );
343         repoGroupConfiguration.setRepositories( repositories );
344         return repoGroupConfiguration;
345     }
346 }