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