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