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