1 package org.apache.maven.archiva.webdav;
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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
23 import java.io.InputStream;
24 import java.util.ArrayList;
25 import java.util.List;
27 import javax.servlet.http.HttpServletResponse;
29 import org.apache.commons.io.FileUtils;
30 import org.apache.commons.lang.StringUtils;
31 import org.apache.maven.archiva.configuration.Configuration;
32 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
33 import org.apache.maven.archiva.configuration.RepositoryGroupConfiguration;
34 import org.apache.maven.archiva.model.ArchivaRepositoryMetadata;
35 import org.apache.maven.archiva.repository.metadata.RepositoryMetadataReader;
37 import com.meterware.httpunit.GetMethodWebRequest;
38 import com.meterware.httpunit.PutMethodWebRequest;
39 import com.meterware.httpunit.WebRequest;
40 import com.meterware.httpunit.WebResponse;
44 * RepositoryServletRepositoryGroupTest
46 * Test Case 1. Accessing a valid repository group root url (e.g. http://machine.com/repository/repository-group/) returns a Bad Request (HTTP 400)
47 * Test Case 2. Accessing an invalid repository group root url is forwarded to managed repository checking (this is not covered here)
48 * Test Case 3. Accessing an artifact in a valid repository group will iterate over the managed repositories in the repository group
49 * Test Case 3.a. If an invalid managed repository is encountered (managed repository doesn't exist),
50 * a Not Found (HTTP 404) is returned and the iteration is broken
51 * Test Case 3.b. If an artifact is not found in a valid managed repository (after proxying, etc.),
52 * a Not Found (HTTP 404) is set but not returned yet, the iteration continues to the next managed repository.
53 * The Not Found (HTTP 404) is returned after exhausting all valid managed repositories
54 * Test Case 3.c. If an artifact is found in a valid managed repository,
55 * the artifact is returned, the iteration is broken and any Not Found (HTTP 404) is disregarded
56 * Test Case 4. Accessing a valid repository group with any http write method returns a Bad Request (HTTP 400)
60 public class RepositoryServletRepositoryGroupTest
61 extends AbstractRepositoryServletTestCase
63 protected File repoRootFirst;
65 protected File repoRootLast;
67 protected File repoRootInvalid;
69 protected static final String MANAGED_REPO_FIRST = "first";
71 protected static final String MANAGED_REPO_LAST = "last";
73 protected static final String MANAGED_REPO_INVALID = "invalid";
75 protected static final String REPO_GROUP_WITH_VALID_REPOS = "group-with-valid-repos";
77 protected static final String REPO_GROUP_WITH_INVALID_REPOS = "group-with-invalid-repos";
81 protected void setUp()
86 String appserverBase = System.getProperty( "appserver.base" );
88 Configuration configuration = archivaConfiguration.getConfiguration();
90 repoRootFirst = new File( appserverBase, "data/repositories/" + MANAGED_REPO_FIRST );
91 repoRootLast = new File( appserverBase, "data/repositories/" + MANAGED_REPO_LAST );
93 configuration.addManagedRepository( createManagedRepository( MANAGED_REPO_FIRST, "First Test Repo", repoRootFirst ) );
94 configuration.addManagedRepository( createManagedRepository( MANAGED_REPO_LAST, "Last Test Repo", repoRootLast ) );
96 List<String> managedRepoIds = new ArrayList<String>();
97 managedRepoIds.add( MANAGED_REPO_FIRST );
98 managedRepoIds.add( MANAGED_REPO_LAST );
100 configuration.addRepositoryGroup( createRepositoryGroup( REPO_GROUP_WITH_VALID_REPOS, managedRepoIds ) );
102 // Create the repository group with an invalid managed repository
103 repoRootInvalid = new File( appserverBase, "data/repositories/" + MANAGED_REPO_INVALID );
104 ManagedRepositoryConfiguration managedRepositoryConfiguration = createManagedRepository( MANAGED_REPO_INVALID, "Invalid Test Repo", repoRootInvalid );
106 configuration.addManagedRepository( createManagedRepository( MANAGED_REPO_FIRST, "First Test Repo", repoRootFirst ) );
107 configuration.addManagedRepository( managedRepositoryConfiguration );
108 configuration.addManagedRepository( createManagedRepository( MANAGED_REPO_LAST, "Last Test Repo", repoRootLast ) );
110 List<String> invalidManagedRepoIds = new ArrayList<String>();
111 invalidManagedRepoIds.add( MANAGED_REPO_FIRST );
112 invalidManagedRepoIds.add( MANAGED_REPO_INVALID );
113 invalidManagedRepoIds.add( MANAGED_REPO_LAST );
115 configuration.addRepositoryGroup( createRepositoryGroup( REPO_GROUP_WITH_INVALID_REPOS, invalidManagedRepoIds ) );
117 configuration.removeManagedRepository( managedRepositoryConfiguration );
118 FileUtils.deleteDirectory( repoRootInvalid );
120 saveConfiguration( archivaConfiguration );
124 protected void tearDown()
127 setupCleanRepo( repoRootFirst );
128 setupCleanRepo( repoRootLast );
136 public void testGetFromFirstManagedRepositoryReturnOk()
139 String resourceName = "dummy/dummy-first-resource/1.0/dummy-first-resource-1.0.txt";
141 File dummyInternalResourceFile = new File( repoRootFirst, resourceName );
142 dummyInternalResourceFile.getParentFile().mkdirs();
143 FileUtils.writeStringToFile( dummyInternalResourceFile, "first", null );
145 WebRequest request = new GetMethodWebRequest( "http://machine.com/repository/" + REPO_GROUP_WITH_VALID_REPOS + "/" + resourceName );
146 WebResponse response = sc.getResponse( request );
148 assertResponseOK( response );
149 assertEquals( "Expected file contents", "first", response.getText() );
155 public void testGetFromLastManagedRepositoryReturnOk()
158 String resourceName = "dummy/dummy-last-resource/1.0/dummy-last-resource-1.0.txt";
160 File dummyReleasesResourceFile = new File( repoRootLast, resourceName );
161 dummyReleasesResourceFile.getParentFile().mkdirs();
162 FileUtils.writeStringToFile( dummyReleasesResourceFile, "last", null );
164 WebRequest request = new GetMethodWebRequest( "http://machine.com/repository/" + REPO_GROUP_WITH_VALID_REPOS + "/" + resourceName );
165 WebResponse response = sc.getResponse( request );
167 assertResponseOK( response );
168 assertEquals( "Expected file contents", "last", response.getText() );
174 public void testGetFromValidRepositoryGroupReturnNotFound()
177 String resourceName = "dummy/dummy-no-resource/1.0/dummy-no-resource-1.0.txt";
179 WebRequest request = new GetMethodWebRequest( "http://machine.com/repository/" + REPO_GROUP_WITH_VALID_REPOS + "/" + resourceName );
180 WebResponse response = sc.getResponse( request );
182 assertResponseNotFound( response );
188 public void testGetInvalidManagedRepositoryInGroupReturnNotFound()
191 String resourceName = "dummy/dummy-no-resource/1.0/dummy-no-resource-1.0.txt";
193 WebRequest request = new GetMethodWebRequest( "http://machine.com/repository/" + REPO_GROUP_WITH_INVALID_REPOS + "/" + resourceName );
194 WebResponse response = sc.getResponse( request );
196 assertResponseNotFound( response );
202 public void testPutValidRepositoryGroupReturnBadRequest()
205 String resourceName = "dummy/dummy-put-resource/1.0/dummy-put-resource-1.0.txt";
206 String putUrl = "http://machine.com/repository/" + REPO_GROUP_WITH_VALID_REPOS + "/" + resourceName;
207 InputStream is = getClass().getResourceAsStream( "/artifact.jar" );
209 WebRequest request = new PutMethodWebRequest( putUrl, is, "text/plain" );
210 WebResponse response = sc.getResponse( request );
212 assertResponseMethodNotAllowed( response );
216 public void testGetMergedMetadata()
219 // first metadata file
220 String resourceName = "dummy/dummy-merged-metadata-resource/maven-metadata.xml";
222 File dummyInternalResourceFile = new File( repoRootFirst, resourceName );
223 dummyInternalResourceFile.getParentFile().mkdirs();
224 FileUtils.writeStringToFile( dummyInternalResourceFile, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
225 "<metadata>\n<groupId>dummy</groupId>\n<artifactId>dummy-merged-metadata-resource</artifactId>\n" +
226 "<versioning>\n<latest>1.0</latest>\n<release>1.0</release>\n<versions>\n<version>1.0</version>\n" +
227 "<version>2.5</version>\n</versions>\n<lastUpdated>20080708095554</lastUpdated>\n</versioning>\n</metadata>", null );
229 //second metadata file
230 resourceName = "dummy/dummy-merged-metadata-resource/maven-metadata.xml";
231 dummyInternalResourceFile = new File( repoRootLast, resourceName );
232 dummyInternalResourceFile.getParentFile().mkdirs();
233 FileUtils.writeStringToFile( dummyInternalResourceFile, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
234 "<metadata><groupId>dummy</groupId><artifactId>dummy-merged-metadata-resource</artifactId>" +
235 "<versioning><latest>2.0</latest><release>2.0</release><versions><version>1.0</version>" +
236 "<version>1.5</version><version>2.0</version></versions><lastUpdated>20080709095554</lastUpdated>" +
237 "</versioning></metadata>", null );
239 WebRequest request = new GetMethodWebRequest( "http://machine.com/repository/" + REPO_GROUP_WITH_VALID_REPOS + "/dummy/" +
240 "dummy-merged-metadata-resource/maven-metadata.xml" );
241 WebResponse response = sc.getResource( request );
243 File returnedMetadata = new File( getBasedir(), "/target/test-classes/retrievedMetadataFile.xml");
244 FileUtils.writeStringToFile( returnedMetadata, response.getText() );
245 ArchivaRepositoryMetadata metadata = RepositoryMetadataReader.read( returnedMetadata );
247 assertResponseOK( response );
248 assertEquals( "Versions list size", 4, metadata.getAvailableVersions().size() );
249 assertTrue( "Versions list contains version 1.0", metadata.getAvailableVersions().contains( "1.0" ) );
250 assertTrue( "Versions list contains version 1.5", metadata.getAvailableVersions().contains( "1.5" ) );
251 assertTrue( "Versions list contains version 2.0", metadata.getAvailableVersions().contains( "2.0" ) );
252 assertTrue( "Versions list contains version 2.5", metadata.getAvailableVersions().contains( "2.5" ) );
254 //check if the checksum files were generated
255 File checksumFileSha1 = new File( repoRootFirst, resourceName + ".sha1" );
256 checksumFileSha1.getParentFile().mkdirs();
257 FileUtils.writeStringToFile( checksumFileSha1, "3290853214d3687134", null );
259 File checksumFileMd5 = new File( repoRootFirst, resourceName + ".md5" );
260 checksumFileMd5.getParentFile().mkdirs();
261 FileUtils.writeStringToFile( checksumFileMd5, "98745897234eda12836423", null );
263 // request the sha1 checksum of the metadata
265 new GetMethodWebRequest( "http://machine.com/repository/" + REPO_GROUP_WITH_VALID_REPOS + "/dummy/" +
266 "dummy-merged-metadata-resource/maven-metadata.xml.sha1" );
267 response = sc.getResource( request );
269 assertResponseOK( response );
270 assertEquals( "add113b0d7f8c6adb92a5015a7a3701081edf998 maven-metadata-group-with-valid-repos.xml", response.getText() );
272 // request the md5 checksum of the metadata
274 new GetMethodWebRequest( "http://machine.com/repository/" + REPO_GROUP_WITH_VALID_REPOS + "/dummy/" +
275 "dummy-merged-metadata-resource/maven-metadata.xml.md5" );
276 response = sc.getResource( request );
278 assertResponseOK( response );
279 assertEquals( "5b85ea4aa5f52bb76760041a52f98de8 maven-metadata-group-with-valid-repos.xml", response.getText().trim() );
283 public void testBrowseWithTwoArtifactsWithSameGroupIdInRepos()
286 String resourceName = "dummy/dummy-artifact/1.0/dummy-artifact-1.0.txt";
288 File dummyInternalResourceFile = new File( repoRootFirst, resourceName );
289 dummyInternalResourceFile.getParentFile().mkdirs();
290 FileUtils.writeStringToFile( dummyInternalResourceFile, "first", null );
292 resourceName = "dummy/dummy-artifact/2.0/dummy-artifact-2.0.txt";
294 File dummyReleasesResourceFile = new File( repoRootLast, resourceName );
295 dummyReleasesResourceFile.getParentFile().mkdirs();
296 FileUtils.writeStringToFile( dummyReleasesResourceFile, "last", null );
298 WebRequest request = new GetMethodWebRequest( "http://machine.com/repository/" + REPO_GROUP_WITH_VALID_REPOS + "/dummy/dummy-artifact/" );
299 WebResponse response = sc.getResource( request );
301 assertResponseOK( response );
302 assertTrue( StringUtils.contains( response.getText(), "Collection" ) );
303 assertTrue( StringUtils.contains( response.getText(), "dummy/dummy-artifact" ) );
304 assertTrue( StringUtils.contains( response.getText(), "1.0" ) );
305 assertTrue( StringUtils.contains( response.getText(), "2.0" ) );
308 protected void assertResponseMethodNotAllowed( WebResponse response )
310 assertNotNull( "Should have recieved a response", response );
311 assertEquals( "Should have been an 405/Method Not Allowed response code.", HttpServletResponse.SC_METHOD_NOT_ALLOWED, response.getResponseCode() );
314 protected RepositoryGroupConfiguration createRepositoryGroup( String id, List<String> repositories )
316 RepositoryGroupConfiguration repoGroupConfiguration = new RepositoryGroupConfiguration();
317 repoGroupConfiguration.setId( id );
318 repoGroupConfiguration.setRepositories( repositories );
319 return repoGroupConfiguration;